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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

79.8
/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 {
232✔
110
                c.ChainHash.Val = chainHash.Val
54✔
111
        }
54✔
112

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

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

121
        if _, ok := typeMap[c.MerkleRootHash.TlvType()]; ok {
205✔
122
                c.MerkleRootHash = tlv.SomeRecordT(merkleRootHash)
27✔
123
        }
27✔
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) {
194✔
156
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
51✔
157
                hash.Val = c.ChainHash.Val
51✔
158

51✔
159
                recordProducers = append(recordProducers, &hash)
51✔
160
        }
51✔
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]) {
196✔
168
                recordProducers = append(recordProducers, &key)
53✔
169
        })
53✔
170

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

175
        c.MerkleRootHash.WhenSome(
143✔
176
                func(hash tlv.RecordT[tlv.TlvType16, [32]byte]) {
167✔
177
                        recordProducers = append(recordProducers, &hash)
24✔
178
                },
24✔
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
// A compile time check to ensure ChannelAnnouncement2 implements the
198
// lnwire.Message interface.
199
var _ Message = (*ChannelAnnouncement2)(nil)
200

201
// Node1KeyBytes returns the bytes representing the public key of node 1 in the
202
// channel.
203
//
204
// NOTE: This is part of the ChannelAnnouncement interface.
205
func (c *ChannelAnnouncement2) Node1KeyBytes() [33]byte {
×
206
        return c.NodeID1.Val
×
207
}
×
208

209
// Node2KeyBytes returns the bytes representing the public key of node 2 in the
210
// channel.
211
//
212
// NOTE: This is part of the ChannelAnnouncement interface.
213
func (c *ChannelAnnouncement2) Node2KeyBytes() [33]byte {
×
214
        return c.NodeID2.Val
×
215
}
×
216

217
// GetChainHash returns the hash of the chain which this channel's funding
218
// transaction is confirmed in.
219
//
220
// NOTE: This is part of the ChannelAnnouncement interface.
221
func (c *ChannelAnnouncement2) GetChainHash() chainhash.Hash {
×
222
        return c.ChainHash.Val
×
223
}
×
224

225
// SCID returns the short channel ID of the channel.
226
//
227
// NOTE: This is part of the ChannelAnnouncement interface.
228
func (c *ChannelAnnouncement2) SCID() ShortChannelID {
×
229
        return c.ShortChannelID.Val
×
230
}
×
231

232
// A compile-time check to ensure that ChannelAnnouncement2 implements the
233
// ChannelAnnouncement interface.
234
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