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

lightningnetwork / lnd / 18197857992

02 Oct 2025 03:32PM UTC coverage: 66.622% (-0.02%) from 66.646%
18197857992

Pull #10267

github

web-flow
Merge 0d9bfccfe into 1c2ff4a7e
Pull Request #10267: [g175] multi: small G175 preparations

24 of 141 new or added lines in 12 files covered. (17.02%)

64 existing lines in 20 files now uncovered.

137216 of 205963 relevant lines covered (66.62%)

21302.01 hits per line

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

56.49
/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
)
11

12
// ChannelAnnouncement2 message is used to announce the existence of a taproot
13
// channel between two peers in the network.
14
type ChannelAnnouncement2 struct {
15
        // ChainHash denotes the target chain that this channel was opened
16
        // within. This value should be the genesis hash of the target chain.
17
        ChainHash tlv.RecordT[tlv.TlvType0, chainhash.Hash]
18

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

25
        // ShortChannelID is the unique description of the funding transaction,
26
        // or where exactly it's located within the target blockchain.
27
        ShortChannelID tlv.RecordT[tlv.TlvType4, ShortChannelID]
28

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

34
        // NodeID1 is the numerically-lesser public key ID of one of the channel
35
        // operators.
36
        NodeID1 tlv.RecordT[tlv.TlvType8, [33]byte]
37

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

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

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

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

59
        // Outpoint is the outpoint of the funding transaction.
60
        Outpoint tlv.RecordT[tlv.TlvType18, OutPoint]
61

62
        // Signature is a Schnorr signature over serialised signed-range TLV
63
        // stream of the message.
64
        Signature tlv.RecordT[tlv.TlvType160, Sig]
65

66
        // Any extra fields in the signed range that we do not yet know about,
67
        // but we need to keep them for signature validation and to produce a
68
        // valid message.
69
        ExtraSignedFields
70
}
71

72
// Encode serializes the target AnnounceSignatures1 into the passed io.Writer
73
// observing the protocol version specified.
74
//
75
// This is part of the lnwire.Message interface.
76
func (c *ChannelAnnouncement2) Encode(w *bytes.Buffer, _ uint32) error {
102✔
77
        return EncodePureTLVMessage(c, w)
102✔
78
}
102✔
79

80
// AllRecords returns all the TLV records for the message. This will include all
81
// the records we know about along with any that we don't know about but that
82
// fall in the signed TLV range.
83
//
84
// NOTE: this is part of the PureTLVMessage interface.
85
func (c *ChannelAnnouncement2) AllRecords() []tlv.Record {
108✔
86
        recordProducers := append(
108✔
87
                c.nonSignatureRecordProducers(), &c.Signature,
108✔
88
        )
108✔
89

108✔
90
        return ProduceRecordsSorted(recordProducers...)
108✔
91
}
108✔
92

93
// nonSignatureRecordProducers returns all the TLV record producers for the
94
// message except the signature record producer.
95
//
96
//nolint:ll
97
func (c *ChannelAnnouncement2) nonSignatureRecordProducers() []tlv.RecordProducer {
108✔
98
        // The chain-hash record is only included if it is _not_ equal to the
108✔
99
        // bitcoin mainnet genisis block hash.
108✔
100
        var recordProducers []tlv.RecordProducer
108✔
101
        if !c.ChainHash.Val.IsEqual(chaincfg.MainNetParams.GenesisHash) {
209✔
102
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
101✔
103
                hash.Val = c.ChainHash.Val
101✔
104

101✔
105
                recordProducers = append(recordProducers, &hash)
101✔
106
        }
101✔
107

108
        recordProducers = append(recordProducers,
108✔
109
                &c.Features, &c.ShortChannelID, &c.Capacity, &c.NodeID1,
108✔
110
                &c.NodeID2,
108✔
111
        )
108✔
112

108✔
113
        c.BitcoinKey1.WhenSome(func(key tlv.RecordT[tlv.TlvType12, [33]byte]) {
162✔
114
                recordProducers = append(recordProducers, &key)
54✔
115
        })
54✔
116

117
        c.BitcoinKey2.WhenSome(func(key tlv.RecordT[tlv.TlvType14, [33]byte]) {
156✔
118
                recordProducers = append(recordProducers, &key)
48✔
119
        })
48✔
120

121
        c.MerkleRootHash.WhenSome(
108✔
122
                func(hash tlv.RecordT[tlv.TlvType16, [32]byte]) {
164✔
123
                        recordProducers = append(recordProducers, &hash)
56✔
124
                },
56✔
125
        )
126

127
        recordProducers = append(recordProducers, &c.Outpoint)
108✔
128
        recordProducers = append(recordProducers, RecordsAsProducers(
108✔
129
                tlv.MapToRecords(c.ExtraSignedFields),
108✔
130
        )...)
108✔
131

108✔
132
        return recordProducers
108✔
133
}
134

135
// Decode deserializes a serialized AnnounceSignatures1 stored in the passed
136
// io.Reader observing the specified protocol version.
137
//
138
// This is part of the lnwire.Message interface.
139
func (c *ChannelAnnouncement2) Decode(r io.Reader, _ uint32) error {
194✔
140
        var (
194✔
141
                chainHash      = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
194✔
142
                btcKey1        = tlv.ZeroRecordT[tlv.TlvType12, [33]byte]()
194✔
143
                btcKey2        = tlv.ZeroRecordT[tlv.TlvType14, [33]byte]()
194✔
144
                merkleRootHash = tlv.ZeroRecordT[tlv.TlvType16, [32]byte]()
194✔
145
        )
194✔
146
        stream, err := tlv.NewStream(ProduceRecordsSorted(
194✔
147
                &chainHash,
194✔
148
                &c.Features,
194✔
149
                &c.ShortChannelID,
194✔
150
                &c.Capacity,
194✔
151
                &c.NodeID1,
194✔
152
                &c.NodeID2,
194✔
153
                &btcKey1,
194✔
154
                &btcKey2,
194✔
155
                &merkleRootHash,
194✔
156
                &c.Outpoint,
194✔
157
                &c.Signature,
194✔
158
        )...)
194✔
159
        if err != nil {
194✔
160
                return err
×
161
        }
×
162
        c.Signature.Val.ForceSchnorr()
194✔
163

194✔
164
        typeMap, err := stream.DecodeWithParsedTypesP2P(r)
194✔
165
        if err != nil {
285✔
166
                return err
91✔
167
        }
91✔
168

169
        // By default, the chain-hash is the bitcoin mainnet genesis block hash.
170
        c.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
103✔
171
        if _, ok := typeMap[c.ChainHash.TlvType()]; ok {
204✔
172
                c.ChainHash.Val = chainHash.Val
101✔
173
        }
101✔
174

175
        if _, ok := typeMap[c.BitcoinKey1.TlvType()]; ok {
153✔
176
                c.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
50✔
177
        }
50✔
178

179
        if _, ok := typeMap[c.BitcoinKey2.TlvType()]; ok {
147✔
180
                c.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
44✔
181
        }
44✔
182

183
        if _, ok := typeMap[c.MerkleRootHash.TlvType()]; ok {
159✔
184
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
56✔
185
        }
56✔
186

187
        c.ExtraSignedFields = ExtraSignedFieldsFromTypeMap(typeMap)
103✔
188

103✔
189
        return nil
103✔
190
}
191

192
// DecodeNonSigTLVRecords decodes only the TLV section of the message.
193
func (c *ChannelAnnouncement2) DecodeNonSigTLVRecords(r io.Reader) error {
×
194
        var (
×
195
                chainHash      = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
×
196
                btcKey1        = tlv.ZeroRecordT[tlv.TlvType12, [33]byte]()
×
197
                btcKey2        = tlv.ZeroRecordT[tlv.TlvType14, [33]byte]()
×
198
                merkleRootHash = tlv.ZeroRecordT[tlv.TlvType16, [32]byte]()
×
199
        )
×
200
        stream, err := tlv.NewStream(ProduceRecordsSorted(
×
201
                &chainHash,
×
202
                &c.Features,
×
203
                &c.ShortChannelID,
×
204
                &c.Capacity,
×
205
                &c.NodeID1,
×
206
                &c.NodeID2,
×
207
                &btcKey1,
×
208
                &btcKey2,
×
209
                &merkleRootHash,
×
210
                &c.Outpoint,
×
211
        )...)
×
212
        if err != nil {
×
213
                return err
×
214
        }
×
215

216
        typeMap, err := stream.DecodeWithParsedTypesP2P(r)
×
217
        if err != nil {
×
218
                return err
×
219
        }
×
220

221
        // By default, the chain-hash is the bitcoin mainnet genesis block hash.
222
        c.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
×
223
        if _, ok := typeMap[c.ChainHash.TlvType()]; ok {
×
224
                c.ChainHash.Val = chainHash.Val
×
225
        }
×
226

227
        if _, ok := typeMap[c.BitcoinKey1.TlvType()]; ok {
×
228
                c.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
×
229
        }
×
230

231
        if _, ok := typeMap[c.BitcoinKey2.TlvType()]; ok {
×
232
                c.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
×
233
        }
×
234

235
        if _, ok := typeMap[c.MerkleRootHash.TlvType()]; ok {
×
236
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
×
237
        }
×
238

239
        c.ExtraSignedFields = ExtraSignedFieldsFromTypeMap(typeMap)
×
240

×
241
        return nil
×
242
}
243

244
// EncodeAllNonSigFields encodes the entire message to the given writer but
245
// excludes the signature field.
246
func (c *ChannelAnnouncement2) EncodeAllNonSigFields(w io.Writer) error {
×
247
        return EncodeRecordsTo(
×
248
                w, ProduceRecordsSorted(c.nonSignatureRecordProducers()...),
×
249
        )
×
250
}
×
251

252
// MsgType returns the integer uniquely identifying this message type on the
253
// wire.
254
//
255
// This is part of the lnwire.Message interface.
256
func (c *ChannelAnnouncement2) MsgType() MessageType {
101✔
257
        return MsgChannelAnnouncement2
101✔
258
}
101✔
259

260
// SerializedSize returns the serialized size of the message in bytes.
261
//
262
// This is part of the lnwire.SizeableMessage interface.
263
func (c *ChannelAnnouncement2) SerializedSize() (uint32, error) {
×
264
        return MessageSerializedSize(c)
×
265
}
×
266

267
// A compile time check to ensure ChannelAnnouncement2 implements the
268
// lnwire.Message interface.
269
var _ Message = (*ChannelAnnouncement2)(nil)
270

271
// A compile time check to ensure ChannelAnnouncement2 implements the
272
// lnwire.SizeableMessage interface.
273
var _ SizeableMessage = (*ChannelAnnouncement2)(nil)
274

275
// A compile time check to ensure ChannelAnnouncement2 implements the
276
// lnwire.PureTLVMessage interface.
277
var _ PureTLVMessage = (*ChannelAnnouncement2)(nil)
278

279
// Node1KeyBytes returns the bytes representing the public key of node 1 in the
280
// channel.
281
//
282
// NOTE: This is part of the ChannelAnnouncement interface.
283
func (c *ChannelAnnouncement2) Node1KeyBytes() [33]byte {
×
284
        return c.NodeID1.Val
×
285
}
×
286

287
// Node2KeyBytes returns the bytes representing the public key of node 2 in the
288
// channel.
289
//
290
// NOTE: This is part of the ChannelAnnouncement interface.
291
func (c *ChannelAnnouncement2) Node2KeyBytes() [33]byte {
×
292
        return c.NodeID2.Val
×
293
}
×
294

295
// GetChainHash returns the hash of the chain which this channel's funding
296
// transaction is confirmed in.
297
//
298
// NOTE: This is part of the ChannelAnnouncement interface.
299
func (c *ChannelAnnouncement2) GetChainHash() chainhash.Hash {
×
300
        return c.ChainHash.Val
×
301
}
×
302

303
// SCID returns the short channel ID of the channel.
304
//
305
// NOTE: This is part of the ChannelAnnouncement interface.
306
func (c *ChannelAnnouncement2) SCID() ShortChannelID {
×
307
        return c.ShortChannelID.Val
×
308
}
×
309

310
// GossipVersion returns the gossip version that this message is part of.
311
//
312
// NOTE: this is part of the GossipMessage interface.
NEW
313
func (c *ChannelAnnouncement2) GossipVersion() GossipVersion {
×
NEW
314
        return GossipVersion2
×
NEW
315
}
×
316

317
// A compile-time check to ensure that ChannelAnnouncement2 implements the
318
// ChannelAnnouncement interface.
319
var _ ChannelAnnouncement = (*ChannelAnnouncement2)(nil)
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