mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 14:40:30 +01:00
channeldb/migration33: migrate MC store pairs to default namespace
In this commit, the mission control store is migrated such that all existing pairs which are currently stored directly in the top level results bucket are now instead moved to a "default" namespace bucket. Note that this migration is not yet invoked in this commit. The migration will be invoked in the same commit that starts writing and reading the new format.
This commit is contained in:
parent
2dd9046622
commit
bfe4a08341
3 changed files with 124 additions and 0 deletions
14
channeldb/migration33/log.go
Normal file
14
channeldb/migration33/log.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package migration33
|
||||
|
||||
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
|
||||
}
|
69
channeldb/migration33/migration.go
Normal file
69
channeldb/migration33/migration.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package migration33
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
)
|
||||
|
||||
var (
|
||||
// resultsKey is the fixed key under which the attempt results are
|
||||
// stored.
|
||||
resultsKey = []byte("missioncontrol-results")
|
||||
|
||||
// defaultMCNamespaceKey is the key of the default mission control store
|
||||
// namespace.
|
||||
defaultMCNamespaceKey = []byte("default")
|
||||
)
|
||||
|
||||
// MigrateMCStoreNameSpacedResults reads in all the current mission control
|
||||
// entries and re-writes them under a new default namespace.
|
||||
func MigrateMCStoreNameSpacedResults(tx kvdb.RwTx) error {
|
||||
log.Infof("Migrating Mission Control store to use namespaced results")
|
||||
|
||||
// Get the top level bucket. All the MC results are currently stored
|
||||
// as KV pairs in this bucket
|
||||
topLevelBucket := tx.ReadWriteBucket(resultsKey)
|
||||
|
||||
// If the results bucket does not exist then there are no entries in
|
||||
// the mission control store yet and so there is nothing to migrate.
|
||||
if topLevelBucket == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create a new default namespace bucket under the top-level bucket.
|
||||
defaultNSBkt, err := topLevelBucket.CreateBucket(defaultMCNamespaceKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Iterate through each of the existing result pairs, write them to the
|
||||
// new namespaced bucket. Also collect the set of keys so that we can
|
||||
// later delete them from the top level bucket.
|
||||
var keys [][]byte
|
||||
err = topLevelBucket.ForEach(func(k, v []byte) error {
|
||||
// Skip the new default namespace key.
|
||||
if bytes.Equal(k, defaultMCNamespaceKey) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Collect the key.
|
||||
keys = append(keys, k)
|
||||
|
||||
// Write the pair under the default namespace.
|
||||
return defaultNSBkt.Put(k, v)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Finally, iterate through the set of keys and delete them from the
|
||||
// top level bucket.
|
||||
for _, k := range keys {
|
||||
if err := topLevelBucket.Delete(k); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
41
channeldb/migration33/migration_test.go
Normal file
41
channeldb/migration33/migration_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package migration33
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/channeldb/migtest"
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
)
|
||||
|
||||
var (
|
||||
// before represents the structure of the MC store before the migration.
|
||||
before = map[string]interface{}{
|
||||
"key1": "result1",
|
||||
"key2": "result2",
|
||||
"key3": "result3",
|
||||
"key4": "result4",
|
||||
}
|
||||
|
||||
// after represents the expected structure of the store after the
|
||||
// migration. It should be identical to before except all the kv pairs
|
||||
// are now under a new default namespace key.
|
||||
after = map[string]interface{}{
|
||||
string(defaultMCNamespaceKey): before,
|
||||
}
|
||||
)
|
||||
|
||||
// TestMigrateMCStoreNameSpacedResults tests that the MC store results are
|
||||
// correctly moved to be under a new default namespace bucket.
|
||||
func TestMigrateMCStoreNameSpacedResults(t *testing.T) {
|
||||
before := func(tx kvdb.RwTx) error {
|
||||
return migtest.RestoreDB(tx, resultsKey, before)
|
||||
}
|
||||
|
||||
after := func(tx kvdb.RwTx) error {
|
||||
return migtest.VerifyDB(tx, resultsKey, after)
|
||||
}
|
||||
|
||||
migtest.ApplyMigration(
|
||||
t, before, after, MigrateMCStoreNameSpacedResults, false,
|
||||
)
|
||||
}
|
Loading…
Add table
Reference in a new issue