2019-05-23 21:05:26 +03:00
|
|
|
package channeldb
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-23 21:05:28 +03:00
|
|
|
"bytes"
|
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:26 +03:00
|
|
|
"github.com/coreos/bbolt"
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
func genInfo() (*PaymentCreationInfo, *PaymentAttemptInfo,
|
|
|
|
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,
|
|
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
|
|
|
PaymentRequest: []byte("hola"),
|
|
|
|
},
|
|
|
|
&PaymentAttemptInfo{
|
|
|
|
PaymentID: 1,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, 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
|
|
|
|
err = pControl.Fail(info.PaymentHash, failReason)
|
|
|
|
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.
|
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusFailed)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, 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)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, nil, lntypes.Preimage{},
|
2019-05-23 21:05:30 +03:00
|
|
|
nil,
|
2019-05-23 21:05:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Record a new attempt.
|
|
|
|
attempt.PaymentID = 2
|
|
|
|
err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, 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
|
|
|
|
2019-05-23 21:05:31 +03:00
|
|
|
// Verifies that status was changed to StatusSucceeded.
|
2019-05-23 21:05:28 +03:00
|
|
|
if err := pControl.Success(info.PaymentHash, preimg); err != nil {
|
2018-08-11 00:01:24 +03:00
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2019-05-23 21:05:31 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusSucceeded)
|
2019-05-23 21:05:30 +03:00
|
|
|
assertPaymentInfo(t, db, info.PaymentHash, info, 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)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, 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
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, 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.
|
|
|
|
if err := pControl.Success(info.PaymentHash, preimg); err != nil {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
2019-05-23 21:05:31 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusSucceeded)
|
2019-05-23 21:05:30 +03:00
|
|
|
assertPaymentInfo(t, db, info.PaymentHash, info, 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.
|
2019-05-23 21:05:28 +03:00
|
|
|
err = pControl.Success(info.PaymentHash, preimg)
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:31 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusUnknown)
|
2019-05-23 21:05:30 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, nil, nil, lntypes.Preimage{},
|
|
|
|
nil,
|
|
|
|
)
|
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-05-23 21:05:30 +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)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:31 +03:00
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusUnknown)
|
2019-05-23 21:05:30 +03:00
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, nil, nil, lntypes.Preimage{}, nil,
|
|
|
|
)
|
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 {
|
|
|
|
// Fail the payment, which should moved it to Failed.
|
|
|
|
failReason := FailureReasonNoRoute
|
|
|
|
err = pControl.Fail(info.PaymentHash, failReason)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the status is indeed Failed.
|
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusFailed)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, attempt,
|
|
|
|
lntypes.Preimage{}, &failReason,
|
|
|
|
)
|
|
|
|
} else if p.success {
|
|
|
|
// Verifies that status was changed to StatusSucceeded.
|
|
|
|
err := pControl.Success(info.PaymentHash, preimg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusSucceeded)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, attempt, preimg, nil,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
assertPaymentStatus(t, db, info.PaymentHash, StatusInFlight)
|
|
|
|
assertPaymentInfo(
|
|
|
|
t, db, info.PaymentHash, info, attempt,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
func assertPaymentStatus(t *testing.T, db *DB,
|
|
|
|
hash [32]byte, expStatus PaymentStatus) {
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2019-05-23 21:05:31 +03:00
|
|
|
var paymentStatus = StatusUnknown
|
2019-05-23 21:05:26 +03:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
|
|
payments := tx.Bucket(paymentsRootBucket)
|
|
|
|
if payments == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := payments.Bucket(hash[:])
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the existing status of this payment, if any.
|
|
|
|
paymentStatus = fetchPaymentStatus(bucket)
|
|
|
|
return nil
|
|
|
|
})
|
2018-08-14 04:47:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch payment status: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
if paymentStatus != expStatus {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("payment status mismatch: expected %v, got %v",
|
2019-05-23 21:05:26 +03:00
|
|
|
expStatus, paymentStatus)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
|
|
|
|
func checkPaymentCreationInfo(bucket *bbolt.Bucket, c *PaymentCreationInfo) error {
|
|
|
|
b := bucket.Get(paymentCreationInfoKey)
|
|
|
|
switch {
|
|
|
|
case b == nil && c == nil:
|
|
|
|
return nil
|
|
|
|
case b == nil:
|
|
|
|
return fmt.Errorf("expected creation info not found")
|
|
|
|
case c == nil:
|
|
|
|
return fmt.Errorf("unexpected creation info found")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
c2, err := deserializePaymentCreationInfo(r)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("creation info err: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(c, c2) {
|
|
|
|
return fmt.Errorf("PaymentCreationInfos don't match: %v vs %v",
|
|
|
|
spew.Sdump(c), spew.Sdump(c2))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPaymentAttemptInfo(bucket *bbolt.Bucket, a *PaymentAttemptInfo) error {
|
|
|
|
b := bucket.Get(paymentAttemptInfoKey)
|
|
|
|
switch {
|
|
|
|
case b == nil && a == nil:
|
|
|
|
return nil
|
|
|
|
case b == nil:
|
|
|
|
return fmt.Errorf("expected attempt info not found")
|
|
|
|
case a == nil:
|
|
|
|
return fmt.Errorf("unexpected attempt info found")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
a2, err := deserializePaymentAttemptInfo(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a, a2) {
|
|
|
|
return fmt.Errorf("PaymentAttemptInfos don't match: %v vs %v",
|
|
|
|
spew.Sdump(a), spew.Sdump(a2))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSettleInfo(bucket *bbolt.Bucket, preimg lntypes.Preimage) error {
|
|
|
|
zero := lntypes.Preimage{}
|
|
|
|
b := bucket.Get(paymentSettleInfoKey)
|
|
|
|
switch {
|
|
|
|
case b == nil && preimg == zero:
|
|
|
|
return nil
|
|
|
|
case b == nil:
|
|
|
|
return fmt.Errorf("expected preimage not found")
|
|
|
|
case preimg == zero:
|
|
|
|
return fmt.Errorf("unexpected preimage found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var pre2 lntypes.Preimage
|
|
|
|
copy(pre2[:], b[:])
|
|
|
|
if preimg != pre2 {
|
|
|
|
return fmt.Errorf("Preimages don't match: %x vs %x",
|
|
|
|
preimg, pre2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
func checkFailInfo(bucket *bbolt.Bucket, failReason *FailureReason) error {
|
|
|
|
b := bucket.Get(paymentFailInfoKey)
|
|
|
|
switch {
|
|
|
|
case b == nil && failReason == nil:
|
|
|
|
return nil
|
|
|
|
case b == nil:
|
|
|
|
return fmt.Errorf("expected fail info not found")
|
|
|
|
case failReason == nil:
|
|
|
|
return fmt.Errorf("unexpected fail info found")
|
|
|
|
}
|
|
|
|
|
|
|
|
failReason2 := FailureReason(b[0])
|
|
|
|
if *failReason != failReason2 {
|
|
|
|
return fmt.Errorf("Failure infos don't match: %v vs %v",
|
|
|
|
*failReason, failReason2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
func assertPaymentInfo(t *testing.T, db *DB, hash lntypes.Hash,
|
2019-05-23 21:05:30 +03:00
|
|
|
c *PaymentCreationInfo, a *PaymentAttemptInfo, s lntypes.Preimage,
|
|
|
|
f *FailureReason) {
|
2019-05-23 21:05:28 +03:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
|
|
payments := tx.Bucket(paymentsRootBucket)
|
|
|
|
if payments == nil && c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if payments == nil {
|
|
|
|
return fmt.Errorf("sent payments not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := payments.Bucket(hash[:])
|
|
|
|
if bucket == nil && c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("payment not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkPaymentCreationInfo(bucket, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkPaymentAttemptInfo(bucket, a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkSettleInfo(bucket, s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-23 21:05:30 +03:00
|
|
|
|
|
|
|
if err := checkFailInfo(bucket, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-23 21:05:28 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("assert payment info failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|