From d8c48fa3a5a66189391488bd53da0793793c36c8 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 22 Jul 2021 17:56:22 +0800 Subject: [PATCH] channeldb: wipe all forwarding pkgs when close channel --- channeldb/channel.go | 6 ++++++ channeldb/channel_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/channeldb/channel.go b/channeldb/channel.go index 7414c5927..765883525 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -2862,6 +2862,12 @@ func (c *OpenChannel) CloseChannel(summary *ChannelCloseSummary, return err } + // Delete all the forwarding packages stored for this particular + // channel. + if err = chanState.Packager.Wipe(tx); err != nil { + return err + } + // Now that the index to this channel has been deleted, purge // the remaining channel metadata from the database. err = deleteOpenChannel(chanBucket) diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index 4632d22f8..ad1b3c07c 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -130,6 +130,25 @@ func remoteHtlcsOption(htlcs []HTLC) testChannelOption { } } +// loadFwdPkgs is a helper method that reads all forwarding packages for a +// particular packager. +func loadFwdPkgs(t *testing.T, db kvdb.Backend, + packager FwdPackager) []*FwdPkg { + + var ( + fwdPkgs []*FwdPkg + err error + ) + + err = kvdb.View(db, func(tx kvdb.RTx) error { + fwdPkgs, err = packager.LoadFwdPkgs(tx) + return err + }, func() {}) + require.NoError(t, err, "unable to load fwd pkgs") + + return fwdPkgs +} + // localShutdownOption is an option which sets the local upfront shutdown // script for the channel. func localShutdownOption(addr lnwire.DeliveryAddress) testChannelOption { @@ -822,6 +841,10 @@ func TestChannelStateTransition(t *testing.T) { t.Fatalf("revocation state was not synced") } + // At this point, we should have 2 forwarding packages added. + fwdPkgs := loadFwdPkgs(t, cdb, channel.Packager) + require.Len(t, fwdPkgs, 2, "wrong number of forwarding packages") + // Now attempt to delete the channel from the database. closeSummary := &ChannelCloseSummary{ ChanPoint: channel.FundingOutpoint, @@ -852,6 +875,10 @@ func TestChannelStateTransition(t *testing.T) { if err == nil { t.Fatal("revocation log search should have failed") } + + // All forwarding packages of this channel has been deleted too. + fwdPkgs = loadFwdPkgs(t, cdb, channel.Packager) + require.Empty(t, fwdPkgs, "no forwarding packages should exist") } func TestFetchPendingChannels(t *testing.T) {