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

lightningnetwork / lnd / 12414491588

19 Dec 2024 02:18PM UTC coverage: 57.468% (+8.1%) from 49.414%
12414491588

Pull #8777

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 #8777: multi: make reassignment of alias channel edge atomic

129 of 274 new or added lines in 7 files covered. (47.08%)

19694 existing lines in 247 files now uncovered.

102586 of 178511 relevant lines covered (57.47%)

24598.76 hits per line

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

79.12
/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 {
299✔
56
        return &ChannelReady{
299✔
57
                ChanID:                 cid,
299✔
58
                NextPerCommitmentPoint: npcp,
299✔
59
                ExtraData:              nil,
299✔
60
        }
299✔
61
}
299✔
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 {
193✔
73
        // Read all the mandatory fields in the message.
193✔
74
        err := ReadElements(r,
193✔
75
                &c.ChanID,
193✔
76
                &c.NextPerCommitmentPoint,
193✔
77
        )
193✔
78
        if err != nil {
201✔
79
                return err
8✔
80
        }
8✔
81

82
        var tlvRecords ExtraOpaqueData
185✔
83
        if err := ReadElements(r, &tlvRecords); err != nil {
185✔
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 (
185✔
90
                aliasScid  ShortChannelID
185✔
91
                localNonce = c.NextLocalNonce.Zero()
185✔
92
                nodeNonce  = tlv.ZeroRecordT[tlv.TlvType0, Musig2Nonce]()
185✔
93
                btcNonce   = tlv.ZeroRecordT[tlv.TlvType2, Musig2Nonce]()
185✔
94
        )
185✔
95
        typeMap, err := tlvRecords.ExtractRecords(
185✔
96
                &btcNonce, &aliasScid, &nodeNonce, &localNonce,
185✔
97
        )
185✔
98
        if err != nil {
230✔
99
                return err
45✔
100
        }
45✔
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 {
195✔
105
                c.AliasScid = &aliasScid
55✔
106
        }
55✔
107
        if val, ok := typeMap[c.NextLocalNonce.TlvType()]; ok && val == nil {
191✔
108
                c.NextLocalNonce = tlv.SomeRecordT(localNonce)
51✔
109
        }
51✔
110
        val, ok := typeMap[c.AnnouncementBitcoinNonce.TlvType()]
140✔
111
        if ok && val == nil {
191✔
112
                c.AnnouncementBitcoinNonce = tlv.SomeRecordT(btcNonce)
51✔
113
        }
51✔
114
        val, ok = typeMap[c.AnnouncementNodeNonce.TlvType()]
140✔
115
        if ok && val == nil {
197✔
116
                c.AnnouncementNodeNonce = tlv.SomeRecordT(nodeNonce)
57✔
117
        }
57✔
118

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

123
        return nil
140✔
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 {
120✔
132
        if err := WriteChannelID(w, c.ChanID); err != nil {
120✔
133
                return err
×
134
        }
×
135

136
        if err := WritePublicKey(w, c.NextPerCommitmentPoint); err != nil {
120✔
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)
120✔
142
        if c.AliasScid != nil {
169✔
143
                recordProducers = append(recordProducers, c.AliasScid)
49✔
144
        }
49✔
145
        c.NextLocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
167✔
146
                recordProducers = append(recordProducers, &localNonce)
47✔
147
        })
47✔
148
        c.AnnouncementBitcoinNonce.WhenSome(
120✔
149
                func(nonce tlv.RecordT[tlv.TlvType2, Musig2Nonce]) {
170✔
150
                        recordProducers = append(recordProducers, &nonce)
50✔
151
                },
50✔
152
        )
153
        c.AnnouncementNodeNonce.WhenSome(
120✔
154
                func(nonce tlv.RecordT[tlv.TlvType0, Musig2Nonce]) {
173✔
155
                        recordProducers = append(recordProducers, &nonce)
53✔
156
                },
53✔
157
        )
158

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

164
        return WriteBytes(w, c.ExtraData)
120✔
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 {
146✔
172
        return MsgChannelReady
146✔
173
}
146✔
174

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

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