channeldb: add method to wipe all forwarding packages

This commit adds a method, Wipe, to remove all forwarding packages on
disk for a given channel.
This commit is contained in:
yyforyongyu
2021-07-22 13:16:55 +08:00
parent 0fff613f61
commit 3d50edf9f8
3 changed files with 69 additions and 0 deletions

View File

@@ -433,6 +433,9 @@ type FwdPackager interface {
// RemovePkg deletes a forwarding package owned by this channel at
// the provided remote `height`.
RemovePkg(tx kvdb.RwTx, height uint64) error
// Wipe deletes all the forwarding packages owned by this channel.
Wipe(tx kvdb.RwTx) error
}
// ChannelPackager is used by a channel to manage the lifecycle of its forwarding
@@ -944,6 +947,24 @@ func (p *ChannelPackager) RemovePkg(tx kvdb.RwTx, height uint64) error {
return sourceBkt.DeleteNestedBucket(heightKey[:])
}
// Wipe deletes all the channel's forwarding packages, if any.
func (p *ChannelPackager) Wipe(tx kvdb.RwTx) error {
// If the root bucket doesn't exist, there's no need to delete.
fwdPkgBkt := tx.ReadWriteBucket(fwdPackagesKey)
if fwdPkgBkt == nil {
return nil
}
sourceBytes := makeLogKey(p.source.ToUint64())
// If the nested bucket doesn't exist, there's no need to delete.
if fwdPkgBkt.NestedReadWriteBucket(sourceBytes[:]) == nil {
return nil
}
return fwdPkgBkt.DeleteNestedBucket(sourceBytes[:])
}
// uint16Key writes the provided 16-bit unsigned integer to a 2-byte slice.
func uint16Key(i uint16) []byte {
key := make([]byte, 2)