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

lightningnetwork / lnd / 12027808511

26 Nov 2024 08:58AM UTC coverage: 59.095% (+1.0%) from 58.071%
12027808511

Pull #9307

github

yyforyongyu
lntest: fix flakeness in `openChannelsForNodes`

We now make sure the channel participants have heard their private
channel when opening channels.
Pull Request #9307: Beat itest [2/3]: document and fix itest flakes

1 of 26 new or added lines in 4 files covered. (3.85%)

20 existing lines in 7 files now uncovered.

133850 of 226499 relevant lines covered (59.1%)

19519.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
)
20

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

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

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

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

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

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

×
52
        return args
×
53
}
×
54

55
// ConnectMiner is called to establish a connection to the test miner.
56
func (b BtcdBackendConfig) ConnectMiner() error {
×
57
        return b.harness.Client.Node(btcjson.NConnect, b.minerAddr, &miner.Temp)
×
58
}
×
59

60
// DisconnectMiner is called to disconnect the miner.
61
func (b BtcdBackendConfig) DisconnectMiner() error {
×
62
        return b.harness.Client.Node(
×
63
                btcjson.NDisconnect, b.minerAddr, &miner.Temp,
×
64
        )
×
65
}
×
66

67
// Credentials returns the rpc username, password and host for the backend.
68
func (b BtcdBackendConfig) Credentials() (string, string, string, error) {
×
69
        return b.rpcConfig.User, b.rpcConfig.Pass, b.rpcConfig.Host, nil
×
70
}
×
71

72
// Name returns the name of the backend type.
73
func (b BtcdBackendConfig) Name() string {
×
74
        return "btcd"
×
75
}
×
76

77
// NewBackend starts a new rpctest.Harness and returns a BtcdBackendConfig for
78
// that node. miner should be set to the P2P address of the miner to connect
79
// to.
80
func NewBackend(miner string, netParams *chaincfg.Params) (
81
        *BtcdBackendConfig, func() error, error) {
×
82

×
83
        baseLogDir := fmt.Sprintf(logDirPattern, node.GetLogDir())
×
84
        args := []string{
×
85
                "--rejectnonstd",
×
86
                "--txindex",
×
87
                "--trickleinterval=100ms",
×
88
                "--debuglevel=debug",
×
89
                "--logdir=" + baseLogDir,
×
90
                "--nowinservice",
×
91
                // The miner will get banned and disconnected from the node if
×
92
                // its requested data are not found. We add a nobanning flag to
×
93
                // make sure they stay connected if it happens.
×
94
                "--nobanning",
×
95
                // Don't disconnect if a reply takes too long.
×
96
                "--nostalldetect",
×
NEW
97

×
NEW
98
                // The default max num of websockets is 25, but the closed
×
NEW
99
                // connections are not cleaned up immediately so we double the
×
NEW
100
                // size.
×
NEW
101
                //
×
NEW
102
                // TODO(yy): fix this in `btcd` to clean up the stale
×
NEW
103
                // connections.
×
NEW
104
                "--rpcmaxwebsockets=50",
×
105
        }
×
106
        chainBackend, err := rpctest.New(
×
107
                netParams, nil, args, node.GetBtcdBinary(),
×
108
        )
×
109
        if err != nil {
×
110
                return nil, nil, fmt.Errorf("unable to create btcd node: %w",
×
111
                        err)
×
112
        }
×
113

114
        // We want to overwrite some of the connection settings to make the
115
        // tests more robust. We might need to restart the backend while there
116
        // are already blocks present, which will take a bit longer than the
117
        // 1 second the default settings amount to. Doubling both values will
118
        // give us retries up to 4 seconds.
119
        const (
×
120
                maxConnRetries   = rpctest.DefaultMaxConnectionRetries * 2
×
121
                connRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
×
122
        )
×
123

×
124
        chainBackend.MaxConnRetries = maxConnRetries
×
125
        chainBackend.ConnectionRetryTimeout = connRetryTimeout
×
126

×
127
        if err := chainBackend.SetUp(false, 0); err != nil {
×
128
                return nil, nil, fmt.Errorf("unable to set up btcd backend: %w",
×
129
                        err)
×
130
        }
×
131

132
        bd := &BtcdBackendConfig{
×
133
                rpcConfig: chainBackend.RPCConfig(),
×
134
                harness:   chainBackend,
×
135
                minerAddr: miner,
×
136
        }
×
137

×
138
        cleanUp := func() error {
×
139
                var errStr string
×
140
                if err := chainBackend.TearDown(); err != nil {
×
141
                        errStr += err.Error() + "\n"
×
142
                }
×
143

144
                // After shutting down the chain backend, we'll make a copy of
145
                // the log files, including any compressed log files from
146
                // logrorate, before deleting the temporary log dir.
147
                logDir := fmt.Sprintf("%s/%s", baseLogDir, netParams.Name)
×
148
                files, err := os.ReadDir(logDir)
×
149
                if err != nil {
×
150
                        errStr += fmt.Sprintf(
×
151
                                "unable to read log directory: %v\n", err,
×
152
                        )
×
153
                }
×
154

155
                for _, file := range files {
×
156
                        logFile := fmt.Sprintf("%s/%s", logDir, file.Name())
×
157
                        newFilename := strings.Replace(
×
158
                                file.Name(), "btcd.log",
×
159
                                "output_btcd_chainbackend.log", 1,
×
160
                        )
×
161
                        logDestination := fmt.Sprintf(
×
162
                                "%s/%s", node.GetLogDir(), newFilename,
×
163
                        )
×
164
                        err := node.CopyFile(logDestination, logFile)
×
165
                        if err != nil {
×
166
                                errStr += fmt.Sprintf("unable to copy file: "+
×
167
                                        "%v\n", err)
×
168
                        }
×
169
                }
170

171
                if err = os.RemoveAll(baseLogDir); err != nil {
×
172
                        errStr += fmt.Sprintf(
×
173
                                "cannot remove dir %s: %v\n", baseLogDir, err,
×
174
                        )
×
175
                }
×
176
                if errStr != "" {
×
177
                        return errors.New(errStr)
×
178
                }
×
179
                return nil
×
180
        }
181

182
        return bd, cleanUp, nil
×
183
}
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