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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

5.0
/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,
UNCOV
36
        createChain bool, spendableOutputs uint32) *rpctest.Harness {
×
UNCOV
37

×
UNCOV
38
        t.Helper()
×
UNCOV
39

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

×
UNCOV
44
        node, err := rpctest.New(netParams, nil, extraArgs, "")
×
UNCOV
45
        require.NoError(t, err, "unable to create backend node")
×
UNCOV
46
        t.Cleanup(func() {
×
UNCOV
47
                require.NoError(t, node.TearDown())
×
UNCOV
48
        })
×
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.
UNCOV
55
        node.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
×
UNCOV
56
        node.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
×
UNCOV
57

×
UNCOV
58
        if err := node.SetUp(createChain, spendableOutputs); err != nil {
×
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.
UNCOV
64
        numBlocks := netParams.MinerConfirmationWindow*2 + 17
×
UNCOV
65
        _, err = node.Client.Generate(numBlocks)
×
UNCOV
66
        require.NoError(t, err, "failed to generate blocks")
×
UNCOV
67

×
UNCOV
68
        return node
×
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,
UNCOV
78
        minerAddr string, txindex, rpcpolling bool) *chain.BitcoindConn {
×
UNCOV
79

×
UNCOV
80
        t.Helper()
×
UNCOV
81

×
UNCOV
82
        tempBitcoindDir := t.TempDir()
×
UNCOV
83

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

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

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

114
        // Wait for the bitcoind instance to start up.
UNCOV
115
        time.Sleep(time.Second)
×
UNCOV
116

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

×
UNCOV
129
        if rpcpolling {
×
UNCOV
130
                cfg.PollingConfig = &chain.PollingConfig{
×
UNCOV
131
                        BlockPollingInterval: time.Millisecond * 20,
×
UNCOV
132
                        TxPollingInterval:    time.Millisecond * 20,
×
UNCOV
133
                }
×
UNCOV
134
        } else {
×
UNCOV
135
                cfg.ZMQConfig = &chain.ZMQConfig{
×
UNCOV
136
                        ZMQBlockHost:    zmqBlockHost,
×
UNCOV
137
                        ZMQTxHost:       zmqTxHost,
×
UNCOV
138
                        ZMQReadDeadline: 5 * time.Second,
×
UNCOV
139
                }
×
UNCOV
140
        }
×
141

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

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

×
UNCOV
158
        return conn
×
159
}
160

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

×
UNCOV
166
        t.Helper()
×
UNCOV
167

×
UNCOV
168
        spvDir := t.TempDir()
×
UNCOV
169

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

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

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

UNCOV
204
        return spvNode
×
205
}
206

207
func init() {
2✔
208
        // Before we start any node, we need to make sure that any btcd or
2✔
209
        // bitcoind node that is started through the RPC harness uses a unique
2✔
210
        // port as well to avoid any port collisions.
2✔
211
        rpctest.ListenAddressGenerator =
2✔
212
                port.GenerateSystemUniqueListenerAddresses
2✔
213
}
2✔
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