Skip to content

Commit

Permalink
graph/db: move cache write for UpdateEdgePolicy
Browse files Browse the repository at this point in the history
To the ChannelGraph.
  • Loading branch information
ellemouton committed Feb 27, 2025
1 parent 6bf8697 commit 3ce4799
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/release-notes-0.19.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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).
Expand Down
32 changes: 32 additions & 0 deletions graph/db/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 15 additions & 19 deletions graph/db/kv_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 (
Expand All @@ -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
Expand Down

0 comments on commit 3ce4799

Please sign in to comment.