|  | 
|  | 1 | +package main | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"strconv" | 
|  | 6 | +	"strings" | 
|  | 7 | + | 
|  | 8 | +	"github.com/btcsuite/btcd/chaincfg/chainhash" | 
|  | 9 | +	"github.com/btcsuite/btcd/wire" | 
|  | 10 | +	"github.com/guggero/chantools/lnd" | 
|  | 11 | +	"github.com/lightningnetwork/lnd/channeldb" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +type removeChannelCommand struct { | 
|  | 15 | +	ChannelDB string `long:"channeldb" description:"The lnd channel.db file to remove the channel from."` | 
|  | 16 | +	Channel   string `long:"channel" description:"The channel to remove from the DB file, identified by its channel point (<txid>:<txindex>)."` | 
|  | 17 | +} | 
|  | 18 | + | 
|  | 19 | +func (c *removeChannelCommand) Execute(_ []string) error { | 
|  | 20 | +	setupChainParams(cfg) | 
|  | 21 | + | 
|  | 22 | +	// Check that we have a channel DB. | 
|  | 23 | +	if c.ChannelDB == "" { | 
|  | 24 | +		return fmt.Errorf("channel DB is required") | 
|  | 25 | +	} | 
|  | 26 | +	db, err := lnd.OpenDB(c.ChannelDB, false) | 
|  | 27 | +	if err != nil { | 
|  | 28 | +		return fmt.Errorf("error opening channel DB: %v", err) | 
|  | 29 | +	} | 
|  | 30 | +	defer func() { | 
|  | 31 | +		if err := db.Close(); err != nil { | 
|  | 32 | +			log.Errorf("Error closing DB: %v", err) | 
|  | 33 | +		} | 
|  | 34 | +	}() | 
|  | 35 | + | 
|  | 36 | +	parts := strings.Split(c.Channel, ":") | 
|  | 37 | +	if len(parts) != 2 { | 
|  | 38 | +		return fmt.Errorf("invalid channel point format: %v", c.Channel) | 
|  | 39 | +	} | 
|  | 40 | +	hash, err := chainhash.NewHashFromStr(parts[0]) | 
|  | 41 | +	if err != nil { | 
|  | 42 | +		return err | 
|  | 43 | +	} | 
|  | 44 | +	index, err := strconv.ParseUint(parts[1], 10, 64) | 
|  | 45 | +	if err != nil { | 
|  | 46 | +		return err | 
|  | 47 | +	} | 
|  | 48 | + | 
|  | 49 | +	return removeChannel(db, &wire.OutPoint{ | 
|  | 50 | +		Hash:  *hash, | 
|  | 51 | +		Index: uint32(index), | 
|  | 52 | +	}) | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +func removeChannel(db *channeldb.DB, chanPoint *wire.OutPoint) error { | 
|  | 56 | +	dbChan, err := db.FetchChannel(*chanPoint) | 
|  | 57 | +	if err != nil { | 
|  | 58 | +		return err | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	if err := dbChan.MarkBorked(); err != nil { | 
|  | 62 | +		return err | 
|  | 63 | +	} | 
|  | 64 | + | 
|  | 65 | +	// Abandoning a channel is a three step process: remove from the open | 
|  | 66 | +	// channel state, remove from the graph, remove from the contract | 
|  | 67 | +	// court. Between any step it's possible that the users restarts the | 
|  | 68 | +	// process all over again. As a result, each of the steps below are | 
|  | 69 | +	// intended to be idempotent. | 
|  | 70 | +	return db.AbandonChannel(chanPoint, uint32(100000)) | 
|  | 71 | +} | 
0 commit comments