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
|
|
|
|
2019-05-08 16:00:19 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2016-12-31 03:32:20 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-05-23 21:05:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2017-08-22 08:49:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-05-08 16:00:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
priv, _ = btcec.NewPrivateKey(btcec.S256())
|
|
|
|
pub = priv.PubKey()
|
|
|
|
|
|
|
|
testHop = &route.Hop{
|
|
|
|
PubKeyBytes: route.NewVertex(pub),
|
|
|
|
ChannelID: 12345,
|
|
|
|
OutgoingTimeLock: 111,
|
|
|
|
AmtToForward: 555,
|
|
|
|
}
|
|
|
|
|
|
|
|
testRoute = route.Route{
|
|
|
|
TotalTimeLock: 123,
|
|
|
|
TotalAmount: 1234567,
|
|
|
|
SourcePubKey: route.NewVertex(pub),
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
testHop,
|
|
|
|
testHop,
|
|
|
|
},
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
func makeFakePayment() *outgoingPayment {
|
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.
|
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
|
|
|
|
2019-05-23 21:05:30 +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
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:27 +03:00
|
|
|
func makeFakeInfo() (*PaymentCreationInfo, *PaymentAttemptInfo) {
|
|
|
|
var preimg lntypes.Preimage
|
|
|
|
copy(preimg[:], rev[:])
|
|
|
|
|
|
|
|
c := &PaymentCreationInfo{
|
|
|
|
PaymentHash: preimg.Hash(),
|
|
|
|
Value: 1000,
|
|
|
|
// Use single second precision to avoid false positive test
|
|
|
|
// failures due to the monotonic time component.
|
|
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
|
|
|
PaymentRequest: []byte(""),
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &PaymentAttemptInfo{
|
|
|
|
PaymentID: 44,
|
|
|
|
SessionKey: priv,
|
|
|
|
Route: testRoute,
|
|
|
|
}
|
|
|
|
return c, a
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
func makeRandomFakePayment() (*outgoingPayment, error) {
|
2016-12-21 12:19:01 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:28 +03:00
|
|
|
fakeInvoice.PaymentRequest, err = randomBytes(1, 50)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-05 18:59:52 +03:00
|
|
|
|
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
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
fakePayment := &outgoingPayment{
|
2016-12-21 12:19:01 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:27 +03:00
|
|
|
func TestSentPaymentSerialization(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-05-23 21:05:27 +03:00
|
|
|
c, s := makeFakeInfo()
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2019-05-23 21:05:27 +03:00
|
|
|
if err := serializePaymentCreationInfo(&b, c); err != nil {
|
|
|
|
t.Fatalf("unable to serialize creation info: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2019-05-23 21:05:27 +03:00
|
|
|
newCreationInfo, err := deserializePaymentCreationInfo(&b)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
2019-05-23 21:05:27 +03:00
|
|
|
t.Fatalf("unable to deserialize creation info: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2019-05-23 21:05:27 +03:00
|
|
|
if !reflect.DeepEqual(c, newCreationInfo) {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("Payments do not match after "+
|
|
|
|
"serialization/deserialization %v vs %v",
|
2019-05-23 21:05:27 +03:00
|
|
|
spew.Sdump(c), spew.Sdump(newCreationInfo),
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
}
|
2019-05-23 21:05:27 +03:00
|
|
|
|
|
|
|
b.Reset()
|
|
|
|
if err := serializePaymentAttemptInfo(&b, s); err != nil {
|
|
|
|
t.Fatalf("unable to serialize info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newAttemptInfo, err := deserializePaymentAttemptInfo(&b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to deserialize info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(s, newAttemptInfo) {
|
|
|
|
t.Fatalf("Payments do not match after "+
|
|
|
|
"serialization/deserialization %v vs %v",
|
|
|
|
spew.Sdump(s), spew.Sdump(newAttemptInfo),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 16:00:19 +03:00
|
|
|
func TestRouteSerialization(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
2019-06-14 16:01:48 +03:00
|
|
|
if err := SerializeRoute(&b, testRoute); err != nil {
|
2019-05-08 16:00:19 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(b.Bytes())
|
2019-06-14 16:01:48 +03:00
|
|
|
route2, err := DeserializeRoute(r)
|
2019-05-08 16:00:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(testRoute, route2) {
|
|
|
|
t.Fatalf("routes not equal: \n%v vs \n%v",
|
|
|
|
spew.Sdump(testRoute), spew.Sdump(route2))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|