From b07c7b2442e3f251de8fc4540c1b53c408c7e648 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Tue, 5 Jan 2021 12:01:34 +0100 Subject: [PATCH] cmd+doc: add new dropchannelgraph command --- README.md | 1 + cmd/chantools/dropchannelgraph.go | 72 +++++++++++++++++++++++++++++++ cmd/chantools/root.go | 3 +- doc/chantools.md | 1 + doc/chantools_dropchannelgraph.md | 38 ++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 cmd/chantools/dropchannelgraph.go create mode 100644 doc/chantools_dropchannelgraph.md diff --git a/README.md b/README.md index 0021c74..32c0cd2 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ Quick access: + [chanbackup](doc/chantools_chanbackup.md) + [compactdb](doc/chantools_compactdb.md) + [derivekey](doc/chantools_derivekey.md) ++ [dropchannelgraph](chantools_dropchannelgraph.md) + [dumpbackup](doc/chantools_dumpbackup.md) + [dumpchannels](doc/chantools_dumpchannels.md) + [filterbackup](doc/chantools_filterbackup.md) diff --git a/cmd/chantools/dropchannelgraph.go b/cmd/chantools/dropchannelgraph.go new file mode 100644 index 0000000..298b97c --- /dev/null +++ b/cmd/chantools/dropchannelgraph.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + + "github.com/guggero/chantools/lnd" + "github.com/spf13/cobra" +) + +var ( + nodeBucket = []byte("graph-node") + edgeBucket = []byte("graph-edge") + graphMetaBucket = []byte("graph-meta") +) + +type dropChannelGraphCommand struct { + ChannelDB string + + cmd *cobra.Command +} + +func newDropChannelGraphCommand() *cobra.Command { + cc := &dropChannelGraphCommand{} + cc.cmd = &cobra.Command{ + Use: "dropchannelgraph", + Short: "Remove all graph related data from a channel DB", + Long: `This command removes all graph data from a channel DB, +forcing the lnd node to do a full graph sync. + +CAUTION: Running this command will make it impossible to use the channel DB +with an older version of lnd. Downgrading is not possible and you'll need to +run lnd v0.12.0-beta or later after using this command!'`, + Example: `chantools dropchannelgraph \ + --channeldb ~/.lnd/data/graph/mainnet/channel.db`, + RunE: cc.Execute, + } + cc.cmd.Flags().StringVar( + &cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+ + "channels from", + ) + + return cc.cmd +} + +func (c *dropChannelGraphCommand) Execute(_ *cobra.Command, _ []string) error { + // Check that we have a channel DB. + if c.ChannelDB == "" { + return fmt.Errorf("channel DB is required") + } + db, err := lnd.OpenDB(c.ChannelDB, false) + if err != nil { + return fmt.Errorf("error opening rescue DB: %v", err) + } + defer func() { _ = db.Close() }() + + rwTx, err := db.BeginReadWriteTx() + if err != nil { + return err + } + + if err := rwTx.DeleteTopLevelBucket(nodeBucket); err != nil { + return err + } + if err := rwTx.DeleteTopLevelBucket(edgeBucket); err != nil { + return err + } + if err := rwTx.DeleteTopLevelBucket(graphMetaBucket); err != nil { + return err + } + + return rwTx.Commit() +} diff --git a/cmd/chantools/root.go b/cmd/chantools/root.go index 587a26d..be0e706 100644 --- a/cmd/chantools/root.go +++ b/cmd/chantools/root.go @@ -26,7 +26,7 @@ import ( const ( defaultAPIURL = "https://blockstream.info/api" - version = "0.8.0" + version = "0.8.1" na = "n/a" Commit = "" @@ -82,6 +82,7 @@ func main() { newChanBackupCommand(), newCompactDBCommand(), newDeriveKeyCommand(), + newDropChannelGraphCommand(), newDumpBackupCommand(), newDumpChannelsCommand(), newDocCommand(), diff --git a/doc/chantools.md b/doc/chantools.md index afb1ff9..4cfca64 100644 --- a/doc/chantools.md +++ b/doc/chantools.md @@ -21,6 +21,7 @@ Complete documentation is available at https://github.com/guggero/chantools/. * [chantools chanbackup](chantools_chanbackup.md) - Create a channel.backup file from a channel database * [chantools compactdb](chantools_compactdb.md) - Create a copy of a channel.db file in safe/read-only mode * [chantools derivekey](chantools_derivekey.md) - Derive a key with a specific derivation path +* [chantools dropchannelgraph](chantools_dropchannelgraph.md) - Remove all graph related data from a channel DB * [chantools dumpbackup](chantools_dumpbackup.md) - Dump the content of a channel.backup file * [chantools dumpchannels](chantools_dumpchannels.md) - Dump all channel information from an lnd channel database * [chantools filterbackup](chantools_filterbackup.md) - Filter an lnd channel.backup file and remove certain channels diff --git a/doc/chantools_dropchannelgraph.md b/doc/chantools_dropchannelgraph.md new file mode 100644 index 0000000..49789c5 --- /dev/null +++ b/doc/chantools_dropchannelgraph.md @@ -0,0 +1,38 @@ +## chantools dropchannelgraph + +Remove all graph related data from a channel DB + +### Synopsis + +This command removes all graph data from a channel DB, +forcing the lnd node to do a full graph sync. + +``` +chantools dropchannelgraph [flags] +``` + +### Examples + +``` +chantools dropchannelgraph \ + --channeldb ~/.lnd/data/graph/mainnet/channel.db +``` + +### Options + +``` + --channeldb string lnd channel.db file to dump channels from + -h, --help help for dropchannelgraph +``` + +### Options inherited from parent commands + +``` + -r, --regtest Indicates if regtest parameters should be used + -t, --testnet Indicates if testnet parameters should be used +``` + +### SEE ALSO + +* [chantools](chantools.md) - Chantools helps recover funds from lightning channels +