2019-05-23 20:05:26 +02:00
|
|
|
package channeldb
|
2018-08-12 15:19:12 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-10 12:34:27 +02:00
|
|
|
"bytes"
|
2019-05-23 20:05:26 +02:00
|
|
|
"crypto/rand"
|
2020-05-20 11:07:38 +02:00
|
|
|
"crypto/sha256"
|
2022-11-24 10:40:49 +01:00
|
|
|
"errors"
|
2018-08-12 15:19:12 +02:00
|
|
|
"fmt"
|
2019-05-23 20:05:26 +02:00
|
|
|
"io"
|
2019-05-23 20:05:28 +02:00
|
|
|
"reflect"
|
2018-08-12 15:19:12 +02:00
|
|
|
"testing"
|
2019-05-23 20:05:28 +02:00
|
|
|
"time"
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2019-05-23 20:05:28 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2019-05-23 20:05:28 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-04-01 00:13:27 +02:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2020-06-10 12:34:27 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-12 15:19:12 +02:00
|
|
|
)
|
|
|
|
|
2019-05-23 20:05:26 +02:00
|
|
|
func genPreimage() ([32]byte, error) {
|
|
|
|
var preimage [32]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, preimage[:]); err != nil {
|
|
|
|
return preimage, err
|
|
|
|
}
|
|
|
|
return preimage, nil
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:31:27 +01:00
|
|
|
func genInfo() (*PaymentCreationInfo, *HTLCAttemptInfo,
|
2019-05-23 20:05:28 +02:00
|
|
|
lntypes.Preimage, error) {
|
|
|
|
|
2018-08-12 15:19:12 +02:00
|
|
|
preimage, err := genPreimage()
|
|
|
|
if err != nil {
|
2019-05-23 20:05:28 +02:00
|
|
|
return nil, nil, preimage, fmt.Errorf("unable to "+
|
|
|
|
"generate preimage: %v", err)
|
2018-08-12 15:19:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:07:38 +02:00
|
|
|
rhash := sha256.Sum256(preimage[:])
|
2023-03-06 10:04:44 +01:00
|
|
|
attempt := NewHtlcAttempt(
|
2021-05-19 09:03:52 +02:00
|
|
|
0, priv, *testRoute.Copy(), time.Time{}, nil,
|
|
|
|
)
|
2019-05-23 20:05:28 +02:00
|
|
|
return &PaymentCreationInfo{
|
2021-05-19 09:03:52 +02:00
|
|
|
PaymentIdentifier: rhash,
|
|
|
|
Value: testRoute.ReceiverAmt(),
|
|
|
|
CreationTime: time.Unix(time.Now().Unix(), 0),
|
|
|
|
PaymentRequest: []byte("hola"),
|
2023-03-06 10:04:44 +01:00
|
|
|
}, &attempt.HTLCAttemptInfo, preimage, nil
|
2018-08-12 15:19:12 +02:00
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// TestPaymentControlSwitchFail checks that payment status returns to Failed
|
|
|
|
// status after failing, and that InitPayment allows another HTLC for the
|
2018-08-10 23:01:24 +02:00
|
|
|
// same payment hash.
|
2019-05-23 20:05:28 +02:00
|
|
|
func TestPaymentControlSwitchFail(t *testing.T) {
|
2018-08-12 15:19:12 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
info, attempt, preimg, err := genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2018-08-10 23:01:24 +02:00
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentIdentifier)
|
2022-11-24 10:40:49 +01:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, StatusInitiated,
|
|
|
|
)
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, nil,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Fail the payment, which should moved it to Failed.
|
2019-05-23 20:05:30 +02:00
|
|
|
failReason := FailureReasonNoRoute
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.Fail(info.PaymentIdentifier, failReason)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to fail payment hash")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Verify the status is indeed Failed.
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed)
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, &failReason, nil,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
// Lookup the payment so we can get its old sequence number before it is
|
|
|
|
// overwritten.
|
2021-03-31 12:23:08 +02:00
|
|
|
payment, err := pControl.FetchPayment(info.PaymentIdentifier)
|
2022-06-11 23:04:50 +02:00
|
|
|
require.NoError(t, err)
|
2020-06-10 12:34:27 +02:00
|
|
|
|
2018-08-10 23:01:24 +02:00
|
|
|
// Sends the htlc again, which should succeed since the prior payment
|
|
|
|
// failed.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
// Check that our index has been updated, and the old index has been
|
|
|
|
// removed.
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentIdentifier)
|
2020-06-10 12:34:27 +02:00
|
|
|
assertNoIndex(t, pControl, payment.SequenceNum)
|
|
|
|
|
2022-11-24 10:40:49 +01:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, StatusInitiated,
|
|
|
|
)
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, nil,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
|
|
|
|
2020-02-20 14:56:24 +01:00
|
|
|
// Record a new attempt. In this test scenario, the attempt fails.
|
|
|
|
// However, this is not communicated to control tower in the current
|
|
|
|
// implementation. It only registers the initiation of the attempt.
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to register attempt")
|
2020-02-20 14:56:24 +01:00
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
htlcReason := HTLCFailUnreadable
|
2020-04-06 09:26:52 +02:00
|
|
|
_, err = pControl.FailAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcReason,
|
2020-02-20 18:08:01 +01:00
|
|
|
},
|
2020-02-20 18:08:01 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-11-24 10:40:49 +01:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, StatusInFlight,
|
|
|
|
)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
failure: &htlcReason,
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc)
|
2020-02-20 18:08:01 +01:00
|
|
|
|
2020-02-20 14:56:24 +01:00
|
|
|
// Record another attempt.
|
2020-04-01 00:13:25 +02:00
|
|
|
attempt.AttemptID = 1
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc = &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
// Settle the attempt and verify that status was changed to
|
|
|
|
// StatusSucceeded.
|
2020-02-20 18:08:01 +01:00
|
|
|
payment, err = pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
2020-02-20 18:08:01 +01:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "error shouldn't have been received, got")
|
2019-07-31 06:44:50 +02:00
|
|
|
|
2020-02-20 18:08:01 +01:00
|
|
|
if len(payment.HTLCs) != 2 {
|
|
|
|
t.Fatalf("payment should have two htlcs, got: %d",
|
2019-11-08 12:39:51 +01:00
|
|
|
len(payment.HTLCs))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = assertRouteEqual(&payment.HTLCs[0].Route, &attempt.Route)
|
2019-07-31 06:44:50 +02:00
|
|
|
if err != nil {
|
2019-09-10 15:34:02 +02:00
|
|
|
t.Fatalf("unexpected route returned: %v vs %v: %v",
|
2019-11-08 12:39:51 +01:00
|
|
|
spew.Sdump(attempt.Route),
|
|
|
|
spew.Sdump(payment.HTLCs[0].Route), err)
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
2018-08-10 23:01:24 +02:00
|
|
|
|
|
|
|
// Attempt a final payment, which should now fail since the prior
|
|
|
|
// payment succeed.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2019-05-23 20:05:28 +02:00
|
|
|
if err != ErrAlreadyPaid {
|
2018-08-10 23:01:24 +02:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
2018-08-12 15:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// TestPaymentControlSwitchDoubleSend checks the ability of payment control to
|
2018-08-10 23:01:24 +02:00
|
|
|
// prevent double sending of htlc message, when message is in StatusInFlight.
|
2019-05-23 20:05:28 +02:00
|
|
|
func TestPaymentControlSwitchDoubleSend(t *testing.T) {
|
2018-08-12 15:19:12 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
info, attempt, preimg, err := genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2018-08-10 23:01:24 +02:00
|
|
|
// Sends base htlc message which initiate base status and move it to
|
|
|
|
// StatusInFlight and verifies that it was changed.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentIdentifier)
|
2022-11-24 10:40:49 +01:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, StatusInitiated,
|
|
|
|
)
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, nil,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2018-08-10 23:01:24 +02:00
|
|
|
// Try to initiate double sending of htlc message with the same
|
|
|
|
// payment hash, should result in error indicating that payment has
|
|
|
|
// already been sent.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-11-24 10:40:49 +01:00
|
|
|
require.Equal(t, ErrPaymentExists, err, "payment control wrong "+
|
|
|
|
"behaviour: init payment again must trigger ErrPaymentExists "+
|
|
|
|
"error")
|
2018-08-12 15:19:12 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Record an attempt.
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
2019-05-23 20:05:28 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2019-05-23 20:05:28 +02:00
|
|
|
)
|
2018-08-12 15:19:12 +02:00
|
|
|
|
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2019-05-23 20:05:28 +02:00
|
|
|
if err != ErrPaymentInFlight {
|
|
|
|
t.Fatalf("payment control wrong behaviour: " +
|
|
|
|
"double sending must trigger ErrPaymentInFlight error")
|
2018-08-12 15:19:12 +02:00
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// After settling, the error should be ErrAlreadyPaid.
|
2020-02-20 18:08:01 +01:00
|
|
|
_, err = pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
2020-02-20 18:08:01 +01:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "error shouldn't have been received, got")
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc)
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2019-05-23 20:05:28 +02:00
|
|
|
if err != ErrAlreadyPaid {
|
2019-05-23 20:05:28 +02:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
2018-08-14 03:47:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// TestPaymentControlSuccessesWithoutInFlight checks that the payment
|
2018-08-14 03:47:19 +02:00
|
|
|
// control will disallow calls to Success when no payment is in flight.
|
2019-05-23 20:05:28 +02:00
|
|
|
func TestPaymentControlSuccessesWithoutInFlight(t *testing.T) {
|
2018-08-14 03:47:19 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
info, _, preimg, err := genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Attempt to complete the payment should fail.
|
2020-02-20 18:08:01 +01:00
|
|
|
_, err = pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, 0,
|
2020-02-20 18:08:01 +01:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2018-08-14 03:47:19 +02:00
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// TestPaymentControlFailsWithoutInFlight checks that a strict payment
|
2018-08-14 03:47:19 +02:00
|
|
|
// control will disallow calls to Fail when no payment is in flight.
|
2019-05-23 20:05:28 +02:00
|
|
|
func TestPaymentControlFailsWithoutInFlight(t *testing.T) {
|
2018-08-14 03:47:19 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
info, _, _, err := genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2018-08-14 03:47:19 +02:00
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Calling Fail should return an error.
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.Fail(info.PaymentIdentifier, FailureReasonNoRoute)
|
2018-08-14 03:47:19 +02:00
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
// TestPaymentControlDeleteNonInFlight checks that calling DeletePayments only
|
2019-05-27 14:35:09 +02:00
|
|
|
// deletes payments from the database that are not in-flight.
|
2022-06-12 06:18:07 +02:00
|
|
|
func TestPaymentControlDeleteNonInFlight(t *testing.T) {
|
2019-05-27 14:35:09 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2019-05-27 14:35:09 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
// Create a sequence number for duplicate payments that will not collide
|
|
|
|
// with the sequence numbers for the payments we create. These values
|
|
|
|
// start at 1, so 9999 is a safe bet for this test.
|
|
|
|
var duplicateSeqNr = 9999
|
|
|
|
|
2019-05-27 14:35:09 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
payments := []struct {
|
2020-06-10 12:34:27 +02:00
|
|
|
failed bool
|
|
|
|
success bool
|
|
|
|
hasDuplicate bool
|
2019-05-27 14:35:09 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-06-10 12:34:27 +02:00
|
|
|
failed: true,
|
|
|
|
success: false,
|
|
|
|
hasDuplicate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
failed: false,
|
|
|
|
success: true,
|
|
|
|
hasDuplicate: false,
|
2019-05-27 14:35:09 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-10 12:34:27 +02:00
|
|
|
failed: false,
|
|
|
|
success: false,
|
|
|
|
hasDuplicate: false,
|
2019-05-27 14:35:09 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-10 12:34:27 +02:00
|
|
|
failed: false,
|
|
|
|
success: true,
|
|
|
|
hasDuplicate: true,
|
2019-05-27 14:35:09 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:11:24 +02:00
|
|
|
var numSuccess, numInflight int
|
|
|
|
|
2019-05-27 14:35:09 +02:00
|
|
|
for _, p := range payments {
|
|
|
|
info, attempt, preimg, err := genInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2019-05-27 14:35:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2019-05-27 14:35:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:35:09 +02:00
|
|
|
if p.failed {
|
2020-02-20 18:08:01 +01:00
|
|
|
// Fail the payment attempt.
|
2020-04-01 00:13:25 +02:00
|
|
|
htlcFailure := HTLCFailUnreadable
|
2020-04-06 09:26:52 +02:00
|
|
|
_, err := pControl.FailAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
2020-02-20 18:08:01 +01:00
|
|
|
&HTLCFailInfo{
|
2020-04-01 00:13:25 +02:00
|
|
|
Reason: htlcFailure,
|
2020-02-20 18:08:01 +01:00
|
|
|
},
|
2020-02-20 18:08:01 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:35:09 +02:00
|
|
|
// Fail the payment, which should moved it to Failed.
|
|
|
|
failReason := FailureReasonNoRoute
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.Fail(info.PaymentIdentifier, failReason)
|
2019-05-27 14:35:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the status is indeed Failed.
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc.failure = &htlcFailure
|
2019-05-27 14:35:09 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info,
|
2020-04-01 00:13:25 +02:00
|
|
|
&failReason, htlc,
|
2019-05-27 14:35:09 +02:00
|
|
|
)
|
|
|
|
} else if p.success {
|
|
|
|
// Verifies that status was changed to StatusSucceeded.
|
2020-02-20 18:08:01 +01:00
|
|
|
_, err := pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
2020-02-20 18:08:01 +01:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2019-05-27 14:35:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
2019-05-27 14:35:09 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2019-05-27 14:35:09 +02:00
|
|
|
)
|
2019-06-11 15:11:24 +02:00
|
|
|
|
|
|
|
numSuccess++
|
2019-05-27 14:35:09 +02:00
|
|
|
} else {
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight)
|
2019-05-27 14:35:09 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2019-05-27 14:35:09 +02:00
|
|
|
)
|
2019-06-11 15:11:24 +02:00
|
|
|
|
|
|
|
numInflight++
|
2019-05-27 14:35:09 +02:00
|
|
|
}
|
2020-06-10 12:34:27 +02:00
|
|
|
|
|
|
|
// If the payment is intended to have a duplicate payment, we
|
|
|
|
// add one.
|
|
|
|
if p.hasDuplicate {
|
|
|
|
appendDuplicatePayment(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl.db, info.PaymentIdentifier,
|
2021-01-22 14:04:20 +01:00
|
|
|
uint64(duplicateSeqNr), preimg,
|
2020-06-10 12:34:27 +02:00
|
|
|
)
|
|
|
|
duplicateSeqNr++
|
2019-06-11 15:11:24 +02:00
|
|
|
numSuccess++
|
2020-06-10 12:34:27 +02:00
|
|
|
}
|
2019-05-27 14:35:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 15:11:24 +02:00
|
|
|
// Delete all failed payments.
|
2021-01-22 15:47:55 +01:00
|
|
|
if err := db.DeletePayments(true, false); err != nil {
|
2019-05-27 14:35:09 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:11:24 +02:00
|
|
|
// This should leave the succeeded and in-flight payments.
|
2019-05-27 14:35:09 +02:00
|
|
|
dbPayments, err := db.FetchPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:11:24 +02:00
|
|
|
if len(dbPayments) != numSuccess+numInflight {
|
|
|
|
t.Fatalf("expected %d payments, got %d",
|
|
|
|
numSuccess+numInflight, len(dbPayments))
|
2019-05-27 14:35:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 15:11:24 +02:00
|
|
|
var s, i int
|
|
|
|
for _, p := range dbPayments {
|
2022-02-07 13:58:25 +01:00
|
|
|
t.Log("fetch payment has status", p.Status)
|
2019-06-11 15:11:24 +02:00
|
|
|
switch p.Status {
|
|
|
|
case StatusSucceeded:
|
|
|
|
s++
|
|
|
|
case StatusInFlight:
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != numSuccess {
|
|
|
|
t.Fatalf("expected %d succeeded payments , got %d",
|
|
|
|
numSuccess, s)
|
|
|
|
}
|
|
|
|
if i != numInflight {
|
|
|
|
t.Fatalf("expected %d in-flight payments, got %d",
|
|
|
|
numInflight, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete all payments except in-flight.
|
2021-01-22 15:47:55 +01:00
|
|
|
if err := db.DeletePayments(false, false); err != nil {
|
2019-06-11 15:11:24 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should leave the in-flight payment.
|
|
|
|
dbPayments, err = db.FetchPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbPayments) != numInflight {
|
|
|
|
t.Fatalf("expected %d payments, got %d", numInflight,
|
|
|
|
len(dbPayments))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range dbPayments {
|
|
|
|
if p.Status != StatusInFlight {
|
|
|
|
t.Fatalf("expected in-fligth status, got %v", p.Status)
|
|
|
|
}
|
2019-05-27 14:35:09 +02:00
|
|
|
}
|
2020-06-10 12:34:27 +02:00
|
|
|
|
|
|
|
// Finally, check that we only have a single index left in the payment
|
|
|
|
// index bucket.
|
|
|
|
var indexCount int
|
|
|
|
err = kvdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
index := tx.ReadBucket(paymentsIndexBucket)
|
|
|
|
|
|
|
|
return index.ForEach(func(k, v []byte) error {
|
|
|
|
indexCount++
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() { indexCount = 0 })
|
2020-06-10 12:34:27 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, indexCount)
|
2019-05-27 14:35:09 +02:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:29:43 +01:00
|
|
|
// TestPaymentControlDeletePayments tests that DeletePayments correctly deletes
|
2021-01-22 15:47:55 +01:00
|
|
|
// information about completed payments from the database.
|
|
|
|
func TestPaymentControlDeletePayments(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2021-09-13 12:49:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2021-01-22 15:47:55 +01:00
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
// Register three payments:
|
|
|
|
// 1. A payment with two failed attempts.
|
2022-06-12 06:18:07 +02:00
|
|
|
// 2. A payment with one failed and one settled attempt.
|
2021-01-22 15:47:55 +01:00
|
|
|
// 3. A payment with one failed and one in-flight attempt.
|
2021-09-13 12:49:50 +02:00
|
|
|
payments := []*payment{
|
|
|
|
{status: StatusFailed},
|
|
|
|
{status: StatusSucceeded},
|
|
|
|
{status: StatusInFlight},
|
|
|
|
}
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Use helper function to register the test payments in the data and
|
|
|
|
// populate the data to the payments slice.
|
|
|
|
createTestPayments(t, pControl, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Check that all payments are there as we added them.
|
|
|
|
assertPayments(t, db, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Delete HTLC attempts for failed payments only.
|
|
|
|
require.NoError(t, db.DeletePayments(true, true))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The failed payment is the only altered one.
|
|
|
|
payments[0].htlcs = 0
|
|
|
|
assertPayments(t, db, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Delete failed attempts for all payments.
|
|
|
|
require.NoError(t, db.DeletePayments(false, true))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The failed attempts should be deleted, except for the in-flight
|
|
|
|
// payment, that shouldn't be altered until it has completed.
|
|
|
|
payments[1].htlcs = 1
|
|
|
|
assertPayments(t, db, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Now delete all failed payments.
|
|
|
|
require.NoError(t, db.DeletePayments(true, false))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
assertPayments(t, db, payments[1:])
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Finally delete all completed payments.
|
|
|
|
require.NoError(t, db.DeletePayments(false, false))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
assertPayments(t, db, payments[2:])
|
|
|
|
}
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2022-01-13 17:29:43 +01:00
|
|
|
// TestPaymentControlDeleteSinglePayment tests that DeletePayment correctly
|
2021-09-13 12:49:50 +02:00
|
|
|
// deletes information about a completed payment from the database.
|
|
|
|
func TestPaymentControlDeleteSinglePayment(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2021-09-13 12:49:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
pControl := NewPaymentControl(db)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Register four payments:
|
|
|
|
// All payments will have one failed HTLC attempt and one HTLC attempt
|
|
|
|
// according to its final status.
|
|
|
|
// 1. A payment with two failed attempts.
|
|
|
|
// 2. Another payment with two failed attempts.
|
2022-06-12 06:18:07 +02:00
|
|
|
// 3. A payment with one failed and one settled attempt.
|
2021-09-13 12:49:50 +02:00
|
|
|
// 4. A payment with one failed and one in-flight attempt.
|
|
|
|
|
|
|
|
// Initiate payments, which is a slice of payment that is used as
|
|
|
|
// template to create the corresponding test payments in the database.
|
|
|
|
//
|
|
|
|
// Note: The payment id and number of htlc attempts of each payment will
|
|
|
|
// be added to this slice when creating the payments below.
|
|
|
|
// This allows the slice to be used directly for testing purposes.
|
|
|
|
payments := []*payment{
|
|
|
|
{status: StatusFailed},
|
|
|
|
{status: StatusFailed},
|
|
|
|
{status: StatusSucceeded},
|
|
|
|
{status: StatusInFlight},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use helper function to register the test payments in the data and
|
|
|
|
// populate the data to the payments slice.
|
|
|
|
createTestPayments(t, pControl, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Check that all payments are there as we added them.
|
|
|
|
assertPayments(t, db, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Delete HTLC attempts for first payment only.
|
|
|
|
require.NoError(t, db.DeletePayment(payments[0].id, true))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The first payment is the only altered one as its failed HTLC should
|
|
|
|
// have been removed but is still present as payment.
|
|
|
|
payments[0].htlcs = 0
|
|
|
|
assertPayments(t, db, payments)
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Delete the first payment completely.
|
|
|
|
require.NoError(t, db.DeletePayment(payments[0].id, false))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The first payment should have been deleted.
|
|
|
|
assertPayments(t, db, payments[1:])
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Now delete the second payment completely.
|
|
|
|
require.NoError(t, db.DeletePayment(payments[1].id, false))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The Second payment should have been deleted.
|
|
|
|
assertPayments(t, db, payments[2:])
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Delete failed HTLC attempts for the third payment.
|
|
|
|
require.NoError(t, db.DeletePayment(payments[2].id, true))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Only the successful HTLC attempt should be left for the third payment.
|
|
|
|
payments[2].htlcs = 1
|
|
|
|
assertPayments(t, db, payments[2:])
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Now delete the third payment completely.
|
|
|
|
require.NoError(t, db.DeletePayment(payments[2].id, false))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Only the last payment should be left.
|
|
|
|
assertPayments(t, db, payments[3:])
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// Deleting HTLC attempts from InFlight payments should not work and an
|
|
|
|
// error returned.
|
|
|
|
require.Error(t, db.DeletePayment(payments[3].id, true))
|
2021-01-22 15:47:55 +01:00
|
|
|
|
2021-09-13 12:49:50 +02:00
|
|
|
// The payment is InFlight and therefore should not have been altered.
|
|
|
|
assertPayments(t, db, payments[3:])
|
|
|
|
|
|
|
|
// Finally deleting the InFlight payment should also not work and an
|
|
|
|
// error returned.
|
|
|
|
require.Error(t, db.DeletePayment(payments[3].id, false))
|
|
|
|
|
|
|
|
// The payment is InFlight and therefore should not have been altered.
|
|
|
|
assertPayments(t, db, payments[3:])
|
2021-01-22 15:47:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
// TestPaymentControlMultiShard checks the ability of payment control to
|
|
|
|
// have multiple in-flight HTLCs for a single payment.
|
|
|
|
func TestPaymentControlMultiShard(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// We will register three HTLC attempts, and always fail the second
|
|
|
|
// one. We'll generate all combinations of settling/failing the first
|
|
|
|
// and third HTLC, and assert that the payment status end up as we
|
|
|
|
// expect.
|
|
|
|
type testCase struct {
|
|
|
|
settleFirst bool
|
|
|
|
settleLast bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests []testCase
|
|
|
|
for _, f := range []bool{true, false} {
|
|
|
|
for _, l := range []bool{true, false} {
|
|
|
|
tests = append(tests, testCase{f, l})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runSubTest := func(t *testing.T, test testCase) {
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2020-04-01 00:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
info, attempt, preimg, err := genInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the payment, moving it to the StatusInFlight state.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2020-04-01 00:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentIdentifier)
|
2022-11-24 10:40:49 +01:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, StatusInitiated,
|
|
|
|
)
|
2020-04-01 00:13:25 +02:00
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, nil,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create three unique attempts we'll use for the test, and
|
2020-04-01 00:13:27 +02:00
|
|
|
// register them with the payment control. We set each
|
|
|
|
// attempts's value to one third of the payment amount, and
|
|
|
|
// populate the MPP options.
|
|
|
|
shardAmt := info.Value / 3
|
|
|
|
attempt.Route.FinalHop().AmtToForward = shardAmt
|
|
|
|
attempt.Route.FinalHop().MPP = record.NewMPP(
|
|
|
|
info.Value, [32]byte{1},
|
|
|
|
)
|
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
var attempts []*HTLCAttemptInfo
|
|
|
|
for i := uint64(0); i < 3; i++ {
|
|
|
|
a := *attempt
|
|
|
|
a.AttemptID = i
|
|
|
|
attempts = append(attempts, &a)
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &a)
|
2020-04-01 00:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
assertPaymentStatus(
|
2022-11-24 10:40:49 +01:00
|
|
|
t, pControl, info.PaymentIdentifier,
|
|
|
|
StatusInFlight,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: &a,
|
|
|
|
}
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:27 +02:00
|
|
|
// For a fourth attempt, check that attempting to
|
|
|
|
// register it will fail since the total sent amount
|
|
|
|
// will be too large.
|
|
|
|
b := *attempt
|
|
|
|
b.AttemptID = 3
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != ErrValueExceedsAmt {
|
|
|
|
t.Fatalf("expected ErrValueExceedsAmt, got: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
// Fail the second attempt.
|
|
|
|
a := attempts[1]
|
|
|
|
htlcFail := HTLCFailUnreadable
|
2020-04-06 09:26:52 +02:00
|
|
|
_, err = pControl.FailAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, a.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFail,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: a,
|
|
|
|
failure: &htlcFail,
|
|
|
|
}
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Payment should still be in-flight.
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
// Depending on the test case, settle or fail the first attempt.
|
|
|
|
a = attempts[0]
|
|
|
|
htlc = &htlcStatus{
|
|
|
|
HTLCAttemptInfo: a,
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstFailReason *FailureReason
|
|
|
|
if test.settleFirst {
|
|
|
|
_, err := pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, a.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been "+
|
|
|
|
"received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the HTLC has had the preimage recorded.
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
} else {
|
2020-04-06 09:26:52 +02:00
|
|
|
_, err := pControl.FailAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, a.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFail,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been "+
|
|
|
|
"received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the failure was recorded.
|
|
|
|
htlc.failure = &htlcFail
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info, nil, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// We also record a payment level fail, to move it into
|
|
|
|
// a terminal state.
|
|
|
|
failReason := FailureReasonNoRoute
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.Fail(info.PaymentIdentifier, failReason)
|
2020-04-01 00:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the reason we failed the payment, such that
|
|
|
|
// we can assert this later in the test.
|
|
|
|
firstFailReason = &failReason
|
|
|
|
|
2022-11-24 10:40:49 +01:00
|
|
|
// The payment is now considered pending fail, since
|
|
|
|
// there is still an active HTLC.
|
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier,
|
|
|
|
StatusInFlight,
|
|
|
|
)
|
|
|
|
}
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
// Try to register yet another attempt. This should fail now
|
|
|
|
// that the payment has reached a terminal condition.
|
2020-04-01 00:13:27 +02:00
|
|
|
b = *attempt
|
2020-04-01 00:13:25 +02:00
|
|
|
b.AttemptID = 3
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2023-05-26 00:50:19 +02:00
|
|
|
if test.settleFirst {
|
|
|
|
require.ErrorIs(t, err, ErrPaymentPendingSettled)
|
|
|
|
} else {
|
|
|
|
require.ErrorIs(t, err, ErrPaymentPendingFailed)
|
2020-04-01 00:13:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
// Settle or fail the remaining attempt based on the testcase.
|
|
|
|
a = attempts[2]
|
|
|
|
htlc = &htlcStatus{
|
|
|
|
HTLCAttemptInfo: a,
|
|
|
|
}
|
|
|
|
if test.settleLast {
|
|
|
|
// Settle the last outstanding attempt.
|
|
|
|
_, err = pControl.SettleAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, a.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2022-11-24 10:40:49 +01:00
|
|
|
require.NoError(t, err, "unable to settle")
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(
|
2022-11-24 10:40:49 +01:00
|
|
|
t, pControl, info.PaymentIdentifier,
|
|
|
|
info, firstFailReason, htlc,
|
2020-04-01 00:13:25 +02:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Fail the attempt.
|
2020-04-06 09:26:52 +02:00
|
|
|
_, err := pControl.FailAttempt(
|
2021-03-31 12:23:08 +02:00
|
|
|
info.PaymentIdentifier, a.AttemptID,
|
2020-04-01 00:13:25 +02:00
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFail,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been "+
|
|
|
|
"received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the failure was recorded.
|
|
|
|
htlc.failure = &htlcFail
|
|
|
|
assertPaymentInfo(
|
2021-03-31 12:23:08 +02:00
|
|
|
t, pControl, info.PaymentIdentifier, info,
|
2020-04-01 00:13:25 +02:00
|
|
|
firstFailReason, htlc,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check that we can override any perevious terminal
|
|
|
|
// failure. This is to allow multiple concurrent shard
|
|
|
|
// write a terminal failure to the database without
|
|
|
|
// syncing.
|
|
|
|
failReason := FailureReasonPaymentDetails
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.Fail(info.PaymentIdentifier, failReason)
|
2022-11-24 10:40:49 +01:00
|
|
|
require.NoError(t, err, "unable to fail")
|
2020-04-01 00:13:25 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 00:50:19 +02:00
|
|
|
var (
|
|
|
|
finalStatus PaymentStatus
|
|
|
|
registerErr error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// If one of the attempts settled but the other failed with
|
|
|
|
// terminal error, we would still consider the payment is
|
|
|
|
// settled.
|
|
|
|
case test.settleFirst && !test.settleLast:
|
|
|
|
finalStatus = StatusSucceeded
|
|
|
|
registerErr = ErrPaymentAlreadySucceeded
|
|
|
|
|
|
|
|
case !test.settleFirst && test.settleLast:
|
2020-04-01 00:13:25 +02:00
|
|
|
finalStatus = StatusSucceeded
|
2023-05-26 00:50:19 +02:00
|
|
|
registerErr = ErrPaymentAlreadySucceeded
|
|
|
|
|
|
|
|
// If both failed, we end up in a failed status.
|
|
|
|
case !test.settleFirst && !test.settleLast:
|
|
|
|
finalStatus = StatusFailed
|
|
|
|
registerErr = ErrPaymentAlreadyFailed
|
|
|
|
|
|
|
|
// Otherwise, the payment has a succeed status.
|
|
|
|
case test.settleFirst && test.settleLast:
|
|
|
|
finalStatus = StatusSucceeded
|
|
|
|
registerErr = ErrPaymentAlreadySucceeded
|
2020-04-01 00:13:25 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 00:50:19 +02:00
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentIdentifier, finalStatus,
|
|
|
|
)
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
// Finally assert we cannot register more attempts.
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2023-05-26 00:50:19 +02:00
|
|
|
require.Equal(t, registerErr, err)
|
2020-04-01 00:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
subTest := fmt.Sprintf("first=%v, second=%v",
|
|
|
|
test.settleFirst, test.settleLast)
|
|
|
|
|
|
|
|
t.Run(subTest, func(t *testing.T) {
|
|
|
|
runSubTest(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:27 +02:00
|
|
|
func TestPaymentControlMPPRecordValidation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to init db")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
info, attempt, _, err := genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
// Init the payment.
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
// Create three unique attempts we'll use for the test, and
|
|
|
|
// register them with the payment control. We set each
|
|
|
|
// attempts's value to one third of the payment amount, and
|
|
|
|
// populate the MPP options.
|
|
|
|
shardAmt := info.Value / 3
|
|
|
|
attempt.Route.FinalHop().AmtToForward = shardAmt
|
|
|
|
attempt.Route.FinalHop().MPP = record.NewMPP(
|
|
|
|
info.Value, [32]byte{1},
|
|
|
|
)
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
// Now try to register a non-MPP attempt, which should fail.
|
|
|
|
b := *attempt
|
|
|
|
b.AttemptID = 1
|
|
|
|
b.Route.FinalHop().MPP = nil
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != ErrMPPayment {
|
|
|
|
t.Fatalf("expected ErrMPPayment, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to register attempt one with a different payment address.
|
|
|
|
b.Route.FinalHop().MPP = record.NewMPP(
|
|
|
|
info.Value, [32]byte{2},
|
|
|
|
)
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != ErrMPPPaymentAddrMismatch {
|
|
|
|
t.Fatalf("expected ErrMPPPaymentAddrMismatch, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try registering one with a different total amount.
|
|
|
|
b.Route.FinalHop().MPP = record.NewMPP(
|
|
|
|
info.Value/2, [32]byte{1},
|
|
|
|
)
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != ErrMPPTotalAmountMismatch {
|
|
|
|
t.Fatalf("expected ErrMPPTotalAmountMismatch, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and init a new payment. This time we'll check that we cannot
|
|
|
|
// register an MPP attempt if we already registered a non-MPP one.
|
|
|
|
info, attempt, _, err = genInfo()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
attempt.Route.FinalHop().MPP = nil
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
2020-04-01 00:13:27 +02:00
|
|
|
|
|
|
|
// Attempt to register an MPP attempt, which should fail.
|
|
|
|
b = *attempt
|
|
|
|
b.AttemptID = 1
|
|
|
|
b.Route.FinalHop().MPP = record.NewMPP(
|
|
|
|
info.Value, [32]byte{1},
|
|
|
|
)
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != ErrNonMPPayment {
|
|
|
|
t.Fatalf("expected ErrNonMPPayment, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 06:18:07 +02:00
|
|
|
// TestDeleteFailedAttempts checks that DeleteFailedAttempts properly removes
|
|
|
|
// failed HTLCs from finished payments.
|
|
|
|
func TestDeleteFailedAttempts(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("keep failed payment attempts", func(t *testing.T) {
|
|
|
|
testDeleteFailedAttempts(t, true)
|
|
|
|
})
|
|
|
|
t.Run("remove failed payment attempts", func(t *testing.T) {
|
|
|
|
testDeleteFailedAttempts(t, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-06-12 06:18:07 +02:00
|
|
|
|
|
|
|
require.NoError(t, err, "unable to init db")
|
|
|
|
db.keepFailedPaymentAttempts = keepFailedPaymentAttempts
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
// Register three payments:
|
|
|
|
// All payments will have one failed HTLC attempt and one HTLC attempt
|
|
|
|
// according to its final status.
|
|
|
|
// 1. A payment with two failed attempts.
|
|
|
|
// 2. A payment with one failed and one in-flight attempt.
|
|
|
|
// 3. A payment with one failed and one settled attempt.
|
|
|
|
|
|
|
|
// Initiate payments, which is a slice of payment that is used as
|
|
|
|
// template to create the corresponding test payments in the database.
|
|
|
|
//
|
|
|
|
// Note: The payment id and number of htlc attempts of each payment will
|
|
|
|
// be added to this slice when creating the payments below.
|
|
|
|
// This allows the slice to be used directly for testing purposes.
|
|
|
|
payments := []*payment{
|
|
|
|
{status: StatusFailed},
|
|
|
|
{status: StatusInFlight},
|
|
|
|
{status: StatusSucceeded},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use helper function to register the test payments in the data and
|
|
|
|
// populate the data to the payments slice.
|
|
|
|
createTestPayments(t, pControl, payments)
|
|
|
|
|
|
|
|
// Check that all payments are there as we added them.
|
|
|
|
assertPayments(t, db, payments)
|
|
|
|
|
|
|
|
// Calling DeleteFailedAttempts on a failed payment should delete all
|
|
|
|
// HTLCs.
|
|
|
|
require.NoError(t, pControl.DeleteFailedAttempts(payments[0].id))
|
|
|
|
|
|
|
|
// Expect all HTLCs to be deleted if the config is set to delete them.
|
|
|
|
if !keepFailedPaymentAttempts {
|
|
|
|
payments[0].htlcs = 0
|
|
|
|
}
|
|
|
|
assertPayments(t, db, payments)
|
|
|
|
|
|
|
|
// Calling DeleteFailedAttempts on an in-flight payment should return
|
|
|
|
// an error.
|
|
|
|
if keepFailedPaymentAttempts {
|
|
|
|
require.NoError(t, pControl.DeleteFailedAttempts(payments[1].id))
|
|
|
|
} else {
|
|
|
|
require.Error(t, pControl.DeleteFailedAttempts(payments[1].id))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since DeleteFailedAttempts returned an error, we should expect the
|
|
|
|
// payment to be unchanged.
|
|
|
|
assertPayments(t, db, payments)
|
|
|
|
|
|
|
|
// Cleaning up a successful payment should remove failed htlcs.
|
|
|
|
require.NoError(t, pControl.DeleteFailedAttempts(payments[2].id))
|
|
|
|
// Expect all HTLCs except for the settled one to be deleted if the
|
|
|
|
// config is set to delete them.
|
|
|
|
if !keepFailedPaymentAttempts {
|
|
|
|
payments[2].htlcs = 1
|
|
|
|
}
|
|
|
|
assertPayments(t, db, payments)
|
|
|
|
|
|
|
|
if keepFailedPaymentAttempts {
|
|
|
|
// DeleteFailedAttempts is ignored, even for non-existent
|
|
|
|
// payments, if the control tower is configured to keep failed
|
|
|
|
// HTLCs.
|
|
|
|
require.NoError(t, pControl.DeleteFailedAttempts(lntypes.ZeroHash))
|
|
|
|
} else {
|
|
|
|
// Attempting to cleanup a non-existent payment returns an error.
|
|
|
|
require.Error(t, pControl.DeleteFailedAttempts(lntypes.ZeroHash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
// assertPaymentStatus retrieves the status of the payment referred to by hash
|
|
|
|
// and compares it with the expected state.
|
|
|
|
func assertPaymentStatus(t *testing.T, p *PaymentControl,
|
|
|
|
hash lntypes.Hash, expStatus PaymentStatus) {
|
2018-08-14 03:47:19 +02:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
payment, err := p.FetchPayment(hash)
|
2022-11-24 10:40:49 +01:00
|
|
|
if errors.Is(err, ErrPaymentNotInitiated) {
|
2020-02-20 14:43:25 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-14 03:47:19 +02:00
|
|
|
if err != nil {
|
2020-02-20 14:43:25 +01:00
|
|
|
t.Fatal(err)
|
2018-08-14 03:47:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
if payment.Status != expStatus {
|
2018-08-14 03:47:19 +02:00
|
|
|
t.Fatalf("payment status mismatch: expected %v, got %v",
|
2020-02-20 14:43:25 +01:00
|
|
|
expStatus, payment.Status)
|
2018-08-12 15:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
type htlcStatus struct {
|
|
|
|
*HTLCAttemptInfo
|
|
|
|
settle *lntypes.Preimage
|
|
|
|
failure *HTLCFailReason
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
// assertPaymentInfo retrieves the payment referred to by hash and verifies the
|
|
|
|
// expected values.
|
|
|
|
func assertPaymentInfo(t *testing.T, p *PaymentControl, hash lntypes.Hash,
|
2020-04-01 00:13:25 +02:00
|
|
|
c *PaymentCreationInfo, f *FailureReason, a *htlcStatus) {
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
t.Helper()
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
payment, err := p.FetchPayment(hash)
|
2019-05-23 20:05:28 +02:00
|
|
|
if err != nil {
|
2020-02-20 14:43:25 +01:00
|
|
|
t.Fatal(err)
|
2019-05-23 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
if !reflect.DeepEqual(payment.Info, c) {
|
|
|
|
t.Fatalf("PaymentCreationInfos don't match: %v vs %v",
|
|
|
|
spew.Sdump(payment.Info), spew.Sdump(c))
|
2019-05-23 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
if f != nil {
|
|
|
|
if *payment.FailureReason != *f {
|
|
|
|
t.Fatal("unexpected failure reason")
|
2019-05-23 20:05:28 +02:00
|
|
|
}
|
2020-02-20 14:43:25 +01:00
|
|
|
} else {
|
|
|
|
if payment.FailureReason != nil {
|
|
|
|
t.Fatal("unexpected failure reason")
|
2019-05-23 20:05:28 +02:00
|
|
|
}
|
2020-02-20 14:43:25 +01:00
|
|
|
}
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-02-20 14:43:25 +01:00
|
|
|
if a == nil {
|
|
|
|
if len(payment.HTLCs) > 0 {
|
|
|
|
t.Fatal("expected no htlcs")
|
2019-05-23 20:05:28 +02:00
|
|
|
}
|
2020-02-20 14:43:25 +01:00
|
|
|
return
|
|
|
|
}
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
htlc := payment.HTLCs[a.AttemptID]
|
2020-02-20 14:43:25 +01:00
|
|
|
if err := assertRouteEqual(&htlc.Route, &a.Route); err != nil {
|
|
|
|
t.Fatal("routes do not match")
|
|
|
|
}
|
2019-05-23 20:05:28 +02:00
|
|
|
|
2020-04-01 00:13:25 +02:00
|
|
|
if htlc.AttemptID != a.AttemptID {
|
|
|
|
t.Fatalf("unnexpected attempt ID %v, expected %v",
|
|
|
|
htlc.AttemptID, a.AttemptID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.failure != nil {
|
|
|
|
if htlc.Failure == nil {
|
|
|
|
t.Fatalf("expected HTLC to be failed")
|
2019-05-23 20:05:28 +02:00
|
|
|
}
|
2020-04-01 00:13:25 +02:00
|
|
|
|
|
|
|
if htlc.Failure.Reason != *a.failure {
|
|
|
|
t.Fatalf("expected HTLC failure %v, had %v",
|
|
|
|
*a.failure, htlc.Failure.Reason)
|
|
|
|
}
|
|
|
|
} else if htlc.Failure != nil {
|
|
|
|
t.Fatalf("expected no HTLC failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.settle != nil {
|
|
|
|
if htlc.Settle.Preimage != *a.settle {
|
|
|
|
t.Fatalf("Preimages don't match: %x vs %x",
|
|
|
|
htlc.Settle.Preimage, a.settle)
|
2019-05-23 20:05:30 +02:00
|
|
|
}
|
2020-04-01 00:13:25 +02:00
|
|
|
} else if htlc.Settle != nil {
|
|
|
|
t.Fatal("expected no settle info")
|
2019-05-23 20:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 12:34:27 +02:00
|
|
|
|
|
|
|
// fetchPaymentIndexEntry gets the payment hash for the sequence number provided
|
|
|
|
// from our payment indexes bucket.
|
|
|
|
func fetchPaymentIndexEntry(_ *testing.T, p *PaymentControl,
|
|
|
|
sequenceNumber uint64) (*lntypes.Hash, error) {
|
|
|
|
|
|
|
|
var hash lntypes.Hash
|
|
|
|
|
|
|
|
if err := kvdb.View(p.db, func(tx walletdb.ReadTx) error {
|
|
|
|
indexBucket := tx.ReadBucket(paymentsIndexBucket)
|
|
|
|
key := make([]byte, 8)
|
|
|
|
byteOrder.PutUint64(key, sequenceNumber)
|
|
|
|
|
|
|
|
indexValue := indexBucket.Get(key)
|
|
|
|
if indexValue == nil {
|
|
|
|
return errNoSequenceNrIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(indexValue)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
hash, err = deserializePaymentIndex(r)
|
|
|
|
return err
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {
|
|
|
|
hash = lntypes.Hash{}
|
2020-06-10 12:34:27 +02:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertPaymentIndex looks up the index for a payment in the db and checks
|
|
|
|
// that its payment hash matches the expected hash passed in.
|
|
|
|
func assertPaymentIndex(t *testing.T, p *PaymentControl,
|
|
|
|
expectedHash lntypes.Hash) {
|
|
|
|
|
|
|
|
// Lookup the payment so that we have its sequence number and check
|
|
|
|
// that is has correctly been indexed in the payment indexes bucket.
|
|
|
|
pmt, err := p.FetchPayment(expectedHash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
hash, err := fetchPaymentIndexEntry(t, p, pmt.SequenceNum)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedHash, *hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertNoIndex checks that an index for the sequence number provided does not
|
|
|
|
// exist.
|
|
|
|
func assertNoIndex(t *testing.T, p *PaymentControl, seqNr uint64) {
|
|
|
|
_, err := fetchPaymentIndexEntry(t, p, seqNr)
|
|
|
|
require.Equal(t, errNoSequenceNrIndex, err)
|
|
|
|
}
|
2021-09-13 12:49:50 +02:00
|
|
|
|
|
|
|
// payment is a helper structure that holds basic information on a test payment,
|
|
|
|
// such as the payment id, the status and the total number of HTLCs attempted.
|
|
|
|
type payment struct {
|
|
|
|
id lntypes.Hash
|
|
|
|
status PaymentStatus
|
|
|
|
htlcs int
|
|
|
|
}
|
|
|
|
|
|
|
|
// createTestPayments registers payments depending on the provided statuses in
|
|
|
|
// the payments slice. Each payment will receive one failed HTLC and another
|
|
|
|
// HTLC depending on the final status of the payment provided.
|
|
|
|
func createTestPayments(t *testing.T, p *PaymentControl, payments []*payment) {
|
|
|
|
attemptID := uint64(0)
|
|
|
|
|
|
|
|
for i := 0; i < len(payments); i++ {
|
|
|
|
info, attempt, preimg, err := genInfo()
|
|
|
|
require.NoError(t, err, "unable to generate htlc message")
|
|
|
|
|
|
|
|
// Set the payment id accordingly in the payments slice.
|
|
|
|
payments[i].id = info.PaymentIdentifier
|
|
|
|
|
|
|
|
attempt.AttemptID = attemptID
|
|
|
|
attemptID++
|
|
|
|
|
|
|
|
// Init the payment.
|
|
|
|
err = p.InitPayment(info.PaymentIdentifier, info)
|
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
|
|
|
|
|
|
|
// Register and fail the first attempt for all payments.
|
|
|
|
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
|
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
|
|
|
|
|
|
|
htlcFailure := HTLCFailUnreadable
|
|
|
|
_, err = p.FailAttempt(
|
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFailure,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err, "unable to fail htlc")
|
|
|
|
|
|
|
|
// Increase the HTLC counter in the payments slice for the
|
|
|
|
// failed attempt.
|
|
|
|
payments[i].htlcs++
|
|
|
|
|
|
|
|
// Depending on the test case, fail or succeed the next
|
|
|
|
// attempt.
|
|
|
|
attempt.AttemptID = attemptID
|
|
|
|
attemptID++
|
|
|
|
|
|
|
|
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
|
|
|
|
require.NoError(t, err, "unable to send htlc message")
|
|
|
|
|
|
|
|
switch payments[i].status {
|
|
|
|
// Fail the attempt and the payment overall.
|
|
|
|
case StatusFailed:
|
|
|
|
htlcFailure := HTLCFailUnreadable
|
|
|
|
_, err = p.FailAttempt(
|
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFailure,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err, "unable to fail htlc")
|
|
|
|
|
|
|
|
failReason := FailureReasonNoRoute
|
|
|
|
_, err = p.Fail(info.PaymentIdentifier,
|
|
|
|
failReason)
|
|
|
|
require.NoError(t, err, "unable to fail payment hash")
|
|
|
|
|
|
|
|
// Settle the attempt
|
|
|
|
case StatusSucceeded:
|
|
|
|
_, err := p.SettleAttempt(
|
|
|
|
info.PaymentIdentifier, attempt.AttemptID,
|
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err, "no error should have been "+
|
|
|
|
"received from settling a htlc attempt")
|
|
|
|
|
|
|
|
// We leave the attempt in-flight by doing nothing.
|
|
|
|
case StatusInFlight:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the HTLC counter in the payments slice for any
|
|
|
|
// attempt above.
|
|
|
|
payments[i].htlcs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertPayments is a helper function that given a slice of payment and
|
|
|
|
// indices for the slice asserts that exactly the same payments in the
|
|
|
|
// slice for the provided indices exist when fetching payments from the
|
|
|
|
// database.
|
|
|
|
func assertPayments(t *testing.T, db *DB, payments []*payment) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
dbPayments, err := db.FetchPayments()
|
|
|
|
require.NoError(t, err, "could not fetch payments from db")
|
|
|
|
|
|
|
|
// Make sure that the number of fetched payments is the same
|
|
|
|
// as expected.
|
|
|
|
require.Len(t, dbPayments, len(payments), "unexpected number of payments")
|
|
|
|
|
|
|
|
// Convert fetched payments of type MPPayment to our helper structure.
|
|
|
|
p := make([]*payment, len(dbPayments))
|
|
|
|
for i, dbPayment := range dbPayments {
|
|
|
|
p[i] = &payment{
|
|
|
|
id: dbPayment.Info.PaymentIdentifier,
|
|
|
|
status: dbPayment.Status,
|
|
|
|
htlcs: len(dbPayment.HTLCs),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that each payment we want to assert exists in the database.
|
|
|
|
require.Equal(t, payments, p)
|
|
|
|
}
|