routing: convert to nillable failure reason

This commit converts several functions from returning a bool and a
failure reason to a nillable failure reason as return parameter. This
will take away confusion about the interpretation of the two separate
values.
This commit is contained in:
Joost Jager 2019-08-05 12:13:58 +02:00
parent d134e0362e
commit e7af6a077a
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
4 changed files with 31 additions and 25 deletions

@ -420,12 +420,11 @@ func (m *MissionControl) GetHistorySnapshot() *MissionControlSnapshot {
// ReportPaymentFail reports a failed payment to mission control as input for
// future probability estimates. The failureSourceIdx argument indicates the
// failure source. If it is nil, the failure source is unknown. This function
// returns a bool indicating whether this error is a final error. If it is
// final, a failure reason is returned and no further payment attempts need to
// be made.
// returns a reason if this failure is a final failure. In that case no further
// payment attempts need to be made.
func (m *MissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
failureSourceIdx *int, failure lnwire.FailureMessage) (bool,
channeldb.FailureReason, error) {
failureSourceIdx *int, failure lnwire.FailureMessage) (
*channeldb.FailureReason, error) {
timestamp := m.now()
@ -442,13 +441,18 @@ func (m *MissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
// Store complete result in database.
if err := m.store.AddResult(result); err != nil {
return false, 0, err
return nil, err
}
// Apply result to update mission control state.
final, reason := m.applyPaymentResult(result)
return final, reason, nil
// Convert final bool and reason to nillable reason.
if final {
return &reason, nil
}
return nil, nil
}
// applyPaymentResult applies a payment result as input for future probability

@ -98,11 +98,11 @@ type mockMissionControl struct {
var _ MissionController = (*mockMissionControl)(nil)
func (m *mockMissionControl) ReportPaymentFail(paymentID uint64,
rt *route.Route, failureSourceIdx *int, failure lnwire.FailureMessage) (
bool, channeldb.FailureReason, error) {
func (m *mockMissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
failureSourceIdx *int, failure lnwire.FailureMessage) (
*channeldb.FailureReason, error) {
return false, 0, nil
return nil, nil
}
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,

@ -342,10 +342,10 @@ func (p *paymentLifecycle) sendPaymentAttempt(firstHop lnwire.ShortChannelID,
// whether we should make another payment attempt.
func (p *paymentLifecycle) handleSendError(sendErr error) error {
final, reason := p.router.processSendError(
reason := p.router.processSendError(
p.attempt.PaymentID, &p.attempt.Route, sendErr,
)
if !final {
if reason == nil {
// Save the forwarding error so it can be returned if
// this turns out to be the last attempt.
p.lastError = sendErr
@ -354,14 +354,14 @@ func (p *paymentLifecycle) handleSendError(sendErr error) error {
}
log.Debugf("Payment %x failed: final_outcome=%v, raw_err=%v",
p.payment.PaymentHash, reason, sendErr)
p.payment.PaymentHash, *reason, sendErr)
// Mark the payment failed with no route.
//
// TODO(halseth): make payment codes for the actual reason we don't
// continue path finding.
err := p.router.cfg.Control.Fail(
p.payment.PaymentHash, reason,
p.payment.PaymentHash, *reason,
)
if err != nil {
return err

@ -179,8 +179,8 @@ type MissionController interface {
// whether this error is a final error and no further payment attempts
// need to be made.
ReportPaymentFail(paymentID uint64, rt *route.Route,
failureSourceIdx *int, failure lnwire.FailureMessage) (bool,
channeldb.FailureReason, error)
failureSourceIdx *int, failure lnwire.FailureMessage) (
*channeldb.FailureReason, error)
// GetProbability is expected to return the success probability of a
// payment from fromNode along edge.
@ -1887,23 +1887,25 @@ func (r *ChannelRouter) tryApplyChannelUpdate(rt *route.Route,
// to continue with an alternative route. This is indicated by the boolean
// return value.
func (r *ChannelRouter) processSendError(paymentID uint64, rt *route.Route,
sendErr error) (bool, channeldb.FailureReason) {
sendErr error) *channeldb.FailureReason {
reportFail := func(srcIdx *int, msg lnwire.FailureMessage) (bool,
channeldb.FailureReason) {
internalErrorReason := channeldb.FailureReasonError
reportFail := func(srcIdx *int,
msg lnwire.FailureMessage) *channeldb.FailureReason {
// Report outcome to mission control.
final, reason, err := r.cfg.MissionControl.ReportPaymentFail(
reason, err := r.cfg.MissionControl.ReportPaymentFail(
paymentID, rt, srcIdx, msg,
)
if err != nil {
log.Errorf("Error reporting payment result to mc: %v",
err)
return true, channeldb.FailureReasonError
return &internalErrorReason
}
return final, reason
return reason
}
if sendErr == htlcswitch.ErrUnreadableFailureMessage {
@ -1915,7 +1917,7 @@ func (r *ChannelRouter) processSendError(paymentID uint64, rt *route.Route,
// trying.
fErr, ok := sendErr.(*htlcswitch.ForwardingError)
if !ok {
return true, channeldb.FailureReasonError
return &internalErrorReason
}
failureMessage := fErr.FailureMessage
@ -1928,7 +1930,7 @@ func (r *ChannelRouter) processSendError(paymentID uint64, rt *route.Route,
rt, failureSourceIdx, failureMessage,
)
if err != nil {
return true, channeldb.FailureReasonError
return &internalErrorReason
}
}