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

lightningnetwork / lnd / 13558005087

27 Feb 2025 03:04AM UTC coverage: 58.834% (-0.001%) from 58.835%
13558005087

Pull #8453

github

Roasbeef
lnwallet/chancloser: increase test coverage of state machine
Pull Request #8453: [4/4] - multi: integrate new rbf coop close FSM into the existing peer flow

1079 of 1370 new or added lines in 23 files covered. (78.76%)

578 existing lines in 40 files now uncovered.

137063 of 232965 relevant lines covered (58.83%)

19205.84 hits per line

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

87.6
/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 {
37

38
        t.Helper()
61✔
39

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

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

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

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.
61✔
64
        numBlocks := netParams.MinerConfirmationWindow*2 + 17
61✔
65
        _, err = node.Client.Generate(numBlocks)
61✔
66
        require.NoError(t, err, "failed to generate blocks")
61✔
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
61✔
73
// backend node should maintain a transaction index. The rpcpolling boolean
61✔
74
// can be set to determine whether bitcoind's RPC polling interface should be
61✔
75
// used for block and tx notifications or if its ZMQ interface should be used.
61✔
76
// A connection to the newly spawned bitcoind node is returned.
61✔
77
func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
78
        minerAddr string, txindex, rpcpolling bool) *chain.BitcoindConn {
79

80
        t.Helper()
81

82
        tempBitcoindDir := t.TempDir()
83

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

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

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

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

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

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

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

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

18✔
160
        return conn
18✔
161
}
18✔
162

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

18✔
168
        t.Helper()
9✔
169

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

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

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

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

18✔
206
        return spvNode
18✔
207
}
18✔
208

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