2019-05-23 21:05:26 +03:00
|
|
|
package channeldb
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
import (
|
2020-06-10 13:34:27 +03:00
|
|
|
"bytes"
|
2019-05-23 21:05:26 +03:00
|
|
|
"crypto/rand"
|
2020-05-20 12:07:38 +03:00
|
|
|
"crypto/sha256"
|
2018-08-12 16:19:12 +03:00
|
|
|
"fmt"
|
2019-05-23 21:05:26 +03:00
|
|
|
"io"
|
2019-05-23 21:05:28 +03:00
|
|
|
"reflect"
|
2018-08-12 16:19:12 +03:00
|
|
|
"testing"
|
2019-05-23 21:05:28 +03:00
|
|
|
"time"
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2019-05-23 21:05:28 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-06-10 13:34:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-05-23 21:05:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-04-01 01:13:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2020-06-10 13:34:27 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-12 16:19:12 +03:00
|
|
|
)
|
|
|
|
|
2019-05-23 21:05:26 +03: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 12:31:27 +03:00
|
|
|
func genInfo() (*PaymentCreationInfo, *HTLCAttemptInfo,
|
2019-05-23 21:05:28 +03:00
|
|
|
lntypes.Preimage, error) {
|
|
|
|
|
2018-08-12 16:19:12 +03:00
|
|
|
preimage, err := genPreimage()
|
|
|
|
if err != nil {
|
2019-05-23 21:05:28 +03:00
|
|
|
return nil, nil, preimage, fmt.Errorf("unable to "+
|
|
|
|
"generate preimage: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:07:38 +03:00
|
|
|
rhash := sha256.Sum256(preimage[:])
|
2019-05-23 21:05:28 +03:00
|
|
|
return &PaymentCreationInfo{
|
|
|
|
PaymentHash: rhash,
|
2020-04-01 01:13:27 +03:00
|
|
|
Value: testRoute.ReceiverAmt(),
|
2020-02-19 11:53:13 +03:00
|
|
|
CreationTime: time.Unix(time.Now().Unix(), 0),
|
2019-05-23 21:05:28 +03:00
|
|
|
PaymentRequest: []byte("hola"),
|
|
|
|
},
|
2020-02-07 12:31:27 +03:00
|
|
|
&HTLCAttemptInfo{
|
2020-04-01 01:13:25 +03:00
|
|
|
AttemptID: 0,
|
2019-05-23 21:05:28 +03:00
|
|
|
SessionKey: priv,
|
2020-04-01 01:13:27 +03:00
|
|
|
Route: *testRoute.Copy(),
|
2019-05-23 21:05:28 +03:00
|
|
|
}, preimage, nil
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// TestPaymentControlSwitchFail checks that payment status returns to Failed
|
|
|
|
// status after failing, and that InitPayment allows another HTLC for the
|
2018-08-11 00:01:24 +03:00
|
|
|
// same payment hash.
|
2019-05-23 21:05:28 +03:00
|
|
|
func TestPaymentControlSwitchFail(t *testing.T) {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
info, attempt, preimg, err := genInfo()
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentHash)
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// Fail the payment, which should moved it to Failed.
|
2019-05-23 21:05:30 +03:00
|
|
|
failReason := FailureReasonNoRoute
|
2019-06-04 18:18:41 +03:00
|
|
|
_, err = pControl.Fail(info.PaymentHash, failReason)
|
2019-05-23 21:05:30 +03:00
|
|
|
if err != nil {
|
2018-08-11 00:01:24 +03:00
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// Verify the status is indeed Failed.
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusFailed)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, &failReason, nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// Lookup the payment so we can get its old sequence number before it is
|
|
|
|
// overwritten.
|
|
|
|
payment, err := pControl.FetchPayment(info.PaymentHash)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Sends the htlc again, which should succeed since the prior payment
|
|
|
|
// failed.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// Check that our index has been updated, and the old index has been
|
|
|
|
// removed.
|
|
|
|
assertPaymentIndex(t, pControl, info.PaymentHash)
|
|
|
|
assertNoIndex(t, pControl, payment.SequenceNum)
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
|
|
|
|
2020-02-20 16:56:24 +03: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.
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2020-02-20 16:56:24 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to register attempt: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
htlcReason := HTLCFailUnreadable
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.FailAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, attempt.AttemptID,
|
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcReason,
|
2020-02-20 20:08:01 +03:00
|
|
|
},
|
2020-02-20 20:08:01 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-01 01:13:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
failure: &htlcReason,
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPaymentInfo(t, pControl, info.PaymentHash, info, nil, htlc)
|
2020-02-20 20:08:01 +03:00
|
|
|
|
2020-02-20 16:56:24 +03:00
|
|
|
// Record another attempt.
|
2020-04-01 01:13:25 +03:00
|
|
|
attempt.AttemptID = 1
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2019-05-23 21:05:28 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc = &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// Settle the attempt and verify that status was changed to
|
|
|
|
// StatusSucceeded.
|
2020-02-20 20:08:01 +03:00
|
|
|
payment, err = pControl.SettleAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, attempt.AttemptID,
|
2020-02-20 20:08:01 +03:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2019-04-30 14:24:37 +03:00
|
|
|
if err != nil {
|
2018-08-11 00:01:24 +03:00
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
2019-07-31 07:44:50 +03:00
|
|
|
|
2020-02-20 20:08:01 +03:00
|
|
|
if len(payment.HTLCs) != 2 {
|
|
|
|
t.Fatalf("payment should have two htlcs, got: %d",
|
2019-11-08 14:39:51 +03:00
|
|
|
len(payment.HTLCs))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = assertRouteEqual(&payment.HTLCs[0].Route, &attempt.Route)
|
2019-07-31 07:44:50 +03:00
|
|
|
if err != nil {
|
2019-09-10 16:34:02 +03:00
|
|
|
t.Fatalf("unexpected route returned: %v vs %v: %v",
|
2019-11-08 14:39:51 +03:00
|
|
|
spew.Sdump(attempt.Route),
|
|
|
|
spew.Sdump(payment.HTLCs[0].Route), err)
|
2019-04-30 14:24:37 +03:00
|
|
|
}
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
|
|
|
)
|
2018-08-11 00:01:24 +03:00
|
|
|
|
|
|
|
// Attempt a final payment, which should now fail since the prior
|
|
|
|
// payment succeed.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != ErrAlreadyPaid {
|
2018-08-11 00:01:24 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// TestPaymentControlSwitchDoubleSend checks the ability of payment control to
|
2018-08-11 00:01:24 +03:00
|
|
|
// prevent double sending of htlc message, when message is in StatusInFlight.
|
2019-05-23 21:05:28 +03:00
|
|
|
func TestPaymentControlSwitchDoubleSend(t *testing.T) {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
info, attempt, preimg, err := genInfo()
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Sends base htlc message which initiate base status and move it to
|
|
|
|
// StatusInFlight and verifies that it was changed.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentHash)
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2018-08-11 00:01:24 +03: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.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != ErrPaymentInFlight {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("payment control wrong behaviour: " +
|
|
|
|
"double sending must trigger ErrPaymentInFlight error")
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// Record an attempt.
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
2019-05-23 21:05:28 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
2019-05-23 21:05:28 +03:00
|
|
|
if err != ErrPaymentInFlight {
|
|
|
|
t.Fatalf("payment control wrong behaviour: " +
|
|
|
|
"double sending must trigger ErrPaymentInFlight error")
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// After settling, the error should be ErrAlreadyPaid.
|
2020-02-20 20:08:01 +03:00
|
|
|
_, err = pControl.SettleAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, attempt.AttemptID,
|
2020-02-20 20:08:01 +03:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(t, pControl, info.PaymentHash, info, nil, htlc)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != ErrAlreadyPaid {
|
2019-05-23 21:05:28 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// TestPaymentControlSuccessesWithoutInFlight checks that the payment
|
2018-08-14 04:47:19 +03:00
|
|
|
// control will disallow calls to Success when no payment is in flight.
|
2019-05-23 21:05:28 +03:00
|
|
|
func TestPaymentControlSuccessesWithoutInFlight(t *testing.T) {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
info, _, preimg, err := genInfo()
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// Attempt to complete the payment should fail.
|
2020-02-20 20:08:01 +03:00
|
|
|
_, err = pControl.SettleAttempt(
|
|
|
|
info.PaymentHash, 0,
|
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusUnknown)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// TestPaymentControlFailsWithoutInFlight checks that a strict payment
|
2018-08-14 04:47:19 +03:00
|
|
|
// control will disallow calls to Fail when no payment is in flight.
|
2019-05-23 21:05:28 +03:00
|
|
|
func TestPaymentControlFailsWithoutInFlight(t *testing.T) {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
pControl := NewPaymentControl(db)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
info, _, _, err := genInfo()
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
// Calling Fail should return an error.
|
2019-06-04 18:18:41 +03:00
|
|
|
_, err = pControl.Fail(info.PaymentHash, FailureReasonNoRoute)
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusUnknown)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// TestPaymentControlDeleteNonInFlight checks that calling DeletePayments only
|
2019-05-27 15:35:09 +03:00
|
|
|
// deletes payments from the database that are not in-flight.
|
|
|
|
func TestPaymentControlDeleteNonInFligt(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2019-05-27 15:35:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03: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 15:35:09 +03:00
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
payments := []struct {
|
2020-06-10 13:34:27 +03:00
|
|
|
failed bool
|
|
|
|
success bool
|
|
|
|
hasDuplicate bool
|
2019-05-27 15:35:09 +03:00
|
|
|
}{
|
|
|
|
{
|
2020-06-10 13:34:27 +03:00
|
|
|
failed: true,
|
|
|
|
success: false,
|
|
|
|
hasDuplicate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
failed: false,
|
|
|
|
success: true,
|
|
|
|
hasDuplicate: false,
|
2019-05-27 15:35:09 +03:00
|
|
|
},
|
|
|
|
{
|
2020-06-10 13:34:27 +03:00
|
|
|
failed: false,
|
|
|
|
success: false,
|
|
|
|
hasDuplicate: false,
|
2019-05-27 15:35:09 +03:00
|
|
|
},
|
|
|
|
{
|
2020-06-10 13:34:27 +03:00
|
|
|
failed: false,
|
|
|
|
success: true,
|
|
|
|
hasDuplicate: true,
|
2019-05-27 15:35:09 +03: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.
|
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2019-05-27 15:35:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: attempt,
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:35:09 +03:00
|
|
|
if p.failed {
|
2020-02-20 20:08:01 +03:00
|
|
|
// Fail the payment attempt.
|
2020-04-01 01:13:25 +03:00
|
|
|
htlcFailure := HTLCFailUnreadable
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err := pControl.FailAttempt(
|
2020-02-20 20:08:01 +03:00
|
|
|
info.PaymentHash, attempt.AttemptID,
|
2020-02-20 20:08:01 +03:00
|
|
|
&HTLCFailInfo{
|
2020-04-01 01:13:25 +03:00
|
|
|
Reason: htlcFailure,
|
2020-02-20 20:08:01 +03:00
|
|
|
},
|
2020-02-20 20:08:01 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:35:09 +03:00
|
|
|
// Fail the payment, which should moved it to Failed.
|
|
|
|
failReason := FailureReasonNoRoute
|
2019-06-04 18:18:41 +03:00
|
|
|
_, err = pControl.Fail(info.PaymentHash, failReason)
|
2019-05-27 15:35:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the status is indeed Failed.
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusFailed)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc.failure = &htlcFailure
|
2019-05-27 15:35:09 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info,
|
|
|
|
&failReason, htlc,
|
2019-05-27 15:35:09 +03:00
|
|
|
)
|
|
|
|
} else if p.success {
|
|
|
|
// Verifies that status was changed to StatusSucceeded.
|
2020-02-20 20:08:01 +03:00
|
|
|
_, err := pControl.SettleAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, attempt.AttemptID,
|
2020-02-20 20:08:01 +03:00
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
2019-05-27 15:35:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded)
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
htlc.settle = &preimg
|
2019-05-27 15:35:09 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
2019-05-27 15:35:09 +03:00
|
|
|
)
|
|
|
|
} else {
|
2020-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-27 15:35:09 +03:00
|
|
|
assertPaymentInfo(
|
2020-04-01 01:13:25 +03:00
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
2019-05-27 15:35:09 +03:00
|
|
|
)
|
|
|
|
}
|
2020-06-10 13:34:27 +03:00
|
|
|
|
|
|
|
// If the payment is intended to have a duplicate payment, we
|
|
|
|
// add one.
|
|
|
|
if p.hasDuplicate {
|
|
|
|
appendDuplicatePayment(
|
|
|
|
t, pControl.db, info.PaymentHash,
|
|
|
|
uint64(duplicateSeqNr),
|
|
|
|
)
|
|
|
|
duplicateSeqNr++
|
|
|
|
}
|
2019-05-27 15:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete payments.
|
|
|
|
if err := db.DeletePayments(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should leave the in-flight payment.
|
|
|
|
dbPayments, err := db.FetchPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbPayments) != 1 {
|
|
|
|
t.Fatalf("expected one payment, got %d", len(dbPayments))
|
|
|
|
}
|
|
|
|
|
|
|
|
status := dbPayments[0].Status
|
|
|
|
if status != StatusInFlight {
|
|
|
|
t.Fatalf("expected in-fligth status, got %v", status)
|
|
|
|
}
|
2020-06-10 13:34:27 +03: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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, indexCount)
|
2019-05-27 15:35:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03: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) {
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-04-01 01:13:25 +03: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.
|
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
assertPaymentIndex(t, pControl, info.PaymentHash)
|
2020-04-01 01:13:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create three unique attempts we'll use for the test, and
|
2020-04-01 01:13:27 +03: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 01:13:25 +03:00
|
|
|
var attempts []*HTLCAttemptInfo
|
|
|
|
for i := uint64(0); i < 3; i++ {
|
|
|
|
a := *attempt
|
|
|
|
a.AttemptID = i
|
|
|
|
attempts = append(attempts, &a)
|
|
|
|
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &a)
|
2020-04-01 01:13:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
assertPaymentStatus(
|
|
|
|
t, pControl, info.PaymentHash, StatusInFlight,
|
|
|
|
)
|
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: &a,
|
|
|
|
}
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:27 +03: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
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:27 +03:00
|
|
|
if err != ErrValueExceedsAmt {
|
|
|
|
t.Fatalf("expected ErrValueExceedsAmt, got: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// Fail the second attempt.
|
|
|
|
a := attempts[1]
|
|
|
|
htlcFail := HTLCFailUnreadable
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.FailAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, a.AttemptID,
|
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: htlcFail,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
htlc := &htlcStatus{
|
|
|
|
HTLCAttemptInfo: a,
|
|
|
|
failure: &htlcFail,
|
|
|
|
}
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Payment should still be in-flight.
|
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
info.PaymentHash, a.AttemptID,
|
|
|
|
&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(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
|
|
|
)
|
|
|
|
} else {
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err := pControl.FailAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, a.AttemptID,
|
|
|
|
&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(
|
|
|
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
|
|
|
)
|
|
|
|
|
|
|
|
// We also record a payment level fail, to move it into
|
|
|
|
// a terminal state.
|
|
|
|
failReason := FailureReasonNoRoute
|
|
|
|
_, err = pControl.Fail(info.PaymentHash, failReason)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payment should still be considered in-flight, since there
|
|
|
|
// is still an active HTLC.
|
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
|
|
|
|
|
|
|
// Try to register yet another attempt. This should fail now
|
|
|
|
// that the payment has reached a terminal condition.
|
2020-04-01 01:13:27 +03:00
|
|
|
b = *attempt
|
2020-04-01 01:13:25 +03:00
|
|
|
b.AttemptID = 3
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:25 +03:00
|
|
|
if err != ErrPaymentTerminal {
|
|
|
|
t.Fatalf("expected ErrPaymentTerminal, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
info.PaymentHash, a.AttemptID,
|
|
|
|
&HTLCSettleInfo{
|
|
|
|
Preimage: preimg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been "+
|
|
|
|
"received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
htlc.settle = &preimg
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, pControl, info.PaymentHash, info,
|
|
|
|
firstFailReason, htlc,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Fail the attempt.
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err := pControl.FailAttempt(
|
2020-04-01 01:13:25 +03:00
|
|
|
info.PaymentHash, a.AttemptID,
|
|
|
|
&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(
|
|
|
|
t, pControl, info.PaymentHash, info,
|
|
|
|
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
|
|
|
|
_, err = pControl.Fail(info.PaymentHash, failReason)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the two attempts settled, the payment should end
|
|
|
|
// up in the Succeeded state. If both failed the payment should
|
|
|
|
// also be Failed at this poinnt.
|
|
|
|
finalStatus := StatusFailed
|
|
|
|
expRegErr := ErrPaymentAlreadyFailed
|
|
|
|
if test.settleFirst || test.settleLast {
|
|
|
|
finalStatus = StatusSucceeded
|
|
|
|
expRegErr = ErrPaymentAlreadySucceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, finalStatus)
|
|
|
|
|
|
|
|
// Finally assert we cannot register more attempts.
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:25 +03:00
|
|
|
if err != expRegErr {
|
|
|
|
t.Fatalf("expected error %v, got: %v", expRegErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 01:13:27 +03:00
|
|
|
func TestPaymentControlMPPRecordValidation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
db, cleanup, err := MakeTestDB()
|
2020-05-08 18:49:19 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-04-01 01:13:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
info, attempt, _, err := genInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the payment.
|
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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},
|
|
|
|
)
|
|
|
|
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2020-04-01 01:13:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to register a non-MPP attempt, which should fail.
|
|
|
|
b := *attempt
|
|
|
|
b.AttemptID = 1
|
|
|
|
b.Route.FinalHop().MPP = nil
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:27 +03: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},
|
|
|
|
)
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:27 +03: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},
|
|
|
|
)
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:27 +03: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()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pControl.InitPayment(info.PaymentHash, info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt.Route.FinalHop().MPP = nil
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
2020-04-01 01:13:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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},
|
|
|
|
)
|
|
|
|
|
2020-04-06 10:26:52 +03:00
|
|
|
_, err = pControl.RegisterAttempt(info.PaymentHash, &b)
|
2020-04-01 01:13:27 +03:00
|
|
|
if err != ErrNonMPPayment {
|
|
|
|
t.Fatalf("expected ErrNonMPPayment, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03: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 04:47:19 +03:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
payment, err := p.FetchPayment(hash)
|
|
|
|
if expStatus == StatusUnknown && err == ErrPaymentNotInitiated {
|
|
|
|
return
|
|
|
|
}
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
2020-02-20 16:43:25 +03:00
|
|
|
t.Fatal(err)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
if payment.Status != expStatus {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("payment status mismatch: expected %v, got %v",
|
2020-02-20 16:43:25 +03:00
|
|
|
expStatus, payment.Status)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
type htlcStatus struct {
|
|
|
|
*HTLCAttemptInfo
|
|
|
|
settle *lntypes.Preimage
|
|
|
|
failure *HTLCFailReason
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03: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 01:13:25 +03:00
|
|
|
c *PaymentCreationInfo, f *FailureReason, a *htlcStatus) {
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
t.Helper()
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
payment, err := p.FetchPayment(hash)
|
2019-05-23 21:05:28 +03:00
|
|
|
if err != nil {
|
2020-02-20 16:43:25 +03:00
|
|
|
t.Fatal(err)
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03: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 21:05:30 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
if f != nil {
|
|
|
|
if *payment.FailureReason != *f {
|
|
|
|
t.Fatal("unexpected failure reason")
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
} else {
|
|
|
|
if payment.FailureReason != nil {
|
|
|
|
t.Fatal("unexpected failure reason")
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-02-20 16:43:25 +03:00
|
|
|
if a == nil {
|
|
|
|
if len(payment.HTLCs) > 0 {
|
|
|
|
t.Fatal("expected no htlcs")
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
return
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
htlc := payment.HTLCs[a.AttemptID]
|
2020-02-20 16:43:25 +03:00
|
|
|
if err := assertRouteEqual(&htlc.Route, &a.Route); err != nil {
|
|
|
|
t.Fatal("routes do not match")
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
|
2020-04-01 01:13:25 +03: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 21:05:28 +03:00
|
|
|
}
|
2020-04-01 01:13:25 +03: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 21:05:30 +03:00
|
|
|
}
|
2020-04-01 01:13:25 +03:00
|
|
|
} else if htlc.Settle != nil {
|
|
|
|
t.Fatal("expected no settle info")
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 13:34:27 +03: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
|
|
|
|
|
|
|
|
}); 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)
|
|
|
|
}
|