• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

web-flow
Merge b9b960345 into 09b674508
Pull Request #9623: Size msg test msg

0 of 1518 new or added lines in 42 files covered. (0.0%)

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lnwire/channel_announcement_2.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/btcsuite/btcd/chaincfg"
8
        "github.com/btcsuite/btcd/chaincfg/chainhash"
9
        "github.com/lightningnetwork/lnd/tlv"
10
        "pgregory.net/rapid"
11
)
12

13
// ChannelAnnouncement2 message is used to announce the existence of a taproot
14
// channel between two peers in the network.
15
type ChannelAnnouncement2 struct {
16
        // Signature is a Schnorr signature over the TLV stream of the message.
17
        Signature Sig
18

19
        // ChainHash denotes the target chain that this channel was opened
20
        // within. This value should be the genesis hash of the target chain.
21
        ChainHash tlv.RecordT[tlv.TlvType0, chainhash.Hash]
22

23
        // Features is the feature vector that encodes the features supported
24
        // by the target node. This field can be used to signal the type of the
25
        // channel, or modifications to the fields that would normally follow
26
        // this vector.
27
        Features tlv.RecordT[tlv.TlvType2, RawFeatureVector]
28

29
        // ShortChannelID is the unique description of the funding transaction,
30
        // or where exactly it's located within the target blockchain.
31
        ShortChannelID tlv.RecordT[tlv.TlvType4, ShortChannelID]
32

33
        // Capacity is the number of satoshis of the capacity of this channel.
34
        // It must be less than or equal to the value of the on-chain funding
35
        // output.
36
        Capacity tlv.RecordT[tlv.TlvType6, uint64]
37

38
        // NodeID1 is the numerically-lesser public key ID of one of the channel
39
        // operators.
40
        NodeID1 tlv.RecordT[tlv.TlvType8, [33]byte]
41

42
        // NodeID2 is the numerically-greater public key ID of one of the
43
        // channel operators.
44
        NodeID2 tlv.RecordT[tlv.TlvType10, [33]byte]
45

46
        // BitcoinKey1 is the public key of the key used by Node1 in the
47
        // construction of the on-chain funding transaction. This is an optional
48
        // field and only needs to be set if the 4-of-4 MuSig construction was
49
        // used in the creation of the message signature.
50
        BitcoinKey1 tlv.OptionalRecordT[tlv.TlvType12, [33]byte]
51

52
        // BitcoinKey2 is the public key of the key used by Node2 in the
53
        // construction of the on-chain funding transaction. This is an optional
54
        // field and only needs to be set if the 4-of-4 MuSig construction was
55
        // used in the creation of the message signature.
56
        BitcoinKey2 tlv.OptionalRecordT[tlv.TlvType14, [33]byte]
57

58
        // MerkleRootHash is the hash used to create the optional tweak in the
59
        // funding output. If this is not set but the bitcoin keys are, then
60
        // the funding output is a pure 2-of-2 MuSig aggregate public key.
61
        MerkleRootHash tlv.OptionalRecordT[tlv.TlvType16, [32]byte]
62

63
        // ExtraOpaqueData is the set of data that was appended to this
64
        // message, some of which we may not actually know how to iterate or
65
        // parse. By holding onto this data, we ensure that we're able to
66
        // properly validate the set of signatures that cover these new fields,
67
        // and ensure we're able to make upgrades to the network in a forwards
68
        // compatible manner.
69
        ExtraOpaqueData ExtraOpaqueData
70
}
71

72
// Decode deserializes a serialized AnnounceSignatures1 stored in the passed
73
// io.Reader observing the specified protocol version.
74
//
75
// This is part of the lnwire.Message interface.
UNCOV
76
func (c *ChannelAnnouncement2) Decode(r io.Reader, _ uint32) error {
×
UNCOV
77
        err := ReadElement(r, &c.Signature)
×
UNCOV
78
        if err != nil {
×
UNCOV
79
                return err
×
UNCOV
80
        }
×
UNCOV
81
        c.Signature.ForceSchnorr()
×
UNCOV
82

×
UNCOV
83
        return c.DecodeTLVRecords(r)
×
84
}
85

86
// DecodeTLVRecords decodes only the TLV section of the message.
UNCOV
87
func (c *ChannelAnnouncement2) DecodeTLVRecords(r io.Reader) error {
×
UNCOV
88
        // First extract into extra opaque data.
×
UNCOV
89
        var tlvRecords ExtraOpaqueData
×
UNCOV
90
        if err := ReadElements(r, &tlvRecords); err != nil {
×
91
                return err
×
92
        }
×
93

UNCOV
94
        var (
×
UNCOV
95
                chainHash      = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
×
UNCOV
96
                btcKey1        = tlv.ZeroRecordT[tlv.TlvType12, [33]byte]()
×
UNCOV
97
                btcKey2        = tlv.ZeroRecordT[tlv.TlvType14, [33]byte]()
×
UNCOV
98
                merkleRootHash = tlv.ZeroRecordT[tlv.TlvType16, [32]byte]()
×
UNCOV
99
        )
×
UNCOV
100
        typeMap, err := tlvRecords.ExtractRecords(
×
UNCOV
101
                &chainHash, &c.Features, &c.ShortChannelID, &c.Capacity,
×
UNCOV
102
                &c.NodeID1, &c.NodeID2, &btcKey1, &btcKey2, &merkleRootHash,
×
UNCOV
103
        )
×
UNCOV
104
        if err != nil {
×
UNCOV
105
                return err
×
UNCOV
106
        }
×
107

108
        // By default, the chain-hash is the bitcoin mainnet genesis block hash.
UNCOV
109
        c.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
×
UNCOV
110
        if _, ok := typeMap[c.ChainHash.TlvType()]; ok {
×
UNCOV
111
                c.ChainHash.Val = chainHash.Val
×
UNCOV
112
        }
×
113

UNCOV
114
        if _, ok := typeMap[c.BitcoinKey1.TlvType()]; ok {
×
UNCOV
115
                c.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
×
UNCOV
116
        }
×
117

UNCOV
118
        if _, ok := typeMap[c.BitcoinKey2.TlvType()]; ok {
×
UNCOV
119
                c.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
×
UNCOV
120
        }
×
121

UNCOV
122
        if _, ok := typeMap[c.MerkleRootHash.TlvType()]; ok {
×
UNCOV
123
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
×
UNCOV
124
        }
×
125

UNCOV
126
        if len(tlvRecords) != 0 {
×
UNCOV
127
                c.ExtraOpaqueData = tlvRecords
×
UNCOV
128
        }
×
129

UNCOV
130
        return nil
×
131
}
132

133
// Encode serializes the target AnnounceSignatures1 into the passed io.Writer
134
// observing the protocol version specified.
135
//
136
// This is part of the lnwire.Message interface.
UNCOV
137
func (c *ChannelAnnouncement2) Encode(w *bytes.Buffer, _ uint32) error {
×
UNCOV
138
        _, err := w.Write(c.Signature.RawBytes())
×
UNCOV
139
        if err != nil {
×
140
                return err
×
141
        }
×
UNCOV
142
        _, err = c.DataToSign()
×
UNCOV
143
        if err != nil {
×
144
                return err
×
145
        }
×
146

UNCOV
147
        return WriteBytes(w, c.ExtraOpaqueData)
×
148
}
149

150
// DataToSign encodes the data to be signed into the ExtraOpaqueData member and
151
// returns it.
UNCOV
152
func (c *ChannelAnnouncement2) DataToSign() ([]byte, error) {
×
UNCOV
153
        // The chain-hash record is only included if it is _not_ equal to the
×
UNCOV
154
        // bitcoin mainnet genisis block hash.
×
UNCOV
155
        var recordProducers []tlv.RecordProducer
×
UNCOV
156
        if !c.ChainHash.Val.IsEqual(chaincfg.MainNetParams.GenesisHash) {
×
UNCOV
157
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
×
UNCOV
158
                hash.Val = c.ChainHash.Val
×
UNCOV
159

×
UNCOV
160
                recordProducers = append(recordProducers, &hash)
×
UNCOV
161
        }
×
162

UNCOV
163
        recordProducers = append(recordProducers,
×
UNCOV
164
                &c.Features, &c.ShortChannelID, &c.Capacity, &c.NodeID1,
×
UNCOV
165
                &c.NodeID2,
×
UNCOV
166
        )
×
UNCOV
167

×
UNCOV
168
        c.BitcoinKey1.WhenSome(func(key tlv.RecordT[tlv.TlvType12, [33]byte]) {
×
UNCOV
169
                recordProducers = append(recordProducers, &key)
×
UNCOV
170
        })
×
171

UNCOV
172
        c.BitcoinKey2.WhenSome(func(key tlv.RecordT[tlv.TlvType14, [33]byte]) {
×
UNCOV
173
                recordProducers = append(recordProducers, &key)
×
UNCOV
174
        })
×
175

UNCOV
176
        c.MerkleRootHash.WhenSome(
×
UNCOV
177
                func(hash tlv.RecordT[tlv.TlvType16, [32]byte]) {
×
UNCOV
178
                        recordProducers = append(recordProducers, &hash)
×
UNCOV
179
                },
×
180
        )
181

UNCOV
182
        err := EncodeMessageExtraData(&c.ExtraOpaqueData, recordProducers...)
×
UNCOV
183
        if err != nil {
×
184
                return nil, err
×
185
        }
×
186

UNCOV
187
        return c.ExtraOpaqueData, nil
×
188
}
189

190
// MsgType returns the integer uniquely identifying this message type on the
191
// wire.
192
//
193
// This is part of the lnwire.Message interface.
UNCOV
194
func (c *ChannelAnnouncement2) MsgType() MessageType {
×
UNCOV
195
        return MsgChannelAnnouncement2
×
UNCOV
196
}
×
197

198
// SerializedSize returns the serialized size of the message in bytes.
199
//
200
// This is part of the lnwire.SizeableMessage interface.
NEW
201
func (c *ChannelAnnouncement2) SerializedSize() (uint32, error) {
×
NEW
202
        return MessageSerializedSize(c)
×
NEW
203
}
×
204

205
// A compile time check to ensure ChannelAnnouncement2 implements the
206
// lnwire.Message interface.
207
var _ Message = (*ChannelAnnouncement2)(nil)
208

209
// A compile time check to ensure ChannelAnnouncement2 implements the
210
// lnwire.SizeableMessage interface.
211
var _ SizeableMessage = (*ChannelAnnouncement2)(nil)
212

213
// A compile time check to ensure ChannelAnnouncement2 implements the
214
// lnwire.TestMessage interface.
215
var _ TestMessage = (*ChannelAnnouncement2)(nil)
216

217
// Node1KeyBytes returns the bytes representing the public key of node 1 in the
218
// channel.
219
//
220
// NOTE: This is part of the ChannelAnnouncement interface.
221
func (c *ChannelAnnouncement2) Node1KeyBytes() [33]byte {
×
222
        return c.NodeID1.Val
×
223
}
×
224

225
// Node2KeyBytes returns the bytes representing the public key of node 2 in the
226
// channel.
227
//
228
// NOTE: This is part of the ChannelAnnouncement interface.
229
func (c *ChannelAnnouncement2) Node2KeyBytes() [33]byte {
×
230
        return c.NodeID2.Val
×
231
}
×
232

233
// GetChainHash returns the hash of the chain which this channel's funding
234
// transaction is confirmed in.
235
//
236
// NOTE: This is part of the ChannelAnnouncement interface.
237
func (c *ChannelAnnouncement2) GetChainHash() chainhash.Hash {
×
238
        return c.ChainHash.Val
×
239
}
×
240

241
// SCID returns the short channel ID of the channel.
242
//
243
// NOTE: This is part of the ChannelAnnouncement interface.
244
func (c *ChannelAnnouncement2) SCID() ShortChannelID {
×
245
        return c.ShortChannelID.Val
×
246
}
×
247

248
// A compile-time check to ensure that ChannelAnnouncement2 implements the
249
// ChannelAnnouncement interface.
250
var _ ChannelAnnouncement = (*ChannelAnnouncement2)(nil)
251

252
// RandTestMessage populates the message with random data suitable for testing.
253
// It uses the rapid testing framework to generate random values.
254
//
255
// This is part of the TestMessage interface.
NEW
256
func (c *ChannelAnnouncement2) RandTestMessage(t *rapid.T) Message {
×
NEW
257
        features := RandFeatureVector(t)
×
NEW
258
        shortChanID := RandShortChannelID(t)
×
NEW
259
        capacity := uint64(rapid.IntRange(1, 16777215).Draw(t, "capacity"))
×
NEW
260

×
NEW
261
        var nodeID1, nodeID2 [33]byte
×
NEW
262
        copy(nodeID1[:], RandPubKey(t).SerializeCompressed())
×
NEW
263
        copy(nodeID2[:], RandPubKey(t).SerializeCompressed())
×
NEW
264

×
NEW
265
        // Make sure nodeID1 is numerically less than nodeID2 (as per spec).
×
NEW
266
        if bytes.Compare(nodeID1[:], nodeID2[:]) > 0 {
×
NEW
267
                nodeID1, nodeID2 = nodeID2, nodeID1
×
NEW
268
        }
×
269

NEW
270
        chainHash := RandChainHash(t)
×
NEW
271
        var chainHashObj chainhash.Hash
×
NEW
272
        copy(chainHashObj[:], chainHash[:])
×
NEW
273

×
NEW
274
        msg := &ChannelAnnouncement2{
×
NEW
275
                Signature:       RandSignature(t),
×
NEW
276
                ChainHash:       tlv.NewPrimitiveRecord[tlv.TlvType0, chainhash.Hash](chainHashObj),
×
NEW
277
                Features:        tlv.NewRecordT[tlv.TlvType2, RawFeatureVector](*features),
×
NEW
278
                ShortChannelID:  tlv.NewRecordT[tlv.TlvType4, ShortChannelID](shortChanID),
×
NEW
279
                Capacity:        tlv.NewPrimitiveRecord[tlv.TlvType6, uint64](capacity),
×
NEW
280
                NodeID1:         tlv.NewPrimitiveRecord[tlv.TlvType8, [33]byte](nodeID1),
×
NEW
281
                NodeID2:         tlv.NewPrimitiveRecord[tlv.TlvType10, [33]byte](nodeID2),
×
NEW
282
                ExtraOpaqueData: RandExtraOpaqueData(t, nil),
×
NEW
283
        }
×
NEW
284

×
NEW
285
        msg.Signature.ForceSchnorr()
×
NEW
286

×
NEW
287
        // Randomly include optional fields
×
NEW
288
        if rapid.Bool().Draw(t, "includeBitcoinKey1") {
×
NEW
289
                var bitcoinKey1 [33]byte
×
NEW
290
                copy(bitcoinKey1[:], RandPubKey(t).SerializeCompressed())
×
NEW
291
                msg.BitcoinKey1 = tlv.SomeRecordT(
×
NEW
292
                        tlv.RecordT[tlv.TlvType12, [33]byte]{Val: bitcoinKey1},
×
NEW
293
                )
×
NEW
294
        }
×
295

NEW
296
        if rapid.Bool().Draw(t, "includeBitcoinKey2") {
×
NEW
297
                var bitcoinKey2 [33]byte
×
NEW
298
                copy(bitcoinKey2[:], RandPubKey(t).SerializeCompressed())
×
NEW
299
                msg.BitcoinKey2 = tlv.SomeRecordT(
×
NEW
300
                        tlv.RecordT[tlv.TlvType14, [33]byte]{Val: bitcoinKey2},
×
NEW
301
                )
×
NEW
302
        }
×
303

NEW
304
        if rapid.Bool().Draw(t, "includeMerkleRootHash") {
×
NEW
305
                hash := RandSHA256Hash(t)
×
NEW
306
                var merkleRootHash [32]byte
×
NEW
307
                copy(merkleRootHash[:], hash[:])
×
NEW
308
                msg.MerkleRootHash = tlv.SomeRecordT(
×
NEW
309
                        tlv.RecordT[tlv.TlvType16, [32]byte]{Val: merkleRootHash},
×
NEW
310
                )
×
NEW
311
        }
×
312

NEW
313
        return msg
×
314
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc