2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-12-21 12:19:01 +03:00
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-08-22 08:49:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeFakePayment() *OutgoingPayment {
|
|
|
|
fakeInvoice := &Invoice{
|
2017-08-29 06:04:06 +03:00
|
|
|
// Use single second precision to avoid false positive test
|
|
|
|
// failures due to the monotonic time component.
|
2017-09-05 18:59:52 +03:00
|
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
|
|
|
Memo: []byte("fake memo"),
|
|
|
|
Receipt: []byte("fake receipt"),
|
|
|
|
PaymentRequest: []byte(""),
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], rev[:])
|
2017-08-22 08:49:56 +03:00
|
|
|
fakeInvoice.Terms.Value = lnwire.NewMSatFromSatoshis(10000)
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
fakePath := make([][33]byte, 3)
|
|
|
|
for i := 0; i < 3; i++ {
|
2016-12-31 03:32:20 +03:00
|
|
|
copy(fakePath[i][:], bytes.Repeat([]byte{byte(i)}, 33))
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2017-12-14 04:04:18 +03:00
|
|
|
fakePayment := &OutgoingPayment{
|
2016-12-21 12:19:01 +03:00
|
|
|
Invoice: *fakeInvoice,
|
|
|
|
Fee: 101,
|
|
|
|
Path: fakePath,
|
2016-12-05 14:59:36 +03:00
|
|
|
TimeLockLength: 1000,
|
|
|
|
}
|
2017-12-14 04:04:18 +03:00
|
|
|
copy(fakePayment.PaymentPreimage[:], rev[:])
|
|
|
|
return fakePayment
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2018-08-12 16:13:59 +03:00
|
|
|
func makeFakePaymentHash() [32]byte {
|
|
|
|
var paymentHash [32]byte
|
|
|
|
rBytes, _ := randomBytes(0, 32)
|
|
|
|
copy(paymentHash[:], rBytes)
|
|
|
|
|
|
|
|
return paymentHash
|
|
|
|
}
|
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// randomBytes creates random []byte with length in range [minLen, maxLen)
|
2016-12-21 12:19:01 +03:00
|
|
|
func randomBytes(minLen, maxLen int) ([]byte, error) {
|
2016-12-31 03:32:20 +03:00
|
|
|
randBuf := make([]byte, minLen+rand.Intn(maxLen-minLen))
|
|
|
|
|
|
|
|
if _, err := rand.Read(randBuf); err != nil {
|
2016-12-21 12:19:01 +03:00
|
|
|
return nil, fmt.Errorf("Internal error. "+
|
|
|
|
"Cannot generate random string: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
return randBuf, nil
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
func makeRandomFakePayment() (*OutgoingPayment, error) {
|
|
|
|
var err error
|
2016-12-05 14:59:36 +03:00
|
|
|
fakeInvoice := &Invoice{
|
2017-08-29 06:04:06 +03:00
|
|
|
// Use single second precision to avoid false positive test
|
|
|
|
// failures due to the monotonic time component.
|
|
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
fakeInvoice.Memo, err = randomBytes(1, 50)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeInvoice.Receipt, err = randomBytes(1, 50)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:59:52 +03:00
|
|
|
fakeInvoice.PaymentRequest = []byte("")
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
preImg, err := randomBytes(32, 33)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], preImg)
|
|
|
|
|
2017-08-22 08:49:56 +03:00
|
|
|
fakeInvoice.Terms.Value = lnwire.MilliSatoshi(rand.Intn(10000))
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
fakePathLen := 1 + rand.Intn(5)
|
2016-12-21 12:19:01 +03:00
|
|
|
fakePath := make([][33]byte, fakePathLen)
|
|
|
|
for i := 0; i < fakePathLen; i++ {
|
|
|
|
b, err := randomBytes(33, 34)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(fakePath[i][:], b)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
fakePayment := &OutgoingPayment{
|
|
|
|
Invoice: *fakeInvoice,
|
2017-08-22 08:49:56 +03:00
|
|
|
Fee: lnwire.MilliSatoshi(rand.Intn(1001)),
|
2016-12-21 12:19:01 +03:00
|
|
|
Path: fakePath,
|
|
|
|
TimeLockLength: uint32(rand.Intn(10000)),
|
|
|
|
}
|
2017-12-14 04:04:18 +03:00
|
|
|
copy(fakePayment.PaymentPreimage[:], fakeInvoice.Terms.PaymentPreimage[:])
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
return fakePayment, nil
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutgoingPaymentSerialization(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
fakePayment := makeFakePayment()
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := serializeOutgoingPayment(&b, fakePayment); err != nil {
|
|
|
|
t.Fatalf("unable to serialize outgoing payment: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
newPayment, err := deserializeOutgoingPayment(&b)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
2016-12-31 03:32:20 +03:00
|
|
|
t.Fatalf("unable to deserialize outgoing payment: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
if !reflect.DeepEqual(fakePayment, newPayment) {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("Payments do not match after "+
|
|
|
|
"serialization/deserialization %v vs %v",
|
2016-12-05 14:59:36 +03:00
|
|
|
spew.Sdump(fakePayment),
|
|
|
|
spew.Sdump(newPayment),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutgoingPaymentWorkflow(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-31 03:32:20 +03:00
|
|
|
defer cleanUp()
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fakePayment := makeFakePayment()
|
2016-12-31 03:32:20 +03:00
|
|
|
if err = db.AddPayment(fakePayment); err != nil {
|
|
|
|
t.Fatalf("unable to put payment in DB: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
payments, err := db.FetchAllPayments()
|
|
|
|
if err != nil {
|
2016-12-31 03:32:20 +03:00
|
|
|
t.Fatalf("unable to fetch payments from DB: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
expectedPayments := []*OutgoingPayment{fakePayment}
|
|
|
|
if !reflect.DeepEqual(payments, expectedPayments) {
|
2016-12-05 14:59:36 +03:00
|
|
|
t.Fatalf("Wrong payments after reading from DB."+
|
|
|
|
"Got %v, want %v",
|
|
|
|
spew.Sdump(payments),
|
2016-12-31 03:32:20 +03:00
|
|
|
spew.Sdump(expectedPayments),
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make some random payments
|
2016-12-21 12:19:01 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
randomPayment, err := makeRandomFakePayment()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Internal error in tests: %v", err)
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
if err = db.AddPayment(randomPayment); err != nil {
|
|
|
|
t.Fatalf("unable to put payment in DB: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
expectedPayments = append(expectedPayments, randomPayment)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
payments, err = db.FetchAllPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get payments from DB: %v", err)
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if !reflect.DeepEqual(payments, expectedPayments) {
|
2016-12-05 14:59:36 +03:00
|
|
|
t.Fatalf("Wrong payments after reading from DB."+
|
|
|
|
"Got %v, want %v",
|
|
|
|
spew.Sdump(payments),
|
2016-12-31 03:32:20 +03:00
|
|
|
spew.Sdump(expectedPayments),
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all payments.
|
2016-12-31 03:32:20 +03:00
|
|
|
if err = db.DeleteAllPayments(); err != nil {
|
|
|
|
t.Fatalf("unable to delete payments from DB: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
// Check that there is no payments after deletion
|
2016-12-05 14:59:36 +03:00
|
|
|
paymentsAfterDeletion, err := db.FetchAllPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get payments after deletion: %v", err)
|
|
|
|
}
|
|
|
|
if len(paymentsAfterDeletion) != 0 {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("After deletion DB has %v payments, want %v",
|
|
|
|
len(paymentsAfterDeletion), 0)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 16:13:59 +03:00
|
|
|
|
|
|
|
func TestPaymentStatusWorkflow(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
paymentHash [32]byte
|
|
|
|
status PaymentStatus
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
paymentHash: makeFakePaymentHash(),
|
|
|
|
status: StatusGrounded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
paymentHash: makeFakePaymentHash(),
|
|
|
|
status: StatusInFlight,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
paymentHash: makeFakePaymentHash(),
|
|
|
|
status: StatusCompleted,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
err := db.UpdatePaymentStatus(testCase.paymentHash, testCase.status)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to put payment in DB: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := db.FetchPaymentStatus(testCase.paymentHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch payments from DB: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != testCase.status {
|
|
|
|
t.Fatalf("Wrong payments status after reading from DB."+
|
|
|
|
"Got %v, want %v",
|
|
|
|
spew.Sdump(status),
|
|
|
|
spew.Sdump(testCase.status),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|