Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: add sql backend support #167

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 19 additions & 6 deletions cmd/chantools/chanbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"path/filepath"

"github.com/lightninglabs/chantools/lnd"
"github.com/lightningnetwork/lnd/chanbackup"
Expand All @@ -13,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 @@ -54,11 +56,22 @@ func (c *chanBackupCommand) Execute(_ *cobra.Command, _ []string) error {
return errors.New("backup file is required")
}

// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("channel DB is required")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
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
}
db, err := lnd.OpenDB(c.ChannelDB, true)

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

import (
"errors"
"fmt"
"path/filepath"

"github.com/lightninglabs/chantools/lnd"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,11 +44,17 @@ 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")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, false)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/chantools/dropchannelgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"path/filepath"
"time"

"github.com/btcsuite/btcd/btcec/v2"
Expand Down Expand Up @@ -60,8 +61,8 @@ chantools dropchannelgraph \
RunE: cc.Execute,
}
cc.cmd.Flags().StringVar(
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+
"channels from",
&cc.ChannelDB, "channeldb", "", "lnd's channel database file "+
"to drop channels from",
)
cc.cmd.Flags().Uint64Var(
&cc.SingleChannel, "single_channel", 0, "the single channel "+
Expand All @@ -81,11 +82,16 @@ 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")
dbConfig := GetDBConfig()
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, false)

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

import (
"errors"
"fmt"
"path/filepath"

"github.com/lightninglabs/chantools/lnd"
"github.com/lightningnetwork/lnd/channeldb"
Expand Down Expand Up @@ -51,11 +51,17 @@ 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")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, false)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
22 changes: 17 additions & 5 deletions cmd/chantools/dumpchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"path/filepath"

"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/chantools/dump"
Expand All @@ -17,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 @@ -53,11 +55,21 @@ 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")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
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
}
db, err := lnd.OpenDB(c.ChannelDB, true)

db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/chantools/forceclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/btcsuite/btcd/btcutil/hdkeychain"
Expand Down Expand Up @@ -78,11 +78,17 @@ func (c *forceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
return fmt.Errorf("error reading root key: %w", err)
}

// Check that we have a channel DB.
if c.ChannelDB == "" {
return errors.New("rescue DB is required")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, true)

dbConfig := GetDBConfig()

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

import (
"errors"
"fmt"
"path/filepath"

"github.com/lightninglabs/chantools/lnd"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,13 +40,19 @@ 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")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, false)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
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
18 changes: 12 additions & 6 deletions cmd/chantools/removechannel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -52,13 +52,19 @@ 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")
var opts []lnd.DBOption

// In case the channel DB is specified, we get the graph dir from it.
if c.ChannelDB != "" {
graphDir := filepath.Dir(c.ChannelDB)
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
}
db, err := lnd.OpenDB(c.ChannelDB, false)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
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
Loading
Loading