|  | 
|  | 1 | +package main | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | + | 
|  | 6 | +	"github.com/guggero/chantools/lnd" | 
|  | 7 | +	"github.com/spf13/cobra" | 
|  | 8 | +) | 
|  | 9 | + | 
|  | 10 | +var ( | 
|  | 11 | +	nodeBucket      = []byte("graph-node") | 
|  | 12 | +	edgeBucket      = []byte("graph-edge") | 
|  | 13 | +	graphMetaBucket = []byte("graph-meta") | 
|  | 14 | +) | 
|  | 15 | + | 
|  | 16 | +type dropChannelGraphCommand struct { | 
|  | 17 | +	ChannelDB string | 
|  | 18 | + | 
|  | 19 | +	cmd *cobra.Command | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +func newDropChannelGraphCommand() *cobra.Command { | 
|  | 23 | +	cc := &dropChannelGraphCommand{} | 
|  | 24 | +	cc.cmd = &cobra.Command{ | 
|  | 25 | +		Use:   "dropchannelgraph", | 
|  | 26 | +		Short: "Remove all graph related data from a channel DB", | 
|  | 27 | +		Long: `This command removes all graph data from a channel DB, | 
|  | 28 | +forcing the lnd node to do a full graph sync. | 
|  | 29 | +
 | 
|  | 30 | +CAUTION: Running this command will make it impossible to use the channel DB | 
|  | 31 | +with an older version of lnd. Downgrading is not possible and you'll need to | 
|  | 32 | +run lnd v0.12.0-beta or later after using this command!'`, | 
|  | 33 | +		Example: `chantools dropchannelgraph \ | 
|  | 34 | +	--channeldb ~/.lnd/data/graph/mainnet/channel.db`, | 
|  | 35 | +		RunE: cc.Execute, | 
|  | 36 | +	} | 
|  | 37 | +	cc.cmd.Flags().StringVar( | 
|  | 38 | +		&cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+ | 
|  | 39 | +			"channels from", | 
|  | 40 | +	) | 
|  | 41 | + | 
|  | 42 | +	return cc.cmd | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +func (c *dropChannelGraphCommand) Execute(_ *cobra.Command, _ []string) error { | 
|  | 46 | +	// Check that we have a channel DB. | 
|  | 47 | +	if c.ChannelDB == "" { | 
|  | 48 | +		return fmt.Errorf("channel DB is required") | 
|  | 49 | +	} | 
|  | 50 | +	db, err := lnd.OpenDB(c.ChannelDB, false) | 
|  | 51 | +	if err != nil { | 
|  | 52 | +		return fmt.Errorf("error opening rescue DB: %v", err) | 
|  | 53 | +	} | 
|  | 54 | +	defer func() { _ = db.Close() }() | 
|  | 55 | + | 
|  | 56 | +	rwTx, err := db.BeginReadWriteTx() | 
|  | 57 | +	if err != nil { | 
|  | 58 | +		return err | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	if err := rwTx.DeleteTopLevelBucket(nodeBucket); err != nil { | 
|  | 62 | +		return err | 
|  | 63 | +	} | 
|  | 64 | +	if err := rwTx.DeleteTopLevelBucket(edgeBucket); err != nil { | 
|  | 65 | +		return err | 
|  | 66 | +	} | 
|  | 67 | +	if err := rwTx.DeleteTopLevelBucket(graphMetaBucket); err != nil { | 
|  | 68 | +		return err | 
|  | 69 | +	} | 
|  | 70 | +	 | 
|  | 71 | +	return rwTx.Commit() | 
|  | 72 | +} | 
0 commit comments