From 28bbaa2a94a79b3e3966ce1d7461183c3253d996 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 9 Jan 2020 18:44:43 -0800 Subject: [PATCH] routing: convert to use new kvdb abstraction --- routing/graph.go | 8 +++---- routing/integrated_routing_context_test.go | 4 ++-- routing/missioncontrol.go | 4 ++-- routing/missioncontrol_store.go | 28 +++++++++++----------- routing/missioncontrol_store_test.go | 4 ++-- routing/missioncontrol_test.go | 6 ++--- routing/router.go | 8 +++---- routing/router_test.go | 2 +- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/routing/graph.go b/routing/graph.go index 14eca1786..c1a68bae9 100644 --- a/routing/graph.go +++ b/routing/graph.go @@ -1,8 +1,8 @@ package routing import ( - "github.com/coreos/bbolt" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -26,7 +26,7 @@ type routingGraph interface { // database. type dbRoutingTx struct { graph *channeldb.ChannelGraph - tx *bbolt.Tx + tx kvdb.ReadTx source route.Vertex } @@ -38,7 +38,7 @@ func newDbRoutingTx(graph *channeldb.ChannelGraph) (*dbRoutingTx, error) { return nil, err } - tx, err := graph.Database().Begin(false) + tx, err := graph.Database().BeginReadTx() if err != nil { return nil, err } @@ -62,7 +62,7 @@ func (g *dbRoutingTx) forEachNodeChannel(nodePub route.Vertex, cb func(*channeldb.ChannelEdgeInfo, *channeldb.ChannelEdgePolicy, *channeldb.ChannelEdgePolicy) error) error { - txCb := func(_ *bbolt.Tx, info *channeldb.ChannelEdgeInfo, + txCb := func(_ kvdb.ReadTx, info *channeldb.ChannelEdgeInfo, p1, p2 *channeldb.ChannelEdgePolicy) error { return cb(info, p1, p2) diff --git a/routing/integrated_routing_context_test.go b/routing/integrated_routing_context_test.go index eb59473e6..db5c6fe9f 100644 --- a/routing/integrated_routing_context_test.go +++ b/routing/integrated_routing_context_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/coreos/bbolt" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -82,7 +82,7 @@ func (c *integratedRoutingContext) testPayment(expectedNofAttempts int) { dbPath := file.Name() defer os.Remove(dbPath) - db, err := bbolt.Open(dbPath, 0600, nil) + db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true) if err != nil { c.t.Fatal(err) } diff --git a/routing/missioncontrol.go b/routing/missioncontrol.go index f52c6fce9..a590f20ae 100644 --- a/routing/missioncontrol.go +++ b/routing/missioncontrol.go @@ -4,8 +4,8 @@ import ( "sync" "time" - "github.com/coreos/bbolt" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -173,7 +173,7 @@ type paymentResult struct { } // NewMissionControl returns a new instance of missionControl. -func NewMissionControl(db *bbolt.DB, cfg *MissionControlConfig) ( +func NewMissionControl(db kvdb.Backend, cfg *MissionControlConfig) ( *MissionControl, error) { log.Debugf("Instantiating mission control with config: "+ diff --git a/routing/missioncontrol_store.go b/routing/missioncontrol_store.go index 329d819f7..9cd99d954 100644 --- a/routing/missioncontrol_store.go +++ b/routing/missioncontrol_store.go @@ -7,8 +7,8 @@ import ( "time" "github.com/btcsuite/btcd/wire" - "github.com/coreos/bbolt" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" ) @@ -35,20 +35,20 @@ const ( // Also changes to mission control parameters can be applied to historical data. // Finally, it enables importing raw data from an external source. type missionControlStore struct { - db *bbolt.DB + db kvdb.Backend maxRecords int numRecords int } -func newMissionControlStore(db *bbolt.DB, maxRecords int) (*missionControlStore, error) { +func newMissionControlStore(db kvdb.Backend, maxRecords int) (*missionControlStore, error) { store := &missionControlStore{ db: db, maxRecords: maxRecords, } // Create buckets if not yet existing. - err := db.Update(func(tx *bbolt.Tx) error { - resultsBucket, err := tx.CreateBucketIfNotExists(resultsKey) + err := kvdb.Update(db, func(tx kvdb.RwTx) error { + resultsBucket, err := tx.CreateTopLevelBucket(resultsKey) if err != nil { return fmt.Errorf("cannot create results bucket: %v", err) @@ -58,7 +58,7 @@ func newMissionControlStore(db *bbolt.DB, maxRecords int) (*missionControlStore, // memory to avoid calling Stats().KeyN. The reliability of // Stats() is doubtful and seemed to have caused crashes in the // past (see #1874). - c := resultsBucket.Cursor() + c := resultsBucket.ReadCursor() for k, _ := c.First(); k != nil; k, _ = c.Next() { store.numRecords++ } @@ -74,12 +74,12 @@ func newMissionControlStore(db *bbolt.DB, maxRecords int) (*missionControlStore, // clear removes all results from the db. func (b *missionControlStore) clear() error { - return b.db.Update(func(tx *bbolt.Tx) error { - if err := tx.DeleteBucket(resultsKey); err != nil { + return kvdb.Update(b.db, func(tx kvdb.RwTx) error { + if err := tx.DeleteTopLevelBucket(resultsKey); err != nil { return err } - _, err := tx.CreateBucket(resultsKey) + _, err := tx.CreateTopLevelBucket(resultsKey) return err }) } @@ -88,8 +88,8 @@ func (b *missionControlStore) clear() error { func (b *missionControlStore) fetchAll() ([]*paymentResult, error) { var results []*paymentResult - err := b.db.View(func(tx *bbolt.Tx) error { - resultBucket := tx.Bucket(resultsKey) + err := kvdb.View(b.db, func(tx kvdb.ReadTx) error { + resultBucket := tx.ReadBucket(resultsKey) results = make([]*paymentResult, 0) return resultBucket.ForEach(func(k, v []byte) error { @@ -218,13 +218,13 @@ func deserializeResult(k, v []byte) (*paymentResult, error) { // AddResult adds a new result to the db. func (b *missionControlStore) AddResult(rp *paymentResult) error { - return b.db.Update(func(tx *bbolt.Tx) error { - bucket := tx.Bucket(resultsKey) + return kvdb.Update(b.db, func(tx kvdb.RwTx) error { + bucket := tx.ReadWriteBucket(resultsKey) // Prune oldest entries. if b.maxRecords > 0 { for b.numRecords >= b.maxRecords { - cursor := bucket.Cursor() + cursor := bucket.ReadWriteCursor() cursor.First() if err := cursor.Delete(); err != nil { return err diff --git a/routing/missioncontrol_store_test.go b/routing/missioncontrol_store_test.go index e44c59197..497affd52 100644 --- a/routing/missioncontrol_store_test.go +++ b/routing/missioncontrol_store_test.go @@ -8,9 +8,9 @@ import ( "time" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" - "github.com/coreos/bbolt" "github.com/lightningnetwork/lnd/routing/route" ) @@ -31,7 +31,7 @@ func TestMissionControlStore(t *testing.T) { dbPath := file.Name() - db, err := bbolt.Open(dbPath, 0600, nil) + db, err := kvdb.Create(kvdb.BoltBackendName, dbPath, true) if err != nil { t.Fatal(err) } diff --git a/routing/missioncontrol_test.go b/routing/missioncontrol_test.go index 98cf95302..3a9c93a75 100644 --- a/routing/missioncontrol_test.go +++ b/routing/missioncontrol_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/coreos/bbolt" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -44,7 +44,7 @@ type mcTestContext struct { mc *MissionControl now time.Time - db *bbolt.DB + db kvdb.Backend dbPath string pid uint64 @@ -63,7 +63,7 @@ func createMcTestContext(t *testing.T) *mcTestContext { ctx.dbPath = file.Name() - ctx.db, err = bbolt.Open(ctx.dbPath, 0600, nil) + ctx.db, err = kvdb.Open(kvdb.BoltBackendName, ctx.dbPath, true) if err != nil { t.Fatal(err) } diff --git a/routing/router.go b/routing/router.go index 6751bfaea..ca7a20f80 100644 --- a/routing/router.go +++ b/routing/router.go @@ -11,12 +11,12 @@ import ( "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" - "github.com/coreos/bbolt" "github.com/davecgh/go-spew/spew" "github.com/go-errors/errors" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" @@ -2111,7 +2111,7 @@ func (r *ChannelRouter) FetchLightningNode(node route.Vertex) (*channeldb.Lightn // // NOTE: This method is part of the ChannelGraphSource interface. func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) error { - return r.cfg.Graph.ForEachNode(nil, func(_ *bbolt.Tx, n *channeldb.LightningNode) error { + return r.cfg.Graph.ForEachNode(nil, func(_ kvdb.ReadTx, n *channeldb.LightningNode) error { return cb(n) }) } @@ -2123,7 +2123,7 @@ func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) err func (r *ChannelRouter) ForAllOutgoingChannels(cb func(*channeldb.ChannelEdgeInfo, *channeldb.ChannelEdgePolicy) error) error { - return r.selfNode.ForEachChannel(nil, func(_ *bbolt.Tx, c *channeldb.ChannelEdgeInfo, + return r.selfNode.ForEachChannel(nil, func(_ kvdb.ReadTx, c *channeldb.ChannelEdgeInfo, e, _ *channeldb.ChannelEdgePolicy) error { if e == nil { @@ -2264,7 +2264,7 @@ func generateBandwidthHints(sourceNode *channeldb.LightningNode, // First, we'll collect the set of outbound edges from the target // source node. var localChans []*channeldb.ChannelEdgeInfo - err := sourceNode.ForEachChannel(nil, func(tx *bbolt.Tx, + err := sourceNode.ForEachChannel(nil, func(tx kvdb.ReadTx, edgeInfo *channeldb.ChannelEdgeInfo, _, _ *channeldb.ChannelEdgePolicy) error { diff --git a/routing/router_test.go b/routing/router_test.go index dd8908c4d..42020b7ea 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -97,7 +97,7 @@ func createTestCtxFromGraphInstance(startingHeight uint32, graphInstance *testGr } mc, err := NewMissionControl( - graphInstance.graph.Database().DB, + graphInstance.graph.Database(), mcConfig, ) if err != nil {