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

lightningnetwork / lnd / 13566028875

27 Feb 2025 12:09PM UTC coverage: 49.396% (-9.4%) from 58.748%
13566028875

Pull #9555

github

ellemouton
graph/db: populate the graph cache in Start instead of during construction

In this commit, we move the graph cache population logic out of the
ChannelGraph constructor and into its Start method instead.
Pull Request #9555: graph: extract cache from CRUD [6]

34 of 54 new or added lines in 4 files covered. (62.96%)

27464 existing lines in 436 files now uncovered.

101095 of 204664 relevant lines covered (49.4%)

1.54 hits per line

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

67.27
/lnwire/accept_channel.go
1
package lnwire
2

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

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

12
// AcceptChannel is the message Bob sends to Alice after she initiates the
13
// single funder channel workflow via an AcceptChannel message. Once Alice
14
// receives Bob's response, then she has all the items necessary to construct
15
// the funding transaction, and both commitment transactions.
16
type AcceptChannel struct {
17
        // PendingChannelID serves to uniquely identify the future channel
18
        // created by the initiated single funder workflow.
19
        PendingChannelID [32]byte
20

21
        // DustLimit is the specific dust limit the sender of this message
22
        // would like enforced on their version of the commitment transaction.
23
        // Any output below this value will be "trimmed" from the commitment
24
        // transaction, with the amount of the HTLC going to dust.
25
        DustLimit btcutil.Amount
26

27
        // MaxValueInFlight represents the maximum amount of coins that can be
28
        // pending within the channel at any given time. If the amount of funds
29
        // in limbo exceeds this amount, then the channel will be failed.
30
        MaxValueInFlight MilliSatoshi
31

32
        // ChannelReserve is the amount of BTC that the receiving party MUST
33
        // maintain a balance above at all times. This is a safety mechanism to
34
        // ensure that both sides always have skin in the game during the
35
        // channel's lifetime.
36
        ChannelReserve btcutil.Amount
37

38
        // HtlcMinimum is the smallest HTLC that the sender of this message
39
        // will accept.
40
        HtlcMinimum MilliSatoshi
41

42
        // MinAcceptDepth is the minimum depth that the initiator of the
43
        // channel should wait before considering the channel open.
44
        MinAcceptDepth uint32
45

46
        // CsvDelay is the number of blocks to use for the relative time lock
47
        // in the pay-to-self output of both commitment transactions.
48
        CsvDelay uint16
49

50
        // MaxAcceptedHTLCs is the total number of incoming HTLC's that the
51
        // sender of this channel will accept.
52
        //
53
        // TODO(roasbeef): acks the initiator's, same with max in flight?
54
        MaxAcceptedHTLCs uint16
55

56
        // FundingKey is the key that should be used on behalf of the sender
57
        // within the 2-of-2 multi-sig output that it contained within the
58
        // funding transaction.
59
        FundingKey *btcec.PublicKey
60

61
        // RevocationPoint is the base revocation point for the sending party.
62
        // Any commitment transaction belonging to the receiver of this message
63
        // should use this key and their per-commitment point to derive the
64
        // revocation key for the commitment transaction.
65
        RevocationPoint *btcec.PublicKey
66

67
        // PaymentPoint is the base payment point for the sending party. This
68
        // key should be combined with the per commitment point for a
69
        // particular commitment state in order to create the key that should
70
        // be used in any output that pays directly to the sending party, and
71
        // also within the HTLC covenant transactions.
72
        PaymentPoint *btcec.PublicKey
73

74
        // DelayedPaymentPoint is the delay point for the sending party. This
75
        // key should be combined with the per commitment point to derive the
76
        // keys that are used in outputs of the sender's commitment transaction
77
        // where they claim funds.
78
        DelayedPaymentPoint *btcec.PublicKey
79

80
        // HtlcPoint is the base point used to derive the set of keys for this
81
        // party that will be used within the HTLC public key scripts.  This
82
        // value is combined with the receiver's revocation base point in order
83
        // to derive the keys that are used within HTLC scripts.
84
        HtlcPoint *btcec.PublicKey
85

86
        // FirstCommitmentPoint is the first commitment point for the sending
87
        // party. This value should be combined with the receiver's revocation
88
        // base point in order to derive the revocation keys that are placed
89
        // within the commitment transaction of the sender.
90
        FirstCommitmentPoint *btcec.PublicKey
91

92
        // UpfrontShutdownScript is the script to which the channel funds should
93
        // be paid when mutually closing the channel. This field is optional, and
94
        // and has a length prefix, so a zero will be written if it is not set
95
        // and its length followed by the script will be written if it is set.
96
        UpfrontShutdownScript DeliveryAddress
97

98
        // ChannelType is the explicit channel type the initiator wishes to
99
        // open.
100
        ChannelType *ChannelType
101

102
        // LeaseExpiry represents the absolute expiration height of a channel
103
        // lease. This is a custom TLV record that will only apply when a leased
104
        // channel is being opened using the script enforced lease commitment
105
        // type.
106
        LeaseExpiry *LeaseExpiry
107

108
        // LocalNonce is an optional field that transmits the
109
        // local/verification nonce for a party. This nonce will be used to
110
        // verify the very first commitment transaction signature.
111
        // This will only be populated if the simple taproot channels type was
112
        // negotiated.
113
        LocalNonce OptMusig2NonceTLV
114

115
        // ExtraData is the set of data that was appended to this message to
116
        // fill out the full maximum transport message size. These fields can
117
        // be used to specify optional data such as custom TLV fields.
118
        //
119
        // NOTE: Since the upfront shutdown script MUST be present (though can
120
        // be zero-length) if any TLV data is available, the script will be
121
        // extracted and removed from this blob when decoding. ExtraData will
122
        // contain all TLV records _except_ the DeliveryAddress record in that
123
        // case.
124
        ExtraData ExtraOpaqueData
125
}
126

127
// A compile time check to ensure AcceptChannel implements the lnwire.Message
128
// interface.
129
var _ Message = (*AcceptChannel)(nil)
130

131
// Encode serializes the target AcceptChannel into the passed io.Writer
132
// implementation. Serialization will observe the rules defined by the passed
133
// protocol version.
134
//
135
// This is part of the lnwire.Message interface.
136
func (a *AcceptChannel) Encode(w *bytes.Buffer, pver uint32) error {
3✔
137
        recordProducers := []tlv.RecordProducer{&a.UpfrontShutdownScript}
3✔
138
        if a.ChannelType != nil {
6✔
139
                recordProducers = append(recordProducers, a.ChannelType)
3✔
140
        }
3✔
141
        if a.LeaseExpiry != nil {
6✔
142
                recordProducers = append(recordProducers, a.LeaseExpiry)
3✔
143
        }
3✔
144
        a.LocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
6✔
145
                recordProducers = append(recordProducers, &localNonce)
3✔
146
        })
3✔
147
        err := EncodeMessageExtraData(&a.ExtraData, recordProducers...)
3✔
148
        if err != nil {
3✔
149
                return err
×
150
        }
×
151

152
        if err := WriteBytes(w, a.PendingChannelID[:]); err != nil {
3✔
153
                return err
×
154
        }
×
155

156
        if err := WriteSatoshi(w, a.DustLimit); err != nil {
3✔
157
                return err
×
158
        }
×
159

160
        if err := WriteMilliSatoshi(w, a.MaxValueInFlight); err != nil {
3✔
161
                return err
×
162
        }
×
163

164
        if err := WriteSatoshi(w, a.ChannelReserve); err != nil {
3✔
165
                return err
×
166
        }
×
167

168
        if err := WriteMilliSatoshi(w, a.HtlcMinimum); err != nil {
3✔
169
                return err
×
170
        }
×
171

172
        if err := WriteUint32(w, a.MinAcceptDepth); err != nil {
3✔
173
                return err
×
174
        }
×
175

176
        if err := WriteUint16(w, a.CsvDelay); err != nil {
3✔
177
                return err
×
178
        }
×
179

180
        if err := WriteUint16(w, a.MaxAcceptedHTLCs); err != nil {
3✔
181
                return err
×
182
        }
×
183

184
        if err := WritePublicKey(w, a.FundingKey); err != nil {
3✔
185
                return err
×
186
        }
×
187

188
        if err := WritePublicKey(w, a.RevocationPoint); err != nil {
3✔
189
                return err
×
190
        }
×
191

192
        if err := WritePublicKey(w, a.PaymentPoint); err != nil {
3✔
193
                return err
×
194
        }
×
195

196
        if err := WritePublicKey(w, a.DelayedPaymentPoint); err != nil {
3✔
197
                return err
×
198
        }
×
199

200
        if err := WritePublicKey(w, a.HtlcPoint); err != nil {
3✔
201
                return err
×
202
        }
×
203

204
        if err := WritePublicKey(w, a.FirstCommitmentPoint); err != nil {
3✔
205
                return err
×
206
        }
×
207

208
        return WriteBytes(w, a.ExtraData)
3✔
209
}
210

211
// Decode deserializes the serialized AcceptChannel stored in the passed
212
// io.Reader into the target AcceptChannel using the deserialization rules
213
// defined by the passed protocol version.
214
//
215
// This is part of the lnwire.Message interface.
216
func (a *AcceptChannel) Decode(r io.Reader, pver uint32) error {
3✔
217
        // Read all the mandatory fields in the accept message.
3✔
218
        err := ReadElements(r,
3✔
219
                a.PendingChannelID[:],
3✔
220
                &a.DustLimit,
3✔
221
                &a.MaxValueInFlight,
3✔
222
                &a.ChannelReserve,
3✔
223
                &a.HtlcMinimum,
3✔
224
                &a.MinAcceptDepth,
3✔
225
                &a.CsvDelay,
3✔
226
                &a.MaxAcceptedHTLCs,
3✔
227
                &a.FundingKey,
3✔
228
                &a.RevocationPoint,
3✔
229
                &a.PaymentPoint,
3✔
230
                &a.DelayedPaymentPoint,
3✔
231
                &a.HtlcPoint,
3✔
232
                &a.FirstCommitmentPoint,
3✔
233
        )
3✔
234
        if err != nil {
3✔
UNCOV
235
                return err
×
UNCOV
236
        }
×
237

238
        // For backwards compatibility, the optional extra data blob for
239
        // AcceptChannel must contain an entry for the upfront shutdown script.
240
        // We'll read it out and attempt to parse it.
241
        var tlvRecords ExtraOpaqueData
3✔
242
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
243
                return err
×
244
        }
×
245

246
        // Next we'll parse out the set of known records, keeping the raw tlv
247
        // bytes untouched to ensure we don't drop any bytes erroneously.
248
        var (
3✔
249
                chanType    ChannelType
3✔
250
                leaseExpiry LeaseExpiry
3✔
251
                localNonce  = a.LocalNonce.Zero()
3✔
252
        )
3✔
253
        typeMap, err := tlvRecords.ExtractRecords(
3✔
254
                &a.UpfrontShutdownScript, &chanType, &leaseExpiry,
3✔
255
                &localNonce,
3✔
256
        )
3✔
257
        if err != nil {
3✔
UNCOV
258
                return err
×
UNCOV
259
        }
×
260

261
        // Set the corresponding TLV types if they were included in the stream.
262
        if val, ok := typeMap[ChannelTypeRecordType]; ok && val == nil {
6✔
263
                a.ChannelType = &chanType
3✔
264
        }
3✔
265
        if val, ok := typeMap[LeaseExpiryRecordType]; ok && val == nil {
6✔
266
                a.LeaseExpiry = &leaseExpiry
3✔
267
        }
3✔
268
        if val, ok := typeMap[a.LocalNonce.TlvType()]; ok && val == nil {
6✔
269
                a.LocalNonce = tlv.SomeRecordT(localNonce)
3✔
270
        }
3✔
271

272
        a.ExtraData = tlvRecords
3✔
273

3✔
274
        return nil
3✔
275
}
276

277
// MsgType returns the MessageType code which uniquely identifies this message
278
// as an AcceptChannel on the wire.
279
//
280
// This is part of the lnwire.Message interface.
281
func (a *AcceptChannel) MsgType() MessageType {
3✔
282
        return MsgAcceptChannel
3✔
283
}
3✔
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