From f191d1bb91b2c06343276adb545e091f9c6c0579 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 18 Sep 2023 15:10:30 +0200 Subject: [PATCH] multi: add new dropgraphzombies command --- README.md | 2 + cmd/chantools/dropchannelgraph.go | 2 +- cmd/chantools/dropgraphzombies.go | 88 +++++++++++++++++++++++++++++++ cmd/chantools/root.go | 1 + doc/chantools.md | 1 + doc/chantools_dropchannelgraph.md | 2 +- doc/chantools_dropgraphzombies.md | 46 ++++++++++++++++ 7 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 cmd/chantools/dropgraphzombies.go create mode 100644 doc/chantools_dropgraphzombies.md diff --git a/README.md b/README.md index 53b6371..2b3e5e2 100644 --- a/README.md +++ b/README.md @@ -414,6 +414,7 @@ Available Commands: derivekey Derive a key with a specific derivation path doublespendinputs Tries to double spend the given inputs by deriving the private for the address and sweeping the funds to the given address. This can only be used with inputs that belong to an lnd wallet. dropchannelgraph Remove all graph related data from a channel DB + dropgraphzombies Remove all channels identified as zombies from the graph to force a re-sync of the graph dumpbackup Dump the content of a channel.backup file dumpchannels Dump all channel information from an lnd channel database fakechanbackup Fake a channel backup file to attempt fund recovery @@ -471,6 +472,7 @@ Legend: | [derivekey](doc/chantools_derivekey.md) | :pencil: Derive a single private/public key from `lnd`'s seed, use to test seed | | [doublespendinputs](doc/chantools_doublespendinputs.md) | :pencil: Tries to double spend the given inputs by deriving the private for the address and sweeping the funds to the given address | | [dropchannelgraph](doc/chantools_dropchannelgraph.md) | (:warning:) Completely drop the channel graph from a `channel.db` to force re-sync | +| [dropgraphzombies](doc/chantools_dropgraphzombies.md) | Drop all zombie channels from a `channel.db` to force a graph re-sync | | [dumpbackup](doc/chantools_dumpbackup.md) | :pencil: Show the content of a `channel.backup` file as text | | [dumpchannels](doc/chantools_dumpchannels.md) | Show the content of a `channel.db` file as text | | [fakechanbackup](doc/chantools_fakechanbackup.md) | :pencil: Create a fake `channel.backup` file from public information | diff --git a/cmd/chantools/dropchannelgraph.go b/cmd/chantools/dropchannelgraph.go index 6456f50..e5e030f 100644 --- a/cmd/chantools/dropchannelgraph.go +++ b/cmd/chantools/dropchannelgraph.go @@ -58,7 +58,7 @@ chantools dropchannelgraph \ RunE: cc.Execute, } cc.cmd.Flags().StringVar( - &cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+ + &cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+ "channels from", ) cc.cmd.Flags().Uint64Var( diff --git a/cmd/chantools/dropgraphzombies.go b/cmd/chantools/dropgraphzombies.go new file mode 100644 index 0000000..f665d01 --- /dev/null +++ b/cmd/chantools/dropgraphzombies.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + + "github.com/lightninglabs/chantools/lnd" + "github.com/lightningnetwork/lnd/channeldb" + "github.com/spf13/cobra" +) + +var ( + zombieBucket = []byte("zombie-index") +) + +type dropGraphZombiesCommand struct { + ChannelDB string + NodeIdentityKey string + FixOnly bool + + SingleChannel uint64 + + cmd *cobra.Command +} + +func newDropGraphZombiesCommand() *cobra.Command { + cc := &dropGraphZombiesCommand{} + cc.cmd = &cobra.Command{ + Use: "dropgraphzombies", + Short: "Remove all channels identified as zombies from the " + + "graph to force a re-sync of the graph", + Long: `This command removes all channels that were identified as +zombies from the local graph. + +This will cause lnd to re-download all those channels from the network and can +be helpful to fix a graph that is out of sync with the network. + +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 ` + lndVersion + ` or later after using this command!'`, + Example: `chantools dropgraphzombies \ + --channeldb ~/.lnd/data/graph/mainnet/channel.db`, + RunE: cc.Execute, + } + cc.cmd.Flags().StringVar( + &cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+ + "zombies from", + ) + + return cc.cmd +} + +func (c *dropGraphZombiesCommand) 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: %w", err) + } + defer func() { _ = db.Close() }() + + log.Infof("Dropping zombie channel bucket") + + rwTx, err := db.BeginReadWriteTx() + if err != nil { + return err + } + + success := false + defer func() { + if !success { + _ = rwTx.Rollback() + } + }() + + edges := rwTx.ReadWriteBucket(edgeBucket) + if edges == nil { + return channeldb.ErrGraphNoEdgesFound + } + + if err := edges.DeleteNestedBucket(zombieBucket); err != nil { + return err + } + + success = true + return rwTx.Commit() +} diff --git a/cmd/chantools/root.go b/cmd/chantools/root.go index 1ff541d..880b460 100644 --- a/cmd/chantools/root.go +++ b/cmd/chantools/root.go @@ -103,6 +103,7 @@ func main() { newDeriveKeyCommand(), newDoubleSpendInputsCommand(), newDropChannelGraphCommand(), + newDropGraphZombiesCommand(), newDumpBackupCommand(), newDumpChannelsCommand(), newDocCommand(), diff --git a/doc/chantools.md b/doc/chantools.md index 9b5386b..5c3fb3a 100644 --- a/doc/chantools.md +++ b/doc/chantools.md @@ -26,6 +26,7 @@ Complete documentation is available at https://github.com/lightninglabs/chantool * [chantools derivekey](chantools_derivekey.md) - Derive a key with a specific derivation path * [chantools doublespendinputs](chantools_doublespendinputs.md) - Tries to double spend the given inputs by deriving the private for the address and sweeping the funds to the given address. This can only be used with inputs that belong to an lnd wallet. * [chantools dropchannelgraph](chantools_dropchannelgraph.md) - Remove all graph related data from a channel DB +* [chantools dropgraphzombies](chantools_dropgraphzombies.md) - Remove all channels identified as zombies from the graph to force a re-sync of the graph * [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 fakechanbackup](chantools_fakechanbackup.md) - Fake a channel backup file to attempt fund recovery diff --git a/doc/chantools_dropchannelgraph.md b/doc/chantools_dropchannelgraph.md index 5c27319..91060e6 100644 --- a/doc/chantools_dropchannelgraph.md +++ b/doc/chantools_dropchannelgraph.md @@ -34,7 +34,7 @@ chantools dropchannelgraph \ ### Options ``` - --channeldb string lnd channel.db file to dump channels from + --channeldb string lnd channel.db file to drop channels from --fix_only fix an already empty graph by re-adding the own node's channels -h, --help help for dropchannelgraph --node_identity_key string your node's identity public key diff --git a/doc/chantools_dropgraphzombies.md b/doc/chantools_dropgraphzombies.md new file mode 100644 index 0000000..cf172e5 --- /dev/null +++ b/doc/chantools_dropgraphzombies.md @@ -0,0 +1,46 @@ +## chantools dropgraphzombies + +Remove all channels identified as zombies from the graph to force a re-sync of the graph + +### Synopsis + +This command removes all channels that were identified as +zombies from the local graph. + +This will cause lnd to re-download all those channels from the network and can +be helpful to fix a graph that is out of sync with the network. + +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.16.0-beta or later after using this command!' + +``` +chantools dropgraphzombies [flags] +``` + +### Examples + +``` +chantools dropgraphzombies \ + --channeldb ~/.lnd/data/graph/mainnet/channel.db +``` + +### Options + +``` + --channeldb string lnd channel.db file to drop zombies from + -h, --help help for dropgraphzombies +``` + +### Options inherited from parent commands + +``` + -r, --regtest Indicates if regtest parameters should be used + -s, --signet Indicates if the public signet 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 +