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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

58.33
/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 {
3✔
23
        return tlv.MakePrimitiveRecord(CRDynHeight, (*uint64)(d))
3✔
24
}
3✔
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
        // ExtraData is the set of data that was appended to this message to
93
        // fill out the full maximum transport message size. These fields can
94
        // be used to specify optional data such as custom TLV fields.
95
        ExtraData ExtraOpaqueData
96
}
97

98
// A compile time check to ensure ChannelReestablish implements the
99
// lnwire.Message interface.
100
var _ Message = (*ChannelReestablish)(nil)
101

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

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

115
        if err := WriteUint64(w, a.NextLocalCommitHeight); err != nil {
3✔
116
                return err
×
117
        }
×
118

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

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

134
        // Otherwise, we'll write out the remaining elements.
135
        if err := WriteBytes(w, a.LastRemoteCommitSecret[:]); err != nil {
3✔
136
                return err
×
137
        }
×
138

139
        if err := WritePublicKey(w, a.LocalUnrevokedCommitPoint); err != nil {
3✔
140
                return err
×
141
        }
×
142

143
        recordProducers := make([]tlv.RecordProducer, 0, 1)
3✔
144
        a.LocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
6✔
145
                recordProducers = append(recordProducers, &localNonce)
3✔
146
        })
3✔
147
        a.DynHeight.WhenSome(func(h DynHeight) {
3✔
UNCOV
148
                recordProducers = append(recordProducers, &h)
×
UNCOV
149
        })
×
150

151
        err := EncodeMessageExtraData(&a.ExtraData, recordProducers...)
3✔
152
        if err != nil {
3✔
153
                return err
×
154
        }
×
155

156
        return WriteBytes(w, a.ExtraData)
3✔
157
}
158

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

173
        // This message has currently defined optional fields. As a result,
174
        // we'll only proceed if there's still bytes remaining within the
175
        // reader.
176
        //
177
        // We'll manually parse out the optional fields in order to be able to
178
        // still utilize the io.Reader interface.
179

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

194
        // If the field is present, then we'll copy it over and proceed.
195
        copy(a.LastRemoteCommitSecret[:], buf[:])
3✔
196

3✔
197
        // We'll conclude by parsing out the commitment point. We don't check
3✔
198
        // the error in this case, as it has included the commit secret, then
3✔
199
        // they MUST also include the commit point.
3✔
200
        if err = ReadElement(r, &a.LocalUnrevokedCommitPoint); err != nil {
3✔
UNCOV
201
                return err
×
UNCOV
202
        }
×
203

204
        var tlvRecords ExtraOpaqueData
3✔
205
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
206
                return err
×
207
        }
×
208

209
        var (
3✔
210
                dynHeight  DynHeight
3✔
211
                localNonce = a.LocalNonce.Zero()
3✔
212
        )
3✔
213
        typeMap, err := tlvRecords.ExtractRecords(
3✔
214
                &localNonce, &dynHeight,
3✔
215
        )
3✔
216
        if err != nil {
3✔
UNCOV
217
                return err
×
UNCOV
218
        }
×
219

220
        if val, ok := typeMap[a.LocalNonce.TlvType()]; ok && val == nil {
6✔
221
                a.LocalNonce = tlv.SomeRecordT(localNonce)
3✔
222
        }
3✔
223
        if val, ok := typeMap[CRDynHeight]; ok && val == nil {
3✔
UNCOV
224
                a.DynHeight = fn.Some(dynHeight)
×
UNCOV
225
        }
×
226

227
        if len(tlvRecords) != 0 {
6✔
228
                a.ExtraData = tlvRecords
3✔
229
        }
3✔
230

231
        return nil
3✔
232
}
233

234
// MsgType returns the integer uniquely identifying this message type on the
235
// wire.
236
//
237
// This is part of the lnwire.Message interface.
238
func (a *ChannelReestablish) MsgType() MessageType {
3✔
239
        return MsgChannelReestablish
3✔
240
}
3✔
241

242
// SerializedSize returns the serialized size of the message in bytes.
243
//
244
// This is part of the lnwire.SizeableMessage interface.
245
func (a *ChannelReestablish) SerializedSize() (uint32, error) {
×
246
        return MessageSerializedSize(a)
×
247
}
×
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