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
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
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)

View file

@ -11,6 +11,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
// TestPkgFilterBruteForce tests the behavior of a pkg filter up to size 1000,
@ -729,6 +730,49 @@ func TestPackagerSettleFailsThenAdds(t *testing.T) {
}
}
// TestPackagerWipeAll checks that when the method is called, all the related
// forwarding packages will be removed.
func TestPackagerWipeAll(t *testing.T) {
t.Parallel()
db := makeFwdPkgDB(t, "")
shortChanID := lnwire.NewShortChanIDFromInt(1)
packager := channeldb.NewChannelPackager(shortChanID)
// To begin, there should be no forwarding packages on disk.
fwdPkgs := loadFwdPkgs(t, db, packager)
require.Empty(t, fwdPkgs, "no forwarding packages should exist")
// Now, check we can wipe without error since it's a noop.
err := kvdb.Update(db, packager.Wipe, func() {})
require.NoError(t, err, "unable to wipe fwdpkg")
// Next, create and write two forwarding packages with no htlcs.
fwdPkg1 := channeldb.NewFwdPkg(shortChanID, 0, nil, nil)
fwdPkg2 := channeldb.NewFwdPkg(shortChanID, 1, nil, nil)
err = kvdb.Update(db, func(tx kvdb.RwTx) error {
if err := packager.AddFwdPkg(tx, fwdPkg2); err != nil {
return err
}
return packager.AddFwdPkg(tx, fwdPkg1)
}, func() {})
require.NoError(t, err, "unable to add fwd pkg")
// There should now be two fwdpkgs on disk.
fwdPkgs = loadFwdPkgs(t, db, packager)
require.Equal(t, 2, len(fwdPkgs), "expected 2 fwdpkg")
// Now, wipe all forwarding packages from disk.
err = kvdb.Update(db, packager.Wipe, func() {})
require.NoError(t, err, "unable to wipe fwdpkg")
// Check that the packages were actually removed.
fwdPkgs = loadFwdPkgs(t, db, packager)
require.Empty(t, fwdPkgs, "no forwarding packages should exist")
}
// assertFwdPkgState checks the current state of a fwdpkg meets our
// expectations.
func assertFwdPkgState(t *testing.T, fwdPkg *channeldb.FwdPkg,

View file

@ -5373,6 +5373,10 @@ func (*mockPackager) RemovePkg(tx kvdb.RwTx, height uint64) error {
return nil
}
func (*mockPackager) Wipe(tx kvdb.RwTx) error {
return nil
}
func (*mockPackager) AckSettleFails(tx kvdb.RwTx,
settleFailRefs ...channeldb.SettleFailRef) error {
return nil