From 674ff32a60faee585a896990e808769dc99587ce Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 27 Oct 2023 12:26:47 +0200 Subject: [PATCH] routing+channeldb: update CachedEdgePolicy In preparation for CachedEdgePolicy being used to represent ChannelEdgePolicy1 or ChannelEdgePolicy2, we update it to have IsDisabled and HasMaxHTLC booleans (which can be extracted from both messages) instead of having the MessageFlags and ChannelFlags which only applies to ChannelEdgePolicy1. --- channeldb/graph_cache_test.go | 4 ++-- channeldb/models/cached_edge_policy.go | 33 +++++++++++++------------- routing/unified_edges.go | 12 ++++------ routing/unified_edges_test.go | 4 ++-- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/channeldb/graph_cache_test.go b/channeldb/graph_cache_test.go index 65e639be5..6ccb99d8a 100644 --- a/channeldb/graph_cache_test.go +++ b/channeldb/graph_cache_test.go @@ -157,8 +157,8 @@ func assertCachedPolicyEqual(t *testing.T, original *models.ChannelEdgePolicy1, cached *models.CachedEdgePolicy) { require.Equal(t, original.ChannelID, cached.ChannelID) - require.Equal(t, original.MessageFlags, cached.MessageFlags) - require.Equal(t, original.ChannelFlags, cached.ChannelFlags) + require.Equal(t, original.MessageFlags.HasMaxHtlc(), cached.HasMaxHTLC) + require.Equal(t, original.ChannelFlags.IsDisabled(), cached.IsDisabled) require.Equal(t, original.TimeLockDelta, cached.TimeLockDelta) require.Equal(t, original.MinHTLC, cached.MinHTLC) require.Equal(t, original.MaxHTLC, cached.MaxHTLC) diff --git a/channeldb/models/cached_edge_policy.go b/channeldb/models/cached_edge_policy.go index 89f9a98a0..a63749af6 100644 --- a/channeldb/models/cached_edge_policy.go +++ b/channeldb/models/cached_edge_policy.go @@ -11,7 +11,7 @@ const ( ) // CachedEdgePolicy is a struct that only caches the information of a -// ChannelEdgePolicy1 that we actually use for pathfinding and therefore need to +// ChannelEdgePolicy that we actually use for pathfinding and therefore need to // store in the cache. type CachedEdgePolicy struct { // ChannelID is the unique channel ID for the channel. The first 3 @@ -19,13 +19,12 @@ type CachedEdgePolicy struct { // and the last 2 bytes are the output index for the channel. ChannelID uint64 - // MessageFlags is a bitfield which indicates the presence of optional - // fields (like max_htlc) in the policy. - MessageFlags lnwire.ChanUpdateMsgFlags + // HasMaxHTLC is true if the policy update includes a value for MaxHTLC. + HasMaxHTLC bool - // ChannelFlags is a bitfield which signals the capabilities of the - // channel as well as the directed edge this update applies to. - ChannelFlags lnwire.ChanUpdateChanFlags + // IsDisabled is true if this policy is signalling that the channel is + // disabled. + IsDisabled bool // TimeLockDelta is the number of blocks this node will subtract from // the expiry of an incoming HTLC. This value expresses the time buffer @@ -72,15 +71,17 @@ func (c *CachedEdgePolicy) ComputeFee( } // NewCachedPolicy turns a full policy into a minimal one that can be cached. -func NewCachedPolicy(policy *ChannelEdgePolicy1) *CachedEdgePolicy { +func NewCachedPolicy(policy ChannelEdgePolicy) *CachedEdgePolicy { + fwdingPolicy := policy.ForwardingPolicy() + return &CachedEdgePolicy{ - ChannelID: policy.ChannelID, - MessageFlags: policy.MessageFlags, - ChannelFlags: policy.ChannelFlags, - TimeLockDelta: policy.TimeLockDelta, - MinHTLC: policy.MinHTLC, - MaxHTLC: policy.MaxHTLC, - FeeBaseMSat: policy.FeeBaseMSat, - FeeProportionalMillionths: policy.FeeProportionalMillionths, + ChannelID: policy.SCID().ToUint64(), + HasMaxHTLC: fwdingPolicy.HasMaxHTLC, + IsDisabled: policy.IsDisabled(), + TimeLockDelta: fwdingPolicy.TimeLockDelta, + MinHTLC: fwdingPolicy.MinHTLC, + MaxHTLC: fwdingPolicy.MaxHTLC, + FeeBaseMSat: fwdingPolicy.BaseFee, + FeeProportionalMillionths: fwdingPolicy.FeeRate, } } diff --git a/routing/unified_edges.go b/routing/unified_edges.go index a0300eea4..8f0ed9052 100644 --- a/routing/unified_edges.go +++ b/routing/unified_edges.go @@ -169,11 +169,10 @@ func (u *unifiedEdge) amtInRange(amt lnwire.MilliSatoshi) bool { } // Skip channels for which this htlc is too large. - if u.policy.MessageFlags.HasMaxHtlc() && - amt > u.policy.MaxHTLC { - + if u.policy.HasMaxHTLC && amt > u.policy.MaxHTLC { log.Tracef("Exceeds policy's MaxHTLC: amt=%v, MaxHTLC=%v", amt, u.policy.MaxHTLC) + return false } @@ -350,18 +349,17 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi, } // For network channels, skip the disabled ones. - edgeFlags := edge.policy.ChannelFlags - isDisabled := edgeFlags&lnwire.ChanUpdateDisabled != 0 - if isDisabled { + if edge.policy.IsDisabled { log.Debugf("Skipped edge %v due to it being disabled", edge.policy.ChannelID) + continue } // Track the maximal capacity for usable channels. If we don't // know the capacity, we fall back to MaxHTLC. capMsat := lnwire.NewMSatFromSatoshis(edge.capacity) - if capMsat == 0 && edge.policy.MessageFlags.HasMaxHtlc() { + if capMsat == 0 && edge.policy.HasMaxHTLC { log.Tracef("No capacity available for channel %v, "+ "using MaxHtlcMsat (%v) as a fallback.", edge.policy.ChannelID, edge.policy.MaxHTLC) diff --git a/routing/unified_edges_test.go b/routing/unified_edges_test.go index 82605e9b3..c4ab1bfd7 100644 --- a/routing/unified_edges_test.go +++ b/routing/unified_edges_test.go @@ -30,7 +30,7 @@ func TestNodeEdgeUnifier(t *testing.T) { FeeProportionalMillionths: 100000, FeeBaseMSat: 30, TimeLockDelta: 60, - MessageFlags: lnwire.ChanUpdateRequiredMaxHtlc, + HasMaxHTLC: true, MaxHTLC: 5000, MinHTLC: 100, } @@ -39,7 +39,7 @@ func TestNodeEdgeUnifier(t *testing.T) { FeeProportionalMillionths: 190000, FeeBaseMSat: 10, TimeLockDelta: 40, - MessageFlags: lnwire.ChanUpdateRequiredMaxHtlc, + HasMaxHTLC: true, MaxHTLC: 4000, MinHTLC: 100, }