multi: Add a channel.db migration.

The new migration removes the sweeper-last-tx top level bucket
from the channel.db database.
This commit is contained in:
ziggie 2023-08-09 14:24:03 +02:00
parent 07502a8fb0
commit 87fc58ecfe
No known key found for this signature in database
GPG Key ID: 1AFF9C4DCED6D666
6 changed files with 105 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/migration27" "github.com/lightningnetwork/lnd/channeldb/migration27"
"github.com/lightningnetwork/lnd/channeldb/migration29" "github.com/lightningnetwork/lnd/channeldb/migration29"
"github.com/lightningnetwork/lnd/channeldb/migration30" "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/channeldb/migration_01_to_11"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
@ -276,6 +277,14 @@ var (
number: 29, number: 29,
migration: migration29.MigrateChanID, 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 // optionalVersions stores all optional migrations that are applied

View File

@ -9,6 +9,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/migration16" "github.com/lightningnetwork/lnd/channeldb/migration16"
"github.com/lightningnetwork/lnd/channeldb/migration24" "github.com/lightningnetwork/lnd/channeldb/migration24"
"github.com/lightningnetwork/lnd/channeldb/migration30" "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/channeldb/migration_01_to_11"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
) )
@ -40,5 +41,6 @@ func UseLogger(logger btclog.Logger) {
migration16.UseLogger(logger) migration16.UseLogger(logger)
migration24.UseLogger(logger) migration24.UseLogger(logger)
migration30.UseLogger(logger) migration30.UseLogger(logger)
migration31.UseLogger(logger)
kvdb.UseLogger(logger) kvdb.UseLogger(logger)
} }

View File

@ -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
}

View File

@ -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
}

View File

@ -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,
)
}

View File

@ -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")
)