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

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 hits per line

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

5.15
/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()
×
39

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

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

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

62
        return node
×
63
}
64

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

×
74
        t.Helper()
×
75

×
76
        tempBitcoindDir := t.TempDir()
×
77

×
78
        rpcPort := port.NextAvailablePort()
×
79
        zmqBlockPort := port.NextAvailablePort()
×
80
        zmqTxPort := port.NextAvailablePort()
×
81
        zmqBlockHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqBlockPort)
×
82
        zmqTxHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqTxPort)
×
83

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

99
        bitcoind := exec.Command("bitcoind", args...)
×
100
        if err := bitcoind.Start(); err != nil {
×
101
                t.Fatalf("unable to start bitcoind: %v", err)
×
102
        }
×
103
        t.Cleanup(func() {
×
104
                _ = bitcoind.Process.Kill()
×
105
                _ = bitcoind.Wait()
×
106
        })
×
107

108
        // Wait for the bitcoind instance to start up.
109
        time.Sleep(time.Second)
×
110

×
111
        host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
×
112
        cfg := &chain.BitcoindConfig{
×
113
                ChainParams: netParams,
×
114
                Host:        host,
×
115
                User:        "weks",
×
116
                Pass:        "weks",
×
117
                // Fields only required for pruned nodes, not needed for these
×
118
                // tests.
×
119
                Dialer:             nil,
×
120
                PrunedModeMaxPeers: 0,
×
121
        }
×
122

×
123
        if rpcpolling {
×
124
                cfg.PollingConfig = &chain.PollingConfig{
×
125
                        BlockPollingInterval: time.Millisecond * 20,
×
126
                        TxPollingInterval:    time.Millisecond * 20,
×
127
                }
×
128
        } else {
×
129
                cfg.ZMQConfig = &chain.ZMQConfig{
×
130
                        ZMQBlockHost:    zmqBlockHost,
×
131
                        ZMQTxHost:       zmqTxHost,
×
132
                        ZMQReadDeadline: 5 * time.Second,
×
133
                }
×
134
        }
×
135

136
        var conn *chain.BitcoindConn
×
137
        err := wait.NoError(func() error {
×
138
                var err error
×
139
                conn, err = chain.NewBitcoindConn(cfg)
×
140
                if err != nil {
×
141
                        return err
×
142
                }
×
143

144
                return conn.Start()
×
145
        }, 10*time.Second)
146
        if err != nil {
×
147
                t.Fatalf("unable to establish connection to bitcoind at %v: "+
×
148
                        "%v", tempBitcoindDir, err)
×
149
        }
×
150
        t.Cleanup(conn.Stop)
×
151

×
152
        return conn
×
153
}
154

155
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
156
// the specified address.
157
func NewNeutrinoBackend(t *testing.T, netParams *chaincfg.Params,
158
        minerAddr string) *neutrino.ChainService {
×
159

×
160
        t.Helper()
×
161

×
162
        spvDir := t.TempDir()
×
163

×
164
        dbName := filepath.Join(spvDir, "neutrino.db")
×
165
        spvDatabase, err := walletdb.Create(
×
166
                "bdb", dbName, true, kvdb.DefaultDBTimeout,
×
167
        )
×
168
        if err != nil {
×
169
                t.Fatalf("unable to create walletdb: %v", err)
×
170
        }
×
171
        t.Cleanup(func() {
×
172
                spvDatabase.Close()
×
173
        })
×
174

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

188
        // We'll also wait for the instance to sync up fully to the chain
189
        // generated by the btcd instance.
190
        _ = spvNode.Start()
×
191
        for !spvNode.IsCurrent() {
×
192
                time.Sleep(time.Millisecond * 100)
×
193
        }
×
194
        t.Cleanup(func() {
×
195
                _ = spvNode.Stop()
×
196
        })
×
197

198
        return spvNode
×
199
}
200

201
func init() {
3✔
202
        // Before we start any node, we need to make sure that any btcd or
3✔
203
        // bitcoind node that is started through the RPC harness uses a unique
3✔
204
        // port as well to avoid any port collisions.
3✔
205
        rpctest.ListenAddressGenerator =
3✔
206
                port.GenerateSystemUniqueListenerAddresses
3✔
207
}
3✔
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