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

lightningnetwork / lnd / 12430538171

20 Dec 2024 11:21AM UTC coverage: 58.716% (+0.1%) from 58.576%
12430538171

push

github

web-flow
Merge pull request #9381 from yyforyongyu/fix-no-space-left

workflows: fix no space left error

135265 of 230373 relevant lines covered (58.72%)

19174.52 hits per line

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

3.59
/lntest/node/config.go
1
package node
2

3
import (
4
        "flag"
5
        "fmt"
6
        "io"
7
        "os"
8
        "path"
9
        "path/filepath"
10

11
        "github.com/btcsuite/btcd/chaincfg"
12
        "github.com/btcsuite/btcd/integration/rpctest"
13
        "github.com/lightningnetwork/lnd"
14
        "github.com/lightningnetwork/lnd/chanbackup"
15
        "github.com/lightningnetwork/lnd/kvdb/etcd"
16
        "github.com/lightningnetwork/lnd/lntest/port"
17
        "github.com/lightningnetwork/lnd/lntest/wait"
18
)
19

20
const (
21
        // ListenerFormat is the format string that is used to generate local
22
        // listener addresses.
23
        ListenerFormat = "127.0.0.1:%d"
24

25
        // DefaultCSV is the CSV delay (remotedelay) we will start our test
26
        // nodes with.
27
        DefaultCSV = 4
28
)
29

30
var (
31
        // logOutput is a flag that can be set to append the output from the
32
        // seed nodes to log files.
33
        logOutput = flag.Bool("logoutput", false,
34
                "log output from node n to file output-n.log")
35

36
        // logSubDir is the default directory where the logs are written to if
37
        // logOutput is true.
38
        logSubDir = flag.String("logdir", ".", "default dir to write logs to")
39

40
        // btcdExecutable is the full path to the btcd binary.
41
        btcdExecutable = flag.String(
42
                "btcdexec", "", "full path to btcd binary",
43
        )
44

45
        // CfgLegacy specifies the config used to create a node that uses the
46
        // legacy channel format.
47
        CfgLegacy = []string{"--protocol.legacy.committweak"}
48

49
        // CfgStaticRemoteKey specifies the config used to create a node that
50
        // uses the static remote key feature.
51
        CfgStaticRemoteKey = []string{}
52

53
        // CfgAnchor specifies the config used to create a node that uses the
54
        // anchor output feature.
55
        CfgAnchor = []string{"--protocol.anchors"}
56

57
        // CfgLeased specifies the config used to create a node that uses the
58
        // leased channel feature.
59
        CfgLeased = []string{
60
                "--protocol.anchors",
61
                "--protocol.script-enforced-lease",
62
        }
63

64
        // CfgSimpleTaproot specifies the config used to create a node that
65
        // uses the simple taproot feature.
66
        CfgSimpleTaproot = []string{
67
                "--protocol.anchors",
68
                "--protocol.simple-taproot-chans",
69
        }
70

71
        // CfgZeroConf specifies the config used to create a node that uses the
72
        // zero-conf channel feature.
73
        CfgZeroConf = []string{
74
                "--protocol.anchors",
75
                "--protocol.option-scid-alias",
76
                "--protocol.zero-conf",
77
        }
78
)
79

80
type DatabaseBackend int
81

82
const (
83
        BackendBbolt DatabaseBackend = iota
84
        BackendEtcd
85
        BackendPostgres
86
        BackendSqlite
87
)
88

89
// Option is a function for updating a node's configuration.
90
type Option func(*BaseNodeConfig)
91

92
// BackendConfig is an interface that abstracts away the specific chain backend
93
// node implementation.
94
type BackendConfig interface {
95
        // GenArgs returns the arguments needed to be passed to LND at startup
96
        // for using this node as a chain backend.
97
        GenArgs() []string
98

99
        // ConnectMiner is called to establish a connection to the test miner.
100
        ConnectMiner() error
101

102
        // DisconnectMiner is called to disconnect the miner.
103
        DisconnectMiner() error
104

105
        // Name returns the name of the backend type.
106
        Name() string
107

108
        // Credentials returns the rpc username, password and host for the
109
        // backend.
110
        Credentials() (string, string, string, error)
111
}
112

113
// BaseNodeConfig is the base node configuration.
114
type BaseNodeConfig struct {
115
        Name string
116

117
        // LogFilenamePrefix is used to prefix node log files. Can be used to
118
        // store the current test case for simpler postmortem debugging.
119
        LogFilenamePrefix string
120

121
        NetParams         *chaincfg.Params
122
        BackendCfg        BackendConfig
123
        BaseDir           string
124
        ExtraArgs         []string
125
        OriginalExtraArgs []string
126

127
        DataDir        string
128
        LogDir         string
129
        TLSCertPath    string
130
        TLSKeyPath     string
131
        AdminMacPath   string
132
        ReadMacPath    string
133
        InvoiceMacPath string
134

135
        SkipUnlock bool
136
        Password   []byte
137

138
        P2PPort     int
139
        RPCPort     int
140
        RESTPort    int
141
        ProfilePort int
142

143
        FeeURL string
144

145
        DBBackend   DatabaseBackend
146
        PostgresDsn string
147
        NativeSQL   bool
148

149
        // NodeID is a unique ID used to identify the node.
150
        NodeID uint32
151

152
        // LndBinary is the full path to the lnd binary that was specifically
153
        // compiled with all required itest flags.
154
        LndBinary string
155

156
        // backupDBDir is the path where a database backup is stored, if any.
157
        backupDBDir string
158

159
        // postgresDBName is the name of the postgres database where lnd data
160
        // is stored in.
161
        postgresDBName string
162
}
163

164
func (cfg BaseNodeConfig) P2PAddr() string {
×
165
        return fmt.Sprintf(ListenerFormat, cfg.P2PPort)
×
166
}
×
167

168
func (cfg BaseNodeConfig) RPCAddr() string {
×
169
        return fmt.Sprintf(ListenerFormat, cfg.RPCPort)
×
170
}
×
171

172
func (cfg BaseNodeConfig) RESTAddr() string {
×
173
        return fmt.Sprintf(ListenerFormat, cfg.RESTPort)
×
174
}
×
175

176
// DBDir returns the holding directory path of the graph database.
177
func (cfg BaseNodeConfig) DBDir() string {
×
178
        return filepath.Join(cfg.DataDir, "graph", cfg.NetParams.Name)
×
179
}
×
180

181
func (cfg BaseNodeConfig) DBPath() string {
×
182
        return filepath.Join(cfg.DBDir(), "channel.db")
×
183
}
×
184

185
func (cfg BaseNodeConfig) ChanBackupPath() string {
×
186
        return filepath.Join(
×
187
                cfg.DataDir, "chain", lnd.BitcoinChainName,
×
188
                fmt.Sprintf(
×
189
                        "%v/%v", cfg.NetParams.Name,
×
190
                        chanbackup.DefaultBackupFileName,
×
191
                ),
×
192
        )
×
193
}
×
194

195
// GenerateListeningPorts generates the ports to listen on designated for the
196
// current lightning network test.
197
func (cfg *BaseNodeConfig) GenerateListeningPorts() {
×
198
        if cfg.P2PPort == 0 {
×
199
                cfg.P2PPort = port.NextAvailablePort()
×
200
        }
×
201
        if cfg.RPCPort == 0 {
×
202
                cfg.RPCPort = port.NextAvailablePort()
×
203
        }
×
204
        if cfg.RESTPort == 0 {
×
205
                cfg.RESTPort = port.NextAvailablePort()
×
206
        }
×
207
        if cfg.ProfilePort == 0 {
×
208
                cfg.ProfilePort = port.NextAvailablePort()
×
209
        }
×
210
}
211

212
// BaseConfig returns the base node configuration struct.
213
func (cfg *BaseNodeConfig) BaseConfig() *BaseNodeConfig {
×
214
        return cfg
×
215
}
×
216

217
// GenArgs generates a slice of command line arguments from the lightning node
218
// config struct.
219
func (cfg *BaseNodeConfig) GenArgs() []string {
×
220
        var args []string
×
221

×
222
        switch cfg.NetParams {
×
223
        case &chaincfg.TestNet3Params:
×
224
                args = append(args, "--bitcoin.testnet")
×
225
        case &chaincfg.SimNetParams:
×
226
                args = append(args, "--bitcoin.simnet")
×
227
        case &chaincfg.RegressionNetParams:
×
228
                args = append(args, "--bitcoin.regtest")
×
229
        }
230

231
        backendArgs := cfg.BackendCfg.GenArgs()
×
232
        args = append(args, backendArgs...)
×
233

×
234
        nodeArgs := []string{
×
235
                "--nobootstrap",
×
236
                "--debuglevel=debug",
×
237
                "--bitcoin.defaultchanconfs=1",
×
238
                "--accept-keysend",
×
239
                "--keep-failed-payment-attempts",
×
240
                "--logging.no-commit-hash",
×
241
                fmt.Sprintf("--db.batch-commit-interval=%v", commitInterval),
×
242
                fmt.Sprintf("--bitcoin.defaultremotedelay=%v", DefaultCSV),
×
243
                fmt.Sprintf("--rpclisten=%v", cfg.RPCAddr()),
×
244
                fmt.Sprintf("--restlisten=%v", cfg.RESTAddr()),
×
245
                fmt.Sprintf("--restcors=https://%v", cfg.RESTAddr()),
×
246
                fmt.Sprintf("--listen=%v", cfg.P2PAddr()),
×
247
                fmt.Sprintf("--externalip=%v", cfg.P2PAddr()),
×
248
                fmt.Sprintf("--lnddir=%v", cfg.BaseDir),
×
249
                fmt.Sprintf("--adminmacaroonpath=%v", cfg.AdminMacPath),
×
250
                fmt.Sprintf("--readonlymacaroonpath=%v", cfg.ReadMacPath),
×
251
                fmt.Sprintf("--invoicemacaroonpath=%v", cfg.InvoiceMacPath),
×
252
                fmt.Sprintf("--trickledelay=%v", trickleDelay),
×
253

×
254
                // Use a small batch delay so we can broadcast the
×
255
                // announcements quickly in the tests.
×
256
                "--gossip.sub-batch-delay=5ms",
×
257

×
258
                // Use a small cache duration so the `DescribeGraph` can be
×
259
                // updated quicker.
×
260
                "--caches.rpc-graph-cache-duration=100ms",
×
261

×
262
                // Speed up the tests for bitcoind backend.
×
263
                "--bitcoind.blockpollinginterval=100ms",
×
264
                "--bitcoind.txpollinginterval=100ms",
×
265

×
266
                // Allow unsafe disconnect in itest.
×
267
                "--dev.unsafedisconnect",
×
268
        }
×
269

×
270
        args = append(args, nodeArgs...)
×
271

×
272
        if cfg.Password == nil {
×
273
                args = append(args, "--noseedbackup")
×
274
        }
×
275

276
        switch cfg.DBBackend {
×
277
        case BackendEtcd:
×
278
                args = append(args, "--db.backend=etcd")
×
279
                args = append(args, "--db.etcd.embedded")
×
280
                args = append(
×
281
                        args, fmt.Sprintf(
×
282
                                "--db.etcd.embedded_client_port=%v",
×
283
                                port.NextAvailablePort(),
×
284
                        ),
×
285
                )
×
286
                args = append(
×
287
                        args, fmt.Sprintf(
×
288
                                "--db.etcd.embedded_peer_port=%v",
×
289
                                port.NextAvailablePort(),
×
290
                        ),
×
291
                )
×
292
                args = append(
×
293
                        args, fmt.Sprintf(
×
294
                                "--db.etcd.embedded_log_file=%v",
×
295
                                path.Join(cfg.LogDir, "etcd.log"),
×
296
                        ),
×
297
                )
×
298

299
        case BackendPostgres:
×
300
                args = append(args, "--db.backend=postgres")
×
301
                args = append(args, "--db.postgres.dsn="+cfg.PostgresDsn)
×
302
                if cfg.NativeSQL {
×
303
                        args = append(args, "--db.use-native-sql")
×
304
                }
×
305

306
        case BackendSqlite:
×
307
                args = append(args, "--db.backend=sqlite")
×
308
                args = append(args, fmt.Sprintf("--db.sqlite.busytimeout=%v",
×
309
                        wait.SqliteBusyTimeout))
×
310
                if cfg.NativeSQL {
×
311
                        args = append(args, "--db.use-native-sql")
×
312
                }
×
313
        }
314

315
        if cfg.FeeURL != "" {
×
316
                args = append(args, "--fee.url="+cfg.FeeURL)
×
317
        }
×
318

319
        // Put extra args in the end so the args can be overwritten.
320
        if cfg.ExtraArgs != nil {
×
321
                args = append(args, cfg.ExtraArgs...)
×
322
        }
×
323

324
        return args
×
325
}
326

327
// ExtraArgsEtcd returns extra args for configuring LND to use an external etcd
328
// database (for remote channel DB and wallet DB).
329
func ExtraArgsEtcd(etcdCfg *etcd.Config, name string, cluster bool,
330
        leaderSessionTTL int) []string {
×
331

×
332
        extraArgs := []string{
×
333
                "--db.backend=etcd",
×
334
                fmt.Sprintf("--db.etcd.host=%v", etcdCfg.Host),
×
335
                fmt.Sprintf("--db.etcd.user=%v", etcdCfg.User),
×
336
                fmt.Sprintf("--db.etcd.pass=%v", etcdCfg.Pass),
×
337
                fmt.Sprintf("--db.etcd.namespace=%v", etcdCfg.Namespace),
×
338
        }
×
339

×
340
        if etcdCfg.InsecureSkipVerify {
×
341
                extraArgs = append(extraArgs, "--db.etcd.insecure_skip_verify")
×
342
        }
×
343

344
        if cluster {
×
345
                clusterArgs := []string{
×
346
                        "--cluster.enable-leader-election",
×
347
                        fmt.Sprintf("--cluster.id=%v", name),
×
348
                        fmt.Sprintf("--cluster.leader-session-ttl=%v",
×
349
                                leaderSessionTTL),
×
350
                }
×
351
                extraArgs = append(extraArgs, clusterArgs...)
×
352
                extraArgs = append(
×
353
                        extraArgs, "--healthcheck.leader.interval=10s",
×
354
                )
×
355
        }
×
356

357
        return extraArgs
×
358
}
359

360
// GetLogDir returns the passed --logdir flag or the default value if it wasn't
361
// set.
362
func GetLogDir() string {
×
363
        if logSubDir != nil && *logSubDir != "" {
×
364
                return *logSubDir
×
365
        }
×
366

367
        return "."
×
368
}
369

370
// CopyFile copies the file src to dest.
371
func CopyFile(dest, src string) error {
×
372
        s, err := os.Open(src)
×
373
        if err != nil {
×
374
                return err
×
375
        }
×
376
        defer s.Close()
×
377

×
378
        d, err := os.Create(dest)
×
379
        if err != nil {
×
380
                return err
×
381
        }
×
382

383
        if _, err := io.Copy(d, s); err != nil {
×
384
                d.Close()
×
385
                return err
×
386
        }
×
387

388
        return d.Close()
×
389
}
390

391
// GetBtcdBinary returns the full path to the binary of the custom built btcd
392
// executable or an empty string if none is set.
393
func GetBtcdBinary() string {
×
394
        if btcdExecutable != nil {
×
395
                return *btcdExecutable
×
396
        }
×
397

398
        return ""
×
399
}
400

401
func init() {
1✔
402
        // Before we start any node, we need to make sure that any btcd or
1✔
403
        // bitcoind node that is started through the RPC harness uses a unique
1✔
404
        // port as well to avoid any port collisions.
1✔
405
        rpctest.ListenAddressGenerator =
1✔
406
                port.GenerateSystemUniqueListenerAddresses
1✔
407
}
1✔
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