Skip to content

Commit f191d1b

Browse files
committed
multi: add new dropgraphzombies command
1 parent 909163b commit f191d1b

File tree

7 files changed

+140
-2
lines changed

7 files changed

+140
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Available Commands:
414414
derivekey Derive a key with a specific derivation path
415415
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.
416416
dropchannelgraph Remove all graph related data from a channel DB
417+
dropgraphzombies Remove all channels identified as zombies from the graph to force a re-sync of the graph
417418
dumpbackup Dump the content of a channel.backup file
418419
dumpchannels Dump all channel information from an lnd channel database
419420
fakechanbackup Fake a channel backup file to attempt fund recovery
@@ -471,6 +472,7 @@ Legend:
471472
| [derivekey](doc/chantools_derivekey.md) | :pencil: Derive a single private/public key from `lnd`'s seed, use to test seed |
472473
| [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 |
473474
| [dropchannelgraph](doc/chantools_dropchannelgraph.md) | (:warning:) Completely drop the channel graph from a `channel.db` to force re-sync |
475+
| [dropgraphzombies](doc/chantools_dropgraphzombies.md) | Drop all zombie channels from a `channel.db` to force a graph re-sync |
474476
| [dumpbackup](doc/chantools_dumpbackup.md) | :pencil: Show the content of a `channel.backup` file as text |
475477
| [dumpchannels](doc/chantools_dumpchannels.md) | Show the content of a `channel.db` file as text |
476478
| [fakechanbackup](doc/chantools_fakechanbackup.md) | :pencil: Create a fake `channel.backup` file from public information |

cmd/chantools/dropchannelgraph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ chantools dropchannelgraph \
5858
RunE: cc.Execute,
5959
}
6060
cc.cmd.Flags().StringVar(
61-
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+
61+
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+
6262
"channels from",
6363
)
6464
cc.cmd.Flags().Uint64Var(

cmd/chantools/dropgraphzombies.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/lightninglabs/chantools/lnd"
7+
"github.com/lightningnetwork/lnd/channeldb"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var (
12+
zombieBucket = []byte("zombie-index")
13+
)
14+
15+
type dropGraphZombiesCommand struct {
16+
ChannelDB string
17+
NodeIdentityKey string
18+
FixOnly bool
19+
20+
SingleChannel uint64
21+
22+
cmd *cobra.Command
23+
}
24+
25+
func newDropGraphZombiesCommand() *cobra.Command {
26+
cc := &dropGraphZombiesCommand{}
27+
cc.cmd = &cobra.Command{
28+
Use: "dropgraphzombies",
29+
Short: "Remove all channels identified as zombies from the " +
30+
"graph to force a re-sync of the graph",
31+
Long: `This command removes all channels that were identified as
32+
zombies from the local graph.
33+
34+
This will cause lnd to re-download all those channels from the network and can
35+
be helpful to fix a graph that is out of sync with the network.
36+
37+
CAUTION: Running this command will make it impossible to use the channel DB
38+
with an older version of lnd. Downgrading is not possible and you'll need to
39+
run lnd ` + lndVersion + ` or later after using this command!'`,
40+
Example: `chantools dropgraphzombies \
41+
--channeldb ~/.lnd/data/graph/mainnet/channel.db`,
42+
RunE: cc.Execute,
43+
}
44+
cc.cmd.Flags().StringVar(
45+
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to drop "+
46+
"zombies from",
47+
)
48+
49+
return cc.cmd
50+
}
51+
52+
func (c *dropGraphZombiesCommand) Execute(_ *cobra.Command, _ []string) error {
53+
// Check that we have a channel DB.
54+
if c.ChannelDB == "" {
55+
return fmt.Errorf("channel DB is required")
56+
}
57+
db, err := lnd.OpenDB(c.ChannelDB, false)
58+
if err != nil {
59+
return fmt.Errorf("error opening rescue DB: %w", err)
60+
}
61+
defer func() { _ = db.Close() }()
62+
63+
log.Infof("Dropping zombie channel bucket")
64+
65+
rwTx, err := db.BeginReadWriteTx()
66+
if err != nil {
67+
return err
68+
}
69+
70+
success := false
71+
defer func() {
72+
if !success {
73+
_ = rwTx.Rollback()
74+
}
75+
}()
76+
77+
edges := rwTx.ReadWriteBucket(edgeBucket)
78+
if edges == nil {
79+
return channeldb.ErrGraphNoEdgesFound
80+
}
81+
82+
if err := edges.DeleteNestedBucket(zombieBucket); err != nil {
83+
return err
84+
}
85+
86+
success = true
87+
return rwTx.Commit()
88+
}

cmd/chantools/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func main() {
103103
newDeriveKeyCommand(),
104104
newDoubleSpendInputsCommand(),
105105
newDropChannelGraphCommand(),
106+
newDropGraphZombiesCommand(),
106107
newDumpBackupCommand(),
107108
newDumpChannelsCommand(),
108109
newDocCommand(),

doc/chantools.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Complete documentation is available at https://github.com/lightninglabs/chantool
2626
* [chantools derivekey](chantools_derivekey.md) - Derive a key with a specific derivation path
2727
* [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.
2828
* [chantools dropchannelgraph](chantools_dropchannelgraph.md) - Remove all graph related data from a channel DB
29+
* [chantools dropgraphzombies](chantools_dropgraphzombies.md) - Remove all channels identified as zombies from the graph to force a re-sync of the graph
2930
* [chantools dumpbackup](chantools_dumpbackup.md) - Dump the content of a channel.backup file
3031
* [chantools dumpchannels](chantools_dumpchannels.md) - Dump all channel information from an lnd channel database
3132
* [chantools fakechanbackup](chantools_fakechanbackup.md) - Fake a channel backup file to attempt fund recovery

doc/chantools_dropchannelgraph.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ chantools dropchannelgraph \
3434
### Options
3535

3636
```
37-
--channeldb string lnd channel.db file to dump channels from
37+
--channeldb string lnd channel.db file to drop channels from
3838
--fix_only fix an already empty graph by re-adding the own node's channels
3939
-h, --help help for dropchannelgraph
4040
--node_identity_key string your node's identity public key

doc/chantools_dropgraphzombies.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## chantools dropgraphzombies
2+
3+
Remove all channels identified as zombies from the graph to force a re-sync of the graph
4+
5+
### Synopsis
6+
7+
This command removes all channels that were identified as
8+
zombies from the local graph.
9+
10+
This will cause lnd to re-download all those channels from the network and can
11+
be helpful to fix a graph that is out of sync with the network.
12+
13+
CAUTION: Running this command will make it impossible to use the channel DB
14+
with an older version of lnd. Downgrading is not possible and you'll need to
15+
run lnd v0.16.0-beta or later after using this command!'
16+
17+
```
18+
chantools dropgraphzombies [flags]
19+
```
20+
21+
### Examples
22+
23+
```
24+
chantools dropgraphzombies \
25+
--channeldb ~/.lnd/data/graph/mainnet/channel.db
26+
```
27+
28+
### Options
29+
30+
```
31+
--channeldb string lnd channel.db file to drop zombies from
32+
-h, --help help for dropgraphzombies
33+
```
34+
35+
### Options inherited from parent commands
36+
37+
```
38+
-r, --regtest Indicates if regtest parameters should be used
39+
-s, --signet Indicates if the public signet parameters should be used
40+
-t, --testnet Indicates if testnet parameters should be used
41+
```
42+
43+
### SEE ALSO
44+
45+
* [chantools](chantools.md) - Chantools helps recover funds from lightning channels
46+

0 commit comments

Comments
 (0)