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"
|
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
|
|
|
}
|