From 08ee754a6d2a7117b9a9dd57cc77b63c14b6cec5 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 10 Dec 2020 17:08:56 -0800 Subject: [PATCH] channeldb/db: properly reinit wallet during Wipe Previously we wouldn't recreate some of the top level buckets that are now considered expected with our migration logic. This bug was preexisting, but never surfaced because the other TLB buckets were not touched by this unit test. --- channeldb/db.go | 7 ++++++- channeldb/db_test.go | 18 ++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index 6cdebd5f..f2e56691 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -315,7 +315,7 @@ var topLevelBuckets = [][]byte{ // database. The deletion is done in a single transaction, therefore this // operation is fully atomic. func (d *DB) Wipe() error { - return kvdb.Update(d, func(tx kvdb.RwTx) error { + err := kvdb.Update(d, func(tx kvdb.RwTx) error { for _, tlb := range topLevelBuckets { err := tx.DeleteTopLevelBucket(tlb) if err != nil && err != kvdb.ErrBucketNotFound { @@ -324,6 +324,11 @@ func (d *DB) Wipe() error { } return nil }, func() {}) + if err != nil { + return err + } + + return initChannelDB(d.Backend) } // createChannelDB creates and initializes a fresh version of channeldb. In diff --git a/channeldb/db_test.go b/channeldb/db_test.go index a86eee69..9b75fd39 100644 --- a/channeldb/db_test.go +++ b/channeldb/db_test.go @@ -19,6 +19,7 @@ import ( "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/shachain" + "github.com/stretchr/testify/require" ) func TestOpenWithCreate(t *testing.T) { @@ -96,16 +97,13 @@ func TestWipe(t *testing.T) { t.Fatalf("unable to wipe channeldb: %v", err) } // Check correct errors are returned - _, err = cdb.FetchAllOpenChannels() - if err != ErrNoActiveChannels { - t.Fatalf("fetching open channels: expected '%v' instead got '%v'", - ErrNoActiveChannels, err) - } - _, err = cdb.FetchClosedChannels(false) - if err != ErrNoClosedChannels { - t.Fatalf("fetching closed channels: expected '%v' instead got '%v'", - ErrNoClosedChannels, err) - } + openChannels, err := cdb.FetchAllOpenChannels() + require.NoError(t, err, "fetching open channels") + require.Equal(t, 0, len(openChannels)) + + closedChannels, err := cdb.FetchClosedChannels(false) + require.NoError(t, err, "fetching closed channels") + require.Equal(t, 0, len(closedChannels)) } // TestFetchClosedChannelForID tests that we are able to properly retrieve a