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

lightningnetwork / lnd / 15778860361

20 Jun 2025 12:23PM UTC coverage: 68.208% (+0.07%) from 68.143%
15778860361

Pull #9752

github

web-flow
Merge 831fefef7 into 7857d2c6a
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

10 of 14 new or added lines in 2 files covered. (71.43%)

2644 existing lines in 29 files now uncovered.

134706 of 197494 relevant lines covered (68.21%)

22122.25 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
UNCOV
78
// that node. miner should be set to the P2P address of the miner to connect
×
UNCOV
79
// to.
×
UNCOV
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",
×
97

×
98
                // The default max num of websockets is 25, but the closed
×
99
                // connections are not cleaned up immediately so we double the
×
100
                // size.
×
101
                //
×
102
                // TODO(yy): fix this in `btcd` to clean up the stale
×
103
                // connections.
×
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
        }
×
UNCOV
113

×
UNCOV
114
        // We want to overwrite some of the connection settings to make the
×
UNCOV
115
        // tests more robust. We might need to restart the backend while there
×
UNCOV
116
        // are already blocks present, which will take a bit longer than the
×
UNCOV
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
        }
×
UNCOV
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
                }
×
UNCOV
143

×
UNCOV
144
                // After shutting down the chain backend, we'll make a copy of
×
UNCOV
145
                // the log files, including any compressed log files from
×
UNCOV
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
                }
×
UNCOV
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
                        }
×
UNCOV
169
                }
×
UNCOV
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
×
UNCOV
180
        }
×
UNCOV
181

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