2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-10-12 10:47:45 +03:00
|
|
|
"math/rand"
|
2016-12-21 12:19:01 +03:00
|
|
|
"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"
|
2019-11-05 01:11:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-05-08 16:00:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-07-31 07:44:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2019-05-08 16:00:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
priv, _ = btcec.NewPrivateKey(btcec.S256())
|
|
|
|
pub = priv.PubKey()
|
|
|
|
|
2019-07-31 07:44:50 +03:00
|
|
|
tlvBytes = []byte{1, 2, 3}
|
|
|
|
tlvEncoder = tlv.StubEncoder(tlvBytes)
|
|
|
|
testHop1 = &route.Hop{
|
2019-05-08 16:00:19 +03:00
|
|
|
PubKeyBytes: route.NewVertex(pub),
|
|
|
|
ChannelID: 12345,
|
|
|
|
OutgoingTimeLock: 111,
|
|
|
|
AmtToForward: 555,
|
2019-12-11 12:52:27 +03:00
|
|
|
CustomRecords: record.CustomSet{
|
2019-12-12 16:47:46 +03:00
|
|
|
65536: []byte{},
|
|
|
|
80001: []byte{},
|
2019-07-31 07:44:50 +03:00
|
|
|
},
|
2019-11-05 01:11:28 +03:00
|
|
|
MPP: record.NewMPP(32, [32]byte{0x42}),
|
2019-07-31 07:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
testHop2 = &route.Hop{
|
|
|
|
PubKeyBytes: route.NewVertex(pub),
|
|
|
|
ChannelID: 12345,
|
|
|
|
OutgoingTimeLock: 111,
|
|
|
|
AmtToForward: 555,
|
|
|
|
LegacyPayload: true,
|
2019-05-08 16:00:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
testRoute = route.Route{
|
|
|
|
TotalTimeLock: 123,
|
|
|
|
TotalAmount: 1234567,
|
|
|
|
SourcePubKey: route.NewVertex(pub),
|
|
|
|
Hops: []*route.Hop{
|
2019-07-31 07:44:50 +03:00
|
|
|
testHop2,
|
2019-11-05 01:11:28 +03:00
|
|
|
testHop1,
|
2019-05-08 16:00:19 +03:00
|
|
|
},
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
2020-02-07 12:31:27 +03:00
|
|
|
func makeFakeInfo() (*PaymentCreationInfo, *HTLCAttemptInfo) {
|
2019-05-23 21:05:27 +03:00
|
|
|
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.
|
2020-02-19 11:53:13 +03:00
|
|
|
CreationTime: time.Unix(time.Now().Unix(), 0),
|
2019-05-23 21:05:27 +03:00
|
|
|
PaymentRequest: []byte(""),
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:31:27 +03:00
|
|
|
a := &HTLCAttemptInfo{
|
2020-02-20 20:08:01 +03:00
|
|
|
AttemptID: 44,
|
|
|
|
SessionKey: priv,
|
|
|
|
Route: testRoute,
|
|
|
|
AttemptTime: time.Unix(100, 0),
|
2019-05-23 21:05:27 +03:00
|
|
|
}
|
|
|
|
return c, a
|
|
|
|
}
|
|
|
|
|
2019-10-12 10:47:45 +03:00
|
|
|
// randomBytes creates random []byte with length in range [minLen, maxLen)
|
|
|
|
func randomBytes(minLen, maxLen int) ([]byte, error) {
|
|
|
|
randBuf := make([]byte, minLen+rand.Intn(maxLen-minLen))
|
|
|
|
|
|
|
|
if _, err := rand.Read(randBuf); err != nil {
|
|
|
|
return nil, fmt.Errorf("Internal error. "+
|
|
|
|
"Cannot generate random string: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return randBuf, nil
|
|
|
|
}
|
|
|
|
|
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()
|
2020-02-07 12:31:27 +03:00
|
|
|
if err := serializeHTLCAttemptInfo(&b, s); err != nil {
|
2019-05-23 21:05:27 +03:00
|
|
|
t.Fatalf("unable to serialize info: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:08:01 +03:00
|
|
|
newWireInfo, err := deserializeHTLCAttemptInfo(&b)
|
2019-05-23 21:05:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to deserialize info: %v", err)
|
|
|
|
}
|
2020-02-20 20:08:01 +03:00
|
|
|
newWireInfo.AttemptID = s.AttemptID
|
2019-05-23 21:05:27 +03:00
|
|
|
|
2019-08-23 22:50:25 +03:00
|
|
|
// First we verify all the records match up porperly, as they aren't
|
|
|
|
// able to be properly compared using reflect.DeepEqual.
|
2020-02-20 20:08:01 +03:00
|
|
|
err = assertRouteEqual(&s.Route, &newWireInfo.Route)
|
2019-09-10 16:34:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Routes do not match after "+
|
|
|
|
"serialization/deserialization: %v", err)
|
|
|
|
}
|
2019-08-23 22:50:25 +03:00
|
|
|
|
2019-09-10 16:34:02 +03:00
|
|
|
// Clear routes to allow DeepEqual to compare the remaining fields.
|
2020-02-20 20:08:01 +03:00
|
|
|
newWireInfo.Route = route.Route{}
|
2019-09-10 16:34:02 +03:00
|
|
|
s.Route = route.Route{}
|
2019-08-23 22:50:25 +03:00
|
|
|
|
2020-02-20 20:08:01 +03:00
|
|
|
if !reflect.DeepEqual(s, newWireInfo) {
|
2019-07-31 07:44:50 +03:00
|
|
|
s.SessionKey.Curve = nil
|
2020-02-20 20:08:01 +03:00
|
|
|
newWireInfo.SessionKey.Curve = nil
|
2019-05-23 21:05:27 +03:00
|
|
|
t.Fatalf("Payments do not match after "+
|
|
|
|
"serialization/deserialization %v vs %v",
|
2020-02-20 20:08:01 +03:00
|
|
|
spew.Sdump(s), spew.Sdump(newWireInfo),
|
2019-05-23 21:05:27 +03:00
|
|
|
)
|
|
|
|
}
|
2019-09-10 16:34:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// assertRouteEquals compares to routes for equality and returns an error if
|
|
|
|
// they are not equal.
|
|
|
|
func assertRouteEqual(a, b *route.Route) error {
|
2019-12-11 12:52:27 +03:00
|
|
|
if !reflect.DeepEqual(a, b) {
|
2020-02-07 12:31:27 +03:00
|
|
|
return fmt.Errorf("HTLCAttemptInfos don't match: %v vs %v",
|
2019-09-10 16:34:02 +03:00
|
|
|
spew.Sdump(a), spew.Sdump(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:44:50 +03:00
|
|
|
// First we verify all the records match up porperly, as they aren't
|
|
|
|
// able to be properly compared using reflect.DeepEqual.
|
2019-09-10 16:34:02 +03:00
|
|
|
err = assertRouteEqual(&testRoute, &route2)
|
2019-07-31 07:44:50 +03:00
|
|
|
if err != nil {
|
2019-05-08 16:00:19 +03:00
|
|
|
t.Fatalf("routes not equal: \n%v vs \n%v",
|
|
|
|
spew.Sdump(testRoute), spew.Sdump(route2))
|
|
|
|
}
|
|
|
|
}
|