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

lightningnetwork / lnd / 15838907453

24 Jun 2025 01:26AM UTC coverage: 57.079% (-11.1%) from 68.172%
15838907453

Pull #9982

github

web-flow
Merge e42780be2 into 45c15646c
Pull Request #9982: lnwire+lnwallet: add LocalNonces field for splice nonce coordination w/ taproot channels

103 of 167 new or added lines in 5 files covered. (61.68%)

30191 existing lines in 463 files now uncovered.

96331 of 168768 relevant lines covered (57.08%)

0.6 hits per line

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

61.54
/lnwire/channel_reestablish.go
1
package lnwire
2

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

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

12
const (
13
        CRDynHeight tlv.Type = 20
14
)
15

16
// DynHeight is a newtype wrapper to get the proper RecordProducer instance
17
// to smoothly integrate with the ChannelReestablish Message instance.
18
type DynHeight uint64
19

20
// Record implements the RecordProducer interface, allowing a full tlv.Record
21
// object to be constructed from a DynHeight.
22
func (d *DynHeight) Record() tlv.Record {
1✔
23
        return tlv.MakePrimitiveRecord(CRDynHeight, (*uint64)(d))
1✔
24
}
1✔
25

26
// ChannelReestablish is a message sent between peers that have an existing
27
// open channel upon connection reestablishment. This message allows both sides
28
// to report their local state, and their current knowledge of the state of the
29
// remote commitment chain. If a deviation is detected and can be recovered
30
// from, then the necessary messages will be retransmitted. If the level of
31
// desynchronization is irreconcilable, then the channel will be force closed.
32
type ChannelReestablish struct {
33
        // ChanID is the channel ID of the channel state we're attempting to
34
        // synchronize with the remote party.
35
        ChanID ChannelID
36

37
        // NextLocalCommitHeight is the next local commitment height of the
38
        // sending party. If the height of the sender's commitment chain from
39
        // the receiver's Pov is one less that this number, then the sender
40
        // should re-send the *exact* same proposed commitment.
41
        //
42
        // In other words, the receiver should re-send their last sent
43
        // commitment iff:
44
        //
45
        //  * NextLocalCommitHeight == remoteCommitChain.Height
46
        //
47
        // This covers the case of a lost commitment which was sent by the
48
        // sender of this message, but never received by the receiver of this
49
        // message.
50
        NextLocalCommitHeight uint64
51

52
        // RemoteCommitTailHeight is the height of the receiving party's
53
        // unrevoked commitment from the PoV of the sender of this message. If
54
        // the height of the receiver's commitment is *one more* than this
55
        // value, then their prior RevokeAndAck message should be
56
        // retransmitted.
57
        //
58
        // In other words, the receiver should re-send their last sent
59
        // RevokeAndAck message iff:
60
        //
61
        //  * localCommitChain.tail().Height == RemoteCommitTailHeight + 1
62
        //
63
        // This covers the case of a lost revocation, wherein the receiver of
64
        // the message sent a revocation for a prior state, but the sender of
65
        // the message never fully processed it.
66
        RemoteCommitTailHeight uint64
67

68
        // LastRemoteCommitSecret is the last commitment secret that the
69
        // receiving node has sent to the sending party. This will be the
70
        // secret of the last revoked commitment transaction. Including this
71
        // provides proof that the sending node at least knows of this state,
72
        // as they couldn't have produced it if it wasn't sent, as the value
73
        // can be authenticated by querying the shachain or the receiving
74
        // party.
75
        LastRemoteCommitSecret [32]byte
76

77
        // LocalUnrevokedCommitPoint is the commitment point used in the
78
        // current un-revoked commitment transaction of the sending party.
79
        LocalUnrevokedCommitPoint *btcec.PublicKey
80

81
        // LocalNonce is an optional field that stores a local musig2 nonce.
82
        // This will only be populated if the simple taproot channels type was
83
        // negotiated.
84
        //
85
        LocalNonce OptMusig2NonceTLV
86

87
        // DynHeight is an optional field that stores the dynamic commitment
88
        // negotiation height that is incremented upon successful completion of
89
        // a dynamic commitment negotiation
90
        DynHeight fn.Option[DynHeight]
91

92
        // LocalNonces is an optional field that stores a map of local musig2
93
        // nonces, keyed by TXID. This is used for splice nonce coordination.
94
        LocalNonces OptLocalNonces
95

96
        // ExtraData is the set of data that was appended to this message to
97
        // fill out the full maximum transport message size. These fields can
98
        // be used to specify optional data such as custom TLV fields.
99
        ExtraData ExtraOpaqueData
100
}
101

102
// A compile time check to ensure ChannelReestablish implements the
103
// lnwire.Message interface.
104
var _ Message = (*ChannelReestablish)(nil)
105

106
// A compile time check to ensure ChannelReestablish implements the
107
// lnwire.SizeableMessage interface.
108
var _ SizeableMessage = (*ChannelReestablish)(nil)
109

110
// Encode serializes the target ChannelReestablish into the passed io.Writer
111
// observing the protocol version specified.
112
//
113
// This is part of the lnwire.Message interface.
114
func (a *ChannelReestablish) Encode(w *bytes.Buffer, pver uint32) error {
1✔
115
        if err := WriteChannelID(w, a.ChanID); err != nil {
1✔
116
                return err
×
117
        }
×
118

119
        if err := WriteUint64(w, a.NextLocalCommitHeight); err != nil {
1✔
120
                return err
×
121
        }
×
122

123
        if err := WriteUint64(w, a.RemoteCommitTailHeight); err != nil {
1✔
124
                return err
×
125
        }
×
126

127
        // If the commit point wasn't sent, then we won't write out any of the
128
        // remaining fields as they're optional.
129
        if a.LocalUnrevokedCommitPoint == nil {
1✔
UNCOV
130
                // However, we'll still write out the extra data if it's
×
UNCOV
131
                // present.
×
UNCOV
132
                //
×
UNCOV
133
                // NOTE: This is here primarily for the quickcheck tests, in
×
UNCOV
134
                // practice, we'll always populate this field.
×
UNCOV
135
                return WriteBytes(w, a.ExtraData)
×
UNCOV
136
        }
×
137

138
        // Otherwise, we'll write out the remaining elements.
139
        if err := WriteBytes(w, a.LastRemoteCommitSecret[:]); err != nil {
1✔
140
                return err
×
141
        }
×
142

143
        if err := WritePublicKey(w, a.LocalUnrevokedCommitPoint); err != nil {
1✔
144
                return err
×
145
        }
×
146

147
        recordProducers := make([]tlv.RecordProducer, 0, 3)
1✔
148
        a.LocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
2✔
149
                recordProducers = append(recordProducers, &localNonce)
1✔
150
        })
1✔
151
        a.DynHeight.WhenSome(func(h DynHeight) {
1✔
UNCOV
152
                recordProducers = append(recordProducers, &h)
×
UNCOV
153
        })
×
154
        a.LocalNonces.WhenSome(func(ln LocalNoncesData) {
2✔
155
                recordProducers = append(recordProducers, &ln)
1✔
156
        })
1✔
157

158
        err := EncodeMessageExtraData(&a.ExtraData, recordProducers...)
1✔
159
        if err != nil {
1✔
160
                return err
×
161
        }
×
162
        return WriteBytes(w, a.ExtraData)
1✔
163
}
164

165
// Decode deserializes a serialized ChannelReestablish stored in the passed
166
// io.Reader observing the specified protocol version.
167
//
168
// This is part of the lnwire.Message interface.
169
func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
1✔
170
        err := ReadElements(r,
1✔
171
                &a.ChanID,
1✔
172
                &a.NextLocalCommitHeight,
1✔
173
                &a.RemoteCommitTailHeight,
1✔
174
        )
1✔
175
        if err != nil {
1✔
UNCOV
176
                return err
×
UNCOV
177
        }
×
178

179
        // This message has currently defined optional fields. As a result,
180
        // we'll only proceed if there's still bytes remaining within the
181
        // reader.
182
        //
183
        // We'll manually parse out the optional fields in order to be able to
184
        // still utilize the io.Reader interface.
185

186
        // We'll first attempt to read the optional commit secret, if we're at
187
        // the EOF, then this means the field wasn't included so we can exit
188
        // early.
189
        var buf [32]byte
1✔
190
        _, err = io.ReadFull(r, buf[:32])
1✔
191
        if err == io.EOF {
1✔
UNCOV
192
                // If there aren't any more bytes, then we'll emplace an empty
×
UNCOV
193
                // extra data to make our quickcheck tests happy.
×
UNCOV
194
                a.ExtraData = make([]byte, 0)
×
UNCOV
195
                return nil
×
196
        } else if err != nil {
1✔
UNCOV
197
                return err
×
UNCOV
198
        }
×
199

200
        // If the field is present, then we'll copy it over and proceed.
201
        copy(a.LastRemoteCommitSecret[:], buf[:])
1✔
202

1✔
203
        // We'll conclude by parsing out the commitment point. We don't check
1✔
204
        // the error in this case, as it has included the commit secret, then
1✔
205
        // they MUST also include the commit point.
1✔
206
        if err = ReadElement(r, &a.LocalUnrevokedCommitPoint); err != nil {
1✔
UNCOV
207
                return err
×
UNCOV
208
        }
×
209

210
        var tlvRecords ExtraOpaqueData
1✔
211
        if err := ReadElements(r, &tlvRecords); err != nil {
1✔
212
                return err
×
213
        }
×
214

215
        var (
1✔
216
                dynHeight       DynHeight
1✔
217
                localNonce      = a.LocalNonce.Zero()
1✔
218
                localNoncesData LocalNoncesData
1✔
219
        )
1✔
220

1✔
221
        typeMap, err := tlvRecords.ExtractRecords(
1✔
222
                &localNonce, &dynHeight, &localNoncesData,
1✔
223
        )
1✔
224
        if err != nil {
1✔
UNCOV
225
                return err
×
UNCOV
226
        }
×
227

228
        if val, ok := typeMap[a.LocalNonce.TlvType()]; ok && val == nil {
2✔
229
                a.LocalNonce = tlv.SomeRecordT(localNonce)
1✔
230
        }
1✔
231
        if val, ok := typeMap[CRDynHeight]; ok && val == nil {
1✔
UNCOV
232
                a.DynHeight = fn.Some(dynHeight)
×
UNCOV
233
        }
×
234
        if val, ok := typeMap[(LocalNoncesRecordTypeDef)(nil).TypeVal()]; ok && val == nil {
2✔
235
                a.LocalNonces = SomeLocalNonces(localNoncesData)
1✔
236
        }
1✔
237

238
        if len(tlvRecords) != 0 {
2✔
239
                a.ExtraData = tlvRecords
1✔
240
        }
1✔
241
        return nil
1✔
242
}
243

244
// MsgType returns the integer uniquely identifying this message type on the
245
// wire.
246
//
247
// This is part of the lnwire.Message interface.
248
func (a *ChannelReestablish) MsgType() MessageType {
1✔
249
        return MsgChannelReestablish
1✔
250
}
1✔
251

252
// SerializedSize returns the serialized size of the message in bytes.
253
//
254
// This is part of the lnwire.SizeableMessage interface.
255
func (a *ChannelReestablish) SerializedSize() (uint32, error) {
×
256
        return MessageSerializedSize(a)
×
257
}
×
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