From 327634e9f7992002cf4d584717671bebf4de9c36 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 3 Apr 2020 17:01:47 +0200 Subject: [PATCH] routerrpc: move payment marshalling --- lnrpc/routerrpc/router_backend.go | 90 +++++++++++++++++++++++++++++++ rpcserver.go | 83 ++-------------------------- 2 files changed, 95 insertions(+), 78 deletions(-) diff --git a/lnrpc/routerrpc/router_backend.go b/lnrpc/routerrpc/router_backend.go index 6465c268..f4aac8a4 100644 --- a/lnrpc/routerrpc/router_backend.go +++ b/lnrpc/routerrpc/router_backend.go @@ -1094,3 +1094,93 @@ func marshallChannelUpdate(update *lnwire.ChannelUpdate) *lnrpc.ChannelUpdate { ExtraOpaqueData: update.ExtraOpaqueData, } } + +// MarshallPayment marshall a payment to its rpc representation. +func (r *RouterBackend) MarshallPayment(payment *channeldb.MPPayment) ( + *lnrpc.Payment, error) { + + // Fetch the payment's route and preimage. If no HTLC was + // successful, an empty route and preimage will be used. + var ( + route route.Route + preimage lntypes.Preimage + ) + for _, htlc := range payment.HTLCs { + // Display the last route attempted. + route = htlc.Route + + // If any of the htlcs have settled, extract a valid + // preimage. + if htlc.Settle != nil { + preimage = htlc.Settle.Preimage + } + } + + // Encode the hops from the successful route, if any. + path := make([]string, len(route.Hops)) + for i, hop := range route.Hops { + path[i] = hex.EncodeToString(hop.PubKeyBytes[:]) + } + + msatValue := int64(payment.Info.Value) + satValue := int64(payment.Info.Value.ToSatoshis()) + + status, err := convertPaymentStatus(payment.Status) + if err != nil { + return nil, err + } + + htlcs := make([]*lnrpc.HTLCAttempt, 0, len(payment.HTLCs)) + for _, dbHTLC := range payment.HTLCs { + htlc, err := r.MarshalHTLCAttempt(dbHTLC) + if err != nil { + return nil, err + } + + htlcs = append(htlcs, htlc) + } + + paymentHash := payment.Info.PaymentHash + creationTimeNS := MarshalTimeNano(payment.Info.CreationTime) + + return &lnrpc.Payment{ + PaymentHash: hex.EncodeToString(paymentHash[:]), + Value: satValue, + ValueMsat: msatValue, + ValueSat: satValue, + CreationDate: payment.Info.CreationTime.Unix(), + CreationTimeNs: creationTimeNS, + Path: path, + Fee: int64(route.TotalFees().ToSatoshis()), + FeeSat: int64(route.TotalFees().ToSatoshis()), + FeeMsat: int64(route.TotalFees()), + PaymentPreimage: hex.EncodeToString(preimage[:]), + PaymentRequest: string(payment.Info.PaymentRequest), + Status: status, + Htlcs: htlcs, + PaymentIndex: payment.SequenceNum, + }, nil +} + +// convertPaymentStatus converts a channeldb.PaymentStatus to the type expected +// by the RPC. +func convertPaymentStatus(dbStatus channeldb.PaymentStatus) ( + lnrpc.Payment_PaymentStatus, error) { + + switch dbStatus { + case channeldb.StatusUnknown: + return lnrpc.Payment_UNKNOWN, nil + + case channeldb.StatusInFlight: + return lnrpc.Payment_IN_FLIGHT, nil + + case channeldb.StatusSucceeded: + return lnrpc.Payment_SUCCEEDED, nil + + case channeldb.StatusFailed: + return lnrpc.Payment_FAILED, nil + + default: + return 0, fmt.Errorf("unhandled payment status %v", dbStatus) + } +} diff --git a/rpcserver.go b/rpcserver.go index f13cce5d..4d7cc1d2 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -5184,94 +5184,21 @@ func (r *rpcServer) ListPayments(ctx context.Context, } for _, payment := range paymentsQuerySlice.Payments { - // Fetch the payment's route and preimage. If no HTLC was - // successful, an empty route and preimage will be used. - var ( - route route.Route - preimage lntypes.Preimage - ) - for _, htlc := range payment.HTLCs { - // Display the last route attempted. - route = htlc.Route + payment := payment - // If any of the htlcs have settled, extract a valid - // preimage. - if htlc.Settle != nil { - preimage = htlc.Settle.Preimage - } - } - - // Encode the hops from the successful route, if any. - path := make([]string, len(route.Hops)) - for i, hop := range route.Hops { - path[i] = hex.EncodeToString(hop.PubKeyBytes[:]) - } - - msatValue := int64(payment.Info.Value) - satValue := int64(payment.Info.Value.ToSatoshis()) - - status, err := convertPaymentStatus(payment.Status) + rpcPayment, err := r.routerBackend.MarshallPayment(&payment) if err != nil { return nil, err } - htlcs := make([]*lnrpc.HTLCAttempt, 0, len(payment.HTLCs)) - for _, dbHTLC := range payment.HTLCs { - htlc, err := r.routerBackend.MarshalHTLCAttempt(dbHTLC) - if err != nil { - return nil, err - } - - htlcs = append(htlcs, htlc) - } - - paymentHash := payment.Info.PaymentHash - creationTimeNS := routerrpc.MarshalTimeNano(payment.Info.CreationTime) - paymentsResp.Payments = append(paymentsResp.Payments, &lnrpc.Payment{ - PaymentHash: hex.EncodeToString(paymentHash[:]), - Value: satValue, - ValueMsat: msatValue, - ValueSat: satValue, - CreationDate: payment.Info.CreationTime.Unix(), - CreationTimeNs: creationTimeNS, - Path: path, - Fee: int64(route.TotalFees().ToSatoshis()), - FeeSat: int64(route.TotalFees().ToSatoshis()), - FeeMsat: int64(route.TotalFees()), - PaymentPreimage: hex.EncodeToString(preimage[:]), - PaymentRequest: string(payment.Info.PaymentRequest), - Status: status, - Htlcs: htlcs, - PaymentIndex: payment.SequenceNum, - }) + paymentsResp.Payments = append( + paymentsResp.Payments, rpcPayment, + ) } return paymentsResp, nil } -// convertPaymentStatus converts a channeldb.PaymentStatus to the type expected -// by the RPC. -func convertPaymentStatus(dbStatus channeldb.PaymentStatus) ( - lnrpc.Payment_PaymentStatus, error) { - - switch dbStatus { - case channeldb.StatusUnknown: - return lnrpc.Payment_UNKNOWN, nil - - case channeldb.StatusInFlight: - return lnrpc.Payment_IN_FLIGHT, nil - - case channeldb.StatusSucceeded: - return lnrpc.Payment_SUCCEEDED, nil - - case channeldb.StatusFailed: - return lnrpc.Payment_FAILED, nil - - default: - return 0, fmt.Errorf("unhandled payment status %v", dbStatus) - } -} - // DeleteAllPayments deletes all outgoing payments from DB. func (r *rpcServer) DeleteAllPayments(ctx context.Context, _ *lnrpc.DeleteAllPaymentsRequest) (*lnrpc.DeleteAllPaymentsResponse, error) {