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