htlcswitch+router: define PaymentResult, GetPaymentResult
This lets us distinguish an critical error from a actual payment result (success or failure). This is important since we know that we can only attempt another payment when a final result from the previous payment attempt is received.
This commit is contained in:
7 files changed
+251
-68
No files matched your search
+40
-7
@@ -1,28 +1,61 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
type mockPaymentAttemptDispatcher struct {
|
||||
onPayment func(firstHop lnwire.ShortChannelID) ([32]byte, error)
|
||||
results map[uint64]*htlcswitch.PaymentResult
|
||||
}
|
||||
|
||||
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcher)(nil)
|
||||
|
||||
func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
|
||||
_ uint64,
|
||||
pid uint64,
|
||||
_ *lnwire.UpdateAddHTLC,
|
||||
_ htlcswitch.ErrorDecrypter) ([sha256.Size]byte, error) {
|
||||
_ htlcswitch.ErrorDecrypter) error {
|
||||
|
||||
if m.onPayment != nil {
|
||||
return m.onPayment(firstHop)
|
||||
if m.onPayment == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return [sha256.Size]byte{}, nil
|
||||
if m.results == nil {
|
||||
m.results = make(map[uint64]*htlcswitch.PaymentResult)
|
||||
}
|
||||
|
||||
var result *htlcswitch.PaymentResult
|
||||
preimage, err := m.onPayment(firstHop)
|
||||
if err != nil {
|
||||
fwdErr, ok := err.(*htlcswitch.ForwardingError)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
result = &htlcswitch.PaymentResult{
|
||||
Error: fwdErr,
|
||||
}
|
||||
} else {
|
||||
result = &htlcswitch.PaymentResult{Preimage: preimage}
|
||||
}
|
||||
|
||||
m.results[pid] = result
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockPaymentAttemptDispatcher) GetPaymentResult(paymentID uint64) (
|
||||
<-chan *htlcswitch.PaymentResult, error) {
|
||||
|
||||
c := make(chan *htlcswitch.PaymentResult, 1)
|
||||
res, ok := m.results[paymentID]
|
||||
if !ok {
|
||||
return nil, htlcswitch.ErrPaymentIDNotFound
|
||||
}
|
||||
c <- res
|
||||
|
||||
return c, nil
|
||||
|
||||
}
|
||||
|
||||
func (m *mockPaymentAttemptDispatcher) setPaymentResult(
|
||||
|
||||
Reference in new issue
Block a user