Skip to content

Commit

Permalink
multi: add sql backend support
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggie1984 committed Nov 22, 2024
1 parent 671ae77 commit 1aac4dc
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 71 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ DOCKER_TOOLS = docker run \

TEST_FLAGS = -test.timeout=20m

DEV_TAGS = kvdb_postgres kvdb_sqlite


UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)
LDFLAGS := -X main.Commit=$(shell git describe --tags)
RELEASE_LDFLAGS := -s -w -buildid= $(LDFLAGS)
Expand All @@ -70,7 +73,7 @@ build:

install:
@$(call print, "Installing chantools.")
$(GOINSTALL) -ldflags "$(LDFLAGS)" ./...
$(GOINSTALL) -tags="$(DEV_TAGS)" -ldflags "$(LDFLAGS)" ./...

release:
@$(call print, "Creating release of chantools.")
Expand Down
11 changes: 7 additions & 4 deletions cmd/chantools/chanbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ func (c *chanBackupCommand) Execute(_ *cobra.Command, _ []string) error {
}

// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, true)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/chantools/deletepayments.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"

"github.com/lightninglabs/chantools/lnd"
Expand Down Expand Up @@ -45,10 +44,12 @@ run lnd ` + lndVersion + ` or later after using this command!'`,

func (c *deletePaymentsCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, false)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/chantools/dropchannelgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ chantools dropchannelgraph \

func (c *dropChannelGraphCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, false)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/chantools/dropgraphzombies.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"

"github.com/lightninglabs/chantools/lnd"
Expand Down Expand Up @@ -52,10 +51,12 @@ run lnd ` + lndVersion + ` or later after using this command!'`,

func (c *dropGraphZombiesCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, false)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/chantools/dumpchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ given lnd channel.db gile in a human readable format.`,

func (c *dumpChannelsCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, true)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/chantools/forceclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -79,10 +78,12 @@ func (c *forceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
}

// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("rescue DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, true)
// if c.ChannelDB == "" {
// return errors.New("rescue DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/chantools/migratedb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"

"github.com/lightninglabs/chantools/lnd"
Expand Down Expand Up @@ -41,12 +40,14 @@ run lnd ` + lndVersion + ` or later after using this command!'`,

func (c *migrateDBCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, false)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening DB: %w", err)
return fmt.Errorf("error opening rescue DB: %w", err)
}

return db.Close()
Expand Down
13 changes: 7 additions & 6 deletions cmd/chantools/removechannel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -53,12 +52,14 @@ run lnd ` + lndVersion + ` or later after using this command!`,

func (c *removeChannelCommand) Execute(_ *cobra.Command, _ []string) error {
// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
}
db, err := lnd.OpenDB(c.ChannelDB, false)
// if c.ChannelDB == "" {
// return errors.New("channel DB is required")
// }
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening channel DB: %w", err)
return fmt.Errorf("error opening rescue DB: %w", err)
}
defer func() {
if err := db.Close(); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/chantools/rescueclosed.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ func (c *rescueClosedCommand) Execute(_ *cobra.Command, _ []string) error {
// What way of recovery has the user chosen? From summary and DB or from
// address and commit point?
switch {
// Needs an update we don't use the string anymore
case c.ChannelDB != "":
db, err := lnd.OpenDB(c.ChannelDB, true)
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/chantools/rescuefunding.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ func (c *rescueFundingCommand) Execute(_ *cobra.Command, _ []string) error {
"channel point or both local and remote pubkey")

case c.ChannelDB != "" && c.DBChannelPoint != "":
db, err := lnd.OpenDB(c.ChannelDB, true)
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
56 changes: 53 additions & 3 deletions cmd/chantools/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btclog"
Expand All @@ -19,7 +20,10 @@ import (
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/chanbackup"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb/postgres"
"github.com/lightningnetwork/lnd/kvdb/sqlite"
"github.com/lightningnetwork/lnd/peer"
"github.com/ory/viper"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -50,6 +54,8 @@ var (
logWriter = build.NewRotatingLogWriter()
log = build.NewSubLogger("CHAN", genSubLogger(logWriter))
chainParams = &chaincfg.MainNetParams
// defaultDataDir is the default data directory for lnd.
defaultDataDir = btcutil.AppDataDir("lnd", false)
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -96,6 +102,24 @@ func main() {
&Signet, "signet", "s", false, "Indicates if the public "+
"signet parameters should be used",
)
rootCmd.PersistentFlags().String("db.backend", "bolt", "The selected database backend (bolt/etcd/postgres/sqlite)")

// Bolt settings
rootCmd.PersistentFlags().Duration("db.bolt.dbtimeout", 10*time.Second, "Specify the timeout value used when opening the database")
rootCmd.PersistentFlags().String("db.bolt.data-dir", defaultDataDir, "Lnd data dir where bolt dbs are located")
rootCmd.PersistentFlags().String("db.bolt.tower-dir", defaultDataDir, "Lnd watchtower dir where bolt dbs are located")

// Sqlite settings
rootCmd.PersistentFlags().String("db.sqlite.data-dir", defaultDataDir, "Lnd data dir where sqlite dbs are located")
rootCmd.PersistentFlags().String("db.sqlite.tower-dir", defaultDataDir, "Lnd watchtower dir where sqlite dbs are located")
rootCmd.PersistentFlags().Duration("db.sqlite.timeout", 10*time.Second, "Specify the timeout value used when opening the database")

// Postgres settings
rootCmd.PersistentFlags().String("db.postgres.dsn", "", "Postgres connection string")
rootCmd.PersistentFlags().Duration("db.postgres.timeout", 10*time.Second, "Specify the timeout value used when opening the database")

// Bind flags to viper
viper.BindPFlags(rootCmd.PersistentFlags())

rootCmd.AddCommand(
newChanBackupCommand(),
Expand Down Expand Up @@ -275,10 +299,11 @@ func (f *inputFlags) parseInputType() ([]*dataformat.SummaryEntry, error) {
target = &dataformat.SummaryEntryFile{}

case f.FromChannelDB != "":
db, err := lnd.OpenDB(f.FromChannelDB, true)
dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, chainParams.Name)
if err != nil {
return nil, fmt.Errorf("error opening channel DB: %w",
err)
return nil, fmt.Errorf("error opening rescue DB: %w", err)
}
target = &dataformat.ChannelDBFile{DB: db.ChannelStateDB()}
return target.AsSummaryEntries()
Expand Down Expand Up @@ -365,3 +390,28 @@ func newExplorerAPI(apiURL string) *btc.ExplorerAPI {
// Otherwise use the provided URL.
return &btc.ExplorerAPI{BaseURL: apiURL}
}

// GetDBConfig returns the database configuration from viper/cobra flags
func GetDBConfig() lnd.DB {
return lnd.DB{
Backend: viper.GetString("db.backend"),
Bolt: &lnd.Bolt{
DBTimeout: viper.GetDuration("db.bolt.dbtimeout"),
DataDir: viper.GetString("db.bolt.data-dir"),
TowerDir: viper.GetString("db.bolt.tower-dir"),
},
Sqlite: &lnd.Sqlite{
DataDir: viper.GetString("db.sqlite.data-dir"),
TowerDir: viper.GetString("db.sqlite.tower-dir"),
Config: &sqlite.Config{
MaxConnections: viper.GetInt("db.sqlite.timeout"),
},
},
Postgres: &postgres.Config{
Dsn: viper.GetString("db.postgres.dsn"),
MaxConnections: viper.GetInt("db.postgres.max-connections"),
Timeout: viper.GetDuration("db.postgres.timeout"),
},
// Add Etcd config if needed
}
}
Loading

0 comments on commit 1aac4dc

Please sign in to comment.