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"
|
2018-08-12 16:19:12 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/fastsha256"
|
2019-05-23 21:05:26 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2018-08-12 16:19:12 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:19:12 +03:00
|
|
|
func genHtlc() (*lnwire.UpdateAddHTLC, error) {
|
|
|
|
preimage, err := genPreimage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rhash := fastsha256.Sum256(preimage[:])
|
|
|
|
htlc := &lnwire.UpdateAddHTLC{
|
|
|
|
PaymentHash: rhash,
|
|
|
|
Amount: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
return htlc, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
type paymentControlTestCase func(*testing.T, bool)
|
|
|
|
|
|
|
|
var paymentControlTests = []struct {
|
|
|
|
name string
|
|
|
|
strict bool
|
|
|
|
testcase paymentControlTestCase
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "fail-strict",
|
|
|
|
strict: true,
|
|
|
|
testcase: testPaymentControlSwitchFail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double-send-strict",
|
|
|
|
strict: true,
|
|
|
|
testcase: testPaymentControlSwitchDoubleSend,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double-pay-strict",
|
|
|
|
strict: true,
|
|
|
|
testcase: testPaymentControlSwitchDoublePay,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "fail-not-strict",
|
|
|
|
strict: false,
|
|
|
|
testcase: testPaymentControlSwitchFail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double-send-not-strict",
|
|
|
|
strict: false,
|
|
|
|
testcase: testPaymentControlSwitchDoubleSend,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double-pay-not-strict",
|
|
|
|
strict: false,
|
|
|
|
testcase: testPaymentControlSwitchDoublePay,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPaymentControls runs a set of common tests against both the strict and
|
|
|
|
// non-strict payment control instances. This ensures that the two both behave
|
|
|
|
// identically when making the expected state-transitions of the stricter
|
|
|
|
// implementation. Behavioral differences in the strict and non-strict
|
|
|
|
// implementations are tested separately.
|
|
|
|
func TestPaymentControls(t *testing.T) {
|
|
|
|
for _, test := range paymentControlTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
test.testcase(t, test.strict)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testPaymentControlSwitchFail checks that payment status returns to Grounded
|
2018-08-11 00:01:24 +03:00
|
|
|
// status after failing, and that ClearForTakeoff allows another HTLC for the
|
|
|
|
// same payment hash.
|
2018-08-14 04:47:19 +03:00
|
|
|
func testPaymentControlSwitchFail(t *testing.T, strict bool) {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
pControl := NewPaymentControl(strict, db)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
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.
|
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusInFlight)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Fail the payment, which should moved it to Grounded.
|
|
|
|
if err := pControl.Fail(htlc.PaymentHash); err != nil {
|
|
|
|
t.Fatalf("unable to fail payment hash: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Verify the status is indeed Grounded.
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusGrounded)
|
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.
|
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusInFlight)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Verifies that status was changed to StatusCompleted.
|
|
|
|
if err := pControl.Success(htlc.PaymentHash); err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
2018-08-12 16:19:12 +03:00
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusCompleted)
|
2018-08-11 00:01:24 +03:00
|
|
|
|
|
|
|
// Attempt a final payment, which should now fail since the prior
|
|
|
|
// payment succeed.
|
2018-08-11 00:02:50 +03:00
|
|
|
if err := pControl.ClearForTakeoff(htlc); 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +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.
|
2018-08-14 04:47:19 +03:00
|
|
|
func testPaymentControlSwitchDoubleSend(t *testing.T, strict bool) {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
pControl := NewPaymentControl(strict, db)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
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.
|
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusInFlight)
|
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.
|
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != ErrPaymentInFlight {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("payment control wrong behaviour: " +
|
|
|
|
"double sending must trigger ErrPaymentInFlight error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// TestPaymentControlSwitchDoublePay checks the ability of payment control to
|
|
|
|
// prevent double payment.
|
2018-08-14 04:47:19 +03:00
|
|
|
func testPaymentControlSwitchDoublePay(t *testing.T, strict bool) {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
pControl := NewPaymentControl(strict, db)
|
2018-08-12 16:19:12 +03:00
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends base htlc message which initiate StatusInFlight.
|
2018-08-11 00:01:24 +03:00
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != nil {
|
2018-08-12 16:19:12 +03:00
|
|
|
t.Fatalf("unable to send htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:01:24 +03:00
|
|
|
// Verify that payment is InFlight.
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusInFlight)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
// Move payment to completed status, second payment should return error.
|
|
|
|
if err := pControl.Success(htlc.PaymentHash); err != nil {
|
|
|
|
t.Fatalf("error shouldn't have been received, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that payment is Completed.
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusCompleted)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
if err := pControl.ClearForTakeoff(htlc); err != ErrAlreadyPaid {
|
|
|
|
t.Fatalf("payment control wrong behaviour:" +
|
|
|
|
" double payment must trigger ErrAlreadyPaid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPaymentControlNonStrictSuccessesWithoutInFlight checks that a non-strict
|
|
|
|
// payment control will allow calls to Success when no payment is in flight. This
|
|
|
|
// is necessary to gracefully handle the case in which the switch already sent
|
|
|
|
// out a payment for a particular payment hash in a prior db version that didn't
|
|
|
|
// have payment statuses.
|
|
|
|
func TestPaymentControlNonStrictSuccessesWithoutInFlight(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
2018-08-12 16:19:12 +03:00
|
|
|
if err != nil {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("unable to init db: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
pControl := NewPaymentControl(false, db)
|
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := pControl.Success(htlc.PaymentHash); err != nil {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("unable to mark payment hash success: %v", err)
|
2018-08-12 16:19:12 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusCompleted)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
err = pControl.Success(htlc.PaymentHash)
|
|
|
|
if err != ErrPaymentAlreadyCompleted {
|
|
|
|
t.Fatalf("unable to remark payment hash failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPaymentControlNonStrictFailsWithoutInFlight checks that a non-strict
|
|
|
|
// payment control will allow calls to Fail when no payment is in flight. This
|
|
|
|
// is necessary to gracefully handle the case in which the switch already sent
|
|
|
|
// out a payment for a particular payment hash in a prior db version that didn't
|
|
|
|
// have payment statuses.
|
|
|
|
func TestPaymentControlNonStrictFailsWithoutInFlight(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
2018-08-11 00:01:24 +03:00
|
|
|
if err != nil {
|
2018-08-14 04:47:19 +03:00
|
|
|
t.Fatalf("unable to init db: %v", err)
|
2018-08-11 00:01:24 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
pControl := NewPaymentControl(false, db)
|
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
2018-08-11 00:01:24 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 04:47:19 +03:00
|
|
|
if err := pControl.Fail(htlc.PaymentHash); err != nil {
|
|
|
|
t.Fatalf("unable to mark payment hash failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusGrounded)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
err = pControl.Fail(htlc.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remark payment hash failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusGrounded)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
err = pControl.Success(htlc.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remark payment hash success: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusCompleted)
|
2018-08-14 04:47:19 +03:00
|
|
|
|
|
|
|
err = pControl.Fail(htlc.PaymentHash)
|
|
|
|
if err != ErrPaymentAlreadyCompleted {
|
|
|
|
t.Fatalf("unable to remark payment hash failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusCompleted)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPaymentControlStrictSuccessesWithoutInFlight checks that a strict payment
|
|
|
|
// control will disallow calls to Success when no payment is in flight.
|
|
|
|
func TestPaymentControlStrictSuccessesWithoutInFlight(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(true, db)
|
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pControl.Success(htlc.PaymentHash)
|
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusGrounded)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPaymentControlStrictFailsWithoutInFlight checks that a strict payment
|
|
|
|
// control will disallow calls to Fail when no payment is in flight.
|
|
|
|
func TestPaymentControlStrictFailsWithoutInFlight(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, err := initDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pControl := NewPaymentControl(true, db)
|
|
|
|
|
|
|
|
htlc, err := genHtlc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate htlc message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pControl.Fail(htlc.PaymentHash)
|
|
|
|
if err != ErrPaymentNotInitiated {
|
|
|
|
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:26 +03:00
|
|
|
assertPaymentStatus(t, db, htlc.PaymentHash, StatusGrounded)
|
2018-08-14 04:47:19 +03:00
|
|
|
}
|
|
|
|
|
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:26 +03:00
|
|
|
var paymentStatus = StatusGrounded
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|