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 5cb7efe
Show file tree
Hide file tree
Showing 15 changed files with 494 additions and 100 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
15 changes: 11 additions & 4 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 Down Expand Up @@ -54,11 +55,17 @@ 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))
}
db, err := lnd.OpenDB(c.ChannelDB, true)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, 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/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, 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, 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, chainParams.Name, opts...)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}
Expand Down
15 changes: 11 additions & 4 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 Down Expand Up @@ -53,11 +54,17 @@ 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))
}
db, err := lnd.OpenDB(c.ChannelDB, true)

dbConfig := GetDBConfig()

db, err := lnd.OpenChannelDB(dbConfig, 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, 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, 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, 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
62 changes: 38 additions & 24 deletions cmd/chantools/rescueclosed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"time"

Expand Down Expand Up @@ -122,30 +123,13 @@ func (c *rescueClosedCommand) Execute(_ *cobra.Command, _ []string) error {
return fmt.Errorf("error reading root key: %w", err)
}

cmdErr := errors.New("you either need to specify --channeldb and " +
"--fromsummary or --force_close_addr and " +
"--commit_point but not a mixture of them")

// What way of recovery has the user chosen? From summary and DB or from
// address and commit point?
switch {
case c.ChannelDB != "":
db, err := lnd.OpenDB(c.ChannelDB, true)
if err != nil {
return fmt.Errorf("error opening rescue DB: %w", err)
}

// Parse channel entries from any of the possible input files.
entries, err := c.inputs.parseInputType()
if err != nil {
return err
}

commitPoints, err := commitPointsFromDB(db.ChannelStateDB())
if err != nil {
return fmt.Errorf("error reading commit points from "+
"db: %w", err)
}
return rescueClosedChannels(
c.NumKeys, extendedKey, entries, commitPoints,
)

case c.Addr != "":
// First parse address to get targetPubKeyHash from it later.
targetAddr, err := btcutil.DecodeAddress(c.Addr, chainParams)
Expand Down Expand Up @@ -185,9 +169,39 @@ func (c *rescueClosedCommand) Execute(_ *cobra.Command, _ []string) error {
)

default:
return errors.New("you either need to specify --channeldb and " +
"--fromsummary or --force_close_addr and " +
"--commit_point but not a mixture of them")
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))
}

dbConfig := GetDBConfig()

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

// Parse channel entries from any of the possible input files.
entries, err := c.inputs.parseInputType()
if err != nil {
return err
}

commitPoints, err := commitPointsFromDB(db.ChannelStateDB())
if err != nil {
return fmt.Errorf("error reading commit points from "+
"db: %w", err)
}
return rescueClosedChannels(
c.NumKeys, extendedKey, entries, commitPoints,
)
}
}

Expand Down
Loading

0 comments on commit 5cb7efe

Please sign in to comment.