2019-05-16 16:27:28 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2019-05-23 21:05:30 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
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"
|
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2019-05-16 16:27:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.results[pid] = result
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:27:29 +03:00
|
|
|
func (m *mockPaymentAttemptDispatcher) 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)
|
|
|
|
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
|
|
|
|
}
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
type mockPaymentSessionSource struct {
|
|
|
|
routes []*route.Route
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentSessionSource = (*mockPaymentSessionSource)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSession(routeHints [][]zpay32.HopHint,
|
|
|
|
target route.Vertex) (PaymentSession, error) {
|
|
|
|
|
|
|
|
return &mockPaymentSession{m.routes}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSessionForRoute(
|
|
|
|
preBuiltRoute *route.Route) PaymentSession {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentSessionSource) NewPaymentSessionEmpty() PaymentSession {
|
|
|
|
return &mockPaymentSession{}
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:30:56 +03:00
|
|
|
type mockMissionControl struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ MissionController = (*mockMissionControl)(nil)
|
|
|
|
|
2019-08-05 13:13:58 +03:00
|
|
|
func (m *mockMissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
|
|
|
|
failureSourceIdx *int, failure lnwire.FailureMessage) (
|
|
|
|
*channeldb.FailureReason, error) {
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2019-08-05 13:13:58 +03:00
|
|
|
return nil, nil
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:20:06 +03:00
|
|
|
func (m *mockMissionControl) ReportPaymentSuccess(paymentID uint64,
|
|
|
|
rt *route.Route) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,
|
2019-06-18 19:30:56 +03:00
|
|
|
amt lnwire.MilliSatoshi) float64 {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
type mockPaymentSession struct {
|
|
|
|
routes []*route.Route
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentSession = (*mockPaymentSession)(nil)
|
|
|
|
|
|
|
|
func (m *mockPaymentSession) RequestRoute(payment *LightningPayment,
|
|
|
|
height uint32, finalCltvDelta uint16) (*route.Route, error) {
|
|
|
|
|
|
|
|
if len(m.routes) == 0 {
|
|
|
|
return nil, fmt.Errorf("no routes")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := m.routes[0]
|
|
|
|
m.routes = m.routes[1:]
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPayer struct {
|
|
|
|
sendResult chan error
|
|
|
|
paymentResultErr chan error
|
|
|
|
paymentResult chan *htlcswitch.PaymentResult
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PaymentAttemptDispatcher = (*mockPayer)(nil)
|
|
|
|
|
|
|
|
func (m *mockPayer) SendHTLC(_ lnwire.ShortChannelID,
|
|
|
|
paymentID uint64,
|
|
|
|
_ *lnwire.UpdateAddHTLC) error {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-m.sendResult:
|
|
|
|
return res
|
|
|
|
case <-m.quit:
|
|
|
|
return fmt.Errorf("test quitting")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-07 17:42:25 +03:00
|
|
|
func (m *mockPayer) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
|
|
|
|
_ htlcswitch.ErrorDecrypter) (<-chan *htlcswitch.PaymentResult, error) {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-m.paymentResult:
|
|
|
|
resChan := make(chan *htlcswitch.PaymentResult, 1)
|
|
|
|
resChan <- res
|
|
|
|
return resChan, nil
|
|
|
|
case err := <-m.paymentResultErr:
|
|
|
|
return nil, err
|
|
|
|
case <-m.quit:
|
|
|
|
return nil, fmt.Errorf("test quitting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type initArgs struct {
|
|
|
|
c *channeldb.PaymentCreationInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type registerArgs struct {
|
2020-02-07 12:31:27 +03:00
|
|
|
a *channeldb.HTLCAttemptInfo
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type successArgs struct {
|
|
|
|
preimg lntypes.Preimage
|
|
|
|
}
|
|
|
|
|
|
|
|
type failArgs struct {
|
2019-05-23 21:05:30 +03:00
|
|
|
reason channeldb.FailureReason
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockControlTower struct {
|
|
|
|
inflights map[lntypes.Hash]channeldb.InFlightPayment
|
|
|
|
successful map[lntypes.Hash]struct{}
|
|
|
|
|
|
|
|
init chan initArgs
|
|
|
|
register chan registerArgs
|
|
|
|
success chan successArgs
|
|
|
|
fail chan failArgs
|
|
|
|
fetchInFlight chan struct{}
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:57:04 +03:00
|
|
|
var _ ControlTower = (*mockControlTower)(nil)
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
func makeMockControlTower() *mockControlTower {
|
|
|
|
return &mockControlTower{
|
|
|
|
inflights: make(map[lntypes.Hash]channeldb.InFlightPayment),
|
|
|
|
successful: make(map[lntypes.Hash]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) InitPayment(phash lntypes.Hash,
|
|
|
|
c *channeldb.PaymentCreationInfo) error {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.init != nil {
|
|
|
|
m.init <- initArgs{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m.successful[phash]; ok {
|
|
|
|
return fmt.Errorf("already successful")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := m.inflights[phash]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("in flight")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.inflights[phash] = channeldb.InFlightPayment{
|
|
|
|
Info: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) RegisterAttempt(phash lntypes.Hash,
|
2020-02-07 12:31:27 +03:00
|
|
|
a *channeldb.HTLCAttemptInfo) error {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.register != nil {
|
|
|
|
m.register <- registerArgs{a}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := m.inflights[phash]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("not in flight")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Attempt = a
|
|
|
|
m.inflights[phash] = p
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) Success(phash lntypes.Hash,
|
|
|
|
preimg lntypes.Preimage) error {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.success != nil {
|
|
|
|
m.success <- successArgs{preimg}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m.inflights, phash)
|
|
|
|
m.successful[phash] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
func (m *mockControlTower) Fail(phash lntypes.Hash,
|
|
|
|
reason channeldb.FailureReason) error {
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.fail != nil {
|
2019-05-23 21:05:30 +03:00
|
|
|
m.fail <- failArgs{reason}
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(m.inflights, phash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockControlTower) FetchInFlightPayments() (
|
|
|
|
[]*channeldb.InFlightPayment, error) {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.fetchInFlight != nil {
|
|
|
|
m.fetchInFlight <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var fl []*channeldb.InFlightPayment
|
|
|
|
for _, ifl := range m.inflights {
|
|
|
|
fl = append(fl, &ifl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fl, nil
|
|
|
|
}
|
2019-04-30 14:24:37 +03:00
|
|
|
|
|
|
|
func (m *mockControlTower) SubscribePayment(paymentHash lntypes.Hash) (
|
|
|
|
bool, chan PaymentResult, error) {
|
|
|
|
|
|
|
|
return false, nil, errors.New("not implemented")
|
|
|
|
}
|