Skip to content

Commit 6b449a6

Browse files
committed
routing: start writing and reading from namespaced MC
and invoke the associated mission control migration.
1 parent bfe4a08 commit 6b449a6

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
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
}

routing/missioncontrol.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ const (
6969
// FeeEstimationTimeout. It defines the maximum duration that the
7070
// probing fee estimation is allowed to take.
7171
DefaultFeeEstimationTimeout = time.Minute
72+
73+
// DefaultMissionControlNamespace is the name of the default mission
74+
// control name space. This is used as the sub-bucket key within the
75+
// top level DB bucket to store mission control results.
76+
DefaultMissionControlNamespace = "default"
7277
)
7378

7479
var (
@@ -227,7 +232,7 @@ func NewMissionControl(db kvdb.Backend, self route.Vertex,
227232
}
228233

229234
store, err := newMissionControlStore(
230-
newNamespacedDB(db), cfg.MaxMcHistory,
235+
newDefaultNamespacedStore(db), cfg.MaxMcHistory,
231236
cfg.McFlushInterval,
232237
)
233238
if err != nil {
@@ -549,22 +554,30 @@ func (m *MissionControl) applyPaymentResult(
549554
}
550555

551556
// namespacedDB is an implementation of the missionControlDB that gives a user
552-
// of the interface access to the top level mission control bucket. In a
553-
// follow-up commit (accompanied by a migration), this will change to giving
554-
// the user of the interface access to a namespaced sub-bucket instead.
557+
// of the interface access to a namespaced bucket within the top level mission
558+
// control bucket.
555559
type namespacedDB struct {
556560
topLevelBucketKey []byte
561+
namespace []byte
557562
db kvdb.Backend
558563
}
559564

560565
// A compile-time check to ensure that namespacedDB implements missionControlDB.
561566
var _ missionControlDB = (*namespacedDB)(nil)
562567

568+
// newDefaultNamespacedStore creates an instance of namespaceDB that uses the
569+
// default namespace.
570+
func newDefaultNamespacedStore(db kvdb.Backend) missionControlDB {
571+
return newNamespacedDB(db, DefaultMissionControlNamespace)
572+
}
573+
563574
// newNamespacedDB creates a new instance of missionControlDB where the DB will
564-
// have access to the top level bucket.
565-
func newNamespacedDB(db kvdb.Backend) missionControlDB {
575+
// have access to a namespaced bucket within the top level mission control
576+
// bucket.
577+
func newNamespacedDB(db kvdb.Backend, namespace string) missionControlDB {
566578
return &namespacedDB{
567579
db: db,
580+
namespace: []byte(namespace),
568581
topLevelBucketKey: resultsKey,
569582
}
570583
}
@@ -582,7 +595,16 @@ func (n *namespacedDB) update(f func(bkt walletdb.ReadWriteBucket) error,
582595
"control bucket: %w", err)
583596
}
584597

585-
return f(mcStoreBkt)
598+
namespacedBkt, err := mcStoreBkt.CreateBucketIfNotExists(
599+
n.namespace,
600+
)
601+
if err != nil {
602+
return fmt.Errorf("cannot create namespaced bucket "+
603+
"(%s) in mission control store: %w",
604+
n.namespace, err)
605+
}
606+
607+
return f(namespacedBkt)
586608
}, reset)
587609
}
588610

@@ -599,7 +621,13 @@ func (n *namespacedDB) view(f func(bkt walletdb.ReadBucket) error,
599621
"not found")
600622
}
601623

602-
return f(mcStoreBkt)
624+
namespacedBkt := mcStoreBkt.NestedReadBucket(n.namespace)
625+
if namespacedBkt == nil {
626+
return fmt.Errorf("namespaced bucket (%s) not found "+
627+
"in mission control store", n.namespace)
628+
}
629+
630+
return f(namespacedBkt)
603631
}, reset)
604632
}
605633

@@ -608,12 +636,17 @@ func (n *namespacedDB) view(f func(bkt walletdb.ReadBucket) error,
608636
// NOTE: this is part of the missionControlDB interface.
609637
func (n *namespacedDB) purge() error {
610638
return n.db.Update(func(tx kvdb.RwTx) error {
611-
err := tx.DeleteTopLevelBucket(n.topLevelBucketKey)
639+
mcStoreBkt := tx.ReadWriteBucket(n.topLevelBucketKey)
640+
if mcStoreBkt == nil {
641+
return nil
642+
}
643+
644+
err := mcStoreBkt.DeleteNestedBucket(n.namespace)
612645
if err != nil {
613646
return err
614647
}
615648

616-
_, err = tx.CreateTopLevelBucket(n.topLevelBucketKey)
649+
_, err = mcStoreBkt.CreateBucket(n.namespace)
617650

618651
return err
619652
}, func() {})

routing/missioncontrol_store_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func newMCStoreTestHarness(t testing.TB, maxRecords int,
6363
})
6464

6565
store, err := newMissionControlStore(
66-
newNamespacedDB(db), maxRecords, flushInterval,
66+
newDefaultNamespacedStore(db), maxRecords, flushInterval,
6767
)
6868
require.NoError(t, err)
6969

@@ -118,7 +118,7 @@ func TestMissionControlStore(t *testing.T) {
118118

119119
// Recreate store to test pruning.
120120
store, err = newMissionControlStore(
121-
newNamespacedDB(db), testMaxRecords, time.Second,
121+
newDefaultNamespacedStore(db), testMaxRecords, time.Second,
122122
)
123123
require.NoError(t, err)
124124

@@ -218,7 +218,7 @@ func TestMissionControlStoreFlushing(t *testing.T) {
218218

219219
// Recreate store.
220220
store, err := newMissionControlStore(
221-
newNamespacedDB(db), testMaxRecords, flushInterval,
221+
newDefaultNamespacedStore(db), testMaxRecords, flushInterval,
222222
)
223223
require.NoError(t, err)
224224
store.run()

0 commit comments

Comments
 (0)