Skip to content

Commit

Permalink
dropchannelgraph: allow removing single channel
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 3, 2021
1 parent dc10f2d commit 328f48d
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions cmd/chantools/dropchannelgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"

"github.com/guggero/chantools/lnd"
"github.com/spf13/cobra"
)
Expand All @@ -16,6 +15,8 @@ var (
type dropChannelGraphCommand struct {
ChannelDB string

SingleChannel uint64

cmd *cobra.Command
}

Expand All @@ -27,17 +28,29 @@ func newDropChannelGraphCommand() *cobra.Command {
Long: `This command removes all graph data from a channel DB,
forcing the lnd node to do a full graph sync.
Or if a single channel is specified, that channel is purged from the graph
without removing any other data.
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.13.1-beta or later after using this command!'`,
Example: `chantools dropchannelgraph \
--channeldb ~/.lnd/data/graph/mainnet/channel.db`,
--channeldb ~/.lnd/data/graph/mainnet/channel.db
chantools dropchannelgraph \
--channeldb ~/.lnd/data/graph/mainnet/channel.db \
--single_channel 726607861215512345`,
RunE: cc.Execute,
}
cc.cmd.Flags().StringVar(
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+
"channels from",
)
cc.cmd.Flags().Uint64Var(
&cc.SingleChannel, "single_channel", 0, "the single channel "+
"identified by its short channel ID (CID) to remove "+
"from the graph",
)

return cc.cmd
}
Expand All @@ -53,20 +66,28 @@ func (c *dropChannelGraphCommand) Execute(_ *cobra.Command, _ []string) error {
}
defer func() { _ = db.Close() }()

rwTx, err := db.BeginReadWriteTx()
if err != nil {
return err
}
if c.SingleChannel != 0 {
log.Infof("Removing single channel %d", c.SingleChannel)
return db.ChannelGraph().DeleteChannelEdges(
true, c.SingleChannel,
)
} else {
log.Infof("Dropping all graph related buckets")

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
}
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()
return rwTx.Commit()
}
}

0 comments on commit 328f48d

Please sign in to comment.