Skip to content

Commit c4c01e7

Browse files
committed
lnwire: patch test and fix extra data in DynCommit
1 parent 9ed998c commit c4c01e7

File tree

3 files changed

+133
-22
lines changed

3 files changed

+133
-22
lines changed

lnwire/dyn_commit.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
5151
producers = append(producers, &rec)
5252
})
5353

54-
var extra ExtraOpaqueData
55-
err := extra.PackRecords(producers...)
54+
// Encode all known records.
55+
var tlvData ExtraOpaqueData
56+
err := tlvData.PackRecords(producers...)
5657
if err != nil {
5758
return err
5859
}
59-
dc.ExtraData = extra
6060

61+
// Write the known records.
62+
if err := WriteBytes(w, tlvData); err != nil {
63+
return err
64+
}
65+
66+
// Encode ExtraData.
6167
return WriteBytes(w, dc.ExtraData)
6268
}
6369

@@ -89,52 +95,51 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
8995
chanType := dc.ChannelType.Zero()
9096
nonce := dc.LocalNonce.Zero()
9197

92-
typeMap, err := tlvRecords.ExtractRecords(
93-
&dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
94-
&chanType, &nonce,
98+
// Parse all known records and extra data.
99+
knownRecords, extraData, err := ParseAndExtractExtraData(
100+
tlvRecords, &dustLimit, &maxValue, &htlcMin, &reserve,
101+
&csvDelay, &maxHtlcs, &chanType, &nonce,
95102
)
96103
if err != nil {
97104
return err
98105
}
99106

100107
// Check the results of the TLV Stream decoding and appropriately set
101108
// message fields.
102-
if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
109+
if _, ok := knownRecords[dc.DustLimit.TlvType()]; ok {
103110
var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
104111
rec.Val = dustLimit.Val
105112
dc.DustLimit = tlv.SomeRecordT(rec)
106113
}
107-
if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
114+
if _, ok := knownRecords[dc.MaxValueInFlight.TlvType()]; ok {
108115
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
109116
rec.Val = maxValue.Val
110117
dc.MaxValueInFlight = tlv.SomeRecordT(rec)
111118
}
112-
if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
119+
if _, ok := knownRecords[dc.HtlcMinimum.TlvType()]; ok {
113120
var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
114121
rec.Val = htlcMin.Val
115122
dc.HtlcMinimum = tlv.SomeRecordT(rec)
116123
}
117-
if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
124+
if _, ok := knownRecords[dc.ChannelReserve.TlvType()]; ok {
118125
var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
119126
rec.Val = reserve.Val
120127
dc.ChannelReserve = tlv.SomeRecordT(rec)
121128
}
122-
if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
129+
if _, ok := knownRecords[dc.CsvDelay.TlvType()]; ok {
123130
dc.CsvDelay = tlv.SomeRecordT(csvDelay)
124131
}
125-
if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
132+
if _, ok := knownRecords[dc.MaxAcceptedHTLCs.TlvType()]; ok {
126133
dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
127134
}
128-
if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
135+
if _, ok := knownRecords[dc.ChannelType.TlvType()]; ok {
129136
dc.ChannelType = tlv.SomeRecordT(chanType)
130137
}
131-
if val, ok := typeMap[dc.LocalNonce.TlvType()]; ok && val == nil {
138+
if _, ok := knownRecords[dc.LocalNonce.TlvType()]; ok {
132139
dc.LocalNonce = tlv.SomeRecordT(nonce)
133140
}
134141

135-
if len(tlvRecords) != 0 {
136-
dc.ExtraData = tlvRecords
137-
}
142+
dc.ExtraData = extraData
138143

139144
return nil
140145
}

lnwire/dyn_commit_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package lnwire
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/lightningnetwork/lnd/lnutils"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// TestDynCommitEncodeDecode checks that the Encode and Decode methods for
12+
// DynCommit work as expected.
13+
func TestDynCommitEncodeDecode(t *testing.T) {
14+
t.Parallel()
15+
16+
// Generate random channel ID.
17+
chanIDBytes, err := generateRandomBytes(32)
18+
require.NoError(t, err)
19+
20+
var chanID ChannelID
21+
copy(chanID[:], chanIDBytes)
22+
23+
// Generate random sig.
24+
sigBytes, err := generateRandomBytes(64)
25+
require.NoError(t, err)
26+
27+
var sig Sig
28+
copy(sig.bytes[:], sigBytes)
29+
30+
// Create test data for the TLVs. The actual value doesn't matter, as we
31+
// only care about that the raw bytes can be decoded into a msg, and the
32+
// msg can be encoded into the exact same raw bytes.
33+
testTlvData := []byte{
34+
// DustLimit tlv.
35+
0x0, // type.
36+
0x5, // length.
37+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 100_000).
38+
39+
// MaxValueInFlight tlv.
40+
0x2, // type.
41+
0x5, // length.
42+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 100_000).
43+
44+
// HtlcMinimum tlv.
45+
0x4, // type.
46+
0x5, // length.
47+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 100_000).
48+
//
49+
// ChannelReserve tlv.
50+
0x6, // type.
51+
0x5, // length.
52+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 100_000).
53+
54+
// CsvDelay tlv.
55+
0x8, // type.
56+
0x2, // length.
57+
0x0, 0x8, // value.
58+
59+
// MaxAcceptedHTLCs tlv.
60+
0xa, // type.
61+
0x2, // length.
62+
0x0, 0x8, // value.
63+
64+
// ChannelType tlv is empty.
65+
//
66+
// LocalNonce tlv.
67+
0x14, // type.
68+
0x42, // length.
69+
0x2c, 0xd4, 0x53, 0x7d, 0xaa, 0x7b, // value.
70+
0x7e, 0xae, 0x18, 0x32, 0xa6, 0xc4, 0x29, 0xe9, 0xe0, 0x91,
71+
0x32, 0x7a, 0xaf, 0xd1, 0x1c, 0x2b, 0x04, 0xa0, 0x4d, 0xb5,
72+
0x6a, 0x6f, 0x8b, 0x6c, 0xdc, 0xd1, 0x80, 0x2d, 0xff, 0x72,
73+
0xd8, 0x3c, 0xfc, 0x01, 0x6e, 0x7c, 0x1a, 0xc8, 0x5e, 0x3a,
74+
0x16, 0x98, 0xbc, 0x9b, 0x6e, 0x22, 0x58, 0x96, 0x96, 0xad,
75+
0x88, 0xbf, 0xff, 0x59, 0x90, 0xbd, 0x36, 0x0b, 0x0b, 0x4d,
76+
77+
// ExtraData tlv.
78+
0x6f, // type.
79+
0x2, // length.
80+
0x79, 0x79, // value.
81+
}
82+
83+
msg := &DynCommit{}
84+
85+
// Pre-allocate a new slice with enough capacity for all three parts for
86+
// efficiency.
87+
totalLen := len(chanIDBytes) + len(sigBytes) + len(testTlvData)
88+
rawBytes := make([]byte, 0, totalLen)
89+
90+
// Append each slice to the new rawBytes slice.
91+
rawBytes = append(rawBytes, chanIDBytes...)
92+
rawBytes = append(rawBytes, sigBytes...)
93+
rawBytes = append(rawBytes, testTlvData...)
94+
95+
// Decode the raw bytes.
96+
r := bytes.NewBuffer(rawBytes)
97+
err = msg.Decode(r, 0)
98+
require.NoError(t, err)
99+
100+
t.Logf("Encoded msg is %v", lnutils.SpewLogClosure(msg))
101+
102+
// Encode the msg into raw bytes and assert the encoded bytes equal to
103+
// the rawBytes.
104+
var encodedBytes []byte
105+
w := bytes.NewBuffer(encodedBytes)
106+
err = msg.Encode(w, 0)
107+
require.NoError(t, err)
108+
109+
require.Equal(t, rawBytes, w.Bytes())
110+
}

lnwire/test_message.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,7 @@ func (dc *DynCommit) RandTestMessage(t *rapid.T) Message {
10141014
DynAck: *da,
10151015
}
10161016

1017-
extraData := RandExtraOpaqueData(t, ignoreRecords)
1018-
if len(extraData) > 0 {
1019-
msg.ExtraData = extraData
1020-
}
1021-
1017+
msg.ExtraData = RandExtraOpaqueData(t, ignoreRecords)
10221018
return msg
10231019
}
10241020

0 commit comments

Comments
 (0)