Skip to content

Commit f1207ef

Browse files
authored
Merge pull request #9001 from ellemouton/namespacedMC
routing: Namespaced Mission Control
2 parents 75eaaf7 + 83c1d73 commit f1207ef

19 files changed

+667
-138
lines changed

channeldb/db.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/lightningnetwork/lnd/channeldb/migration30"
2828
"github.com/lightningnetwork/lnd/channeldb/migration31"
2929
"github.com/lightningnetwork/lnd/channeldb/migration32"
30+
"github.com/lightningnetwork/lnd/channeldb/migration33"
3031
"github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
3132
"github.com/lightningnetwork/lnd/clock"
3233
"github.com/lightningnetwork/lnd/invoices"
@@ -291,6 +292,10 @@ var (
291292
number: 32,
292293
migration: migration32.MigrateMCRouteSerialisation,
293294
},
295+
{
296+
number: 33,
297+
migration: migration33.MigrateMCStoreNameSpacedResults,
298+
},
294299
}
295300

296301
// optionalVersions stores all optional migrations that are applied

channeldb/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightningnetwork/lnd/channeldb/migration30"
1212
"github.com/lightningnetwork/lnd/channeldb/migration31"
1313
"github.com/lightningnetwork/lnd/channeldb/migration32"
14+
"github.com/lightningnetwork/lnd/channeldb/migration33"
1415
"github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
1516
"github.com/lightningnetwork/lnd/kvdb"
1617
)
@@ -44,5 +45,6 @@ func UseLogger(logger btclog.Logger) {
4445
migration30.UseLogger(logger)
4546
migration31.UseLogger(logger)
4647
migration32.UseLogger(logger)
48+
migration33.UseLogger(logger)
4749
kvdb.UseLogger(logger)
4850
}

channeldb/migration33/log.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package migration33
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
)
6+
7+
// log is a logger that is initialized as disabled. This means the package will
8+
// not perform any logging by default until a logger is set.
9+
var log = btclog.Disabled
10+
11+
// UseLogger uses a specified Logger to output package logging info.
12+
func UseLogger(logger btclog.Logger) {
13+
log = logger
14+
}

channeldb/migration33/migration.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package migration33
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/lightningnetwork/lnd/kvdb"
7+
)
8+
9+
var (
10+
// resultsKey is the fixed key under which the attempt results are
11+
// stored.
12+
resultsKey = []byte("missioncontrol-results")
13+
14+
// defaultMCNamespaceKey is the key of the default mission control store
15+
// namespace.
16+
defaultMCNamespaceKey = []byte("default")
17+
)
18+
19+
// MigrateMCStoreNameSpacedResults reads in all the current mission control
20+
// entries and re-writes them under a new default namespace.
21+
func MigrateMCStoreNameSpacedResults(tx kvdb.RwTx) error {
22+
log.Infof("Migrating Mission Control store to use namespaced results")
23+
24+
// Get the top level bucket. All the MC results are currently stored
25+
// as KV pairs in this bucket
26+
topLevelBucket := tx.ReadWriteBucket(resultsKey)
27+
28+
// If the results bucket does not exist then there are no entries in
29+
// the mission control store yet and so there is nothing to migrate.
30+
if topLevelBucket == nil {
31+
return nil
32+
}
33+
34+
// Create a new default namespace bucket under the top-level bucket.
35+
defaultNSBkt, err := topLevelBucket.CreateBucket(defaultMCNamespaceKey)
36+
if err != nil {
37+
return err
38+
}
39+
40+
// Iterate through each of the existing result pairs, write them to the
41+
// new namespaced bucket. Also collect the set of keys so that we can
42+
// later delete them from the top level bucket.
43+
var keys [][]byte
44+
err = topLevelBucket.ForEach(func(k, v []byte) error {
45+
// Skip the new default namespace key.
46+
if bytes.Equal(k, defaultMCNamespaceKey) {
47+
return nil
48+
}
49+
50+
// Collect the key.
51+
keys = append(keys, k)
52+
53+
// Write the pair under the default namespace.
54+
return defaultNSBkt.Put(k, v)
55+
})
56+
if err != nil {
57+
return err
58+
}
59+
60+
// Finally, iterate through the set of keys and delete them from the
61+
// top level bucket.
62+
for _, k := range keys {
63+
if err := topLevelBucket.Delete(k); err != nil {
64+
return err
65+
}
66+
}
67+
68+
return err
69+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package migration33
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lightningnetwork/lnd/channeldb/migtest"
7+
"github.com/lightningnetwork/lnd/kvdb"
8+
)
9+
10+
var (
11+
// before represents the structure of the MC store before the migration.
12+
before = map[string]interface{}{
13+
"key1": "result1",
14+
"key2": "result2",
15+
"key3": "result3",
16+
"key4": "result4",
17+
}
18+
19+
// after represents the expected structure of the store after the
20+
// migration. It should be identical to before except all the kv pairs
21+
// are now under a new default namespace key.
22+
after = map[string]interface{}{
23+
string(defaultMCNamespaceKey): before,
24+
}
25+
)
26+
27+
// TestMigrateMCStoreNameSpacedResults tests that the MC store results are
28+
// correctly moved to be under a new default namespace bucket.
29+
func TestMigrateMCStoreNameSpacedResults(t *testing.T) {
30+
before := func(tx kvdb.RwTx) error {
31+
return migtest.RestoreDB(tx, resultsKey, before)
32+
}
33+
34+
after := func(tx kvdb.RwTx) error {
35+
return migtest.VerifyDB(tx, resultsKey, after)
36+
}
37+
38+
migtest.ApplyMigration(
39+
t, before, after, MigrateMCStoreNameSpacedResults, false,
40+
)
41+
}

docs/release-notes/release-notes-0.19.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
store](https://github.com/lightningnetwork/lnd/pull/8911) to use a more
6767
minimal encoding for payment attempt routes.
6868

69+
* [Migrate the mission control
70+
store](https://github.com/lightningnetwork/lnd/pull/9001) so that results are
71+
namespaced. All existing results are written to the "default" namespace.
72+
6973
## Code Health
7074

7175
## Tooling and Documentation

routing/integrated_routing_context_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,15 @@ func (c *integratedRoutingContext) testPayment(maxParts uint32,
163163
}
164164
})
165165

166-
// Instantiate a new mission control with the current configuration
166+
// Instantiate a new mission controller with the current configuration
167167
// values.
168-
mc, err := NewMissionControl(db, c.source.pubkey, &c.mcCfg)
169-
if err != nil {
170-
c.t.Fatal(err)
171-
}
168+
mcController, err := NewMissionController(db, c.source.pubkey, &c.mcCfg)
169+
require.NoError(c.t, err)
170+
171+
mc, err := mcController.GetNamespacedStore(
172+
DefaultMissionControlNamespace,
173+
)
174+
require.NoError(c.t, err)
172175

173176
getBandwidthHints := func(_ Graph) (bandwidthHints, error) {
174177
// Create bandwidth hints based on local channel balances.

0 commit comments

Comments
 (0)