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.
This commit is contained in:
Elle Mouton
2025-06-04 12:50:31 +02:00
parent 9890d74622
commit cb16c7177a
3 changed files with 29 additions and 17 deletions

View File

@@ -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.

View File

@@ -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,
},

View File

@@ -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 {