|
| 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 | +} |
0 commit comments