@@ -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
7479var (
@@ -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.
555559type 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.
561566var _ 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.
609637func (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 () {})
0 commit comments