Skip to content

Commit 4160fd5

Browse files
committed
lnwire: update AnnounceSigs2 to use pure TLV
1 parent cf8ba06 commit 4160fd5

File tree

2 files changed

+69
-49
lines changed

2 files changed

+69
-49
lines changed

lnwire/announcement_signatures_2.go

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package lnwire
33
import (
44
"bytes"
55
"io"
6+
7+
"github.com/lightningnetwork/lnd/tlv"
68
)
79

810
// AnnounceSignatures2 is a direct message between two endpoints of a
@@ -14,27 +16,40 @@ type AnnounceSignatures2 struct {
1416
// Channel id is better for users and debugging and short channel id is
1517
// used for quick test on existence of the particular utxo inside the
1618
// blockchain, because it contains information about block.
17-
ChannelID ChannelID
19+
ChannelID tlv.RecordT[tlv.TlvType0, ChannelID]
1820

1921
// ShortChannelID is the unique description of the funding transaction.
2022
// It is constructed with the most significant 3 bytes as the block
2123
// height, the next 3 bytes indicating the transaction index within the
2224
// block, and the least significant two bytes indicating the output
2325
// index which pays to the channel.
24-
ShortChannelID ShortChannelID
26+
ShortChannelID tlv.RecordT[tlv.TlvType2, ShortChannelID]
2527

2628
// PartialSignature is the combination of the partial Schnorr signature
2729
// created for the node's bitcoin key with the partial signature created
2830
// for the node's node ID key.
29-
PartialSignature PartialSig
30-
31-
// ExtraOpaqueData is the set of data that was appended to this
32-
// message, some of which we may not actually know how to iterate or
33-
// parse. By holding onto this data, we ensure that we're able to
34-
// properly validate the set of signatures that cover these new fields,
35-
// and ensure we're able to make upgrades to the network in a forwards
36-
// compatible manner.
37-
ExtraOpaqueData ExtraOpaqueData
31+
PartialSignature tlv.RecordT[tlv.TlvType4, PartialSig]
32+
33+
// Any extra fields in the signed range that we do not yet know about,
34+
// but we need to keep them for signature validation and to produce a
35+
// valid message.
36+
ExtraFieldsInSignedRange map[uint64][]byte
37+
}
38+
39+
// NewAnnSigs2 is a constructor for AnnounceSignatures2.
40+
func NewAnnSigs2(chanID ChannelID, scid ShortChannelID,
41+
partialSig PartialSig) *AnnounceSignatures2 {
42+
43+
return &AnnounceSignatures2{
44+
ChannelID: tlv.NewRecordT[tlv.TlvType0, ChannelID](chanID),
45+
ShortChannelID: tlv.NewRecordT[tlv.TlvType2, ShortChannelID](
46+
scid,
47+
),
48+
PartialSignature: tlv.NewRecordT[tlv.TlvType4, PartialSig](
49+
partialSig,
50+
),
51+
ExtraFieldsInSignedRange: make(map[uint64][]byte, 0),
52+
}
3853
}
3954

4055
// A compile time check to ensure AnnounceSignatures2 implements the
@@ -46,32 +61,29 @@ var _ Message = (*AnnounceSignatures2)(nil)
4661
//
4762
// This is part of the lnwire.Message interface.
4863
func (a *AnnounceSignatures2) Decode(r io.Reader, _ uint32) error {
49-
return ReadElements(r,
50-
&a.ChannelID,
51-
&a.ShortChannelID,
52-
&a.PartialSignature,
53-
&a.ExtraOpaqueData,
54-
)
55-
}
56-
57-
// Encode serializes the target AnnounceSignatures2 into the passed io.Writer
58-
// observing the protocol version specified.
59-
//
60-
// This is part of the lnwire.Message interface.
61-
func (a *AnnounceSignatures2) Encode(w *bytes.Buffer, _ uint32) error {
62-
if err := WriteChannelID(w, a.ChannelID); err != nil {
64+
stream, err := tlv.NewStream(ProduceRecordsSorted(
65+
&a.ChannelID, &a.ShortChannelID, &a.PartialSignature,
66+
)...)
67+
if err != nil {
6368
return err
6469
}
6570

66-
if err := WriteShortChannelID(w, a.ShortChannelID); err != nil {
71+
typeMap, err := stream.DecodeWithParsedTypesP2P(r)
72+
if err != nil {
6773
return err
6874
}
6975

70-
if err := WriteElement(w, a.PartialSignature); err != nil {
71-
return err
72-
}
76+
a.ExtraFieldsInSignedRange = ExtraSignedFieldsFromTypeMap(typeMap)
77+
78+
return nil
79+
}
7380

74-
return WriteBytes(w, a.ExtraOpaqueData)
81+
// Encode serializes the target AnnounceSignatures2 into the passed io.Writer
82+
// observing the protocol version specified.
83+
//
84+
// This is part of the lnwire.Message interface.
85+
func (a *AnnounceSignatures2) Encode(w *bytes.Buffer, _ uint32) error {
86+
return EncodePureTLVMessage(a, w)
7587
}
7688

7789
// MsgType returns the integer uniquely identifying this message type on the
@@ -82,16 +94,34 @@ func (a *AnnounceSignatures2) MsgType() MessageType {
8294
return MsgAnnounceSignatures2
8395
}
8496

97+
// AllRecords returns all the TLV records for the message. This will include all
98+
// the records we know about along with any that we don't know about but that
99+
// fall in the signed TLV range.
100+
//
101+
// NOTE: this is part of the PureTLVMessage interface.
102+
func (a *AnnounceSignatures2) AllRecords() []tlv.Record {
103+
recordProducers := []tlv.RecordProducer{
104+
&a.ChannelID, &a.ShortChannelID,
105+
&a.PartialSignature,
106+
}
107+
108+
recordProducers = append(recordProducers, RecordsAsProducers(
109+
tlv.MapToRecords(a.ExtraFieldsInSignedRange),
110+
)...)
111+
112+
return ProduceRecordsSorted(recordProducers...)
113+
}
114+
85115
// SCID returns the ShortChannelID of the channel.
86116
//
87117
// NOTE: this is part of the AnnounceSignatures interface.
88118
func (a *AnnounceSignatures2) SCID() ShortChannelID {
89-
return a.ShortChannelID
119+
return a.ShortChannelID.Val
90120
}
91121

92122
// ChanID returns the ChannelID identifying the channel.
93123
//
94124
// NOTE: this is part of the AnnounceSignatures interface.
95125
func (a *AnnounceSignatures2) ChanID() ChannelID {
96-
return a.ChannelID
126+
return a.ChannelID.Val
97127
}

lnwire/lnwire_test.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,29 +1519,19 @@ func TestLightningWireProtocol(t *testing.T) {
15191519
MsgAnnounceSignatures2: func(v []reflect.Value,
15201520
r *rand.Rand) {
15211521

1522-
req := AnnounceSignatures2{
1523-
ShortChannelID: NewShortChanIDFromInt(
1524-
uint64(r.Int63()),
1525-
),
1526-
ExtraOpaqueData: make([]byte, 0),
1527-
}
1522+
var req AnnounceSignatures2
1523+
1524+
req.ExtraFieldsInSignedRange = randSignedRangeRecords(
1525+
t, r,
1526+
)
15281527

1529-
_, err := r.Read(req.ChannelID[:])
1528+
_, err := r.Read(req.ChannelID.Val[:])
15301529
require.NoError(t, err)
15311530

15321531
partialSig, err := randPartialSig(r)
15331532
require.NoError(t, err)
15341533

1535-
req.PartialSignature = *partialSig
1536-
1537-
numExtraBytes := r.Int31n(1000)
1538-
if numExtraBytes > 0 {
1539-
req.ExtraOpaqueData = make(
1540-
[]byte, numExtraBytes,
1541-
)
1542-
_, err := r.Read(req.ExtraOpaqueData[:])
1543-
require.NoError(t, err)
1544-
}
1534+
req.PartialSignature.Val = *partialSig
15451535

15461536
v[0] = reflect.ValueOf(req)
15471537
},

0 commit comments

Comments
 (0)