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

lightningnetwork / lnd / 13999301927

21 Mar 2025 07:18PM UTC coverage: 68.989% (+9.9%) from 59.126%
13999301927

push

github

web-flow
Merge pull request #9623 from Roasbeef/size-msg-test-msg

Size msg test msg

1461 of 1572 new or added lines in 43 files covered. (92.94%)

28 existing lines in 6 files now uncovered.

132898 of 192637 relevant lines covered (68.99%)

22200.59 hits per line

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

77.45
/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
        // Signature is a Schnorr signature over the TLV stream of the message.
16
        Signature Sig
17

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

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

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

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

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

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

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

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

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

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

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

229✔
82
        return c.DecodeTLVRecords(r)
229✔
83
}
84

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

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

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

113
        if _, ok := typeMap[c.BitcoinKey1.TlvType()]; ok {
231✔
114
                c.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
53✔
115
        }
53✔
116

117
        if _, ok := typeMap[c.BitcoinKey2.TlvType()]; ok {
231✔
118
                c.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
53✔
119
        }
53✔
120

121
        if _, ok := typeMap[c.MerkleRootHash.TlvType()]; ok {
233✔
122
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
55✔
123
        }
55✔
124

125
        if len(tlvRecords) != 0 {
355✔
126
                c.ExtraOpaqueData = tlvRecords
177✔
127
        }
177✔
128

129
        return nil
178✔
130
}
131

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

146
        return WriteBytes(w, c.ExtraOpaqueData)
139✔
147
}
148

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

103✔
159
                recordProducers = append(recordProducers, &hash)
103✔
160
        }
103✔
161

162
        recordProducers = append(recordProducers,
143✔
163
                &c.Features, &c.ShortChannelID, &c.Capacity, &c.NodeID1,
143✔
164
                &c.NodeID2,
143✔
165
        )
143✔
166

143✔
167
        c.BitcoinKey1.WhenSome(func(key tlv.RecordT[tlv.TlvType12, [33]byte]) {
194✔
168
                recordProducers = append(recordProducers, &key)
51✔
169
        })
51✔
170

171
        c.BitcoinKey2.WhenSome(func(key tlv.RecordT[tlv.TlvType14, [33]byte]) {
196✔
172
                recordProducers = append(recordProducers, &key)
53✔
173
        })
53✔
174

175
        c.MerkleRootHash.WhenSome(
143✔
176
                func(hash tlv.RecordT[tlv.TlvType16, [32]byte]) {
195✔
177
                        recordProducers = append(recordProducers, &hash)
52✔
178
                },
52✔
179
        )
180

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

186
        return c.ExtraOpaqueData, nil
143✔
187
}
188

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

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

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

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

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

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

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

236
// SCID returns the short channel ID of the channel.
237
//
238
// NOTE: This is part of the ChannelAnnouncement interface.
239
func (c *ChannelAnnouncement2) SCID() ShortChannelID {
×
240
        return c.ShortChannelID.Val
×
241
}
×
242

243
// A compile-time check to ensure that ChannelAnnouncement2 implements the
244
// ChannelAnnouncement interface.
245
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