routing/route: define route.ReceiverAmt() method

This commit is contained in:
Johan T. Halseth 2020-04-01 00:13:23 +02:00
parent 4485e8261f
commit 79227bab3a
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 31 additions and 5 deletions

@ -308,7 +308,16 @@ func (r *Route) TotalFees() lnwire.MilliSatoshi {
return 0
}
return r.TotalAmount - r.Hops[len(r.Hops)-1].AmtToForward
return r.TotalAmount - r.ReceiverAmt()
}
// ReceiverAmt is the amount received by the final hop of this route.
func (r *Route) ReceiverAmt() lnwire.MilliSatoshi {
if len(r.Hops) == 0 {
return 0
}
return r.Hops[len(r.Hops)-1].AmtToForward
}
// NewRouteFromHops creates a new Route structure from the minimally required

@ -20,15 +20,24 @@ var (
func TestRouteTotalFees(t *testing.T) {
t.Parallel()
// Make sure empty route returns a 0 fee.
// Make sure empty route returns a 0 fee, and zero amount.
r := &Route{}
if r.TotalFees() != 0 {
t.Fatalf("expected 0 fees, got %v", r.TotalFees())
}
if r.ReceiverAmt() != 0 {
t.Fatalf("expected 0 amt, got %v", r.ReceiverAmt())
}
// Make sure empty route won't be allowed in the constructor.
amt := lnwire.MilliSatoshi(1000)
_, err := NewRouteFromHops(amt, 100, Vertex{}, []*Hop{})
if err != ErrNoRouteHopsProvided {
t.Fatalf("expected ErrNoRouteHopsProvided, got %v", err)
}
// For one-hop routes the fee should be 0, since the last node will
// receive the full amount.
amt := lnwire.MilliSatoshi(1000)
hops := []*Hop{
{
PubKeyBytes: Vertex{},
@ -37,7 +46,7 @@ func TestRouteTotalFees(t *testing.T) {
AmtToForward: amt,
},
}
r, err := NewRouteFromHops(amt, 100, Vertex{}, hops)
r, err = NewRouteFromHops(amt, 100, Vertex{}, hops)
if err != nil {
t.Fatal(err)
}
@ -46,6 +55,10 @@ func TestRouteTotalFees(t *testing.T) {
t.Fatalf("expected 0 fees, got %v", r.TotalFees())
}
if r.ReceiverAmt() != amt {
t.Fatalf("expected %v amt, got %v", amt, r.ReceiverAmt())
}
// Append the route with a node, making the first one take a fee.
fee := lnwire.MilliSatoshi(100)
hops = append(hops, &Hop{
@ -64,6 +77,10 @@ func TestRouteTotalFees(t *testing.T) {
if r.TotalFees() != fee {
t.Fatalf("expected %v fees, got %v", fee, r.TotalFees())
}
if r.ReceiverAmt() != amt-fee {
t.Fatalf("expected %v amt, got %v", amt-fee, r.ReceiverAmt())
}
}
var (

@ -1746,7 +1746,7 @@ func (r *ChannelRouter) SendToRoute(hash lntypes.Hash, route *route.Route) (
paySession := r.cfg.SessionSource.NewPaymentSessionForRoute(route)
// Calculate amount paid to receiver.
amt := route.TotalAmount - route.TotalFees()
amt := route.ReceiverAmt()
// Record this payment hash with the ControlTower, ensuring it is not
// already in-flight.