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: local balance check before path finding #3749

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 1 addition & 9 deletions routing/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@ import "github.com/go-errors/errors"
type errorCode uint8

const (
// ErrNoPathFound is returned when a path to the target destination
// does not exist in the graph.
ErrNoPathFound errorCode = iota

// ErrMaxHopsExceeded is returned when a candidate path is found, but
// the length of that path exceeds HopLimit.
ErrMaxHopsExceeded

// ErrTargetNotInNetwork is returned when the target of a path-finding
// or payment attempt isn't known to be within the current version of
// the channel graph.
ErrTargetNotInNetwork
ErrTargetNotInNetwork errorCode = iota
Copy link
Contributor

Choose a reason for hiding this comment

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

preexisting, but errorCode should be exported if ErrTargetNotInNetwork is also exported for godocs

Copy link
Contributor

Choose a reason for hiding this comment

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

could be made into a trivial follow up


// ErrOutdated is returned when the routing update already have
// been applied, or a newer update is already known.
Expand Down
24 changes: 17 additions & 7 deletions routing/pathfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package routing

import (
"container/heap"
"errors"
"fmt"
"math"
"time"

"github.com/btcsuite/btcd/btcec"
"github.com/coreos/bbolt"

"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
Expand Down Expand Up @@ -63,6 +63,19 @@ var (
// DefaultAprioriHopProbability is the default a priori probability for
// a hop.
DefaultAprioriHopProbability = float64(0.6)

// errNoTlvPayload is returned when the destination hop does not support
// a tlv payload.
errNoTlvPayload = errors.New("destination hop doesn't " +
"understand new TLV payloads")

// errNoPathFound is returned when a path to the target destination does
// not exist in the graph.
errNoPathFound = errors.New("unable to find a path to destination")

// errMaxHopsExceeded is returned when a candidate path is found, but
// the length of that path exceeds HopLimit.
errMaxHopsExceeded = errors.New("potential path has too many hops")
)

// edgePolicyWithSource is a helper struct to keep track of the source node
Expand Down Expand Up @@ -347,8 +360,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
lnwire.TLVOnionPayloadOptional,
)
if !supportsTLV {
return nil, fmt.Errorf("destination hop doesn't " +
"understand new TLV paylods")
return nil, errNoTlvPayload
}
}
}
Expand Down Expand Up @@ -610,8 +622,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
currentNodeWithDist, ok := distance[currentNode]
if !ok {
// If the node doesnt have a next hop it means we didn't find a path.
return nil, newErrf(ErrNoPathFound, "unable to find a "+
"path to destination")
return nil, errNoPathFound
}

// Add the next hop to the list of path edges.
Expand All @@ -634,8 +645,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// hops, then it's invalid.
numEdges := len(pathEdges)
if numEdges > HopLimit {
return nil, newErr(ErrMaxHopsExceeded, "potential path has "+
"too many hops")
return nil, errMaxHopsExceeded
}

log.Debugf("Found route: probability=%v, hops=%v, fee=%v\n",
Expand Down
16 changes: 8 additions & 8 deletions routing/pathfind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ func TestPathNotAvailable(t *testing.T) {
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, unknownNode, 100,
)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("path shouldn't have been found: %v", err)
}
}
Expand Down Expand Up @@ -1306,7 +1306,7 @@ func TestPathInsufficientCapacity(t *testing.T) {
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("graph shouldn't be able to support payment: %v", err)
}
}
Expand Down Expand Up @@ -1339,7 +1339,7 @@ func TestRouteFailMinHTLC(t *testing.T) {
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("graph shouldn't be able to support payment: %v", err)
}
}
Expand Down Expand Up @@ -1403,7 +1403,7 @@ func TestRouteFailMaxHTLC(t *testing.T) {
// We'll now attempt to route through that edge with a payment above
// 100k msat, which should fail.
_, err = ctx.findPath(target, payAmt)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("graph shouldn't be able to support payment: %v", err)
}
}
Expand Down Expand Up @@ -1491,7 +1491,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("graph shouldn't be able to support payment: %v", err)
}
}
Expand Down Expand Up @@ -1549,7 +1549,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("graph shouldn't be able to support payment: %v", err)
}

Expand Down Expand Up @@ -1971,7 +1971,7 @@ func testCltvLimit(t *testing.T, limit uint32, expectedChannel uint64) {
path, err := ctx.findPath(target, paymentAmt)
if expectedChannel == 0 {
// Finish test if we expect no route.
if IsError(err, ErrNoPathFound) {
if err == errNoPathFound {
return
}
t.Fatal("expected no path to be found")
Expand Down Expand Up @@ -2137,7 +2137,7 @@ func testProbabilityRouting(t *testing.T, p10, p11, p20, minProbability float64,

path, err := ctx.findPath(target, paymentAmt)
if expectedChan == 0 {
if err == nil || !IsError(err, ErrNoPathFound) {
if err != errNoPathFound {
t.Fatalf("expected no path found, but got %v", err)
}
return
Expand Down