• 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

50.0
/lnwire/channel_ready.go
1
package lnwire
2

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

7
        "github.com/btcsuite/btcd/btcec/v2"
8
        "github.com/lightningnetwork/lnd/tlv"
9
        "pgregory.net/rapid"
10
)
11

12
// ChannelReady is the message that both parties to a new channel creation
13
// send once they have observed the funding transaction being confirmed on the
14
// blockchain. ChannelReady contains the signatures necessary for the channel
15
// participants to advertise the existence of the channel to the rest of the
16
// network.
17
type ChannelReady struct {
18
        // ChanID is the outpoint of the channel's funding transaction. This
19
        // can be used to query for the channel in the database.
20
        ChanID ChannelID
21

22
        // NextPerCommitmentPoint is the secret that can be used to revoke the
23
        // next commitment transaction for the channel.
24
        NextPerCommitmentPoint *btcec.PublicKey
25

26
        // AliasScid is an alias ShortChannelID used to refer to the underlying
27
        // channel. It can be used instead of the confirmed on-chain
28
        // ShortChannelID for forwarding.
29
        AliasScid *ShortChannelID
30

31
        // NextLocalNonce is an optional field that stores a local musig2 nonce.
32
        // This will only be populated if the simple taproot channels type was
33
        // negotiated. This is the local nonce that will be used by the sender
34
        // to accept a new commitment state transition.
35
        NextLocalNonce OptMusig2NonceTLV
36

37
        // AnnouncementNodeNonce is an optional field that stores a public
38
        // nonce that will be used along with the node's ID key during signing
39
        // of the ChannelAnnouncement2 message.
40
        AnnouncementNodeNonce tlv.OptionalRecordT[tlv.TlvType0, Musig2Nonce]
41

42
        // AnnouncementBitcoinNonce is an optional field that stores a public
43
        // nonce that will be used along with the node's bitcoin key during
44
        // signing of the ChannelAnnouncement2 message.
45
        AnnouncementBitcoinNonce tlv.OptionalRecordT[tlv.TlvType2, Musig2Nonce]
46

47
        // ExtraData is the set of data that was appended to this message to
48
        // fill out the full maximum transport message size. These fields can
49
        // be used to specify optional data such as custom TLV fields.
50
        ExtraData ExtraOpaqueData
51
}
52

53
// NewChannelReady creates a new ChannelReady message, populating it with the
54
// necessary IDs and revocation secret.
55
func NewChannelReady(cid ChannelID, npcp *btcec.PublicKey) *ChannelReady {
3✔
56
        return &ChannelReady{
3✔
57
                ChanID:                 cid,
3✔
58
                NextPerCommitmentPoint: npcp,
3✔
59
                ExtraData:              nil,
3✔
60
        }
3✔
61
}
3✔
62

63
// A compile time check to ensure ChannelReady implements the lnwire.Message
64
// interface.
65
var _ Message = (*ChannelReady)(nil)
66

67
// A compile time check to ensure ChannelReady implements the lnwire.SizeableMessage
68
// interface.
69
var _ SizeableMessage = (*ChannelReady)(nil)
70

71
// A compile time check to ensure ChannelReady implements the lnwire.TestMessage
72
// interface.
73
var _ TestMessage = (*ChannelReady)(nil)
74

75
// Decode deserializes the serialized ChannelReady message stored in the
76
// passed io.Reader into the target ChannelReady using the deserialization
77
// rules defined by the passed protocol version.
78
//
79
// This is part of the lnwire.Message interface.
80
func (c *ChannelReady) Decode(r io.Reader, _ uint32) error {
3✔
81
        // Read all the mandatory fields in the message.
3✔
82
        err := ReadElements(r,
3✔
83
                &c.ChanID,
3✔
84
                &c.NextPerCommitmentPoint,
3✔
85
        )
3✔
86
        if err != nil {
3✔
UNCOV
87
                return err
×
UNCOV
88
        }
×
89

90
        var tlvRecords ExtraOpaqueData
3✔
91
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
92
                return err
×
93
        }
×
94

95
        // Next we'll parse out the set of known records. For now, this is just
96
        // the AliasScidRecordType.
97
        var (
3✔
98
                aliasScid  ShortChannelID
3✔
99
                localNonce = c.NextLocalNonce.Zero()
3✔
100
                nodeNonce  = tlv.ZeroRecordT[tlv.TlvType0, Musig2Nonce]()
3✔
101
                btcNonce   = tlv.ZeroRecordT[tlv.TlvType2, Musig2Nonce]()
3✔
102
        )
3✔
103
        typeMap, err := tlvRecords.ExtractRecords(
3✔
104
                &btcNonce, &aliasScid, &nodeNonce, &localNonce,
3✔
105
        )
3✔
106
        if err != nil {
3✔
UNCOV
107
                return err
×
UNCOV
108
        }
×
109

110
        // We'll only set AliasScid if the corresponding TLV type was included
111
        // in the stream.
112
        if val, ok := typeMap[AliasScidRecordType]; ok && val == nil {
6✔
113
                c.AliasScid = &aliasScid
3✔
114
        }
3✔
115
        if val, ok := typeMap[c.NextLocalNonce.TlvType()]; ok && val == nil {
6✔
116
                c.NextLocalNonce = tlv.SomeRecordT(localNonce)
3✔
117
        }
3✔
118
        val, ok := typeMap[c.AnnouncementBitcoinNonce.TlvType()]
3✔
119
        if ok && val == nil {
3✔
UNCOV
120
                c.AnnouncementBitcoinNonce = tlv.SomeRecordT(btcNonce)
×
UNCOV
121
        }
×
122
        val, ok = typeMap[c.AnnouncementNodeNonce.TlvType()]
3✔
123
        if ok && val == nil {
3✔
UNCOV
124
                c.AnnouncementNodeNonce = tlv.SomeRecordT(nodeNonce)
×
UNCOV
125
        }
×
126

127
        if len(tlvRecords) != 0 {
6✔
128
                c.ExtraData = tlvRecords
3✔
129
        }
3✔
130

131
        return nil
3✔
132
}
133

134
// Encode serializes the target ChannelReady message into the passed io.Writer
135
// implementation. Serialization will observe the rules defined by the passed
136
// protocol version.
137
//
138
// This is part of the lnwire.Message interface.
139
func (c *ChannelReady) Encode(w *bytes.Buffer, _ uint32) error {
3✔
140
        if err := WriteChannelID(w, c.ChanID); err != nil {
3✔
141
                return err
×
142
        }
×
143

144
        if err := WritePublicKey(w, c.NextPerCommitmentPoint); err != nil {
3✔
145
                return err
×
146
        }
×
147

148
        // We'll only encode the AliasScid in a TLV segment if it exists.
149
        recordProducers := make([]tlv.RecordProducer, 0, 4)
3✔
150
        if c.AliasScid != nil {
6✔
151
                recordProducers = append(recordProducers, c.AliasScid)
3✔
152
        }
3✔
153
        c.NextLocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
6✔
154
                recordProducers = append(recordProducers, &localNonce)
3✔
155
        })
3✔
156
        c.AnnouncementBitcoinNonce.WhenSome(
3✔
157
                func(nonce tlv.RecordT[tlv.TlvType2, Musig2Nonce]) {
3✔
UNCOV
158
                        recordProducers = append(recordProducers, &nonce)
×
UNCOV
159
                },
×
160
        )
161
        c.AnnouncementNodeNonce.WhenSome(
3✔
162
                func(nonce tlv.RecordT[tlv.TlvType0, Musig2Nonce]) {
3✔
UNCOV
163
                        recordProducers = append(recordProducers, &nonce)
×
UNCOV
164
                },
×
165
        )
166

167
        err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
3✔
168
        if err != nil {
3✔
169
                return err
×
170
        }
×
171

172
        return WriteBytes(w, c.ExtraData)
3✔
173
}
174

175
// MsgType returns the uint32 code which uniquely identifies this message as a
176
// ChannelReady message on the wire.
177
//
178
// This is part of the lnwire.Message interface.
179
func (c *ChannelReady) MsgType() MessageType {
3✔
180
        return MsgChannelReady
3✔
181
}
3✔
182

183
// SerializedSize returns the serialized size of the message in bytes.
184
//
185
// This is part of the lnwire.SizeableMessage interface.
NEW
186
func (c *ChannelReady) SerializedSize() (uint32, error) {
×
NEW
187
        return MessageSerializedSize(c)
×
NEW
188
}
×
189

190
// RandTestMessage populates the message with random data suitable for testing.
191
// It uses the rapid testing framework to generate random values.
192
//
193
// This is part of the TestMessage interface.
NEW
194
func (c *ChannelReady) RandTestMessage(t *rapid.T) Message {
×
NEW
195
        msg := &ChannelReady{
×
NEW
196
                ChanID:                 RandChannelID(t),
×
NEW
197
                NextPerCommitmentPoint: RandPubKey(t),
×
NEW
198
                ExtraData:              RandExtraOpaqueData(t, nil),
×
NEW
199
        }
×
NEW
200

×
NEW
201
        includeAliasScid := rapid.Bool().Draw(t, "includeAliasScid")
×
NEW
202
        includeNextLocalNonce := rapid.Bool().Draw(t, "includeNextLocalNonce")
×
NEW
203
        includeAnnouncementNodeNonce := rapid.Bool().Draw(
×
NEW
204
                t, "includeAnnouncementNodeNonce",
×
NEW
205
        )
×
NEW
206
        includeAnnouncementBitcoinNonce := rapid.Bool().Draw(
×
NEW
207
                t, "includeAnnouncementBitcoinNonce",
×
NEW
208
        )
×
NEW
209

×
NEW
210
        if includeAliasScid {
×
NEW
211
                scid := RandShortChannelID(t)
×
NEW
212
                msg.AliasScid = &scid
×
NEW
213
        }
×
214

NEW
215
        if includeNextLocalNonce {
×
NEW
216
                nonce := RandMusig2Nonce(t)
×
NEW
217
                msg.NextLocalNonce = SomeMusig2Nonce(nonce)
×
NEW
218
        }
×
219

NEW
220
        if includeAnnouncementNodeNonce {
×
NEW
221
                nonce := RandMusig2Nonce(t)
×
NEW
222
                msg.AnnouncementNodeNonce = tlv.SomeRecordT(
×
NEW
223
                        tlv.NewRecordT[tlv.TlvType0, Musig2Nonce](nonce),
×
NEW
224
                )
×
NEW
225
        }
×
226

NEW
227
        if includeAnnouncementBitcoinNonce {
×
NEW
228
                nonce := RandMusig2Nonce(t)
×
NEW
229
                msg.AnnouncementBitcoinNonce = tlv.SomeRecordT(
×
NEW
230
                        tlv.NewRecordT[tlv.TlvType2, Musig2Nonce](nonce),
×
NEW
231
                )
×
NEW
232
        }
×
233

NEW
234
        return msg
×
235
}
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