2019-05-16 16:27:28 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2019-05-23 21:05:30 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-15 19:00:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-04-30 14:24:37 +03:00
|
|
|
"github.com/go-errors/errors"
|
2019-05-23 21:05:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-05-16 16:27:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-05-23 21:05:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-05-16 16:27:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-05-23 21:05:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2021-05-24 14:40:53 +03:00
|
|
|
"github.com/stretchr/testify/mock"
|
2019-05-16 16:27:28 +03:00
|
|
|
)
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockPaymentAttemptDispatcherOld struct {
|
2019-05-16 16:27:28 +03:00
|
|
|
onPayment func(firstHop lnwire.ShortChannelID) ([32]byte, error)
|
2019-05-16 16:27:29 +03:00
|
|
|
results map[uint64]*htlcswitch.PaymentResult
|
2020-04-01 01:13:27 +03:00
|
|
|
|
|
|
|
sync.Mutex
|
2019-05-16 16:27:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcherOld)(nil)
|
2019-05-16 16:27:28 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcherOld) SendHTLC(
|
|
|
|
firstHop lnwire.ShortChannelID, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *htlcswitch.PaymentResult
|
|
|
|
preimage, err := m.onPayment(firstHop)
|
|
|
|
if err != nil {
|
2020-01-14 16:07:42 +03:00
|
|
|
rtErr, ok := err.(htlcswitch.ClearTextError)
|
2019-05-16 16:27:29 +03:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result = &htlcswitch.PaymentResult{
|
2020-01-14 16:07:42 +03:00
|
|
|
Error: rtErr,
|
2019-05-16 16:27:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = &htlcswitch.PaymentResult{Preimage: preimage}
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:27 +03:00
|
|
|
m.Lock()
|
|
|
|
if m.results == nil {
|
|
|
|
m.results = make(map[uint64]*htlcswitch.PaymentResult)
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:27:29 +03:00
|
|
|
m.results[pid] = result
|
2020-04-01 01:13:27 +03:00
|
|
|
m.Unlock()
|
2019-05-16 16:27:29 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcherOld) GetPaymentResult(paymentID uint64,
|
2019-06-07 17:42:25 +03:00
|
|
|
_ lntypes.Hash, _ 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)
|
2020-04-01 01:13:27 +03:00
|
|
|
|
|
|
|
m.Lock()
|
2019-05-16 16:27:29 +03:00
|
|
|
res, ok := m.results[paymentID]
|
2020-04-01 01:13:27 +03:00
|
|
|
m.Unlock()
|
|
|
|
|
2019-05-16 16:27:29 +03:00
|
|
|
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
|
|
|
|
|
|
|
}
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcherOld) CleanStore(
|
|
|
|
map[uint64]struct{}) error {
|
|
|
|
|
2020-09-24 10:53:07 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-16 16:27:28 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcherOld) setPaymentResult(
|
2019-05-16 16:27:28 +03:00
|
|
|
f func(firstHop lnwire.ShortChannelID) ([32]byte, error)) {
|
|
|
|
|
|
|
|
m.onPayment = f
|
|
|
|
}
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockPaymentSessionSourceOld struct {
|
2021-04-23 09:39:43 +03:00
|
|
|
routes []*route.Route
|
|
|
|
routeRelease chan struct{}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ PaymentSessionSource = (*mockPaymentSessionSourceOld)(nil)
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionSourceOld) NewPaymentSession(
|
2020-04-01 01:13:22 +03:00
|
|
|
_ *LightningPayment) (PaymentSession, error) {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
return &mockPaymentSessionOld{
|
2021-04-23 09:39:43 +03:00
|
|
|
routes: m.routes,
|
|
|
|
release: m.routeRelease,
|
|
|
|
}, nil
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionSourceOld) NewPaymentSessionForRoute(
|
2019-05-23 21:05:30 +03:00
|
|
|
preBuiltRoute *route.Route) PaymentSession {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionSourceOld) NewPaymentSessionEmpty() PaymentSession {
|
|
|
|
return &mockPaymentSessionOld{}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockMissionControlOld struct {
|
2021-01-19 11:57:15 +03:00
|
|
|
MissionControl
|
2019-06-18 19:30:56 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ MissionController = (*mockMissionControlOld)(nil)
|
2019-06-18 19:30:56 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockMissionControlOld) ReportPaymentFail(
|
|
|
|
paymentID uint64, rt *route.Route,
|
2019-08-05 13:13:58 +03:00
|
|
|
failureSourceIdx *int, failure lnwire.FailureMessage) (
|
|
|
|
*channeldb.FailureReason, error) {
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
// Report a permanent failure if this is an error caused
|
|
|
|
// by incorrect details.
|
|
|
|
if failure.Code() == lnwire.CodeIncorrectOrUnknownPaymentDetails {
|
|
|
|
reason := channeldb.FailureReasonPaymentDetails
|
|
|
|
return &reason, nil
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:13:58 +03:00
|
|
|
return nil, nil
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockMissionControlOld) ReportPaymentSuccess(paymentID uint64,
|
2019-07-29 15:20:06 +03:00
|
|
|
rt *route.Route) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockMissionControlOld) GetProbability(fromNode, toNode route.Vertex,
|
2019-06-18 19:30:56 +03:00
|
|
|
amt lnwire.MilliSatoshi) float64 {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockPaymentSessionOld struct {
|
2019-05-23 21:05:30 +03:00
|
|
|
routes []*route.Route
|
2021-04-23 09:39:43 +03:00
|
|
|
|
|
|
|
// release is a channel that optionally blocks requesting a route
|
|
|
|
// from our mock payment channel. If this value is nil, we will just
|
|
|
|
// release the route automatically.
|
|
|
|
release chan struct{}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ PaymentSession = (*mockPaymentSessionOld)(nil)
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionOld) RequestRoute(_, _ lnwire.MilliSatoshi,
|
2020-04-01 01:13:22 +03:00
|
|
|
_, height uint32) (*route.Route, error) {
|
|
|
|
|
2021-04-23 09:39:43 +03:00
|
|
|
if m.release != nil {
|
|
|
|
m.release <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
if len(m.routes) == 0 {
|
2020-04-01 01:13:27 +03:00
|
|
|
return nil, errNoPathFound
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
r := m.routes[0]
|
|
|
|
m.routes = m.routes[1:]
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionOld) UpdateAdditionalEdge(_ *lnwire.ChannelUpdate,
|
2021-04-15 19:00:17 +03:00
|
|
|
_ *btcec.PublicKey, _ *channeldb.ChannelEdgePolicy) bool {
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPaymentSessionOld) GetAdditionalEdgePolicy(_ *btcec.PublicKey,
|
2021-04-15 19:00:17 +03:00
|
|
|
_ uint64) *channeldb.ChannelEdgePolicy {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockPayerOld struct {
|
2021-04-23 09:39:40 +03:00
|
|
|
sendResult chan error
|
|
|
|
paymentResult chan *htlcswitch.PaymentResult
|
|
|
|
quit chan struct{}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ PaymentAttemptDispatcher = (*mockPayerOld)(nil)
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPayerOld) SendHTLC(_ lnwire.ShortChannelID,
|
2019-05-23 21:05:30 +03:00
|
|
|
paymentID uint64,
|
|
|
|
_ *lnwire.UpdateAddHTLC) error {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-m.sendResult:
|
|
|
|
return res
|
|
|
|
case <-m.quit:
|
|
|
|
return fmt.Errorf("test quitting")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPayerOld) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
|
2019-06-07 17:42:25 +03:00
|
|
|
_ htlcswitch.ErrorDecrypter) (<-chan *htlcswitch.PaymentResult, error) {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
select {
|
2021-04-23 09:39:40 +03:00
|
|
|
case res, ok := <-m.paymentResult:
|
2019-05-23 21:05:30 +03:00
|
|
|
resChan := make(chan *htlcswitch.PaymentResult, 1)
|
2021-04-23 09:39:40 +03:00
|
|
|
if !ok {
|
|
|
|
close(resChan)
|
|
|
|
} else {
|
|
|
|
resChan <- res
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
return resChan, nil
|
2021-04-23 09:39:40 +03:00
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
case <-m.quit:
|
|
|
|
return nil, fmt.Errorf("test quitting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockPayerOld) CleanStore(pids map[uint64]struct{}) error {
|
2020-09-24 10:53:07 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
type initArgs struct {
|
|
|
|
c *channeldb.PaymentCreationInfo
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
type registerAttemptArgs struct {
|
2020-02-07 12:31:27 +03:00
|
|
|
a *channeldb.HTLCAttemptInfo
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
type settleAttemptArgs struct {
|
2019-05-23 21:05:30 +03:00
|
|
|
preimg lntypes.Preimage
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
type failAttemptArgs struct {
|
|
|
|
reason *channeldb.HTLCFailInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type failPaymentArgs struct {
|
2019-05-23 21:05:30 +03:00
|
|
|
reason channeldb.FailureReason
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
type testPayment struct {
|
|
|
|
info channeldb.PaymentCreationInfo
|
|
|
|
attempts []channeldb.HTLCAttempt
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
type mockControlTowerOld struct {
|
2020-04-01 01:13:23 +03:00
|
|
|
payments map[lntypes.Hash]*testPayment
|
2019-05-23 21:05:30 +03:00
|
|
|
successful map[lntypes.Hash]struct{}
|
2020-04-01 01:13:23 +03:00
|
|
|
failed map[lntypes.Hash]channeldb.FailureReason
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
init chan initArgs
|
|
|
|
registerAttempt chan registerAttemptArgs
|
|
|
|
settleAttempt chan settleAttemptArgs
|
|
|
|
failAttempt chan failAttemptArgs
|
|
|
|
failPayment chan failPaymentArgs
|
|
|
|
fetchInFlight chan struct{}
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
var _ ControlTower = (*mockControlTowerOld)(nil)
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func makeMockControlTower() *mockControlTowerOld {
|
|
|
|
return &mockControlTowerOld{
|
2020-04-01 01:13:23 +03:00
|
|
|
payments: make(map[lntypes.Hash]*testPayment),
|
2019-05-23 21:05:30 +03:00
|
|
|
successful: make(map[lntypes.Hash]struct{}),
|
2020-04-01 01:13:23 +03:00
|
|
|
failed: make(map[lntypes.Hash]channeldb.FailureReason),
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
|
2019-05-23 21:05:30 +03:00
|
|
|
c *channeldb.PaymentCreationInfo) error {
|
|
|
|
|
|
|
|
if m.init != nil {
|
|
|
|
m.init <- initArgs{c}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:39 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
// Don't allow re-init a successful payment.
|
2019-05-23 21:05:30 +03:00
|
|
|
if _, ok := m.successful[phash]; ok {
|
2020-04-01 01:13:23 +03:00
|
|
|
return channeldb.ErrAlreadyPaid
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
_, failed := m.failed[phash]
|
|
|
|
_, ok := m.payments[phash]
|
|
|
|
|
|
|
|
// If the payment is known, only allow re-init if failed.
|
|
|
|
if ok && !failed {
|
|
|
|
return channeldb.ErrPaymentInFlight
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
delete(m.failed, phash)
|
|
|
|
m.payments[phash] = &testPayment{
|
|
|
|
info: *c,
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) RegisterAttempt(phash lntypes.Hash,
|
2020-02-07 12:31:27 +03:00
|
|
|
a *channeldb.HTLCAttemptInfo) error {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
if m.registerAttempt != nil {
|
|
|
|
m.registerAttempt <- registerAttemptArgs{a}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:39 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-04-23 09:39:44 +03:00
|
|
|
// Lookup payment.
|
|
|
|
p, ok := m.payments[phash]
|
|
|
|
if !ok {
|
|
|
|
return channeldb.ErrPaymentNotInitiated
|
|
|
|
}
|
|
|
|
|
|
|
|
var inFlight bool
|
|
|
|
for _, a := range p.attempts {
|
|
|
|
if a.Settle != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Failure != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
inFlight = true
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
// Cannot register attempts for successful or failed payments.
|
2021-04-23 09:39:44 +03:00
|
|
|
_, settled := m.successful[phash]
|
|
|
|
_, failed := m.failed[phash]
|
|
|
|
|
2021-04-23 09:39:45 +03:00
|
|
|
if settled || failed {
|
|
|
|
return channeldb.ErrPaymentTerminal
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:44 +03:00
|
|
|
if settled && !inFlight {
|
2020-04-01 01:13:23 +03:00
|
|
|
return channeldb.ErrPaymentAlreadySucceeded
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:44 +03:00
|
|
|
if failed && !inFlight {
|
2020-04-01 01:13:23 +03:00
|
|
|
return channeldb.ErrPaymentAlreadyFailed
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:44 +03:00
|
|
|
// Add attempt to payment.
|
2020-04-01 01:13:23 +03:00
|
|
|
p.attempts = append(p.attempts, channeldb.HTLCAttempt{
|
|
|
|
HTLCAttemptInfo: *a,
|
|
|
|
})
|
|
|
|
m.payments[phash] = p
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
|
2020-05-06 16:44:36 +03:00
|
|
|
pid uint64, settleInfo *channeldb.HTLCSettleInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error) {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
if m.settleAttempt != nil {
|
|
|
|
m.settleAttempt <- settleAttemptArgs{settleInfo.Preimage}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:39 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// Only allow setting attempts if the payment is known.
|
2020-04-01 01:13:23 +03:00
|
|
|
p, ok := m.payments[phash]
|
|
|
|
if !ok {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrPaymentNotInitiated
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the attempt with this pid, and set the settle info.
|
|
|
|
for i, a := range p.attempts {
|
|
|
|
if a.AttemptID != pid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
if a.Settle != nil {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrAttemptAlreadySettled
|
2020-04-01 01:13:25 +03:00
|
|
|
}
|
|
|
|
if a.Failure != nil {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrAttemptAlreadyFailed
|
2020-04-01 01:13:25 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
p.attempts[i].Settle = settleInfo
|
|
|
|
|
|
|
|
// Mark the payment successful on first settled attempt.
|
|
|
|
m.successful[phash] = struct{}{}
|
2020-05-06 16:44:36 +03:00
|
|
|
return &channeldb.HTLCAttempt{
|
|
|
|
Settle: settleInfo,
|
|
|
|
}, nil
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, fmt.Errorf("pid not found")
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) FailAttempt(phash lntypes.Hash, pid uint64,
|
2020-05-06 16:44:36 +03:00
|
|
|
failInfo *channeldb.HTLCFailInfo) (*channeldb.HTLCAttempt, error) {
|
2020-04-01 01:13:23 +03:00
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
if m.failAttempt != nil {
|
|
|
|
m.failAttempt <- failAttemptArgs{failInfo}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:39 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// Only allow failing attempts if the payment is known.
|
2020-04-01 01:13:23 +03:00
|
|
|
p, ok := m.payments[phash]
|
|
|
|
if !ok {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrPaymentNotInitiated
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the attempt with this pid, and set the failure info.
|
|
|
|
for i, a := range p.attempts {
|
|
|
|
if a.AttemptID != pid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
if a.Settle != nil {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrAttemptAlreadySettled
|
2020-04-01 01:13:25 +03:00
|
|
|
}
|
|
|
|
if a.Failure != nil {
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, channeldb.ErrAttemptAlreadyFailed
|
2020-04-01 01:13:25 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
p.attempts[i].Failure = failInfo
|
2020-05-06 16:44:36 +03:00
|
|
|
return &channeldb.HTLCAttempt{
|
|
|
|
Failure: failInfo,
|
|
|
|
}, nil
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 16:44:36 +03:00
|
|
|
return nil, fmt.Errorf("pid not found")
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) Fail(phash lntypes.Hash,
|
2019-05-23 21:05:30 +03:00
|
|
|
reason channeldb.FailureReason) error {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-04-01 01:13:26 +03:00
|
|
|
if m.failPayment != nil {
|
|
|
|
m.failPayment <- failPaymentArgs{reason}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// Payment must be known.
|
2020-04-01 01:13:23 +03:00
|
|
|
if _, ok := m.payments[phash]; !ok {
|
|
|
|
return channeldb.ErrPaymentNotInitiated
|
|
|
|
}
|
|
|
|
|
|
|
|
m.failed[phash] = reason
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) FetchPayment(phash lntypes.Hash) (
|
2020-04-01 01:13:23 +03:00
|
|
|
*channeldb.MPPayment, error) {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-03-30 13:10:30 +03:00
|
|
|
return m.fetchPayment(phash)
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) fetchPayment(phash lntypes.Hash) (
|
2021-03-30 13:10:30 +03:00
|
|
|
*channeldb.MPPayment, error) {
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
p, ok := m.payments[phash]
|
|
|
|
if !ok {
|
|
|
|
return nil, channeldb.ErrPaymentNotInitiated
|
|
|
|
}
|
|
|
|
|
|
|
|
mp := &channeldb.MPPayment{
|
|
|
|
Info: &p.info,
|
|
|
|
}
|
|
|
|
|
|
|
|
reason, ok := m.failed[phash]
|
|
|
|
if ok {
|
|
|
|
mp.FailureReason = &reason
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a copy of the current attempts.
|
|
|
|
mp.HTLCs = append(mp.HTLCs, p.attempts...)
|
|
|
|
return mp, nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) FetchInFlightPayments() (
|
2021-03-30 13:10:30 +03:00
|
|
|
[]*channeldb.MPPayment, error) {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
if m.fetchInFlight != nil {
|
|
|
|
m.fetchInFlight <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:39 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-04-01 01:13:23 +03:00
|
|
|
// In flight are all payments not successful or failed.
|
2021-03-30 13:10:30 +03:00
|
|
|
var fl []*channeldb.MPPayment
|
|
|
|
for hash := range m.payments {
|
2020-04-01 01:13:23 +03:00
|
|
|
if _, ok := m.successful[hash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := m.failed[hash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:10:30 +03:00
|
|
|
mp, err := m.fetchPayment(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-01 01:13:23 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 13:10:30 +03:00
|
|
|
fl = append(fl, mp)
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return fl, nil
|
|
|
|
}
|
2019-04-30 14:24:37 +03:00
|
|
|
|
2021-05-24 13:29:27 +03:00
|
|
|
func (m *mockControlTowerOld) SubscribePayment(paymentHash lntypes.Hash) (
|
2020-04-03 18:05:05 +03:00
|
|
|
*ControlTowerSubscriber, error) {
|
2019-04-30 14:24:37 +03:00
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
return nil, errors.New("not implemented")
|
2019-04-30 14:24:37 +03:00
|
|
|
}
|
2021-05-24 14:40:53 +03:00
|
|
|
|
|
|
|
type mockPaymentAttemptDispatcher struct {
|
|
|
|
mock.Mock
|
2021-05-21 14:02:49 +03:00
|
|
|
|
|
|
|
resultChan chan *htlcswitch.PaymentResult
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcher)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
|
|
|
|
pid uint64, htlcAdd *lnwire.UpdateAddHTLC) error {
|
|
|
|
|
|
|
|
args := m.Called(firstHop, pid, htlcAdd)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentAttemptDispatcher) GetPaymentResult(attemptID uint64,
|
|
|
|
paymentHash lntypes.Hash, deobfuscator htlcswitch.ErrorDecrypter) (
|
|
|
|
<-chan *htlcswitch.PaymentResult, error) {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Called(attemptID, paymentHash, deobfuscator)
|
|
|
|
|
|
|
|
// Instead of returning the mocked returned values, we need to return
|
|
|
|
// the chan resultChan so it can be converted into a read-only chan.
|
|
|
|
return m.resultChan, nil
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentAttemptDispatcher) CleanStore(
|
|
|
|
keepPids map[uint64]struct{}) error {
|
|
|
|
|
|
|
|
args := m.Called(keepPids)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPaymentSessionSource struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentSessionSource = (*mockPaymentSessionSource)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSession(
|
|
|
|
payment *LightningPayment) (PaymentSession, error) {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
args := m.Called(payment)
|
2021-05-24 14:40:53 +03:00
|
|
|
return args.Get(0).(PaymentSession), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSessionForRoute(
|
|
|
|
preBuiltRoute *route.Route) PaymentSession {
|
|
|
|
|
|
|
|
args := m.Called(preBuiltRoute)
|
|
|
|
return args.Get(0).(PaymentSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSessionEmpty() PaymentSession {
|
|
|
|
args := m.Called()
|
|
|
|
return args.Get(0).(PaymentSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockMissionControl struct {
|
|
|
|
mock.Mock
|
2021-05-21 14:02:49 +03:00
|
|
|
|
|
|
|
failReason *channeldb.FailureReason
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ MissionController = (*mockMissionControl)(nil)
|
|
|
|
|
|
|
|
func (m *mockMissionControl) ReportPaymentFail(
|
|
|
|
paymentID uint64, rt *route.Route,
|
|
|
|
failureSourceIdx *int, failure lnwire.FailureMessage) (
|
|
|
|
*channeldb.FailureReason, error) {
|
|
|
|
|
|
|
|
args := m.Called(paymentID, rt, failureSourceIdx, failure)
|
2021-05-21 14:02:49 +03:00
|
|
|
return m.failReason, args.Error(1)
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockMissionControl) ReportPaymentSuccess(paymentID uint64,
|
|
|
|
rt *route.Route) error {
|
|
|
|
|
|
|
|
args := m.Called(paymentID, rt)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,
|
|
|
|
amt lnwire.MilliSatoshi) float64 {
|
|
|
|
|
|
|
|
args := m.Called(fromNode, toNode, amt)
|
|
|
|
return args.Get(0).(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPaymentSession struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentSession = (*mockPaymentSession)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentSession) RequestRoute(maxAmt, feeLimit lnwire.MilliSatoshi,
|
|
|
|
activeShards, height uint32) (*route.Route, error) {
|
|
|
|
args := m.Called(maxAmt, feeLimit, activeShards, height)
|
|
|
|
return args.Get(0).(*route.Route), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSession) UpdateAdditionalEdge(msg *lnwire.ChannelUpdate,
|
|
|
|
pubKey *btcec.PublicKey, policy *channeldb.ChannelEdgePolicy) bool {
|
|
|
|
|
|
|
|
args := m.Called(msg, pubKey, policy)
|
|
|
|
return args.Bool(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSession) GetAdditionalEdgePolicy(pubKey *btcec.PublicKey,
|
|
|
|
channelID uint64) *channeldb.ChannelEdgePolicy {
|
|
|
|
|
|
|
|
args := m.Called(pubKey, channelID)
|
|
|
|
return args.Get(0).(*channeldb.ChannelEdgePolicy)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockControlTower struct {
|
|
|
|
mock.Mock
|
2021-05-21 14:02:49 +03:00
|
|
|
sync.Mutex
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ControlTower = (*mockControlTower)(nil)
|
|
|
|
|
|
|
|
func (m *mockControlTower) InitPayment(phash lntypes.Hash,
|
|
|
|
c *channeldb.PaymentCreationInfo) error {
|
|
|
|
|
|
|
|
args := m.Called(phash, c)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) RegisterAttempt(phash lntypes.Hash,
|
|
|
|
a *channeldb.HTLCAttemptInfo) error {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-05-24 14:40:53 +03:00
|
|
|
args := m.Called(phash, a)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) SettleAttempt(phash lntypes.Hash,
|
|
|
|
pid uint64, settleInfo *channeldb.HTLCSettleInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error) {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-05-24 14:40:53 +03:00
|
|
|
args := m.Called(phash, pid, settleInfo)
|
|
|
|
return args.Get(0).(*channeldb.HTLCAttempt), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) FailAttempt(phash lntypes.Hash, pid uint64,
|
|
|
|
failInfo *channeldb.HTLCFailInfo) (*channeldb.HTLCAttempt, error) {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-05-24 14:40:53 +03:00
|
|
|
args := m.Called(phash, pid, failInfo)
|
|
|
|
return args.Get(0).(*channeldb.HTLCAttempt), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) Fail(phash lntypes.Hash,
|
|
|
|
reason channeldb.FailureReason) error {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2021-05-24 14:40:53 +03:00
|
|
|
args := m.Called(phash, reason)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) FetchPayment(phash lntypes.Hash) (
|
|
|
|
*channeldb.MPPayment, error) {
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2021-05-24 14:40:53 +03:00
|
|
|
args := m.Called(phash)
|
|
|
|
|
|
|
|
// Type assertion on nil will fail, so we check and return here.
|
|
|
|
if args.Get(0) == nil {
|
|
|
|
return nil, args.Error(1)
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
// Make a copy of the payment here to avoid data race.
|
|
|
|
p := args.Get(0).(*channeldb.MPPayment)
|
|
|
|
payment := &channeldb.MPPayment{
|
|
|
|
FailureReason: p.FailureReason,
|
|
|
|
}
|
|
|
|
payment.HTLCs = make([]channeldb.HTLCAttempt, len(p.HTLCs))
|
|
|
|
copy(payment.HTLCs, p.HTLCs)
|
2021-05-24 14:40:53 +03:00
|
|
|
|
2021-05-21 14:02:49 +03:00
|
|
|
return payment, args.Error(1)
|
2021-05-24 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) FetchInFlightPayments() (
|
|
|
|
[]*channeldb.MPPayment, error) {
|
|
|
|
|
|
|
|
args := m.Called()
|
|
|
|
return args.Get(0).([]*channeldb.MPPayment), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) SubscribePayment(paymentHash lntypes.Hash) (
|
|
|
|
*ControlTowerSubscriber, error) {
|
|
|
|
|
|
|
|
args := m.Called(paymentHash)
|
|
|
|
return args.Get(0).(*ControlTowerSubscriber), args.Error(1)
|
|
|
|
}
|