channeldb+routing: expose HTLCs in payment subscriptions

This commit modifies the FetchPayment method to return MPPayment structs
converted from the legacy on-disk format. This allows us to attach the
HTLCs to the events given to clients subscribing to the outcome of an
HTLC.

This commit also bubbles up to the routerrpc/router_server, by
populating HTLCAttempts in the response and extracting the legacy route
field from the HTLCAttempts.
This commit is contained in:
Conner Fromknecht
2019-11-08 03:39:51 -08:00
parent 68916eb4b7
commit 063f24f2ed
6 changed files with 143 additions and 86 deletions

View File

@@ -584,19 +584,12 @@ func (s *Server) trackPayment(paymentHash lntypes.Hash,
case result := <-resultChan:
// Marshall result to rpc type.
var status PaymentStatus
if result.Success {
log.Debugf("Payment %v successfully completed",
paymentHash)
status.State = PaymentState_SUCCEEDED
status.Preimage = result.Preimage[:]
status.Route, err = router.MarshallRoute(
result.Route,
)
if err != nil {
return err
}
} else {
state, err := marshallFailureReason(
result.FailureReason,
@@ -605,15 +598,49 @@ func (s *Server) trackPayment(paymentHash lntypes.Hash,
return err
}
status.State = state
if result.Route != nil {
status.Route, err = router.MarshallRoute(
result.Route,
)
if err != nil {
return err
}
}
// Extract the last route from the given list of HTLCs. This
// will populate the legacy route field for backwards
// compatibility.
//
// NOTE: For now there will be at most one HTLC, this code
// should be revisted or the field removed when multiple HTLCs
// are permitted.
var legacyRoute *route.Route
for _, htlc := range result.HTLCs {
switch {
case htlc.Settle != nil:
legacyRoute = &htlc.Route
// Only display the route for failed payments if we got
// an incorrect payment details error, so that it can be
// used for probing or fee estimation.
case htlc.Failure != nil && result.FailureReason ==
channeldb.FailureReasonPaymentDetails:
legacyRoute = &htlc.Route
}
}
if legacyRoute != nil {
status.Route, err = router.MarshallRoute(legacyRoute)
if err != nil {
return err
}
}
// Marshal our list of HTLCs that have been tried for this
// payment.
htlcs := make([]*lnrpc.HTLCAttempt, 0, len(result.HTLCs))
for _, dbHtlc := range result.HTLCs {
htlc, err := router.MarshalHTLCAttempt(dbHtlc)
if err != nil {
return err
}
htlcs = append(htlcs, htlc)
}
status.Htlcs = htlcs
// Send event to the client.
err = stream.Send(&status)