2019-05-23 21:05:26 +03:00
|
|
|
package channeldb
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-23 21:05:26 +03:00
|
|
|
"crypto/rand"
|
2018-08-12 16:19:12 +03:00
|
|
|
"fmt"
|
2019-05-23 21:05:26 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
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
|
|
|
|
|
|
|
"github.com/btcsuite/fastsha256"
|
2019-05-23 21:05:28 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-05-23 21:05:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-08-12 16:19:12 +03:00
|
|
|
)
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
func initDB() (*DB, error) {
|
|
|
|
tempPath, err := ioutil.TempDir("", "switchdb")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := Open(tempPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
rhash := fastsha256.Sum256(preimage[:])
|
2019-05-23 21:05:28 +03:00
|
|
|
return &PaymentCreationInfo{
|
|
|
|
PaymentHash: rhash,
|
|
|
|
Value: 1,
|
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{
|
|
|
|
AttemptID: 1,
|
2019-05-23 21:05:28 +03:00
|
|
|
SessionKey: priv,
|
|
|
|
Route: testRoute,
|
|
|
|
}, 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()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
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-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, nil, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
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-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, nil, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
&failReason,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
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-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, nil, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
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-02-07 12:31:27 +03:00
|
|
|
attempt.AttemptID = 2
|
2019-05-23 21:05:28 +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-02-20 20:08:01 +03:00
|
|
|
err = pControl.FailAttempt(
|
2020-02-20 20:08:01 +03:00
|
|
|
info.PaymentHash, 2, &HTLCFailInfo{
|
|
|
|
Reason: HTLCFailUnreadable,
|
|
|
|
},
|
2020-02-20 20:08:01 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:56:24 +03:00
|
|
|
// Record another attempt.
|
|
|
|
attempt.AttemptID = 3
|
|
|
|
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)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, attempt, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2020-02-20 16:56:24 +03:00
|
|
|
// Settle the attempt and verify that status was changed to StatusSucceeded.
|
2019-11-08 14:39:51 +03:00
|
|
|
var payment *MPPayment
|
2020-02-20 20:08:01 +03:00
|
|
|
payment, err = pControl.SettleAttempt(
|
|
|
|
info.PaymentHash, 3,
|
|
|
|
&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-02-20 20:08:01 +03:00
|
|
|
assertPaymentInfo(t, pControl, info.PaymentHash, info, 1, attempt, preimg, nil)
|
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()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
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-02-20 16:43:25 +03:00
|
|
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, nil, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
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.
|
|
|
|
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)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, attempt, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
nil,
|
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(
|
|
|
|
info.PaymentHash, 1,
|
|
|
|
&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-02-20 20:08:01 +03:00
|
|
|
assertPaymentInfo(t, pControl, info.PaymentHash, info, 0, attempt, preimg, nil)
|
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()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
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()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-27 15:35:09 +03:00
|
|
|
// TestPaymentControlDeleteNonInFlight checks that calling DeletaPayments only
|
|
|
|
// deletes payments from the database that are not in-flight.
|
|
|
|
func TestPaymentControlDeleteNonInFligt(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(db)
|
|
|
|
|
|
|
|
payments := []struct {
|
|
|
|
failed bool
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
failed: true,
|
|
|
|
success: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
failed: false,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
failed: false,
|
|
|
|
success: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.failed {
|
2020-02-20 20:08:01 +03:00
|
|
|
// Fail the payment attempt.
|
|
|
|
err := pControl.FailAttempt(
|
|
|
|
info.PaymentHash, attempt.AttemptID,
|
2020-02-20 20:08:01 +03:00
|
|
|
&HTLCFailInfo{
|
|
|
|
Reason: HTLCFailUnreadable,
|
|
|
|
},
|
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)
|
2019-05-27 15:35:09 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, attempt,
|
2019-05-27 15:35:09 +03:00
|
|
|
lntypes.Preimage{}, &failReason,
|
|
|
|
)
|
|
|
|
} else if p.success {
|
|
|
|
// Verifies that status was changed to StatusSucceeded.
|
2020-02-20 20:08:01 +03:00
|
|
|
_, err := pControl.SettleAttempt(
|
|
|
|
info.PaymentHash, 1,
|
|
|
|
&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)
|
2019-05-27 15:35:09 +03:00
|
|
|
assertPaymentInfo(
|
2020-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, attempt, preimg, nil,
|
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-02-20 20:08:01 +03:00
|
|
|
t, pControl, info.PaymentHash, info, 0, attempt,
|
2019-05-27 15:35:09 +03:00
|
|
|
lntypes.Preimage{}, nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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-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-02-20 20:08:01 +03:00
|
|
|
c *PaymentCreationInfo, aIdx int, a *HTLCAttemptInfo, s lntypes.Preimage,
|
2020-02-20 16:43:25 +03:00
|
|
|
f *FailureReason) {
|
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-02-20 20:08:01 +03:00
|
|
|
htlc := payment.HTLCs[aIdx]
|
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-02-20 16:43:25 +03:00
|
|
|
var zeroPreimage = lntypes.Preimage{}
|
|
|
|
if s != zeroPreimage {
|
|
|
|
if htlc.Settle.Preimage != s {
|
|
|
|
t.Fatalf("Preimages don't match: %x vs %x",
|
|
|
|
htlc.Settle.Preimage, s)
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
2020-02-20 16:43:25 +03:00
|
|
|
} else {
|
|
|
|
if htlc.Settle != nil {
|
|
|
|
t.Fatal("expected no settle info")
|
2019-05-23 21:05:30 +03:00
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
}
|
|
|
|
}
|