• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 11367332825

16 Oct 2024 02:04PM UTC coverage: 58.78% (+9.5%) from 49.297%
11367332825

push

github

web-flow
Merge pull request #9171 from ellemouton/genUnsignedTLVRanges

tlv: generate types for gossip unsigned range

6 of 404 new or added lines in 1 file covered. (1.49%)

271 existing lines in 23 files now uncovered.

130922 of 222731 relevant lines covered (58.78%)

28105.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.85
/lntest/unittest/backend.go
1
package unittest
2

3
import (
4
        "fmt"
5
        "os/exec"
6
        "path/filepath"
7
        "testing"
8
        "time"
9

10
        "github.com/btcsuite/btcd/chaincfg"
11
        "github.com/btcsuite/btcd/integration/rpctest"
12
        "github.com/btcsuite/btcwallet/chain"
13
        "github.com/btcsuite/btcwallet/walletdb"
14
        "github.com/lightninglabs/neutrino"
15
        "github.com/lightningnetwork/lnd/kvdb"
16
        "github.com/lightningnetwork/lnd/lntest/port"
17
        "github.com/lightningnetwork/lnd/lntest/wait"
18
        "github.com/stretchr/testify/require"
19
)
20

21
var (
22
        // TrickleInterval is the interval at which the miner should trickle
23
        // transactions to its peers. We'll set it small to ensure the miner
24
        // propagates transactions quickly in the tests.
25
        TrickleInterval = 10 * time.Millisecond
26
)
27

28
var (
29
        // NetParams are the default network parameters for the tests.
30
        NetParams = &chaincfg.RegressionNetParams
31
)
32

33
// NewMiner spawns testing harness backed by a btcd node that can serve as a
34
// miner.
35
func NewMiner(t *testing.T, netParams *chaincfg.Params, extraArgs []string,
36
        createChain bool, spendableOutputs uint32) *rpctest.Harness {
46✔
37

46✔
38
        t.Helper()
46✔
39

46✔
40
        // Add the trickle interval argument to the extra args.
46✔
41
        trickle := fmt.Sprintf("--trickleinterval=%v", TrickleInterval)
46✔
42
        extraArgs = append(extraArgs, trickle)
46✔
43

46✔
44
        node, err := rpctest.New(netParams, nil, extraArgs, "")
46✔
45
        require.NoError(t, err, "unable to create backend node")
46✔
46
        t.Cleanup(func() {
92✔
47
                require.NoError(t, node.TearDown())
46✔
48
        })
46✔
49

50
        // We want to overwrite some of the connection settings to make the
51
        // tests more robust. We might need to restart the backend while there
52
        // are already blocks present, which will take a bit longer than the
53
        // 1 second the default settings amount to. Doubling both values will
54
        // give us retries up to 4 seconds.
55
        node.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
46✔
56
        node.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
46✔
57

46✔
58
        if err := node.SetUp(createChain, spendableOutputs); err != nil {
46✔
59
                t.Fatalf("unable to set up backend node: %v", err)
×
60
        }
×
61

62
        // Next mine enough blocks in order for segwit and the CSV package
63
        // soft-fork to activate.
64
        numBlocks := netParams.MinerConfirmationWindow*2 + 17
46✔
65
        _, err = node.Client.Generate(numBlocks)
46✔
66
        require.NoError(t, err, "failed to generate blocks")
46✔
67

46✔
68
        return node
46✔
69
}
70

71
// NewBitcoindBackend spawns a new bitcoind node that connects to a miner at the
72
// specified address. The txindex boolean can be set to determine whether the
73
// backend node should maintain a transaction index. The rpcpolling boolean
74
// can be set to determine whether bitcoind's RPC polling interface should be
75
// used for block and tx notifications or if its ZMQ interface should be used.
76
// A connection to the newly spawned bitcoind node is returned.
77
func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
78
        minerAddr string, txindex, rpcpolling bool) *chain.BitcoindConn {
12✔
79

12✔
80
        t.Helper()
12✔
81

12✔
82
        tempBitcoindDir := t.TempDir()
12✔
83

12✔
84
        rpcPort := port.NextAvailablePort()
12✔
85
        torBindPort := port.NextAvailablePort()
12✔
86
        zmqBlockPort := port.NextAvailablePort()
12✔
87
        zmqTxPort := port.NextAvailablePort()
12✔
88
        zmqBlockHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqBlockPort)
12✔
89
        zmqTxHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqTxPort)
12✔
90

12✔
91
        args := []string{
12✔
92
                "-connect=" + minerAddr,
12✔
93
                "-datadir=" + tempBitcoindDir,
12✔
94
                "-regtest",
12✔
95
                "-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a95" +
12✔
96
                        "284294edb5773b05544b220110063096c221be9933c82d38e1",
12✔
97
                fmt.Sprintf("-rpcport=%d", rpcPort),
12✔
98
                fmt.Sprintf("-bind=127.0.0.1:%d=onion", torBindPort),
12✔
99
                "-disablewallet",
12✔
100
                "-zmqpubrawblock=" + zmqBlockHost,
12✔
101
                "-zmqpubrawtx=" + zmqTxHost,
12✔
102
        }
12✔
103
        if txindex {
22✔
104
                args = append(args, "-txindex")
10✔
105
        }
10✔
106

107
        bitcoind := exec.Command("bitcoind", args...)
12✔
108
        if err := bitcoind.Start(); err != nil {
12✔
109
                t.Fatalf("unable to start bitcoind: %v", err)
×
110
        }
×
111
        t.Cleanup(func() {
24✔
112
                _ = bitcoind.Process.Kill()
12✔
113
                _ = bitcoind.Wait()
12✔
114
        })
12✔
115

116
        // Wait for the bitcoind instance to start up.
117
        time.Sleep(time.Second)
12✔
118

12✔
119
        host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
12✔
120
        cfg := &chain.BitcoindConfig{
12✔
121
                ChainParams: netParams,
12✔
122
                Host:        host,
12✔
123
                User:        "weks",
12✔
124
                Pass:        "weks",
12✔
125
                // Fields only required for pruned nodes, not needed for these
12✔
126
                // tests.
12✔
127
                Dialer:             nil,
12✔
128
                PrunedModeMaxPeers: 0,
12✔
129
        }
12✔
130

12✔
131
        if rpcpolling {
18✔
132
                cfg.PollingConfig = &chain.PollingConfig{
6✔
133
                        BlockPollingInterval: time.Millisecond * 20,
6✔
134
                        TxPollingInterval:    time.Millisecond * 20,
6✔
135
                }
6✔
136
        } else {
12✔
137
                cfg.ZMQConfig = &chain.ZMQConfig{
6✔
138
                        ZMQBlockHost:    zmqBlockHost,
6✔
139
                        ZMQTxHost:       zmqTxHost,
6✔
140
                        ZMQReadDeadline: 5 * time.Second,
6✔
141
                }
6✔
142
        }
6✔
143

144
        var conn *chain.BitcoindConn
12✔
145
        err := wait.NoError(func() error {
24✔
146
                var err error
12✔
147
                conn, err = chain.NewBitcoindConn(cfg)
12✔
148
                if err != nil {
12✔
UNCOV
149
                        return err
×
150
                }
×
151

152
                return conn.Start()
12✔
153
        }, 10*time.Second)
154
        if err != nil {
12✔
155
                t.Fatalf("unable to establish connection to bitcoind at %v: "+
×
156
                        "%v", tempBitcoindDir, err)
×
157
        }
×
158
        t.Cleanup(conn.Stop)
12✔
159

12✔
160
        return conn
12✔
161
}
162

163
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
164
// the specified address.
165
func NewNeutrinoBackend(t *testing.T, netParams *chaincfg.Params,
166
        minerAddr string) *neutrino.ChainService {
1✔
167

1✔
168
        t.Helper()
1✔
169

1✔
170
        spvDir := t.TempDir()
1✔
171

1✔
172
        dbName := filepath.Join(spvDir, "neutrino.db")
1✔
173
        spvDatabase, err := walletdb.Create(
1✔
174
                "bdb", dbName, true, kvdb.DefaultDBTimeout,
1✔
175
        )
1✔
176
        if err != nil {
1✔
177
                t.Fatalf("unable to create walletdb: %v", err)
×
178
        }
×
179
        t.Cleanup(func() {
2✔
180
                spvDatabase.Close()
1✔
181
        })
1✔
182

183
        // Create an instance of neutrino connected to the running btcd
184
        // instance.
185
        spvConfig := neutrino.Config{
1✔
186
                DataDir:      spvDir,
1✔
187
                Database:     spvDatabase,
1✔
188
                ChainParams:  *netParams,
1✔
189
                ConnectPeers: []string{minerAddr},
1✔
190
        }
1✔
191
        spvNode, err := neutrino.NewChainService(spvConfig)
1✔
192
        if err != nil {
1✔
UNCOV
193
                t.Fatalf("unable to create neutrino: %v", err)
×
UNCOV
194
        }
×
195

196
        // We'll also wait for the instance to sync up fully to the chain
197
        // generated by the btcd instance.
198
        _ = spvNode.Start()
1✔
199
        for !spvNode.IsCurrent() {
2✔
200
                time.Sleep(time.Millisecond * 100)
1✔
201
        }
1✔
202
        t.Cleanup(func() {
2✔
203
                _ = spvNode.Stop()
1✔
204
        })
1✔
205

206
        return spvNode
1✔
207
}
208

209
func init() {
49✔
210
        // Before we start any node, we need to make sure that any btcd or
49✔
211
        // bitcoind node that is started through the RPC harness uses a unique
49✔
212
        // port as well to avoid any port collisions.
49✔
213
        rpctest.ListenAddressGenerator =
49✔
214
                port.GenerateSystemUniqueListenerAddresses
49✔
215
}
49✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc