routing+refactor: add a constructor for unifiedEdge

Add a constructor for unified edge. In upcoming commits, we will add a
new member to unifiedEdge and a constructor forces us to not forget to
populate a required member.
This commit is contained in:
Elle Mouton 2024-05-14 12:47:36 +02:00
parent 28d1227c04
commit 1ec2a1be11
No known key found for this signature in database
GPG Key ID: D7D916376026F177

View File

@ -86,12 +86,9 @@ func (u *nodeEdgeUnifier) addPolicy(fromNode route.Vertex,
inboundFee = models.InboundFee{}
}
unifier.edges = append(unifier.edges, &unifiedEdge{
policy: edge,
capacity: capacity,
hopPayloadSizeFn: hopPayloadSizeFn,
inboundFees: inboundFee,
})
unifier.edges = append(unifier.edges, newUnifiedEdge(
edge, capacity, inboundFee, hopPayloadSizeFn,
))
}
// addGraphPolicies adds all policies that are known for the toNode in the
@ -139,6 +136,19 @@ type unifiedEdge struct {
hopPayloadSizeFn PayloadSizeFunc
}
// newUnifiedEdge constructs a new unifiedEdge.
func newUnifiedEdge(policy *models.CachedEdgePolicy, capacity btcutil.Amount,
inboundFees models.InboundFee,
hopPayloadSizeFn PayloadSizeFunc) *unifiedEdge {
return &unifiedEdge{
policy: policy,
capacity: capacity,
inboundFees: inboundFees,
hopPayloadSizeFn: hopPayloadSizeFn,
}
}
// amtInRange checks whether an amount falls within the valid range for a
// channel.
func (u *unifiedEdge) amtInRange(amt lnwire.MilliSatoshi) bool {
@ -292,12 +302,10 @@ func (u *edgeUnifier) getEdgeLocal(netAmtReceived lnwire.MilliSatoshi,
maxBandwidth = bandwidth
// Update best edge.
bestEdge = &unifiedEdge{
policy: edge.policy,
capacity: edge.capacity,
hopPayloadSizeFn: edge.hopPayloadSizeFn,
inboundFees: edge.inboundFees,
}
bestEdge = newUnifiedEdge(
edge.policy, edge.capacity, edge.inboundFees,
edge.hopPayloadSizeFn,
)
}
return bestEdge
@ -376,10 +384,9 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,
}
maxFee = fee
bestPolicy = &unifiedEdge{
policy: edge.policy,
inboundFees: edge.inboundFees,
}
bestPolicy = newUnifiedEdge(
edge.policy, 0, edge.inboundFees, nil,
)
// The payload size function for edges to a connected peer is
// always the same hence there is not need to find the maximum.
@ -404,15 +411,13 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,
// chance for this node pair. But this is all only needed for nodes that
// have distinct policies for channels to the same peer.
policyCopy := *bestPolicy.policy
modifiedEdge := unifiedEdge{
policy: &policyCopy,
inboundFees: bestPolicy.inboundFees,
}
modifiedEdge.policy.TimeLockDelta = maxTimelock
modifiedEdge.capacity = maxCapMsat.ToSatoshis()
modifiedEdge.hopPayloadSizeFn = hopPayloadSizeFn
policyCopy.TimeLockDelta = maxTimelock
modifiedEdge := newUnifiedEdge(
&policyCopy, maxCapMsat.ToSatoshis(), bestPolicy.inboundFees,
hopPayloadSizeFn,
)
return &modifiedEdge
return modifiedEdge
}
// minAmt returns the minimum amount that can be forwarded on this connection.