mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 17:51:33 +02:00
multi: do not use tlv.Record outside wire format handling
This commit prepares for more manipulation of custom records. A list of tlv.Record types is more difficult to use than the more basic map[uint64][]byte. Furthermore fields and variables are renamed to make them more consistent.
This commit is contained in:
@@ -2,7 +2,6 @@ package channeldb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
@@ -28,9 +27,9 @@ var (
|
||||
ChannelID: 12345,
|
||||
OutgoingTimeLock: 111,
|
||||
AmtToForward: 555,
|
||||
TLVRecords: []tlv.Record{
|
||||
tlv.MakeStaticRecord(1, nil, 3, tlvEncoder, nil),
|
||||
tlv.MakeStaticRecord(2, nil, 3, tlvEncoder, nil),
|
||||
CustomRecords: record.CustomSet{
|
||||
1: []byte{},
|
||||
2: []byte{},
|
||||
},
|
||||
MPP: record.NewMPP(32, [32]byte{0x42}),
|
||||
}
|
||||
@@ -144,25 +143,7 @@ func TestSentPaymentSerialization(t *testing.T) {
|
||||
// assertRouteEquals compares to routes for equality and returns an error if
|
||||
// they are not equal.
|
||||
func assertRouteEqual(a, b *route.Route) error {
|
||||
err := assertRouteHopRecordsEqual(a, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TLV records have already been compared and need to be cleared to
|
||||
// properly compare the remaining fields using DeepEqual.
|
||||
copyRouteNoHops := func(r *route.Route) *route.Route {
|
||||
copy := *r
|
||||
copy.Hops = make([]*route.Hop, len(r.Hops))
|
||||
for i, hop := range r.Hops {
|
||||
hopCopy := *hop
|
||||
hopCopy.TLVRecords = nil
|
||||
copy.Hops[i] = &hopCopy
|
||||
}
|
||||
return ©
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(copyRouteNoHops(a), copyRouteNoHops(b)) {
|
||||
if !reflect.DeepEqual(a, b) {
|
||||
return fmt.Errorf("PaymentAttemptInfos don't match: %v vs %v",
|
||||
spew.Sdump(a), spew.Sdump(b))
|
||||
}
|
||||
@@ -170,57 +151,6 @@ func assertRouteEqual(a, b *route.Route) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertRouteHopRecordsEqual(r1, r2 *route.Route) error {
|
||||
if len(r1.Hops) != len(r2.Hops) {
|
||||
return errors.New("route hop count mismatch")
|
||||
}
|
||||
|
||||
for i := 0; i < len(r1.Hops); i++ {
|
||||
records1 := r1.Hops[i].TLVRecords
|
||||
records2 := r2.Hops[i].TLVRecords
|
||||
if len(records1) != len(records2) {
|
||||
return fmt.Errorf("route record count for hop %v "+
|
||||
"mismatch", i)
|
||||
}
|
||||
|
||||
for j := 0; j < len(records1); j++ {
|
||||
expectedRecord := records1[j]
|
||||
newRecord := records2[j]
|
||||
|
||||
err := assertHopRecordsEqual(expectedRecord, newRecord)
|
||||
if err != nil {
|
||||
return fmt.Errorf("route record mismatch: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertHopRecordsEqual(h1, h2 tlv.Record) error {
|
||||
if h1.Type() != h2.Type() {
|
||||
return fmt.Errorf("wrong type: expected %v, got %v", h1.Type(),
|
||||
h2.Type())
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := h2.Encode(&b); err != nil {
|
||||
return fmt.Errorf("unable to encode record: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b.Bytes(), tlvBytes) {
|
||||
return fmt.Errorf("wrong raw record: expected %x, got %x",
|
||||
tlvBytes, b.Bytes())
|
||||
}
|
||||
|
||||
if h1.Size() != h2.Size() {
|
||||
return fmt.Errorf("wrong size: expected %v, "+
|
||||
"got %v", h1.Size(), h2.Size())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRouteSerialization(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
Reference in New Issue
Block a user