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

lightningnetwork / lnd / 12209990843

07 Dec 2024 02:45AM UTC coverage: 58.977% (+1.1%) from 57.911%
12209990843

Pull #9260

github

yyforyongyu
lnrpc: sort `Invoice.HTLCs` based on `HtlcIndex`

So the returned HTLCs are ordered.
Pull Request #9260: Beat itest [3/3]: fix all itest flakes

4 of 132 new or added lines in 9 files covered. (3.03%)

17 existing lines in 8 files now uncovered.

134149 of 227459 relevant lines covered (58.98%)

19442.29 hits per line

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

0.0
/lntest/btcd.go
1
//go:build !bitcoind && !neutrino
2
// +build !bitcoind,!neutrino
3

4
package lntest
5

6
import (
7
        "encoding/hex"
8
        "errors"
9
        "fmt"
10
        "os"
11
        "strings"
12

13
        "github.com/btcsuite/btcd/btcjson"
14
        "github.com/btcsuite/btcd/chaincfg"
15
        "github.com/btcsuite/btcd/integration/rpctest"
16
        "github.com/btcsuite/btcd/rpcclient"
17
        "github.com/lightningnetwork/lnd/lntest/miner"
18
        "github.com/lightningnetwork/lnd/lntest/node"
19
        "github.com/lightningnetwork/lnd/lntest/wait"
20
)
21

22
// logDirPattern is the pattern of the name of the temporary log directory.
23
const logDirPattern = "%s/.backendlogs"
24

25
// BtcdBackendConfig is an implementation of the BackendConfig interface
26
// backed by a btcd node.
27
type BtcdBackendConfig struct {
28
        // rpcConfig houses the connection config to the backing btcd instance.
29
        rpcConfig rpcclient.ConnConfig
30

31
        // harness is the backing btcd instance.
32
        harness *rpctest.Harness
33

34
        // minerAddr is the p2p address of the miner to connect to.
35
        minerAddr string
36
}
37

38
// A compile time assertion to ensure BtcdBackendConfig meets the BackendConfig
39
// interface.
40
var _ node.BackendConfig = (*BtcdBackendConfig)(nil)
41

42
// GenArgs returns the arguments needed to be passed to LND at startup for
43
// using this node as a chain backend.
44
func (b BtcdBackendConfig) GenArgs() []string {
×
45
        var args []string
×
46
        encodedCert := hex.EncodeToString(b.rpcConfig.Certificates)
×
47
        args = append(args, "--bitcoin.node=btcd")
×
48
        args = append(args, fmt.Sprintf("--btcd.rpchost=%v", b.rpcConfig.Host))
×
49
        args = append(args, fmt.Sprintf("--btcd.rpcuser=%v", b.rpcConfig.User))
×
50
        args = append(args, fmt.Sprintf("--btcd.rpcpass=%v", b.rpcConfig.Pass))
×
51
        args = append(args, fmt.Sprintf("--btcd.rawrpccert=%v", encodedCert))
×
52

×
53
        return args
×
54
}
×
55

56
// ConnectMiner is called to establish a connection to the test miner.
NEW
57
func (b BtcdBackendConfig) ConnectMiner(minerHeight int32) error {
×
NEW
58
        err := b.harness.Client.Node(btcjson.NConnect, b.minerAddr, &miner.Temp)
×
NEW
59

×
NEW
60
        if err != nil {
×
NEW
61
                return err
×
NEW
62
        }
×
63

64
        // Once connected to the miner, we now wait until the node is synced.
NEW
65
        err = wait.NoError(func() error {
×
NEW
66
                resp, err := b.harness.Client.GetBlockChainInfo()
×
NEW
67
                if err != nil {
×
NEW
68
                        return err
×
NEW
69
                }
×
70

NEW
71
                if resp.Blocks == minerHeight {
×
NEW
72
                        return nil
×
NEW
73
                }
×
74

NEW
75
                return fmt.Errorf("not synced to miner height=%v, blocks=%d, "+
×
NEW
76
                        "headers=%d", minerHeight, resp.Blocks, resp.Headers)
×
77
        }, wait.DefaultTimeout)
78

NEW
79
        return err
×
80
}
81

82
// DisconnectMiner is called to disconnect the miner.
83
func (b BtcdBackendConfig) DisconnectMiner() error {
×
84
        return b.harness.Client.Node(
×
85
                btcjson.NDisconnect, b.minerAddr, &miner.Temp,
×
86
        )
×
87
}
×
88

89
// Credentials returns the rpc username, password and host for the backend.
90
func (b BtcdBackendConfig) Credentials() (string, string, string, error) {
×
91
        return b.rpcConfig.User, b.rpcConfig.Pass, b.rpcConfig.Host, nil
×
92
}
×
93

94
// Name returns the name of the backend type.
95
func (b BtcdBackendConfig) Name() string {
×
96
        return "btcd"
×
97
}
×
98

99
// NewBackend starts a new rpctest.Harness and returns a BtcdBackendConfig for
100
// that node. miner should be set to the P2P address of the miner to connect
101
// to.
102
func NewBackend(miner string, netParams *chaincfg.Params) (
103
        *BtcdBackendConfig, func() error, error) {
×
104

×
105
        baseLogDir := fmt.Sprintf(logDirPattern, node.GetLogDir())
×
106
        args := []string{
×
107
                "--rejectnonstd",
×
108
                "--txindex",
×
109
                "--trickleinterval=100ms",
×
110
                "--debuglevel=debug",
×
111
                "--logdir=" + baseLogDir,
×
112
                "--nowinservice",
×
113
                // The miner will get banned and disconnected from the node if
×
114
                // its requested data are not found. We add a nobanning flag to
×
115
                // make sure they stay connected if it happens.
×
116
                "--nobanning",
×
117
                // Don't disconnect if a reply takes too long.
×
118
                "--nostalldetect",
×
119

×
120
                // The default max num of websockets is 25, but the closed
×
121
                // connections are not cleaned up immediately so we double the
×
122
                // size.
×
123
                //
×
124
                // TODO(yy): fix this in `btcd` to clean up the stale
×
125
                // connections.
×
126
                "--rpcmaxwebsockets=50",
×
127
        }
×
128
        chainBackend, err := rpctest.New(
×
129
                netParams, nil, args, node.GetBtcdBinary(),
×
130
        )
×
131
        if err != nil {
×
132
                return nil, nil, fmt.Errorf("unable to create btcd node: %w",
×
133
                        err)
×
134
        }
×
135

136
        // We want to overwrite some of the connection settings to make the
137
        // tests more robust. We might need to restart the backend while there
138
        // are already blocks present, which will take a bit longer than the
139
        // 1 second the default settings amount to. Doubling both values will
140
        // give us retries up to 4 seconds.
141
        const (
×
142
                maxConnRetries   = rpctest.DefaultMaxConnectionRetries * 2
×
143
                connRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
×
144
        )
×
145

×
146
        chainBackend.MaxConnRetries = maxConnRetries
×
147
        chainBackend.ConnectionRetryTimeout = connRetryTimeout
×
148

×
149
        if err := chainBackend.SetUp(false, 0); err != nil {
×
150
                return nil, nil, fmt.Errorf("unable to set up btcd backend: %w",
×
151
                        err)
×
152
        }
×
153

154
        bd := &BtcdBackendConfig{
×
155
                rpcConfig: chainBackend.RPCConfig(),
×
156
                harness:   chainBackend,
×
157
                minerAddr: miner,
×
158
        }
×
159

×
160
        cleanUp := func() error {
×
161
                var errStr string
×
162
                if err := chainBackend.TearDown(); err != nil {
×
163
                        errStr += err.Error() + "\n"
×
164
                }
×
165

166
                // After shutting down the chain backend, we'll make a copy of
167
                // the log files, including any compressed log files from
168
                // logrorate, before deleting the temporary log dir.
169
                logDir := fmt.Sprintf("%s/%s", baseLogDir, netParams.Name)
×
170
                files, err := os.ReadDir(logDir)
×
171
                if err != nil {
×
172
                        errStr += fmt.Sprintf(
×
173
                                "unable to read log directory: %v\n", err,
×
174
                        )
×
175
                }
×
176

177
                for _, file := range files {
×
178
                        logFile := fmt.Sprintf("%s/%s", logDir, file.Name())
×
179
                        newFilename := strings.Replace(
×
180
                                file.Name(), "btcd.log",
×
181
                                "output_btcd_chainbackend.log", 1,
×
182
                        )
×
183
                        logDestination := fmt.Sprintf(
×
184
                                "%s/%s", node.GetLogDir(), newFilename,
×
185
                        )
×
186
                        err := node.CopyFile(logDestination, logFile)
×
187
                        if err != nil {
×
188
                                errStr += fmt.Sprintf("unable to copy file: "+
×
189
                                        "%v\n", err)
×
190
                        }
×
191
                }
192

193
                if err = os.RemoveAll(baseLogDir); err != nil {
×
194
                        errStr += fmt.Sprintf(
×
195
                                "cannot remove dir %s: %v\n", baseLogDir, err,
×
196
                        )
×
197
                }
×
198
                if errStr != "" {
×
199
                        return errors.New(errStr)
×
200
                }
×
201
                return nil
×
202
        }
203

204
        return bd, cleanUp, nil
×
205
}
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