2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/fastsha256"
|
2016-12-21 12:19:01 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeFakePayment() *OutgoingPayment {
|
2016-12-21 12:19:01 +03:00
|
|
|
// Create a fake invoice which
|
|
|
|
// we'll use several times in the tests below.
|
2016-12-05 14:59:36 +03:00
|
|
|
fakeInvoice := &Invoice{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
}
|
|
|
|
fakeInvoice.Memo = []byte("memo")
|
|
|
|
fakeInvoice.Receipt = []byte("recipt")
|
|
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], rev[:])
|
|
|
|
fakeInvoice.Terms.Value = btcutil.Amount(10000)
|
|
|
|
// Make fake path
|
2016-12-21 12:19:01 +03:00
|
|
|
fakePath := make([][33]byte, 3)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
for j := 0; j < 33; j++ {
|
2016-12-05 14:59:36 +03:00
|
|
|
fakePath[i][j] = byte(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var rHash [32]byte = fastsha256.Sum256(rev[:])
|
2016-12-21 12:19:01 +03:00
|
|
|
fakePayment := &OutgoingPayment{
|
|
|
|
Invoice: *fakeInvoice,
|
|
|
|
Fee: 101,
|
|
|
|
Path: fakePath,
|
2016-12-05 14:59:36 +03:00
|
|
|
TimeLockLength: 1000,
|
2016-12-21 12:19:01 +03:00
|
|
|
RHash: rHash,
|
|
|
|
Timestamp: time.Unix(100000, 0),
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
return fakePayment
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05 14:59:36 +03:00
|
|
|
l := minLen + rand.Intn(maxLen-minLen)
|
|
|
|
b := make([]byte, l)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if 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-21 12:19:01 +03:00
|
|
|
return b, 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{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
preImg, err := randomBytes(32, 33)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], preImg)
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
fakeInvoice.Terms.Value = btcutil.Amount(rand.Intn(10000))
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Make fake path
|
|
|
|
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
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
var rHash [32]byte = fastsha256.Sum256(
|
|
|
|
fakeInvoice.Terms.PaymentPreimage[:],
|
|
|
|
)
|
2016-12-21 12:19:01 +03:00
|
|
|
fakePayment := &OutgoingPayment{
|
|
|
|
Invoice: *fakeInvoice,
|
|
|
|
Fee: btcutil.Amount(rand.Intn(1001)),
|
|
|
|
Path: fakePath,
|
|
|
|
TimeLockLength: uint32(rand.Intn(10000)),
|
|
|
|
RHash: rHash,
|
|
|
|
Timestamp: time.Unix(rand.Int63n(10000), 0),
|
|
|
|
}
|
|
|
|
return fakePayment, nil
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutgoingPaymentSerialization(t *testing.T) {
|
|
|
|
fakePayment := makeFakePayment()
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
err := serializeOutgoingPayment(b, fakePayment)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't serialize outgoing payment: %v", err)
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
newPayment, err := deserializeOutgoingPayment(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't deserialize outgoing payment: %v", err)
|
|
|
|
}
|
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) {
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test db: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
fakePayment := makeFakePayment()
|
|
|
|
err = db.AddPayment(fakePayment)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't put payment in DB: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payments, err := db.FetchAllPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get payments from DB: %v", err)
|
|
|
|
}
|
|
|
|
correctPayments := []*OutgoingPayment{fakePayment}
|
|
|
|
if !reflect.DeepEqual(payments, correctPayments) {
|
|
|
|
t.Fatalf("Wrong payments after reading from DB."+
|
|
|
|
"Got %v, want %v",
|
|
|
|
spew.Sdump(payments),
|
|
|
|
spew.Sdump(correctPayments),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
err = db.AddPayment(randomPayment)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't put payment in DB: %v", err)
|
|
|
|
}
|
|
|
|
correctPayments = append(correctPayments, randomPayment)
|
|
|
|
}
|
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-05 14:59:36 +03:00
|
|
|
if !reflect.DeepEqual(payments, correctPayments) {
|
|
|
|
t.Fatalf("Wrong payments after reading from DB."+
|
|
|
|
"Got %v, want %v",
|
|
|
|
spew.Sdump(payments),
|
|
|
|
spew.Sdump(correctPayments),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all payments.
|
|
|
|
err = db.DeleteAllPayments()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't delete payments from DB: %v", err)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|