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

lightningnetwork / lnd / 12430766295

20 Dec 2024 11:38AM UTC coverage: 52.607% (-6.1%) from 58.716%
12430766295

Pull #9384

github

ziggie1984
funding: refactor gossip msg code

We almost never need to create all messages at the same time
(ChanUpdate,ChanAnnouncement,Proof) so we split it up into own
functions.
Pull Request #9384: Refactor gossip msg code

224 of 279 new or added lines in 7 files covered. (80.29%)

27070 existing lines in 437 files now uncovered.

53540 of 101773 relevant lines covered (52.61%)

4.11 hits per line

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

78.02
/lnwire/channel_ready.go
1
package lnwire
2

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

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/lightningnetwork/lnd/tlv"
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 {
4✔
56
        return &ChannelReady{
4✔
57
                ChanID:                 cid,
4✔
58
                NextPerCommitmentPoint: npcp,
4✔
59
                ExtraData:              nil,
4✔
60
        }
4✔
61
}
4✔
62

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

67
// Decode deserializes the serialized ChannelReady message stored in the
68
// passed io.Reader into the target ChannelReady using the deserialization
69
// rules defined by the passed protocol version.
70
//
71
// This is part of the lnwire.Message interface.
72
func (c *ChannelReady) Decode(r io.Reader, _ uint32) error {
4✔
73
        // Read all the mandatory fields in the message.
4✔
74
        err := ReadElements(r,
4✔
75
                &c.ChanID,
4✔
76
                &c.NextPerCommitmentPoint,
4✔
77
        )
4✔
78
        if err != nil {
4✔
UNCOV
79
                return err
×
UNCOV
80
        }
×
81

82
        var tlvRecords ExtraOpaqueData
4✔
83
        if err := ReadElements(r, &tlvRecords); err != nil {
4✔
84
                return err
×
85
        }
×
86

87
        // Next we'll parse out the set of known records. For now, this is just
88
        // the AliasScidRecordType.
89
        var (
4✔
90
                aliasScid  ShortChannelID
4✔
91
                localNonce = c.NextLocalNonce.Zero()
4✔
92
                nodeNonce  = tlv.ZeroRecordT[tlv.TlvType0, Musig2Nonce]()
4✔
93
                btcNonce   = tlv.ZeroRecordT[tlv.TlvType2, Musig2Nonce]()
4✔
94
        )
4✔
95
        typeMap, err := tlvRecords.ExtractRecords(
4✔
96
                &btcNonce, &aliasScid, &nodeNonce, &localNonce,
4✔
97
        )
4✔
98
        if err != nil {
4✔
UNCOV
99
                return err
×
UNCOV
100
        }
×
101

102
        // We'll only set AliasScid if the corresponding TLV type was included
103
        // in the stream.
104
        if val, ok := typeMap[AliasScidRecordType]; ok && val == nil {
8✔
105
                c.AliasScid = &aliasScid
4✔
106
        }
4✔
107
        if val, ok := typeMap[c.NextLocalNonce.TlvType()]; ok && val == nil {
8✔
108
                c.NextLocalNonce = tlv.SomeRecordT(localNonce)
4✔
109
        }
4✔
110
        val, ok := typeMap[c.AnnouncementBitcoinNonce.TlvType()]
4✔
111
        if ok && val == nil {
4✔
UNCOV
112
                c.AnnouncementBitcoinNonce = tlv.SomeRecordT(btcNonce)
×
UNCOV
113
        }
×
114
        val, ok = typeMap[c.AnnouncementNodeNonce.TlvType()]
4✔
115
        if ok && val == nil {
4✔
UNCOV
116
                c.AnnouncementNodeNonce = tlv.SomeRecordT(nodeNonce)
×
UNCOV
117
        }
×
118

119
        if len(tlvRecords) != 0 {
8✔
120
                c.ExtraData = tlvRecords
4✔
121
        }
4✔
122

123
        return nil
4✔
124
}
125

126
// Encode serializes the target ChannelReady message into the passed io.Writer
127
// implementation. Serialization will observe the rules defined by the passed
128
// protocol version.
129
//
130
// This is part of the lnwire.Message interface.
131
func (c *ChannelReady) Encode(w *bytes.Buffer, _ uint32) error {
4✔
132
        if err := WriteChannelID(w, c.ChanID); err != nil {
4✔
133
                return err
×
134
        }
×
135

136
        if err := WritePublicKey(w, c.NextPerCommitmentPoint); err != nil {
4✔
137
                return err
×
138
        }
×
139

140
        // We'll only encode the AliasScid in a TLV segment if it exists.
141
        recordProducers := make([]tlv.RecordProducer, 0, 4)
4✔
142
        if c.AliasScid != nil {
8✔
143
                recordProducers = append(recordProducers, c.AliasScid)
4✔
144
        }
4✔
145
        c.NextLocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
8✔
146
                recordProducers = append(recordProducers, &localNonce)
4✔
147
        })
4✔
148
        c.AnnouncementBitcoinNonce.WhenSome(
4✔
149
                func(nonce tlv.RecordT[tlv.TlvType2, Musig2Nonce]) {
4✔
UNCOV
150
                        recordProducers = append(recordProducers, &nonce)
×
UNCOV
151
                },
×
152
        )
153
        c.AnnouncementNodeNonce.WhenSome(
4✔
154
                func(nonce tlv.RecordT[tlv.TlvType0, Musig2Nonce]) {
4✔
UNCOV
155
                        recordProducers = append(recordProducers, &nonce)
×
UNCOV
156
                },
×
157
        )
158

159
        err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
4✔
160
        if err != nil {
4✔
161
                return err
×
162
        }
×
163

164
        return WriteBytes(w, c.ExtraData)
4✔
165
}
166

167
// MsgType returns the uint32 code which uniquely identifies this message as a
168
// ChannelReady message on the wire.
169
//
170
// This is part of the lnwire.Message interface.
171
func (c *ChannelReady) MsgType() MessageType {
4✔
172
        return MsgChannelReady
4✔
173
}
4✔
174

175
// String returns a human-readable description of the ChannelReady msg.
176
func (c *ChannelReady) String() string {
4✔
177
        // Handle the case where the AliasScid is nil.
4✔
178
        aliasStr := "nil"
4✔
179
        if c.AliasScid != nil {
8✔
180
                aliasStr = fmt.Sprintf("%v(uint=%d)",
4✔
181
                        c.AliasScid, c.AliasScid.ToUint64())
4✔
182
        }
4✔
183

184
        return fmt.Sprintf("chan_id=%v, next_point=%x, aliasSCID=%s",
4✔
185
                c.ChanID, c.NextPerCommitmentPoint.SerializeCompressed(),
4✔
186
                aliasStr,
4✔
187
        )
4✔
188
}
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