From ba1d21d5c730b5f928a57f49884ba3ec7be76957 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 17 Feb 2025 13:15:49 -0300 Subject: [PATCH] graph/db: move cache write for UpdateEdgePolicy To the ChannelGraph. --- docs/release-notes/release-notes-0.19.0.md | 1 + graph/db/graph.go | 32 ++++++++++++++++++++ graph/db/kv_store.go | 34 ++++++++++------------ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index 54941074c..156b71b5d 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -326,6 +326,7 @@ The underlying functionality between those two options remain the same. - [2](https://github.com/lightningnetwork/lnd/pull/9545) - [3](https://github.com/lightningnetwork/lnd/pull/9550) - [4](https://github.com/lightningnetwork/lnd/pull/9551) + - [5](https://github.com/lightningnetwork/lnd/pull/9552) * [Golang was updated to `v1.22.11`](https://github.com/lightningnetwork/lnd/pull/9462). diff --git a/graph/db/graph.go b/graph/db/graph.go index 5d9ea819e..ac80c259a 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -460,3 +460,35 @@ func (c *ChannelGraph) MarkEdgeZombie(chanID uint64, return nil } + +// UpdateEdgePolicy updates the edge routing policy for a single directed edge +// within the database for the referenced channel. The `flags` attribute within +// the ChannelEdgePolicy determines which of the directed edges are being +// updated. If the flag is 1, then the first node's information is being +// updated, otherwise it's the second node's information. The node ordering is +// determined by the lexicographical ordering of the identity public keys of the +// nodes on either side of the channel. +func (c *ChannelGraph) UpdateEdgePolicy(edge *models.ChannelEdgePolicy, + op ...batch.SchedulerOption) error { + + c.cacheMu.Lock() + defer c.cacheMu.Unlock() + + from, to, err := c.KVStore.UpdateEdgePolicy(edge, op...) + if err != nil { + return err + } + + if c.graphCache == nil { + return nil + } + + var isUpdate1 bool + if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 { + isUpdate1 = true + } + + c.graphCache.UpdatePolicy(edge, from, to, isUpdate1) + + return nil +} diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 3dd336578..d9786c2c6 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -2728,11 +2728,12 @@ func makeZombiePubkeys(info *models.ChannelEdgeInfo, // determined by the lexicographical ordering of the identity public keys of the // nodes on either side of the channel. func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy, - op ...batch.SchedulerOption) error { + op ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) { var ( isUpdate1 bool edgeNotFound bool + from, to route.Vertex ) r := &batch.Request{ @@ -2742,10 +2743,7 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy, }, Update: func(tx kvdb.RwTx) error { var err error - isUpdate1, err = updateEdgePolicy( - tx, edge, c.graphCache, - ) - + from, to, isUpdate1, err = updateEdgePolicy(tx, edge) if err != nil { log.Errorf("UpdateEdgePolicy faild: %v", err) } @@ -2776,7 +2774,9 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy, f(r) } - return c.chanScheduler.Execute(r) + err := c.chanScheduler.Execute(r) + + return from, to, err } func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy, @@ -2813,16 +2813,18 @@ func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy, // buckets using an existing database transaction. The returned boolean will be // true if the updated policy belongs to node1, and false if the policy belonged // to node2. -func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy, - graphCache *GraphCache) (bool, error) { +func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) ( + route.Vertex, route.Vertex, bool, error) { + + var noVertex route.Vertex edges := tx.ReadWriteBucket(edgeBucket) if edges == nil { - return false, ErrEdgeNotFound + return noVertex, noVertex, false, ErrEdgeNotFound } edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket) if edgeIndex == nil { - return false, ErrEdgeNotFound + return noVertex, noVertex, false, ErrEdgeNotFound } // Create the channelID key be converting the channel ID @@ -2834,7 +2836,7 @@ func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy, // nodes which connect this channel edge. nodeInfo := edgeIndex.Get(chanID[:]) if nodeInfo == nil { - return false, ErrEdgeNotFound + return noVertex, noVertex, false, ErrEdgeNotFound } // Depending on the flags value passed above, either the first @@ -2855,7 +2857,7 @@ func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy, // identified, we update the on-disk edge representation. err := putChanEdgePolicy(edges, edge, fromNode, toNode) if err != nil { - return false, err + return noVertex, noVertex, false, err } var ( @@ -2865,13 +2867,7 @@ func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy, copy(fromNodePubKey[:], fromNode) copy(toNodePubKey[:], toNode) - if graphCache != nil { - graphCache.UpdatePolicy( - edge, fromNodePubKey, toNodePubKey, isUpdate1, - ) - } - - return isUpdate1, nil + return fromNodePubKey, toNodePubKey, isUpdate1, nil } // isPublic determines whether the node is seen as public within the graph from