graph/db: move cache write for UpdateEdgePolicy

To the ChannelGraph.
This commit is contained in:
Elle Mouton
2025-02-17 13:15:49 -03:00
parent 9d0b9f9ace
commit ba1d21d5c7
3 changed files with 48 additions and 19 deletions

View File

@@ -326,6 +326,7 @@ The underlying functionality between those two options remain the same.
- [2](https://github.com/lightningnetwork/lnd/pull/9545) - [2](https://github.com/lightningnetwork/lnd/pull/9545)
- [3](https://github.com/lightningnetwork/lnd/pull/9550) - [3](https://github.com/lightningnetwork/lnd/pull/9550)
- [4](https://github.com/lightningnetwork/lnd/pull/9551) - [4](https://github.com/lightningnetwork/lnd/pull/9551)
- [5](https://github.com/lightningnetwork/lnd/pull/9552)
* [Golang was updated to * [Golang was updated to
`v1.22.11`](https://github.com/lightningnetwork/lnd/pull/9462). `v1.22.11`](https://github.com/lightningnetwork/lnd/pull/9462).

View File

@@ -460,3 +460,35 @@ func (c *ChannelGraph) MarkEdgeZombie(chanID uint64,
return nil 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
}

View File

@@ -2728,11 +2728,12 @@ func makeZombiePubkeys(info *models.ChannelEdgeInfo,
// determined by the lexicographical ordering of the identity public keys of the // determined by the lexicographical ordering of the identity public keys of the
// nodes on either side of the channel. // nodes on either side of the channel.
func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy, func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
op ...batch.SchedulerOption) error { op ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
var ( var (
isUpdate1 bool isUpdate1 bool
edgeNotFound bool edgeNotFound bool
from, to route.Vertex
) )
r := &batch.Request{ r := &batch.Request{
@@ -2742,10 +2743,7 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
}, },
Update: func(tx kvdb.RwTx) error { Update: func(tx kvdb.RwTx) error {
var err error var err error
isUpdate1, err = updateEdgePolicy( from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
tx, edge, c.graphCache,
)
if err != nil { if err != nil {
log.Errorf("UpdateEdgePolicy faild: %v", err) log.Errorf("UpdateEdgePolicy faild: %v", err)
} }
@@ -2776,7 +2774,9 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
f(r) f(r)
} }
return c.chanScheduler.Execute(r) err := c.chanScheduler.Execute(r)
return from, to, err
} }
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy, 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 // 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 // true if the updated policy belongs to node1, and false if the policy belonged
// to node2. // to node2.
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy, func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
graphCache *GraphCache) (bool, error) { route.Vertex, route.Vertex, bool, error) {
var noVertex route.Vertex
edges := tx.ReadWriteBucket(edgeBucket) edges := tx.ReadWriteBucket(edgeBucket)
if edges == nil { if edges == nil {
return false, ErrEdgeNotFound return noVertex, noVertex, false, ErrEdgeNotFound
} }
edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket) edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
if edgeIndex == nil { if edgeIndex == nil {
return false, ErrEdgeNotFound return noVertex, noVertex, false, ErrEdgeNotFound
} }
// Create the channelID key be converting the channel ID // 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. // nodes which connect this channel edge.
nodeInfo := edgeIndex.Get(chanID[:]) nodeInfo := edgeIndex.Get(chanID[:])
if nodeInfo == nil { if nodeInfo == nil {
return false, ErrEdgeNotFound return noVertex, noVertex, false, ErrEdgeNotFound
} }
// Depending on the flags value passed above, either the first // 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. // identified, we update the on-disk edge representation.
err := putChanEdgePolicy(edges, edge, fromNode, toNode) err := putChanEdgePolicy(edges, edge, fromNode, toNode)
if err != nil { if err != nil {
return false, err return noVertex, noVertex, false, err
} }
var ( var (
@@ -2865,13 +2867,7 @@ func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy,
copy(fromNodePubKey[:], fromNode) copy(fromNodePubKey[:], fromNode)
copy(toNodePubKey[:], toNode) copy(toNodePubKey[:], toNode)
if graphCache != nil { return fromNodePubKey, toNodePubKey, isUpdate1, nil
graphCache.UpdatePolicy(
edge, fromNodePubKey, toNodePubKey, isUpdate1,
)
}
return isUpdate1, nil
} }
// isPublic determines whether the node is seen as public within the graph from // isPublic determines whether the node is seen as public within the graph from