Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggie1984 committed Nov 23, 2024
1 parent c39af5d commit 10a8b28
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 11 deletions.
12 changes: 9 additions & 3 deletions cmd/chantools/chanbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions cmd/chantools/chanbackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"testing"

"github.com/lightninglabs/chantools/lnd"
"github.com/stretchr/testify/require"
)

Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/chantools/compactdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/coreos/bbolt"
"github.com/lightninglabs/chantools/lnd"
"github.com/spf13/cobra"
)

Expand All @@ -17,6 +18,7 @@ type compactDBCommand struct {
TxMaxSize int64
SourceDB string
DestDB string
dbConfig *lnd.DB

cmd *cobra.Command
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/chantools/compactdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"testing"

"github.com/lightninglabs/chantools/lnd"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
13 changes: 9 additions & 4 deletions cmd/chantools/dumpchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type dumpChannelsCommand struct {
Pending bool
WaitingClose bool

cmd *cobra.Command
cmd *cobra.Command
dbConfig *lnd.DB
}

func newDumpChannelsCommand() *cobra.Command {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/chantools/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/chantools/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion cmd/chantools/walletinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type walletInfoCommand struct {
WalletDB string
WithRootKey bool
DumpAddrs bool
dbConfig *lnd.DB

cmd *cobra.Command
}
Expand Down Expand Up @@ -78,7 +79,12 @@ or simply press <enter> 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,
Expand Down
6 changes: 6 additions & 0 deletions cmd/chantools/walletinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions lnd/channeldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Bolt struct {
DBTimeout time.Duration
DataDir string
TowerDir string
Name string
}

// Sqlite specifies the settings for the sqlite database.
Expand Down Expand Up @@ -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)
Expand All @@ -152,7 +157,7 @@ func openDB(cfg DB, prefix, network string,
}

const (
noFreelistSync = true
noFreelistSync = false
timeout = time.Minute
)

Expand Down

0 comments on commit 10a8b28

Please sign in to comment.