From fff484a6b119a91078c7f10acbf3f0539d8f6e67 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sat, 23 Nov 2024 02:10:40 +0100 Subject: [PATCH] test: fix unit tests --- cmd/chantools/chanbackup.go | 12 +++++++++--- cmd/chantools/chanbackup_test.go | 7 +++++++ cmd/chantools/compactdb_test.go | 11 +++++++++++ cmd/chantools/dumpchannels.go | 13 +++++++++---- cmd/chantools/root.go | 4 ++++ cmd/chantools/root_test.go | 2 +- cmd/chantools/walletinfo.go | 8 +++++++- cmd/chantools/walletinfo_test.go | 6 ++++++ lnd/channeldb.go | 9 +++++++-- 9 files changed, 61 insertions(+), 11 deletions(-) diff --git a/cmd/chantools/chanbackup.go b/cmd/chantools/chanbackup.go index 4850cda..1b28c97 100644 --- a/cmd/chantools/chanbackup.go +++ b/cmd/chantools/chanbackup.go @@ -14,8 +14,9 @@ type chanBackupCommand struct { ChannelDB string MultiFile string - rootKey *rootKey - cmd *cobra.Command + rootKey *rootKey + cmd *cobra.Command + dbConfig *lnd.DB } func newChanBackupCommand() *cobra.Command { @@ -63,7 +64,12 @@ func (c *chanBackupCommand) Execute(_ *cobra.Command, _ []string) error { opts = append(opts, lnd.WithCustomGraphDir(graphDir)) } - dbConfig := GetDBConfig() + var dbConfig lnd.DB + if c.dbConfig == nil { + dbConfig = GetDBConfig() + } else { + dbConfig = *c.dbConfig + } db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...) if err != nil { diff --git a/cmd/chantools/chanbackup_test.go b/cmd/chantools/chanbackup_test.go index c16d48d..fc019a6 100644 --- a/cmd/chantools/chanbackup_test.go +++ b/cmd/chantools/chanbackup_test.go @@ -3,6 +3,7 @@ package main import ( "testing" + "github.com/lightninglabs/chantools/lnd" "github.com/stretchr/testify/require" ) @@ -19,6 +20,12 @@ func TestChanBackupAndDumpBackup(t *testing.T) { ChannelDB: h.testdataFile("channel.db"), MultiFile: h.tempFile("extracted.backup"), rootKey: &rootKey{RootKey: rootKeyAezeed}, + dbConfig: &lnd.DB{ + Backend: "bolt", + Bolt: &lnd.Bolt{ + DataDir: h.tempDir, + }, + }, } err := makeBackup.Execute(nil, nil) diff --git a/cmd/chantools/compactdb_test.go b/cmd/chantools/compactdb_test.go index 7d44d71..a39f53a 100644 --- a/cmd/chantools/compactdb_test.go +++ b/cmd/chantools/compactdb_test.go @@ -3,6 +3,7 @@ package main import ( "testing" + "github.com/lightninglabs/chantools/lnd" "github.com/stretchr/testify/require" ) @@ -30,6 +31,10 @@ func TestCompactDBAndDumpChannels(t *testing.T) { // the logged dump. dump := &dumpChannelsCommand{ ChannelDB: compact.SourceDB, + dbConfig: &lnd.DB{ + Backend: "bolt", + Bolt: &lnd.Bolt{}, + }, } h.clearLog() err = dump.Execute(nil, nil) @@ -38,6 +43,12 @@ func TestCompactDBAndDumpChannels(t *testing.T) { h.clearLog() dump.ChannelDB = compact.DestDB + dump.dbConfig = &lnd.DB{ + Backend: "bolt", + Bolt: &lnd.Bolt{ + Name: "compacted.db", + }, + } err = dump.Execute(nil, nil) require.NoError(t, err) destDump := h.getLog() diff --git a/cmd/chantools/dumpchannels.go b/cmd/chantools/dumpchannels.go index 00fb2f1..05013ff 100644 --- a/cmd/chantools/dumpchannels.go +++ b/cmd/chantools/dumpchannels.go @@ -18,7 +18,8 @@ type dumpChannelsCommand struct { Pending bool WaitingClose bool - cmd *cobra.Command + cmd *cobra.Command + dbConfig *lnd.DB } func newDumpChannelsCommand() *cobra.Command { @@ -61,10 +62,14 @@ func (c *dumpChannelsCommand) Execute(_ *cobra.Command, _ []string) error { graphDir := filepath.Dir(c.ChannelDB) opts = append(opts, lnd.WithCustomGraphDir(graphDir)) } + var dbConfig lnd.DB + if c.dbConfig == nil { + dbConfig = GetDBConfig() + } else { + dbConfig = *c.dbConfig + } - dbConfig := GetDBConfig() - - db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...) + db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...) if err != nil { return fmt.Errorf("error opening rescue DB: %w", err) } diff --git a/cmd/chantools/root.go b/cmd/chantools/root.go index 9339bd5..af87895 100644 --- a/cmd/chantools/root.go +++ b/cmd/chantools/root.go @@ -118,6 +118,9 @@ func main() { rootCmd.PersistentFlags().String("db.bolt.tower-dir", defaultDataDir, "Lnd watchtower dir where bolt dbs are located", ) + rootCmd.PersistentFlags().String("db.bolt.name", "", "Name of the bolt"+ + "db to use", + ) // Sqlite settings rootCmd.PersistentFlags().String("db.sqlite.data-dir", defaultDataDir, @@ -435,6 +438,7 @@ func GetDBConfig() lnd.DB { DBTimeout: viper.GetDuration("db.bolt.dbtimeout"), DataDir: viper.GetString("db.bolt.data-dir"), TowerDir: viper.GetString("db.bolt.tower-dir"), + Name: viper.GetString("db.bolt.name"), }, Sqlite: &lnd.Sqlite{ DataDir: viper.GetString("db.sqlite.data-dir"), diff --git a/cmd/chantools/root_test.go b/cmd/chantools/root_test.go index c94b665..8825a35 100644 --- a/cmd/chantools/root_test.go +++ b/cmd/chantools/root_test.go @@ -37,7 +37,7 @@ var ( datePattern = regexp.MustCompile( `\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} `, ) - addressPattern = regexp.MustCompile(`\(0x[0-9a-f]{10}\)`) + addressPattern = regexp.MustCompile(`\(0x[0-9a-f]*\)`) ) type harness struct { diff --git a/cmd/chantools/walletinfo.go b/cmd/chantools/walletinfo.go index d16fa9a..3b3873e 100644 --- a/cmd/chantools/walletinfo.go +++ b/cmd/chantools/walletinfo.go @@ -37,6 +37,7 @@ type walletInfoCommand struct { WalletDB string WithRootKey bool DumpAddrs bool + dbConfig *lnd.DB cmd *cobra.Command } @@ -78,7 +79,12 @@ or simply press without entering a password when being prompted.`, } func (c *walletInfoCommand) Execute(_ *cobra.Command, _ []string) error { - cfg := GetDBConfig() + var cfg lnd.DB + if c.dbConfig == nil { + cfg = GetDBConfig() + } else { + cfg = *c.dbConfig + } w, privateWalletPw, cleanup, err := lnd.OpenWallet( cfg, c.WalletDB, chainParams, diff --git a/cmd/chantools/walletinfo_test.go b/cmd/chantools/walletinfo_test.go index a5a841d..1e95f75 100644 --- a/cmd/chantools/walletinfo_test.go +++ b/cmd/chantools/walletinfo_test.go @@ -19,6 +19,12 @@ func TestWalletInfo(t *testing.T) { info := &walletInfoCommand{ WalletDB: h.testdataFile("wallet.db"), WithRootKey: true, + dbConfig: &lnd.DB{ + Backend: "bolt", + Bolt: &lnd.Bolt{ + DataDir: h.tempDir, + }, + }, } t.Setenv(lnd.PasswordEnvName, testPassPhrase) diff --git a/lnd/channeldb.go b/lnd/channeldb.go index da6178e..d1001c0 100644 --- a/lnd/channeldb.go +++ b/lnd/channeldb.go @@ -22,6 +22,7 @@ type Bolt struct { DBTimeout time.Duration DataDir string TowerDir string + Name string } // Sqlite specifies the settings for the sqlite database. @@ -131,7 +132,11 @@ func openDB(cfg DB, prefix, network string, var path string switch prefix { case lncfg.NSChannelDB: - path = filepath.Join(graphDir, lncfg.ChannelDBName) + if cfg.Bolt.Name != "" { + path = filepath.Join(graphDir, cfg.Bolt.Name) + } else { + path = filepath.Join(graphDir, lncfg.ChannelDBName) + } case lncfg.NSMacaroonDB: path = filepath.Join(walletDir, lncfg.MacaroonDBName) @@ -152,7 +157,7 @@ func openDB(cfg DB, prefix, network string, } const ( - noFreelistSync = true + noFreelistSync = false timeout = time.Minute )