From cb16c7177ab2ddbf9d8512bc1ae2a08c54108eab Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 4 Jun 2025 12:50:31 +0200 Subject: [PATCH] graph/db: use InboundFee directly from ChannelEdgePolicy Now that we know that the InboundFee on the ChannelEdgePolicy is always set appropriately, we can update the GraphCache UpdatePolicy method to take the InboundFee directly from the ChannelEdgePolicy object. --- graph/db/graph_cache.go | 19 ++++++------------- graph/db/graph_cache_test.go | 7 +++++++ routing/pathfind_test.go | 20 ++++++++++++++++---- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/graph/db/graph_cache.go b/graph/db/graph_cache.go index cefa88a85..141f610b2 100644 --- a/graph/db/graph_cache.go +++ b/graph/db/graph_cache.go @@ -180,17 +180,6 @@ func (c *GraphCache) updateOrAddEdge(node route.Vertex, edge *DirectedChannel) { func (c *GraphCache) UpdatePolicy(policy *models.ChannelEdgePolicy, fromNode, toNode route.Vertex, edge1 bool) { - // Extract inbound fee if possible and available. If there is a decoding - // error, ignore this policy. - var inboundFee lnwire.Fee - _, err := policy.ExtraOpaqueData.ExtractRecords(&inboundFee) - if err != nil { - log.Errorf("Failed to extract records from edge policy %v: %v", - policy.ChannelID, err) - - return - } - c.mtx.Lock() defer c.mtx.Unlock() @@ -216,13 +205,17 @@ func (c *GraphCache) UpdatePolicy(policy *models.ChannelEdgePolicy, fromNode, // policy for node 1. case channel.IsNode1 && edge1: channel.OutPolicySet = true - channel.InboundFee = inboundFee + policy.InboundFee.WhenSome(func(fee lnwire.Fee) { + channel.InboundFee = fee + }) // This is node 2, and it is edge 2, so this is the outgoing // policy for node 2. case !channel.IsNode1 && !edge1: channel.OutPolicySet = true - channel.InboundFee = inboundFee + policy.InboundFee.WhenSome(func(fee lnwire.Fee) { + channel.InboundFee = fee + }) // The other two cases left mean it's the inbound policy for the // node. diff --git a/graph/db/graph_cache_test.go b/graph/db/graph_cache_test.go index 087fb81ac..96318366d 100644 --- a/graph/db/graph_cache_test.go +++ b/graph/db/graph_cache_test.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "testing" + "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" @@ -37,11 +38,17 @@ func TestGraphCacheAddNode(t *testing.T) { channelFlagA, channelFlagB = 1, 0 } + inboundFee := lnwire.Fee{ + BaseFee: 10, + FeeRate: 20, + } + outPolicy1 := &models.ChannelEdgePolicy{ ChannelID: 1000, ChannelFlags: lnwire.ChanUpdateChanFlags(channelFlagA), ToNode: nodeB, // Define an inbound fee. + InboundFee: fn.Some(inboundFee), ExtraOpaqueData: []byte{ 253, 217, 3, 8, 0, 0, 0, 10, 0, 0, 0, 20, }, diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index bc3a9ef58..361fb83d4 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -666,15 +666,25 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, return nil, err } - getExtraData := func( - end *testChannelEnd) lnwire.ExtraOpaqueData { + getInboundFees := func( + end *testChannelEnd) fn.Option[lnwire.Fee] { - var extraData lnwire.ExtraOpaqueData inboundFee := lnwire.Fee{ BaseFee: int32(end.InboundFeeBaseMsat), FeeRate: int32(end.InboundFeeRate), } - require.NoError(t, extraData.PackRecords(&inboundFee)) + + return fn.Some(inboundFee) + } + getExtraData := func( + end *testChannelEnd) lnwire.ExtraOpaqueData { + + var extraData lnwire.ExtraOpaqueData + + inboundFee := getInboundFees(end) + inboundFee.WhenSome(func(fee lnwire.Fee) { + require.NoError(t, extraData.PackRecords(&fee)) + }) return extraData } @@ -701,6 +711,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, FeeBaseMSat: node1.FeeBaseMsat, FeeProportionalMillionths: node1.FeeRate, ToNode: node2Vertex, + InboundFee: getInboundFees(node1), //nolint:ll ExtraOpaqueData: getExtraData(node1), } if err := graph.UpdateEdgePolicy(edgePolicy); err != nil { @@ -731,6 +742,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, FeeBaseMSat: node2.FeeBaseMsat, FeeProportionalMillionths: node2.FeeRate, ToNode: node1Vertex, + InboundFee: getInboundFees(node2), //nolint:ll ExtraOpaqueData: getExtraData(node2), } if err := graph.UpdateEdgePolicy(edgePolicy); err != nil {