htlcswitch+router: move deobfuscator creation to GetPaymentResult call
In this commit we move handing the deobfuscator from the router to the switch from when the payment is initiated, to when the result is queried. We do this because only the router can recreate the deobfuscator after a restart, and we are preparing for being able to handle results across restarts. Since the deobfuscator cannot be nil anymore, we can also get rid of that special case.
This commit is contained in:
6 files changed
+132
-82
No files matched your search
+55
-43
@@ -71,12 +71,7 @@ type pendingPayment struct {
|
||||
paymentHash lntypes.Hash
|
||||
amount lnwire.MilliSatoshi
|
||||
|
||||
resultChan chan *PaymentResult
|
||||
|
||||
// deobfuscator is a serializable entity which is used if we received
|
||||
// an error, it deobfuscates the onion failure blob, and extracts the
|
||||
// exact error from it.
|
||||
deobfuscator ErrorDecrypter
|
||||
resultChan chan *networkResult
|
||||
}
|
||||
|
||||
// plexPacket encapsulates switch packet and adds error channel to receive
|
||||
@@ -347,9 +342,13 @@ func (s *Switch) ProcessContractResolution(msg contractcourt.ResolutionMsg) erro
|
||||
|
||||
// GetPaymentResult returns the the result of the payment attempt with the
|
||||
// given paymentID. The method returns a channel where the payment result will
|
||||
// be sent when available, or an error is encountered. If the paymentID is
|
||||
// unknown, ErrPaymentIDNotFound will be returned.
|
||||
func (s *Switch) GetPaymentResult(paymentID uint64) (<-chan *PaymentResult, error) {
|
||||
// be sent when available, or an error is encountered during forwarding. When a
|
||||
// result is received on the channel, the HTLC is guaranteed to no longer be in
|
||||
// flight. The switch shutting down is signaled by closing the channel. If the
|
||||
// paymentID is unknown, ErrPaymentIDNotFound will be returned.
|
||||
func (s *Switch) GetPaymentResult(paymentID uint64,
|
||||
deobfuscator ErrorDecrypter) (<-chan *PaymentResult, error) {
|
||||
|
||||
s.pendingMutex.Lock()
|
||||
payment, ok := s.pendingPayments[paymentID]
|
||||
s.pendingMutex.Unlock()
|
||||
@@ -358,7 +357,42 @@ func (s *Switch) GetPaymentResult(paymentID uint64) (<-chan *PaymentResult, erro
|
||||
return nil, ErrPaymentIDNotFound
|
||||
}
|
||||
|
||||
return payment.resultChan, nil
|
||||
resultChan := make(chan *PaymentResult, 1)
|
||||
|
||||
// Since the payment was known, we can start a goroutine that can
|
||||
// extract the result when it is available, and pass it on to the
|
||||
// caller.
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
|
||||
var n *networkResult
|
||||
select {
|
||||
case n = <-payment.resultChan:
|
||||
case <-s.quit:
|
||||
// We close the result channel to signal a shutdown. We
|
||||
// don't send any result in this case since the HTLC is
|
||||
// still in flight.
|
||||
close(resultChan)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract the result and pass it to the result channel.
|
||||
result, err := s.extractResult(
|
||||
deobfuscator, n, paymentID, payment.paymentHash,
|
||||
)
|
||||
if err != nil {
|
||||
e := fmt.Errorf("Unable to extract result: %v", err)
|
||||
log.Error(e)
|
||||
resultChan <- &PaymentResult{
|
||||
Error: e,
|
||||
}
|
||||
return
|
||||
}
|
||||
resultChan <- result
|
||||
}()
|
||||
|
||||
return resultChan, nil
|
||||
}
|
||||
|
||||
// SendHTLC is used by other subsystems which aren't belong to htlc switch
|
||||
@@ -366,7 +400,7 @@ func (s *Switch) GetPaymentResult(paymentID uint64) (<-chan *PaymentResult, erro
|
||||
// for this HTLC, and MUST be used only once, otherwise the switch might reject
|
||||
// it.
|
||||
func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, paymentID uint64,
|
||||
htlc *lnwire.UpdateAddHTLC, deobfuscator ErrorDecrypter) error {
|
||||
htlc *lnwire.UpdateAddHTLC) error {
|
||||
|
||||
// Before sending, double check that we don't already have 1) an
|
||||
// in-flight payment to this payment hash, or 2) a complete payment for
|
||||
@@ -378,10 +412,9 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, paymentID uint64,
|
||||
// Create payment and add to the map of payment in order later to be
|
||||
// able to retrieve it and return response to the user.
|
||||
payment := &pendingPayment{
|
||||
resultChan: make(chan *PaymentResult, 1),
|
||||
paymentHash: htlc.PaymentHash,
|
||||
amount: htlc.Amount,
|
||||
deobfuscator: deobfuscator,
|
||||
resultChan: make(chan *networkResult, 1),
|
||||
paymentHash: htlc.PaymentHash,
|
||||
amount: htlc.Amount,
|
||||
}
|
||||
|
||||
s.pendingMutex.Lock()
|
||||
@@ -889,25 +922,16 @@ func (s *Switch) handleLocalResponse(pkt *htlcPacket) {
|
||||
isResolution: pkt.isResolution,
|
||||
}
|
||||
|
||||
result, err := s.extractResult(
|
||||
payment, n, pkt.incomingHTLCID,
|
||||
pkt.circuit.PaymentHash,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to extract result: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Deliver the payment error and preimage to the application, if it is
|
||||
// waiting for a response.
|
||||
if payment != nil {
|
||||
payment.resultChan <- result
|
||||
payment.resultChan <- n
|
||||
}
|
||||
}
|
||||
|
||||
// extractResult uses the given deobfuscator to extract the payment result from
|
||||
// the given network message.
|
||||
func (s *Switch) extractResult(payment *pendingPayment, n *networkResult,
|
||||
func (s *Switch) extractResult(deobfuscator ErrorDecrypter, n *networkResult,
|
||||
paymentID uint64, paymentHash lntypes.Hash) (*PaymentResult, error) {
|
||||
|
||||
switch htlc := n.msg.(type) {
|
||||
@@ -940,7 +964,7 @@ func (s *Switch) extractResult(payment *pendingPayment, n *networkResult,
|
||||
"%x: %v", paymentHash, err)
|
||||
}
|
||||
paymentErr := s.parseFailedPayment(
|
||||
payment, paymentID, payment.paymentHash, n.unencrypted,
|
||||
deobfuscator, paymentID, paymentHash, n.unencrypted,
|
||||
n.isResolution, htlc,
|
||||
)
|
||||
|
||||
@@ -961,9 +985,9 @@ func (s *Switch) extractResult(payment *pendingPayment, n *networkResult,
|
||||
// reason attached.
|
||||
// 3) A failure from the remote party, which will need to be decrypted using
|
||||
// the payment deobfuscator.
|
||||
func (s *Switch) parseFailedPayment(payment *pendingPayment, paymentID uint64,
|
||||
paymentHash lntypes.Hash, unencrypted, isResolution bool,
|
||||
htlc *lnwire.UpdateFailHTLC) *ForwardingError {
|
||||
func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter,
|
||||
paymentID uint64, paymentHash lntypes.Hash, unencrypted,
|
||||
isResolution bool, htlc *lnwire.UpdateFailHTLC) *ForwardingError {
|
||||
|
||||
var failure *ForwardingError
|
||||
|
||||
@@ -1007,25 +1031,13 @@ func (s *Switch) parseFailedPayment(payment *pendingPayment, paymentID uint64,
|
||||
FailureMessage: lnwire.FailPermanentChannelFailure{},
|
||||
}
|
||||
|
||||
// If the provided payment is nil, we have discarded the error decryptor
|
||||
// due to a restart. We'll return a fixed error and signal a temporary
|
||||
// channel failure to the router.
|
||||
case payment == nil:
|
||||
userErr := fmt.Sprintf("error decryptor for payment " +
|
||||
"could not be located, likely due to restart")
|
||||
failure = &ForwardingError{
|
||||
ErrorSource: s.cfg.SelfKey,
|
||||
ExtraMsg: userErr,
|
||||
FailureMessage: lnwire.NewTemporaryChannelFailure(nil),
|
||||
}
|
||||
|
||||
// A regular multi-hop payment error that we'll need to
|
||||
// decrypt.
|
||||
default:
|
||||
var err error
|
||||
// We'll attempt to fully decrypt the onion encrypted
|
||||
// error. If we're unable to then we'll bail early.
|
||||
failure, err = payment.deobfuscator.DecryptError(htlc.Reason)
|
||||
failure, err = deobfuscator.DecryptError(htlc.Reason)
|
||||
if err != nil {
|
||||
userErr := fmt.Sprintf("unable to de-obfuscate "+
|
||||
"onion failure (hash=%v, pid=%d): %v",
|
||||
|
||||
Reference in new issue
Block a user