Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing: remove newRoute #6920

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 54 additions & 40 deletions routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2729,11 +2729,16 @@ func (r *ChannelRouter) BuildRoute(amt *lnwire.MilliSatoshi,
return nil, err
}

// Allocate a list that will contain the unified policies for this
// route.
edges := make([]*unifiedPolicy, len(hops))
var (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing newRoute from BuildRoute, we can also skip that and re-route BuildRoute to pathfinding: #6921

routeHops []*route.Hop

// timeLock will accumulate the cumulative time lock across the
// entire route.
timeLock = height

runningAmt lnwire.MilliSatoshi
)

var runningAmt lnwire.MilliSatoshi
if useMinAmt {
// For minimum amount routes, aim to deliver at least 1 msat to
// the destination. There are nodes in the wild that have a
Expand All @@ -2758,7 +2763,12 @@ func (r *ChannelRouter) BuildRoute(amt *lnwire.MilliSatoshi,
fromNode = hops[i-1]
}

localChan := i == 0
// Initialize a route hop for toNode.
currentHop := &route.Hop{
PubKeyBytes: toNode,
AmtToForward: runningAmt,
OutgoingTimeLock: uint32(timeLock),
}

// Build unified policies for this hop based on the channels
// known in the graph.
Expand Down Expand Up @@ -2796,50 +2806,54 @@ func (r *ChannelRouter) BuildRoute(amt *lnwire.MilliSatoshi,
}
}

// Add fee for this hop.
if !localChan {
runningAmt += policy.ComputeFee(runningAmt)
log.Tracef("Select channel %v at position %v", policy.ChannelID, i)

// Define a helper function that checks this edge's feature
// vector for support for a given feature. We assume at this
// point that the feature vectors transitive dependencies have
// been validated.
supports := func(feature lnwire.FeatureBit) bool {
// If this edge comes from router hints, the features
// could be nil.
if policy.ToNodeFeatures == nil {
return false
}

return policy.ToNodeFeatures.HasFeature(feature)
}

log.Tracef("Select channel %v at position %v", policy.ChannelID, i)
// Store the ID for the selected channel.
currentHop.ChannelID = policy.ChannelID

edges[i] = unifiedPolicy
}
// Store the payload type.
tlvPayload := supports(lnwire.TLVOnionPayloadOptional)
currentHop.LegacyPayload = !tlvPayload

// Now that we arrived at the start of the route and found out the route
// total amount, we make a forward pass. Because the amount may have
// been increased in the backward pass, fees need to be recalculated and
// amount ranges re-checked.
var pathEdges []*channeldb.CachedEdgePolicy
receiverAmt := runningAmt
for i, edge := range edges {
policy := edge.getPolicy(receiverAmt, bandwidthHints)
if policy == nil {
return nil, ErrNoChannel{
fromNode: hops[i-1],
position: i,
// Store MPP record for final hop if requested and possible.
isFinalHop := i == len(hops)-1
if isFinalHop && payAddr != nil {
// If we're attaching a payment addr but the receiver
// doesn't support both TLV and payment addrs, fail.
if !supports(lnwire.PaymentAddrOptional) {
return nil, errors.New("cannot attach " +
"payment addr")
}
}

if i > 0 {
// Decrease the amount to send while going forward.
receiverAmt -= policy.ComputeFeeFromIncoming(
receiverAmt,
)
currentHop.MPP = record.NewMPP(runningAmt, *payAddr)
}

pathEdges = append(pathEdges, policy)
routeHops = append([]*route.Hop{currentHop}, routeHops...)

// Add fee for this hop and update time lock.
localChan := i == 0
if !localChan {
runningAmt += policy.ComputeFee(runningAmt)
timeLock += int32(policy.TimeLockDelta)
}
}

// Build and return the final route.
return newRoute(
source, pathEdges, uint32(height),
finalHopParams{
amt: receiverAmt,
totalAmt: receiverAmt,
cltvDelta: uint16(finalCltvDelta),
records: nil,
paymentAddr: payAddr,
},
// With the base routing data expressed as hops, build the full route
return route.NewRouteFromHops(
runningAmt, uint32(timeLock), source, routeHops,
)
}