2019-05-16 16:27:28 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockPaymentAttemptDispatcher struct {
|
|
|
|
onPayment func(firstHop lnwire.ShortChannelID) ([32]byte, error)
|
2019-05-16 16:27:29 +03:00
|
|
|
results map[uint64]*htlcswitch.PaymentResult
|
2019-05-16 16:27:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcher)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
|
2019-05-16 16:27:29 +03:00
|
|
|
pid uint64,
|
2019-05-16 16:27:29 +03:00
|
|
|
_ *lnwire.UpdateAddHTLC) error {
|
2019-05-16 16:27:29 +03:00
|
|
|
|
|
|
|
if m.onPayment == nil {
|
|
|
|
return 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
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:27:29 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcher) GetPaymentResult(paymentID uint64,
|
|
|
|
_ htlcswitch.ErrorDecrypter) (<-chan *htlcswitch.PaymentResult, error) {
|
2019-05-16 16:27:28 +03:00
|
|
|
|
2019-05-16 16:27:29 +03:00
|
|
|
c := make(chan *htlcswitch.PaymentResult, 1)
|
|
|
|
res, ok := m.results[paymentID]
|
|
|
|
if !ok {
|
|
|
|
return nil, htlcswitch.ErrPaymentIDNotFound
|
2019-05-16 16:27:28 +03:00
|
|
|
}
|
2019-05-16 16:27:29 +03:00
|
|
|
c <- res
|
|
|
|
|
|
|
|
return c, nil
|
2019-05-16 16:27:28 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentAttemptDispatcher) setPaymentResult(
|
|
|
|
f func(firstHop lnwire.ShortChannelID) ([32]byte, error)) {
|
|
|
|
|
|
|
|
m.onPayment = f
|
|
|
|
}
|