From 15bed506b1443eb2d6a86b81ffd9d5ecd6aeca4c Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 15 May 2019 14:37:58 +0200 Subject: [PATCH] routing/route+multi: remove redundant TotalFees field Instead get it on demand using method TotalFees(). --- channeldb/payments.go | 4 ++-- channeldb/payments_test.go | 1 - lnrpc/routerrpc/router_backend.go | 4 ++-- lnrpc/routerrpc/router_backend_test.go | 5 ++--- lnrpc/routerrpc/router_server.go | 2 +- routing/route/route.go | 13 +++++++------ routing/router_test.go | 2 +- rpcserver.go | 4 ++-- 8 files changed, 17 insertions(+), 18 deletions(-) diff --git a/channeldb/payments.go b/channeldb/payments.go index 213bd824..08c9e02f 100644 --- a/channeldb/payments.go +++ b/channeldb/payments.go @@ -361,7 +361,7 @@ func deserializeHop(r io.Reader) (*route.Hop, error) { func serializeRoute(w io.Writer, r route.Route) error { if err := WriteElements(w, - r.TotalTimeLock, r.TotalFees, r.TotalAmount, r.SourcePubKey[:], + r.TotalTimeLock, r.TotalAmount, r.SourcePubKey[:], ); err != nil { return err } @@ -382,7 +382,7 @@ func serializeRoute(w io.Writer, r route.Route) error { func deserializeRoute(r io.Reader) (route.Route, error) { rt := route.Route{} if err := ReadElements(r, - &rt.TotalTimeLock, &rt.TotalFees, &rt.TotalAmount, + &rt.TotalTimeLock, &rt.TotalAmount, ); err != nil { return rt, err } diff --git a/channeldb/payments_test.go b/channeldb/payments_test.go index bea87d17..80a61be2 100644 --- a/channeldb/payments_test.go +++ b/channeldb/payments_test.go @@ -27,7 +27,6 @@ var ( testRoute = route.Route{ TotalTimeLock: 123, - TotalFees: 999, TotalAmount: 1234567, SourcePubKey: route.NewVertex(pub), Hops: []*route.Hop{ diff --git a/lnrpc/routerrpc/router_backend.go b/lnrpc/routerrpc/router_backend.go index 916d44f2..2b66f1f7 100644 --- a/lnrpc/routerrpc/router_backend.go +++ b/lnrpc/routerrpc/router_backend.go @@ -185,8 +185,8 @@ func calculateFeeLimit(feeLimit *lnrpc.FeeLimit, func (r *RouterBackend) MarshallRoute(route *route.Route) *lnrpc.Route { resp := &lnrpc.Route{ TotalTimeLock: route.TotalTimeLock, - TotalFees: int64(route.TotalFees.ToSatoshis()), - TotalFeesMsat: int64(route.TotalFees), + TotalFees: int64(route.TotalFees().ToSatoshis()), + TotalFeesMsat: int64(route.TotalFees()), TotalAmt: int64(route.TotalAmount.ToSatoshis()), TotalAmtMsat: int64(route.TotalAmount), Hops: make([]*lnrpc.Hop, len(route.Hops)), diff --git a/lnrpc/routerrpc/router_backend_test.go b/lnrpc/routerrpc/router_backend_test.go index b8f0f6c4..452ebf26 100644 --- a/lnrpc/routerrpc/router_backend_test.go +++ b/lnrpc/routerrpc/router_backend_test.go @@ -55,8 +55,6 @@ func TestQueryRoutes(t *testing.T) { }}, } - rt := &route.Route{} - findRoute := func(source, target route.Vertex, amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams, finalExpiry ...uint16) (*route.Route, error) { @@ -95,7 +93,8 @@ func TestQueryRoutes(t *testing.T) { t.Fatal("unexpected ignored node") } - return rt, nil + hops := []*route.Hop{&route.Hop{}} + return route.NewRouteFromHops(amt, 144, source, hops) } backend := &RouterBackend{ diff --git a/lnrpc/routerrpc/router_server.go b/lnrpc/routerrpc/router_server.go index 9ba35892..1f63cefd 100644 --- a/lnrpc/routerrpc/router_server.go +++ b/lnrpc/routerrpc/router_server.go @@ -269,7 +269,7 @@ func (s *Server) EstimateRouteFee(ctx context.Context, } return &RouteFeeResponse{ - RoutingFeeMsat: int64(route.TotalFees), + RoutingFeeMsat: int64(route.TotalFees()), TimeLockDelay: int64(route.TotalTimeLock), }, nil } diff --git a/routing/route/route.go b/routing/route/route.go index 6aaa4e49..9b010e77 100644 --- a/routing/route/route.go +++ b/routing/route/route.go @@ -69,11 +69,6 @@ type Route struct { // the payment preimage to complete the payment. TotalTimeLock uint32 - // TotalFees is the sum of the fees paid at each hop within the final - // route. In the case of a one-hop payment, this value will be zero as - // we don't need to pay a fee to ourself. - TotalFees lnwire.MilliSatoshi - // TotalAmount is the total amount of funds required to complete a // payment over this route. This value includes the cumulative fees at // each hop. As a result, the HTLC extended to the first-hop in the @@ -104,6 +99,13 @@ func (r *Route) HopFee(hopIndex int) lnwire.MilliSatoshi { return incomingAmt - r.Hops[hopIndex].AmtToForward } +// TotalFees is the sum of the fees paid at each hop within the final route. In +// the case of a one-hop payment, this value will be zero as we don't need to +// pay a fee to ourself. +func (r *Route) TotalFees() lnwire.MilliSatoshi { + return r.TotalAmount - r.Hops[len(r.Hops)-1].AmtToForward +} + // NewRouteFromHops creates a new Route structure from the minimally required // information to perform the payment. It infers fee amounts and populates the // node, chan and prev/next hop maps. @@ -124,7 +126,6 @@ func NewRouteFromHops(amtToSend lnwire.MilliSatoshi, timeLock uint32, Hops: hops, TotalTimeLock: timeLock, TotalAmount: amtToSend, - TotalFees: amtToSend - hops[len(hops)-1].AmtToForward, } return route, nil diff --git a/routing/router_test.go b/routing/router_test.go index f4367d29..4e366432 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -199,7 +199,7 @@ func TestFindRoutesWithFeeLimit(t *testing.T) { t.Fatalf("unable to find any routes: %v", err) } - if route.TotalFees > restrictions.FeeLimit { + if route.TotalFees() > restrictions.FeeLimit { t.Fatalf("route exceeded fee limit: %v", spew.Sdump(route)) } diff --git a/rpcserver.go b/rpcserver.go index 544393a2..6cb92f08 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2753,7 +2753,7 @@ func (r *rpcServer) savePayment(route *route.Route, CreationDate: time.Now(), }, Path: paymentPath, - Fee: route.TotalFees, + Fee: route.TotalFees(), TimeLockLength: route.TotalTimeLock, } copy(payment.PaymentPreimage[:], preImage) @@ -3123,7 +3123,7 @@ func (r *rpcServer) dispatchPaymentIntent( } // Calculate amount paid to receiver. - amt := route.TotalAmount - route.TotalFees + amt := route.TotalAmount - route.TotalFees() // Save the completed payment to the database for record keeping // purposes.