routing: check route length after edges for route is collected

This fixes the bug reproduced in #114. The prevHop map in newRoute may
include many more edges than what is used to produce the final route, and
thus the check prior to building the route could result with incorrect
errors being reported. We move this check to after the number of edges
to be used for the route is deduced.
This commit is contained in:
Christopher Jämthagen 2017-02-01 13:35:28 +01:00 committed by Olaoluwa Osuntokun
parent 08f0d0fbea
commit 4200d8a7e0

@ -90,15 +90,6 @@ func computeFee(amt btcutil.Amount, edge *channeldb.ChannelEdge) btcutil.Amount
func newRoute(amtToSend btcutil.Amount, source, target vertex,
prevHop map[vertex]edgeWithPrev) (*Route, error) {
// As an initial sanity check, the potential route is immediately
// invalidate if it spans more than 20 hops. The current Sphinx (onion
// routing) implementation can only encode up to 20 hops as the entire
// packet is fixed size. If this route is more than 20 hops, then it's
// invalid.
if len(prevHop) > HopLimit {
return nil, ErrMaxHopsExceeded
}
// If the potential route if below the max hop limit, then we'll use
// the prevHop map to unravel the path. We end up with a list of edges
// in the reverse direction which we'll use to properly calculate the
@ -113,6 +104,14 @@ func newRoute(amtToSend btcutil.Amount, source, target vertex,
prev = newVertex(prevHop[prev].prevNode)
}
// The route is invalid if it spans more than 20 hops. The current
// Sphinx (onion routing) implementation can only encode up to 20 hops
// as the entire packet is fixed size. If this route is more than 20 hops,
// then it's invalid.
if len(pathEdges) > HopLimit {
return nil, ErrMaxHopsExceeded
}
route := &Route{
Hops: make([]*Hop, len(pathEdges)),
}