mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-25 18:21:12 +02:00
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:
14
channeldb/migration31/log.go
Normal file
14
channeldb/migration31/log.go
Normal 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
|
||||
}
|
23
channeldb/migration31/migration.go
Normal file
23
channeldb/migration31/migration.go
Normal 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
|
||||
}
|
48
channeldb/migration31/migration_test.go
Normal file
48
channeldb/migration31/migration_test.go
Normal 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,
|
||||
)
|
||||
}
|
9
channeldb/migration31/store.go
Normal file
9
channeldb/migration31/store.go
Normal 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")
|
||||
)
|
Reference in New Issue
Block a user