Merge pull request #3130 from halseth/route-totalfees-hop-check
route: return 0 fee from TotalFees for empty route.
This commit is contained in:
commit
58ed49677f
@ -103,6 +103,10 @@ func (r *Route) HopFee(hopIndex int) lnwire.MilliSatoshi {
|
|||||||
// the case of a one-hop payment, this value will be zero as we don't need to
|
// the case of a one-hop payment, this value will be zero as we don't need to
|
||||||
// pay a fee to ourself.
|
// pay a fee to ourself.
|
||||||
func (r *Route) TotalFees() lnwire.MilliSatoshi {
|
func (r *Route) TotalFees() lnwire.MilliSatoshi {
|
||||||
|
if len(r.Hops) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
return r.TotalAmount - r.Hops[len(r.Hops)-1].AmtToForward
|
return r.TotalAmount - r.Hops[len(r.Hops)-1].AmtToForward
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
routing/route/route_test.go
Normal file
58
routing/route/route_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package route
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestRouteTotalFees checks that a route reports the expected total fee.
|
||||||
|
func TestRouteTotalFees(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Make sure empty route returns a 0 fee.
|
||||||
|
r := &Route{}
|
||||||
|
if r.TotalFees() != 0 {
|
||||||
|
t.Fatalf("expected 0 fees, got %v", r.TotalFees())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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{},
|
||||||
|
ChannelID: 1,
|
||||||
|
OutgoingTimeLock: 44,
|
||||||
|
AmtToForward: amt,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
r, err := NewRouteFromHops(amt, 100, Vertex{}, hops)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.TotalFees() != 0 {
|
||||||
|
t.Fatalf("expected 0 fees, got %v", r.TotalFees())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the route with a node, making the first one take a fee.
|
||||||
|
fee := lnwire.MilliSatoshi(100)
|
||||||
|
hops = append(hops, &Hop{
|
||||||
|
PubKeyBytes: Vertex{},
|
||||||
|
ChannelID: 2,
|
||||||
|
OutgoingTimeLock: 33,
|
||||||
|
AmtToForward: amt - fee,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
r, err = NewRouteFromHops(amt, 100, Vertex{}, hops)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.TotalFees() != fee {
|
||||||
|
t.Fatalf("expected %v fees, got %v", fee, r.TotalFees())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user