Skip to content

Commit c39af5d

Browse files
committed
multi: add sql backend support
1 parent 671ae77 commit c39af5d

17 files changed

+501
-518
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ DOCKER_TOOLS = docker run \
4444

4545
TEST_FLAGS = -test.timeout=20m
4646

47+
DEV_TAGS = kvdb_postgres kvdb_sqlite
48+
49+
4750
UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)
4851
LDFLAGS := -X main.Commit=$(shell git describe --tags)
4952
RELEASE_LDFLAGS := -s -w -buildid= $(LDFLAGS)
@@ -70,7 +73,7 @@ build:
7073

7174
install:
7275
@$(call print, "Installing chantools.")
73-
$(GOINSTALL) -ldflags "$(LDFLAGS)" ./...
76+
$(GOINSTALL) -tags="$(DEV_TAGS)" -ldflags "$(LDFLAGS)" ./...
7477

7578
release:
7679
@$(call print, "Creating release of chantools.")

cmd/chantools/chanbackup.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67

78
"github.com/lightninglabs/chantools/lnd"
89
"github.com/lightningnetwork/lnd/chanbackup"
@@ -54,11 +55,17 @@ func (c *chanBackupCommand) Execute(_ *cobra.Command, _ []string) error {
5455
return errors.New("backup file is required")
5556
}
5657

57-
// Check that we have a channel DB.
58-
if c.ChannelDB == "" {
59-
return errors.New("channel DB is required")
58+
var opts []lnd.DBOption
59+
60+
// In case the channel DB is specified, we get the graph dir from it.
61+
if c.ChannelDB != "" {
62+
graphDir := filepath.Dir(c.ChannelDB)
63+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
6064
}
61-
db, err := lnd.OpenDB(c.ChannelDB, true)
65+
66+
dbConfig := GetDBConfig()
67+
68+
db, err := lnd.OpenChannelDB(dbConfig, true, chainParams.Name, opts...)
6269
if err != nil {
6370
return fmt.Errorf("error opening rescue DB: %w", err)
6471
}

cmd/chantools/deletepayments.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
5+
"path/filepath"
66

77
"github.com/lightninglabs/chantools/lnd"
88
"github.com/spf13/cobra"
@@ -44,11 +44,17 @@ run lnd ` + lndVersion + ` or later after using this command!'`,
4444
}
4545

4646
func (c *deletePaymentsCommand) Execute(_ *cobra.Command, _ []string) error {
47-
// Check that we have a channel DB.
48-
if c.ChannelDB == "" {
49-
return errors.New("channel DB is required")
47+
var opts []lnd.DBOption
48+
49+
// In case the channel DB is specified, we get the graph dir from it.
50+
if c.ChannelDB != "" {
51+
graphDir := filepath.Dir(c.ChannelDB)
52+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
5053
}
51-
db, err := lnd.OpenDB(c.ChannelDB, false)
54+
55+
dbConfig := GetDBConfig()
56+
57+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
5258
if err != nil {
5359
return fmt.Errorf("error opening rescue DB: %w", err)
5460
}

cmd/chantools/dropchannelgraph.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"errors"
77
"fmt"
8+
"path/filepath"
89
"time"
910

1011
"github.com/btcsuite/btcd/btcec/v2"
@@ -60,8 +61,8 @@ chantools dropchannelgraph \
6061
RunE: cc.Execute,
6162
}
6263
cc.cmd.Flags().StringVar(
63-
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+
64-
"channels from",
64+
&cc.ChannelDB, "channeldb", "", "lnd's channel database file "+
65+
"to drop channels from",
6566
)
6667
cc.cmd.Flags().Uint64Var(
6768
&cc.SingleChannel, "single_channel", 0, "the single channel "+
@@ -81,11 +82,16 @@ chantools dropchannelgraph \
8182
}
8283

8384
func (c *dropChannelGraphCommand) Execute(_ *cobra.Command, _ []string) error {
84-
// Check that we have a channel DB.
85-
if c.ChannelDB == "" {
86-
return errors.New("channel DB is required")
85+
dbConfig := GetDBConfig()
86+
var opts []lnd.DBOption
87+
88+
// In case the channel DB is specified, we get the graph dir from it.
89+
if c.ChannelDB != "" {
90+
graphDir := filepath.Dir(c.ChannelDB)
91+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
8792
}
88-
db, err := lnd.OpenDB(c.ChannelDB, false)
93+
94+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
8995
if err != nil {
9096
return fmt.Errorf("error opening rescue DB: %w", err)
9197
}

cmd/chantools/dropgraphzombies.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
5+
"path/filepath"
66

77
"github.com/lightninglabs/chantools/lnd"
88
"github.com/lightningnetwork/lnd/channeldb"
@@ -51,11 +51,17 @@ run lnd ` + lndVersion + ` or later after using this command!'`,
5151
}
5252

5353
func (c *dropGraphZombiesCommand) Execute(_ *cobra.Command, _ []string) error {
54-
// Check that we have a channel DB.
55-
if c.ChannelDB == "" {
56-
return errors.New("channel DB is required")
54+
var opts []lnd.DBOption
55+
56+
// In case the channel DB is specified, we get the graph dir from it.
57+
if c.ChannelDB != "" {
58+
graphDir := filepath.Dir(c.ChannelDB)
59+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
5760
}
58-
db, err := lnd.OpenDB(c.ChannelDB, false)
61+
62+
dbConfig := GetDBConfig()
63+
64+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
5965
if err != nil {
6066
return fmt.Errorf("error opening rescue DB: %w", err)
6167
}

cmd/chantools/dumpchannels.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67

78
"github.com/davecgh/go-spew/spew"
89
"github.com/lightninglabs/chantools/dump"
@@ -53,11 +54,17 @@ given lnd channel.db gile in a human readable format.`,
5354
}
5455

5556
func (c *dumpChannelsCommand) Execute(_ *cobra.Command, _ []string) error {
56-
// Check that we have a channel DB.
57-
if c.ChannelDB == "" {
58-
return errors.New("channel DB is required")
57+
var opts []lnd.DBOption
58+
59+
// In case the channel DB is specified, we get the graph dir from it.
60+
if c.ChannelDB != "" {
61+
graphDir := filepath.Dir(c.ChannelDB)
62+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
5963
}
60-
db, err := lnd.OpenDB(c.ChannelDB, true)
64+
65+
dbConfig := GetDBConfig()
66+
67+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
6168
if err != nil {
6269
return fmt.Errorf("error opening rescue DB: %w", err)
6370
}

cmd/chantools/forceclose.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"bytes"
55
"encoding/hex"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"io"
109
"os"
10+
"path/filepath"
1111
"time"
1212

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

81-
// Check that we have a channel DB.
82-
if c.ChannelDB == "" {
83-
return errors.New("rescue DB is required")
81+
var opts []lnd.DBOption
82+
83+
// In case the channel DB is specified, we get the graph dir from it.
84+
if c.ChannelDB != "" {
85+
graphDir := filepath.Dir(c.ChannelDB)
86+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
8487
}
85-
db, err := lnd.OpenDB(c.ChannelDB, true)
88+
89+
dbConfig := GetDBConfig()
90+
91+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
8692
if err != nil {
8793
return fmt.Errorf("error opening rescue DB: %w", err)
8894
}

cmd/chantools/migratedb.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
5+
"path/filepath"
66

77
"github.com/lightninglabs/chantools/lnd"
88
"github.com/spf13/cobra"
@@ -40,13 +40,19 @@ run lnd ` + lndVersion + ` or later after using this command!'`,
4040
}
4141

4242
func (c *migrateDBCommand) Execute(_ *cobra.Command, _ []string) error {
43-
// Check that we have a channel DB.
44-
if c.ChannelDB == "" {
45-
return errors.New("channel DB is required")
43+
var opts []lnd.DBOption
44+
45+
// In case the channel DB is specified, we get the graph dir from it.
46+
if c.ChannelDB != "" {
47+
graphDir := filepath.Dir(c.ChannelDB)
48+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
4649
}
47-
db, err := lnd.OpenDB(c.ChannelDB, false)
50+
51+
dbConfig := GetDBConfig()
52+
53+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
4854
if err != nil {
49-
return fmt.Errorf("error opening DB: %w", err)
55+
return fmt.Errorf("error opening rescue DB: %w", err)
5056
}
5157

5258
return db.Close()

cmd/chantools/removechannel.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
5+
"path/filepath"
66
"strconv"
77
"strings"
88

@@ -52,13 +52,19 @@ run lnd ` + lndVersion + ` or later after using this command!`,
5252
}
5353

5454
func (c *removeChannelCommand) Execute(_ *cobra.Command, _ []string) error {
55-
// Check that we have a channel DB.
56-
if c.ChannelDB == "" {
57-
return errors.New("channel DB is required")
55+
var opts []lnd.DBOption
56+
57+
// In case the channel DB is specified, we get the graph dir from it.
58+
if c.ChannelDB != "" {
59+
graphDir := filepath.Dir(c.ChannelDB)
60+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
5861
}
59-
db, err := lnd.OpenDB(c.ChannelDB, false)
62+
63+
dbConfig := GetDBConfig()
64+
65+
db, err := lnd.OpenChannelDB(dbConfig, false, chainParams.Name, opts...)
6066
if err != nil {
61-
return fmt.Errorf("error opening channel DB: %w", err)
67+
return fmt.Errorf("error opening rescue DB: %w", err)
6268
}
6369
defer func() {
6470
if err := db.Close(); err != nil {

cmd/chantools/rescueclosed.go

+38-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"os"
10+
"path/filepath"
1011
"regexp"
1112
"time"
1213

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

126+
cmdErr := errors.New("you either need to specify --channeldb and " +
127+
"--fromsummary or --force_close_addr and " +
128+
"--commit_point but not a mixture of them")
129+
125130
// What way of recovery has the user chosen? From summary and DB or from
126131
// address and commit point?
127132
switch {
128-
case c.ChannelDB != "":
129-
db, err := lnd.OpenDB(c.ChannelDB, true)
130-
if err != nil {
131-
return fmt.Errorf("error opening rescue DB: %w", err)
132-
}
133-
134-
// Parse channel entries from any of the possible input files.
135-
entries, err := c.inputs.parseInputType()
136-
if err != nil {
137-
return err
138-
}
139-
140-
commitPoints, err := commitPointsFromDB(db.ChannelStateDB())
141-
if err != nil {
142-
return fmt.Errorf("error reading commit points from "+
143-
"db: %w", err)
144-
}
145-
return rescueClosedChannels(
146-
c.NumKeys, extendedKey, entries, commitPoints,
147-
)
148-
149133
case c.Addr != "":
150134
// First parse address to get targetPubKeyHash from it later.
151135
targetAddr, err := btcutil.DecodeAddress(c.Addr, chainParams)
@@ -185,9 +169,39 @@ func (c *rescueClosedCommand) Execute(_ *cobra.Command, _ []string) error {
185169
)
186170

187171
default:
188-
return errors.New("you either need to specify --channeldb and " +
189-
"--fromsummary or --force_close_addr and " +
190-
"--commit_point but not a mixture of them")
172+
var opts []lnd.DBOption
173+
174+
// In case the channel DB is specified, we get the graph dir
175+
// from it.
176+
if c.ChannelDB != "" {
177+
graphDir := filepath.Dir(c.ChannelDB)
178+
opts = append(opts, lnd.WithCustomGraphDir(graphDir))
179+
}
180+
181+
dbConfig := GetDBConfig()
182+
183+
db, err := lnd.OpenChannelDB(
184+
dbConfig, false, chainParams.Name, opts...,
185+
)
186+
if err != nil {
187+
return fmt.Errorf("error opening rescue DB: %w, %w",
188+
err, cmdErr)
189+
}
190+
191+
// Parse channel entries from any of the possible input files.
192+
entries, err := c.inputs.parseInputType()
193+
if err != nil {
194+
return err
195+
}
196+
197+
commitPoints, err := commitPointsFromDB(db.ChannelStateDB())
198+
if err != nil {
199+
return fmt.Errorf("error reading commit points from "+
200+
"db: %w", err)
201+
}
202+
return rescueClosedChannels(
203+
c.NumKeys, extendedKey, entries, commitPoints,
204+
)
191205
}
192206
}
193207

0 commit comments

Comments
 (0)