htlcswitch: add ForwardingError constructor

Add a constructor for the creation of forwarding errors.
A special constructor is added for the case where we have
an unknown wire failure, and must set a nil failure message.
This commit is contained in:
carla
2020-01-14 15:07:29 +02:00
parent daa08be62a
commit 6f0a342f92
4 changed files with 89 additions and 94 deletions

View File

@@ -40,6 +40,27 @@ func (f *ForwardingError) Error() string {
)
}
// NewForwardingError creates a new payment error which wraps a wire error
// with additional metadata.
func NewForwardingError(failure lnwire.FailureMessage, index int,
extraMsg string) *ForwardingError {
return &ForwardingError{
FailureSourceIdx: index,
FailureMessage: failure,
ExtraMsg: extraMsg,
}
}
// NewUnknownForwardingError returns a forwarding error which has a nil failure
// message. This constructor should only be used in the case where we cannot
// decode the failure we have received from a peer.
func NewUnknownForwardingError(index int) *ForwardingError {
return &ForwardingError{
FailureSourceIdx: index,
}
}
// ErrorDecrypter is an interface that is used to decrypt the onion encrypted
// failure reason an extra out a well formed error.
type ErrorDecrypter interface {
@@ -94,15 +115,10 @@ func (s *SphinxErrorDecrypter) DecryptError(reason lnwire.OpaqueReason) (
r := bytes.NewReader(failure.Message)
failureMsg, err := lnwire.DecodeFailure(r, 0)
if err != nil {
return &ForwardingError{
FailureSourceIdx: failure.SenderIdx,
}, nil
return NewUnknownForwardingError(failure.SenderIdx), nil
}
return &ForwardingError{
FailureSourceIdx: failure.SenderIdx,
FailureMessage: failureMsg,
}, nil
return NewForwardingError(failureMsg, failure.SenderIdx, ""), nil
}
// A compile time check to ensure ErrorDecrypter implements the Deobfuscator

View File

@@ -400,10 +400,7 @@ func (o *mockDeobfuscator) DecryptError(reason lnwire.OpaqueReason) (*Forwarding
return nil, err
}
return &ForwardingError{
FailureSourceIdx: 1,
FailureMessage: failure,
}, nil
return NewForwardingError(failure, 1, ""), nil
}
var _ ErrorDecrypter = (*mockDeobfuscator)(nil)

View File

@@ -756,10 +756,9 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
s.indexMtx.RUnlock()
if err != nil {
log.Errorf("Link %v not found", pkt.outgoingChanID)
return &ForwardingError{
FailureSourceIdx: 0,
FailureMessage: &lnwire.FailUnknownNextPeer{},
}
return NewForwardingError(
&lnwire.FailUnknownNextPeer{}, 0, "",
)
}
if !link.EligibleToForward() {
@@ -770,11 +769,7 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
// The update does not need to be populated as the error
// will be returned back to the router.
htlcErr := lnwire.NewTemporaryChannelFailure(nil)
return &ForwardingError{
FailureSourceIdx: 0,
ExtraMsg: err.Error(),
FailureMessage: htlcErr,
}
return NewForwardingError(htlcErr, 0, err.Error())
}
// Ensure that the htlc satisfies the outgoing channel policy.
@@ -788,10 +783,7 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
log.Errorf("Link %v policy for local forward not "+
"satisfied", pkt.outgoingChanID)
return &ForwardingError{
FailureSourceIdx: 0,
FailureMessage: htlcErr,
}
return NewForwardingError(htlcErr, 0, "")
}
return link.HandleSwitchPacket(pkt)
@@ -930,11 +922,7 @@ func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter,
failureMsg = lnwire.NewTemporaryChannelFailure(nil)
}
return &ForwardingError{
FailureSourceIdx: 0,
ExtraMsg: userErr,
FailureMessage: failureMsg,
}
return NewForwardingError(failureMsg, 0, userErr)
// A payment had to be timed out on chain before it got past
// the first hop. In this case, we'll report a permanent
@@ -945,11 +933,9 @@ func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter,
"on-chain, then canceled back (hash=%v, pid=%d)",
paymentHash, paymentID)
return &ForwardingError{
FailureSourceIdx: 0,
ExtraMsg: userErr,
FailureMessage: &lnwire.FailPermanentChannelFailure{},
}
return NewForwardingError(
&lnwire.FailPermanentChannelFailure{}, 0, userErr,
)
// A regular multi-hop payment error that we'll need to
// decrypt.