|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/guggero/chantools/lnd" |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +type deletePaymentsCommand struct { |
| 11 | + ChannelDB string |
| 12 | + FailedOnly bool |
| 13 | + |
| 14 | + cmd *cobra.Command |
| 15 | +} |
| 16 | + |
| 17 | +func newDeletePaymentsCommand() *cobra.Command { |
| 18 | + cc := &deletePaymentsCommand{} |
| 19 | + cc.cmd = &cobra.Command{ |
| 20 | + Use: "deletepayments", |
| 21 | + Short: "Remove all (failed) payments from a channel DB", |
| 22 | + Long: `This command removes all payments from a channel DB. |
| 23 | +If only the failed payments should be deleted (and not the successful ones), the |
| 24 | +--failedonly flag can be specified. |
| 25 | +
|
| 26 | +CAUTION: Running this command will make it impossible to use the channel DB |
| 27 | +with an older version of lnd. Downgrading is not possible and you'll need to |
| 28 | +run lnd v0.12.1-beta or later after using this command!'`, |
| 29 | + Example: `chantools deletepayments --failedonly \ |
| 30 | + --channeldb ~/.lnd/data/graph/mainnet/channel.db`, |
| 31 | + RunE: cc.Execute, |
| 32 | + } |
| 33 | + cc.cmd.Flags().StringVar( |
| 34 | + &cc.ChannelDB, "channeldb", "", "lnd channel.db file to dump "+ |
| 35 | + "channels from", |
| 36 | + ) |
| 37 | + cc.cmd.Flags().BoolVar( |
| 38 | + &cc.FailedOnly, "failedonly", false, "don't delete all "+ |
| 39 | + "payments, only failed ones", |
| 40 | + ) |
| 41 | + |
| 42 | + return cc.cmd |
| 43 | +} |
| 44 | + |
| 45 | +func (c *deletePaymentsCommand) 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 | + return db.DeletePayments(c.FailedOnly, false) |
| 57 | +} |
0 commit comments