Skip to content

Commit 10a8b28

Browse files
committed
test: fix unit tests
1 parent c39af5d commit 10a8b28

10 files changed

+63
-11
lines changed

cmd/chantools/chanbackup.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type chanBackupCommand struct {
1414
ChannelDB string
1515
MultiFile string
1616

17-
rootKey *rootKey
18-
cmd *cobra.Command
17+
rootKey *rootKey
18+
cmd *cobra.Command
19+
dbConfig *lnd.DB
1920
}
2021

2122
func newChanBackupCommand() *cobra.Command {
@@ -63,7 +64,12 @@ func (c *chanBackupCommand) Execute(_ *cobra.Command, _ []string) error {
6364
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
6465
}
6566

66-
dbConfig := GetDBConfig()
67+
var dbConfig lnd.DB
68+
if c.dbConfig == nil {
69+
dbConfig = GetDBConfig()
70+
} else {
71+
dbConfig = *c.dbConfig
72+
}
6773

6874
db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...)
6975
if err != nil {

cmd/chantools/chanbackup_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"testing"
55

6+
"github.com/lightninglabs/chantools/lnd"
67
"github.com/stretchr/testify/require"
78
)
89

@@ -19,6 +20,12 @@ func TestChanBackupAndDumpBackup(t *testing.T) {
1920
ChannelDB: h.testdataFile("channel.db"),
2021
MultiFile: h.tempFile("extracted.backup"),
2122
rootKey: &rootKey{RootKey: rootKeyAezeed},
23+
dbConfig: &lnd.DB{
24+
Backend: "bolt",
25+
Bolt: &lnd.Bolt{
26+
DataDir: h.tempDir,
27+
},
28+
},
2229
}
2330

2431
err := makeBackup.Execute(nil, nil)

cmd/chantools/compactdb.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/coreos/bbolt"
8+
"github.com/lightninglabs/chantools/lnd"
89
"github.com/spf13/cobra"
910
)
1011

@@ -17,6 +18,7 @@ type compactDBCommand struct {
1718
TxMaxSize int64
1819
SourceDB string
1920
DestDB string
21+
dbConfig *lnd.DB
2022

2123
cmd *cobra.Command
2224
}

cmd/chantools/compactdb_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"testing"
55

6+
"github.com/lightninglabs/chantools/lnd"
67
"github.com/stretchr/testify/require"
78
)
89

@@ -30,6 +31,10 @@ func TestCompactDBAndDumpChannels(t *testing.T) {
3031
// the logged dump.
3132
dump := &dumpChannelsCommand{
3233
ChannelDB: compact.SourceDB,
34+
dbConfig: &lnd.DB{
35+
Backend: "bolt",
36+
Bolt: &lnd.Bolt{},
37+
},
3338
}
3439
h.clearLog()
3540
err = dump.Execute(nil, nil)
@@ -38,6 +43,12 @@ func TestCompactDBAndDumpChannels(t *testing.T) {
3843

3944
h.clearLog()
4045
dump.ChannelDB = compact.DestDB
46+
dump.dbConfig = &lnd.DB{
47+
Backend: "bolt",
48+
Bolt: &lnd.Bolt{
49+
Name: "compacted.db",
50+
},
51+
}
4152
err = dump.Execute(nil, nil)
4253
require.NoError(t, err)
4354
destDump := h.getLog()

cmd/chantools/dumpchannels.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type dumpChannelsCommand struct {
1818
Pending bool
1919
WaitingClose bool
2020

21-
cmd *cobra.Command
21+
cmd *cobra.Command
22+
dbConfig *lnd.DB
2223
}
2324

2425
func newDumpChannelsCommand() *cobra.Command {
@@ -61,10 +62,14 @@ func (c *dumpChannelsCommand) Execute(_ *cobra.Command, _ []string) error {
6162
graphDir := filepath.Dir(c.ChannelDB)
6263
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
6364
}
65+
var dbConfig lnd.DB
66+
if c.dbConfig == nil {
67+
dbConfig = GetDBConfig()
68+
} else {
69+
dbConfig = *c.dbConfig
70+
}
6471

65-
dbConfig := GetDBConfig()
66-
67-
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
72+
db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...)
6873
if err != nil {
6974
return fmt.Errorf("error opening rescue DB: %w", err)
7075
}

cmd/chantools/root.go

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ func main() {
118118
rootCmd.PersistentFlags().String("db.bolt.tower-dir", defaultDataDir,
119119
"Lnd watchtower dir where bolt dbs are located",
120120
)
121+
rootCmd.PersistentFlags().String("db.bolt.name", "", "Name of the bolt"+
122+
"db to use",
123+
)
121124

122125
// Sqlite settings
123126
rootCmd.PersistentFlags().String("db.sqlite.data-dir", defaultDataDir,
@@ -435,6 +438,7 @@ func GetDBConfig() lnd.DB {
435438
DBTimeout: viper.GetDuration("db.bolt.dbtimeout"),
436439
DataDir: viper.GetString("db.bolt.data-dir"),
437440
TowerDir: viper.GetString("db.bolt.tower-dir"),
441+
Name: viper.GetString("db.bolt.name"),
438442
},
439443
Sqlite: &lnd.Sqlite{
440444
DataDir: viper.GetString("db.sqlite.data-dir"),

cmd/chantools/root_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
datePattern = regexp.MustCompile(
3838
`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} `,
3939
)
40-
addressPattern = regexp.MustCompile(`\(0x[0-9a-f]{10}\)`)
40+
addressPattern = regexp.MustCompile(`\(0x[0-9a-f]*\)`)
4141
)
4242

4343
type harness struct {

cmd/chantools/walletinfo.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type walletInfoCommand struct {
3737
WalletDB string
3838
WithRootKey bool
3939
DumpAddrs bool
40+
dbConfig *lnd.DB
4041

4142
cmd *cobra.Command
4243
}
@@ -78,7 +79,12 @@ or simply press <enter> without entering a password when being prompted.`,
7879
}
7980

8081
func (c *walletInfoCommand) Execute(_ *cobra.Command, _ []string) error {
81-
cfg := GetDBConfig()
82+
var cfg lnd.DB
83+
if c.dbConfig == nil {
84+
cfg = GetDBConfig()
85+
} else {
86+
cfg = *c.dbConfig
87+
}
8288

8389
w, privateWalletPw, cleanup, err := lnd.OpenWallet(
8490
cfg, c.WalletDB, chainParams,

cmd/chantools/walletinfo_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func TestWalletInfo(t *testing.T) {
1919
info := &walletInfoCommand{
2020
WalletDB: h.testdataFile("wallet.db"),
2121
WithRootKey: true,
22+
dbConfig: &lnd.DB{
23+
Backend: "bolt",
24+
Bolt: &lnd.Bolt{
25+
DataDir: h.tempDir,
26+
},
27+
},
2228
}
2329

2430
t.Setenv(lnd.PasswordEnvName, testPassPhrase)

lnd/channeldb.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Bolt struct {
2222
DBTimeout time.Duration
2323
DataDir string
2424
TowerDir string
25+
Name string
2526
}
2627

2728
// Sqlite specifies the settings for the sqlite database.
@@ -131,7 +132,11 @@ func openDB(cfg DB, prefix, network string,
131132
var path string
132133
switch prefix {
133134
case lncfg.NSChannelDB:
134-
path = filepath.Join(graphDir, lncfg.ChannelDBName)
135+
if cfg.Bolt.Name != "" {
136+
path = filepath.Join(graphDir, cfg.Bolt.Name)
137+
} else {
138+
path = filepath.Join(graphDir, lncfg.ChannelDBName)
139+
}
135140

136141
case lncfg.NSMacaroonDB:
137142
path = filepath.Join(walletDir, lncfg.MacaroonDBName)
@@ -152,7 +157,7 @@ func openDB(cfg DB, prefix, network string,
152157
}
153158

154159
const (
155-
noFreelistSync = true
160+
noFreelistSync = false
156161
timeout = time.Minute
157162
)
158163

0 commit comments

Comments
 (0)