diff --git a/channeldb/db.go b/channeldb/db.go index 146c07449..7515d1ae5 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -25,6 +25,7 @@ import ( "github.com/lightningnetwork/lnd/channeldb/migration27" "github.com/lightningnetwork/lnd/channeldb/migration29" "github.com/lightningnetwork/lnd/channeldb/migration30" + "github.com/lightningnetwork/lnd/channeldb/migration31" "github.com/lightningnetwork/lnd/channeldb/migration_01_to_11" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/kvdb" @@ -276,6 +277,14 @@ var ( number: 29, migration: migration29.MigrateChanID, }, + { + // Removes the "sweeper-last-tx" bucket. Although we + // do not have a mandatory version 30 we skip this + // version because its naming is already used for the + // first optional migration. + number: 31, + migration: migration31.DeleteLastPublishedTxTLB, + }, } // optionalVersions stores all optional migrations that are applied diff --git a/channeldb/log.go b/channeldb/log.go index 2e2703ccd..a53d662cd 100644 --- a/channeldb/log.go +++ b/channeldb/log.go @@ -9,6 +9,7 @@ import ( "github.com/lightningnetwork/lnd/channeldb/migration16" "github.com/lightningnetwork/lnd/channeldb/migration24" "github.com/lightningnetwork/lnd/channeldb/migration30" + "github.com/lightningnetwork/lnd/channeldb/migration31" "github.com/lightningnetwork/lnd/channeldb/migration_01_to_11" "github.com/lightningnetwork/lnd/kvdb" ) @@ -40,5 +41,6 @@ func UseLogger(logger btclog.Logger) { migration16.UseLogger(logger) migration24.UseLogger(logger) migration30.UseLogger(logger) + migration31.UseLogger(logger) kvdb.UseLogger(logger) } diff --git a/channeldb/migration31/log.go b/channeldb/migration31/log.go new file mode 100644 index 000000000..2a863b7c1 --- /dev/null +++ b/channeldb/migration31/log.go @@ -0,0 +1,14 @@ +package migration31 + +import ( + "github.com/btcsuite/btclog" +) + +// log is a logger that is initialized as disabled. This means the package will +// not perform any logging by default until a logger is set. +var log = btclog.Disabled + +// UseLogger uses a specified Logger to output package logging info. +func UseLogger(logger btclog.Logger) { + log = logger +} diff --git a/channeldb/migration31/migration.go b/channeldb/migration31/migration.go new file mode 100644 index 000000000..4392d8508 --- /dev/null +++ b/channeldb/migration31/migration.go @@ -0,0 +1,23 @@ +package migration31 + +import ( + "errors" + + "github.com/btcsuite/btcwallet/walletdb" + "github.com/lightningnetwork/lnd/kvdb" +) + +// DeleteLastPublishedTxTLB deletes the top level bucket with the key +// "sweeper-last-tx". +func DeleteLastPublishedTxTLB(tx kvdb.RwTx) error { + log.Infof("Deleting top-level bucket: %x ...", lastTxBucketKey) + + err := tx.DeleteTopLevelBucket(lastTxBucketKey) + if err != nil && !errors.Is(err, walletdb.ErrBucketNotFound) { + return err + } + + log.Infof("Deleted top-level bucket: %x", lastTxBucketKey) + + return nil +} diff --git a/channeldb/migration31/migration_test.go b/channeldb/migration31/migration_test.go new file mode 100644 index 000000000..c3290736f --- /dev/null +++ b/channeldb/migration31/migration_test.go @@ -0,0 +1,48 @@ +package migration31 + +import ( + "fmt" + "testing" + + "github.com/lightningnetwork/lnd/channeldb/migtest" + "github.com/lightningnetwork/lnd/kvdb" + "github.com/stretchr/testify/require" +) + +var ( + hexStr = migtest.Hex + + // lastTxBefore is the "sweeper-last-tx" bucket before the migration. + // We fill the last-tx value with a dummy hex string because the actual + // value is not important when deleting the bucket. + lastTxBefore = map[string]interface{}{ + "sweeper-last-tx": hexStr("0000"), + } +) + +// TestDeleteLastPublishTxTLP asserts that the sweeper-last-tx bucket is +// properly deleted. +func TestDeleteLastPublishTxTLP(t *testing.T) { + t.Parallel() + + // Prime the database with the populated sweeper-last-tx bucket. + before := func(tx kvdb.RwTx) error { + return migtest.RestoreDB(tx, lastTxBucketKey, lastTxBefore) + } + + // After the migration, ensure that the sweeper-last-tx bucket was + // properly deleted. + after := func(tx kvdb.RwTx) error { + err := migtest.VerifyDB(tx, lastTxBucketKey, nil) + require.ErrorContains( + t, err, + fmt.Sprintf("bucket %s not found", lastTxBucketKey), + ) + + return nil + } + + migtest.ApplyMigration( + t, before, after, DeleteLastPublishedTxTLB, false, + ) +} diff --git a/channeldb/migration31/store.go b/channeldb/migration31/store.go new file mode 100644 index 000000000..e2f5d7a7e --- /dev/null +++ b/channeldb/migration31/store.go @@ -0,0 +1,9 @@ +package migration31 + +var ( + // lastTxBucketKey is the key that points to a bucket containing a + // single item storing the last published sweep tx. + // + // maps: lastTxKey -> serialized_tx + lastTxBucketKey = []byte("sweeper-last-tx") +)