Skip to content

Commit c589913

Browse files
committed
lnwire: patch test and fix extra data in DynCommit
1 parent 7105853 commit c589913

File tree

3 files changed

+144
-23
lines changed

3 files changed

+144
-23
lines changed

lnwire/dyn_commit.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,29 @@ func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
4545
if err := WriteSig(w, dc.Sig); err != nil {
4646
return err
4747
}
48-
producers := dynProposeRecords(&dc.DynPropose)
48+
49+
// Create extra data records.
50+
producers, err := dc.ExtraData.RecordProducers()
51+
if err != nil {
52+
return err
53+
}
54+
55+
// Append the known records.
56+
producers = append(producers, dynProposeRecords(&dc.DynPropose)...)
4957
dc.LocalNonce.WhenSome(
5058
func(rec tlv.RecordT[tlv.TlvType14, Musig2Nonce]) {
5159
producers = append(producers, &rec)
5260
})
5361

54-
var extra ExtraOpaqueData
55-
err := extra.PackRecords(producers...)
62+
// Encode all known records.
63+
var tlvData ExtraOpaqueData
64+
err = tlvData.PackRecords(producers...)
5665
if err != nil {
5766
return err
5867
}
59-
dc.ExtraData = extra
6068

61-
return WriteBytes(w, dc.ExtraData)
69+
// Write the records.
70+
return WriteBytes(w, tlvData)
6271
}
6372

6473
// Decode deserializes the serialized DynCommit stored in the passed io.Reader
@@ -89,52 +98,51 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
8998
chanType := dc.ChannelType.Zero()
9099
nonce := dc.LocalNonce.Zero()
91100

92-
typeMap, err := tlvRecords.ExtractRecords(
93-
&dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
94-
&chanType, &nonce,
101+
// Parse all known records and extra data.
102+
knownRecords, extraData, err := ParseAndExtractExtraData(
103+
tlvRecords, &dustLimit, &maxValue, &htlcMin, &reserve,
104+
&csvDelay, &maxHtlcs, &chanType, &nonce,
95105
)
96106
if err != nil {
97107
return err
98108
}
99109

100110
// Check the results of the TLV Stream decoding and appropriately set
101111
// message fields.
102-
if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
112+
if _, ok := knownRecords[dc.DustLimit.TlvType()]; ok {
103113
var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
104114
rec.Val = dustLimit.Val
105115
dc.DustLimit = tlv.SomeRecordT(rec)
106116
}
107-
if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
117+
if _, ok := knownRecords[dc.MaxValueInFlight.TlvType()]; ok {
108118
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
109119
rec.Val = maxValue.Val
110120
dc.MaxValueInFlight = tlv.SomeRecordT(rec)
111121
}
112-
if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
122+
if _, ok := knownRecords[dc.HtlcMinimum.TlvType()]; ok {
113123
var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
114124
rec.Val = htlcMin.Val
115125
dc.HtlcMinimum = tlv.SomeRecordT(rec)
116126
}
117-
if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
127+
if _, ok := knownRecords[dc.ChannelReserve.TlvType()]; ok {
118128
var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
119129
rec.Val = reserve.Val
120130
dc.ChannelReserve = tlv.SomeRecordT(rec)
121131
}
122-
if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
132+
if _, ok := knownRecords[dc.CsvDelay.TlvType()]; ok {
123133
dc.CsvDelay = tlv.SomeRecordT(csvDelay)
124134
}
125-
if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
135+
if _, ok := knownRecords[dc.MaxAcceptedHTLCs.TlvType()]; ok {
126136
dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
127137
}
128-
if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
138+
if _, ok := knownRecords[dc.ChannelType.TlvType()]; ok {
129139
dc.ChannelType = tlv.SomeRecordT(chanType)
130140
}
131-
if val, ok := typeMap[dc.LocalNonce.TlvType()]; ok && val == nil {
141+
if _, ok := knownRecords[dc.LocalNonce.TlvType()]; ok {
132142
dc.LocalNonce = tlv.SomeRecordT(nonce)
133143
}
134144

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

139147
return nil
140148
}

lnwire/dyn_commit_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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: 1_000_000).
38+
39+
// ExtraData - unknown tlv record.
40+
//
41+
// NOTE: This record is optional and occupies the type 1.
42+
0x1, // type.
43+
0x2, // length.
44+
0x79, 0x79, // value.
45+
46+
// MaxValueInFlight tlv.
47+
0x2, // type.
48+
0x5, // length.
49+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000).
50+
51+
// HtlcMinimum tlv.
52+
0x4, // type.
53+
0x5, // length.
54+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000).
55+
//
56+
// ChannelReserve tlv.
57+
0x6, // type.
58+
0x5, // length.
59+
0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000).
60+
61+
// CsvDelay tlv.
62+
0x8, // type.
63+
0x2, // length.
64+
0x0, 0x8, // value.
65+
66+
// MaxAcceptedHTLCs tlv.
67+
0xa, // type.
68+
0x2, // length.
69+
0x0, 0x8, // value.
70+
71+
// ChannelType tlv is empty.
72+
//
73+
// LocalNonce tlv.
74+
0x14, // type.
75+
0x42, // length.
76+
0x2c, 0xd4, 0x53, 0x7d, 0xaa, 0x7b, // value.
77+
0x7e, 0xae, 0x18, 0x32, 0xa6, 0xc4, 0x29, 0xe9, 0xe0, 0x91,
78+
0x32, 0x7a, 0xaf, 0xd1, 0x1c, 0x2b, 0x04, 0xa0, 0x4d, 0xb5,
79+
0x6a, 0x6f, 0x8b, 0x6c, 0xdc, 0xd1, 0x80, 0x2d, 0xff, 0x72,
80+
0xd8, 0x3c, 0xfc, 0x01, 0x6e, 0x7c, 0x1a, 0xc8, 0x5e, 0x3a,
81+
0x16, 0x98, 0xbc, 0x9b, 0x6e, 0x22, 0x58, 0x96, 0x96, 0xad,
82+
0x88, 0xbf, 0xff, 0x59, 0x90, 0xbd, 0x36, 0x0b, 0x0b, 0x4d,
83+
84+
// ExtraData - unknown tlv record.
85+
0x6f, // type.
86+
0x2, // length.
87+
0x79, 0x79, // value.
88+
}
89+
90+
msg := &DynCommit{}
91+
92+
// Pre-allocate a new slice with enough capacity for all three parts for
93+
// efficiency.
94+
totalLen := len(chanIDBytes) + len(sigBytes) + len(testTlvData)
95+
rawBytes := make([]byte, 0, totalLen)
96+
97+
// Append each slice to the new rawBytes slice.
98+
rawBytes = append(rawBytes, chanIDBytes...)
99+
rawBytes = append(rawBytes, sigBytes...)
100+
rawBytes = append(rawBytes, testTlvData...)
101+
102+
// Decode the raw bytes.
103+
r := bytes.NewBuffer(rawBytes)
104+
err = msg.Decode(r, 0)
105+
require.NoError(t, err)
106+
107+
t.Logf("Encoded msg is %v", lnutils.SpewLogClosure(msg))
108+
109+
// Encode the msg into raw bytes and assert the encoded bytes equal to
110+
// the rawBytes.
111+
w := new(bytes.Buffer)
112+
err = msg.Encode(w, 0)
113+
require.NoError(t, err)
114+
115+
require.Equal(t, rawBytes, w.Bytes())
116+
}

lnwire/test_message.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,7 @@ func (dc *DynCommit) RandTestMessage(t *rapid.T) Message {
10251025
DynAck: *da,
10261026
}
10271027

1028-
extraData := RandExtraOpaqueData(t, ignoreRecords)
1029-
if len(extraData) > 0 {
1030-
msg.ExtraData = extraData
1031-
}
1028+
msg.ExtraData = RandExtraOpaqueData(t, ignoreRecords)
10321029

10331030
return msg
10341031
}

0 commit comments

Comments
 (0)