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

lightningnetwork / lnd / 13582876201

27 Feb 2025 08:50PM UTC coverage: 58.854% (+0.08%) from 58.77%
13582876201

Pull #9562

github

NishantBansal2003
multi: Add itest for funding timeout

This commit adds an integration test that
verifies the funding timeout behavior in the
funding manager, in dev/integration test.
Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
Pull Request #9562: Make MaxWaitNumBlocksFundingConf Configurable for itest

31 of 34 new or added lines in 5 files covered. (91.18%)

60 existing lines in 11 files now uncovered.

136487 of 231906 relevant lines covered (58.85%)

19244.29 hits per line

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

74.02
/funding/manager.go
1
package funding
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "fmt"
7
        "io"
8
        "sync"
9
        "sync/atomic"
10
        "time"
11

12
        "github.com/btcsuite/btcd/blockchain"
13
        "github.com/btcsuite/btcd/btcec/v2"
14
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
15
        "github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
16
        "github.com/btcsuite/btcd/btcutil"
17
        "github.com/btcsuite/btcd/chaincfg/chainhash"
18
        "github.com/btcsuite/btcd/txscript"
19
        "github.com/btcsuite/btcd/wire"
20
        "github.com/davecgh/go-spew/spew"
21
        "github.com/go-errors/errors"
22
        "github.com/lightningnetwork/lnd/chainntnfs"
23
        "github.com/lightningnetwork/lnd/chanacceptor"
24
        "github.com/lightningnetwork/lnd/channeldb"
25
        "github.com/lightningnetwork/lnd/discovery"
26
        "github.com/lightningnetwork/lnd/fn/v2"
27
        "github.com/lightningnetwork/lnd/graph"
28
        "github.com/lightningnetwork/lnd/graph/db/models"
29
        "github.com/lightningnetwork/lnd/input"
30
        "github.com/lightningnetwork/lnd/keychain"
31
        "github.com/lightningnetwork/lnd/labels"
32
        "github.com/lightningnetwork/lnd/lncfg"
33
        "github.com/lightningnetwork/lnd/lnpeer"
34
        "github.com/lightningnetwork/lnd/lnrpc"
35
        "github.com/lightningnetwork/lnd/lnutils"
36
        "github.com/lightningnetwork/lnd/lnwallet"
37
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
38
        "github.com/lightningnetwork/lnd/lnwallet/chanfunding"
39
        "github.com/lightningnetwork/lnd/lnwire"
40
        "golang.org/x/crypto/salsa20"
41
)
42

43
var (
44
        // byteOrder defines the endian-ness we use for encoding to and from
45
        // buffers.
46
        byteOrder = binary.BigEndian
47

48
        // checkPeerChannelReadyInterval is used when we are waiting for the
49
        // peer to send us ChannelReady. We will check every 1 second to see
50
        // if the message is received.
51
        //
52
        // NOTE: for itest, this value is changed to 10ms.
53
        checkPeerChannelReadyInterval = 1 * time.Second
54

55
        // errNoLocalNonce is returned when a local nonce is not found in the
56
        // expected TLV.
57
        errNoLocalNonce = fmt.Errorf("local nonce not found")
58

59
        // errNoPartialSig is returned when a partial sig is not found in the
60
        // expected TLV.
61
        errNoPartialSig = fmt.Errorf("partial sig not found")
62
)
63

64
// WriteOutpoint writes an outpoint to an io.Writer. This is not the same as
65
// the channeldb variant as this uses WriteVarBytes for the Hash.
66
func WriteOutpoint(w io.Writer, o *wire.OutPoint) error {
370✔
67
        scratch := make([]byte, 4)
370✔
68

370✔
69
        if err := wire.WriteVarBytes(w, 0, o.Hash[:]); err != nil {
370✔
70
                return err
×
71
        }
×
72

73
        byteOrder.PutUint32(scratch, o.Index)
370✔
74
        _, err := w.Write(scratch)
370✔
75
        return err
370✔
76
}
77

78
const (
79
        // MinBtcRemoteDelay is the minimum CSV delay we will require the remote
80
        // to use for its commitment transaction.
81
        MinBtcRemoteDelay uint16 = 144
82

83
        // MaxBtcRemoteDelay is the maximum CSV delay we will require the remote
84
        // to use for its commitment transaction.
85
        MaxBtcRemoteDelay uint16 = 2016
86

87
        // MinChanFundingSize is the smallest channel that we'll allow to be
88
        // created over the RPC interface.
89
        MinChanFundingSize = btcutil.Amount(20000)
90

91
        // MaxBtcFundingAmount is a soft-limit of the maximum channel size
92
        // currently accepted on the Bitcoin chain within the Lightning
93
        // Protocol. This limit is defined in BOLT-0002, and serves as an
94
        // initial precautionary limit while implementations are battle tested
95
        // in the real world.
96
        MaxBtcFundingAmount = btcutil.Amount(1<<24) - 1
97

98
        // MaxBtcFundingAmountWumbo is a soft-limit on the maximum size of wumbo
99
        // channels. This limit is 10 BTC and is the only thing standing between
100
        // you and limitless channel size (apart from 21 million cap).
101
        MaxBtcFundingAmountWumbo = btcutil.Amount(1000000000)
102

103
        msgBufferSize = 50
104

105
        // MaxWaitNumBlocksFundingConf is the maximum number of blocks to wait
106
        // for the funding transaction to be confirmed before forgetting
107
        // channels that aren't initiated by us. 2016 blocks is ~2 weeks.
108
        MaxWaitNumBlocksFundingConf = 2016
109

110
        // pendingChansLimit is the maximum number of pending channels that we
111
        // can have. After this point, pending channel opens will start to be
112
        // rejected.
113
        pendingChansLimit = 1_000
114
)
115

116
var (
117
        // ErrFundingManagerShuttingDown is an error returned when attempting to
118
        // process a funding request/message but the funding manager has already
119
        // been signaled to shut down.
120
        ErrFundingManagerShuttingDown = errors.New("funding manager shutting " +
121
                "down")
122

123
        // ErrConfirmationTimeout is an error returned when we as a responder
124
        // are waiting for a funding transaction to confirm, but too many
125
        // blocks pass without confirmation.
126
        ErrConfirmationTimeout = errors.New("timeout waiting for funding " +
127
                "confirmation")
128

129
        // errUpfrontShutdownScriptNotSupported is returned if an upfront
130
        // shutdown script is set for a peer that does not support the feature
131
        // bit.
132
        errUpfrontShutdownScriptNotSupported = errors.New("peer does not " +
133
                "support option upfront shutdown script")
134

135
        zeroID [32]byte
136
)
137

138
// reservationWithCtx encapsulates a pending channel reservation. This wrapper
139
// struct is used internally within the funding manager to track and progress
140
// the funding workflow initiated by incoming/outgoing methods from the target
141
// peer. Additionally, this struct houses a response and error channel which is
142
// used to respond to the caller in the case a channel workflow is initiated
143
// via a local signal such as RPC.
144
//
145
// TODO(roasbeef): actually use the context package
146
//   - deadlines, etc.
147
type reservationWithCtx struct {
148
        reservation *lnwallet.ChannelReservation
149
        peer        lnpeer.Peer
150

151
        chanAmt btcutil.Amount
152

153
        // forwardingPolicy is the policy provided by the initFundingMsg.
154
        forwardingPolicy models.ForwardingPolicy
155

156
        // Constraints we require for the remote.
157
        remoteCsvDelay    uint16
158
        remoteMinHtlc     lnwire.MilliSatoshi
159
        remoteMaxValue    lnwire.MilliSatoshi
160
        remoteMaxHtlcs    uint16
161
        remoteChanReserve btcutil.Amount
162

163
        // maxLocalCsv is the maximum csv we will accept from the remote.
164
        maxLocalCsv uint16
165

166
        // channelType is the explicit channel type proposed by the initiator of
167
        // the channel.
168
        channelType *lnwire.ChannelType
169

170
        updateMtx   sync.RWMutex
171
        lastUpdated time.Time
172

173
        updates chan *lnrpc.OpenStatusUpdate
174
        err     chan error
175
}
176

177
// isLocked checks the reservation's timestamp to determine whether it is
178
// locked.
179
func (r *reservationWithCtx) isLocked() bool {
6✔
180
        r.updateMtx.RLock()
6✔
181
        defer r.updateMtx.RUnlock()
6✔
182

6✔
183
        // The time zero value represents a locked reservation.
6✔
184
        return r.lastUpdated.IsZero()
6✔
185
}
6✔
186

187
// updateTimestamp updates the reservation's timestamp with the current time.
188
func (r *reservationWithCtx) updateTimestamp() {
137✔
189
        r.updateMtx.Lock()
137✔
190
        defer r.updateMtx.Unlock()
137✔
191

137✔
192
        r.lastUpdated = time.Now()
137✔
193
}
137✔
194

195
// InitFundingMsg is sent by an outside subsystem to the funding manager in
196
// order to kick off a funding workflow with a specified target peer. The
197
// original request which defines the parameters of the funding workflow are
198
// embedded within this message giving the funding manager full context w.r.t
199
// the workflow.
200
type InitFundingMsg struct {
201
        // Peer is the peer that we want to open a channel to.
202
        Peer lnpeer.Peer
203

204
        // TargetPubkey is the public key of the peer.
205
        TargetPubkey *btcec.PublicKey
206

207
        // ChainHash is the target genesis hash for this channel.
208
        ChainHash chainhash.Hash
209

210
        // SubtractFees set to true means that fees will be subtracted
211
        // from the LocalFundingAmt.
212
        SubtractFees bool
213

214
        // LocalFundingAmt is the size of the channel.
215
        LocalFundingAmt btcutil.Amount
216

217
        // BaseFee is the base fee charged for routing payments regardless of
218
        // the number of milli-satoshis sent.
219
        BaseFee *uint64
220

221
        // FeeRate is the fee rate in ppm (parts per million) that will be
222
        // charged proportionally based on the value of each forwarded HTLC, the
223
        // lowest possible rate is 0 with a granularity of 0.000001
224
        // (millionths).
225
        FeeRate *uint64
226

227
        // PushAmt is the amount pushed to the counterparty.
228
        PushAmt lnwire.MilliSatoshi
229

230
        // FundingFeePerKw is the fee for the funding transaction.
231
        FundingFeePerKw chainfee.SatPerKWeight
232

233
        // Private determines whether or not this channel will be private.
234
        Private bool
235

236
        // MinHtlcIn is the minimum incoming HTLC that we accept.
237
        MinHtlcIn lnwire.MilliSatoshi
238

239
        // RemoteCsvDelay is the CSV delay we require for the remote peer.
240
        RemoteCsvDelay uint16
241

242
        // RemoteChanReserve is the channel reserve we required for the remote
243
        // peer.
244
        RemoteChanReserve btcutil.Amount
245

246
        // MinConfs indicates the minimum number of confirmations that each
247
        // output selected to fund the channel should satisfy.
248
        MinConfs int32
249

250
        // ShutdownScript is an optional upfront shutdown script for the
251
        // channel. This value is optional, so may be nil.
252
        ShutdownScript lnwire.DeliveryAddress
253

254
        // MaxValueInFlight is the maximum amount of coins in MilliSatoshi
255
        // that can be pending within the channel. It only applies to the
256
        // remote party.
257
        MaxValueInFlight lnwire.MilliSatoshi
258

259
        // MaxHtlcs is the maximum number of HTLCs that the remote peer
260
        // can offer us.
261
        MaxHtlcs uint16
262

263
        // MaxLocalCsv is the maximum local csv delay we will accept from our
264
        // peer.
265
        MaxLocalCsv uint16
266

267
        // FundUpToMaxAmt is the maximum amount to try to commit to. If set, the
268
        // MinFundAmt field denotes the acceptable minimum amount to commit to,
269
        // while trying to commit as many coins as possible up to this value.
270
        FundUpToMaxAmt btcutil.Amount
271

272
        // MinFundAmt must be set iff FundUpToMaxAmt is set. It denotes the
273
        // minimum amount to commit to.
274
        MinFundAmt btcutil.Amount
275

276
        // Outpoints is a list of client-selected outpoints that should be used
277
        // for funding a channel. If LocalFundingAmt is specified then this
278
        // amount is allocated from the sum of outpoints towards funding. If
279
        // the FundUpToMaxAmt is specified the entirety of selected funds is
280
        // allocated towards channel funding.
281
        Outpoints []wire.OutPoint
282

283
        // ChanFunder is an optional channel funder that allows the caller to
284
        // control exactly how the channel funding is carried out. If not
285
        // specified, then the default chanfunding.WalletAssembler will be
286
        // used.
287
        ChanFunder chanfunding.Assembler
288

289
        // PendingChanID is not all zeroes (the default value), then this will
290
        // be the pending channel ID used for the funding flow within the wire
291
        // protocol.
292
        PendingChanID PendingChanID
293

294
        // ChannelType allows the caller to use an explicit channel type for the
295
        // funding negotiation. This type will only be observed if BOTH sides
296
        // support explicit channel type negotiation.
297
        ChannelType *lnwire.ChannelType
298

299
        // Memo is any arbitrary information we wish to store locally about the
300
        // channel that will be useful to our future selves.
301
        Memo []byte
302

303
        // Updates is a channel which updates to the opening status of the
304
        // channel are sent on.
305
        Updates chan *lnrpc.OpenStatusUpdate
306

307
        // Err is a channel which errors encountered during the funding flow are
308
        // sent on.
309
        Err chan error
310
}
311

312
// fundingMsg is sent by the ProcessFundingMsg function and packages a
313
// funding-specific lnwire.Message along with the lnpeer.Peer that sent it.
314
type fundingMsg struct {
315
        msg  lnwire.Message
316
        peer lnpeer.Peer
317
}
318

319
// pendingChannels is a map instantiated per-peer which tracks all active
320
// pending single funded channels indexed by their pending channel identifier,
321
// which is a set of 32-bytes generated via a CSPRNG.
322
type pendingChannels map[PendingChanID]*reservationWithCtx
323

324
// serializedPubKey is used within the FundingManager's activeReservations list
325
// to identify the nodes with which the FundingManager is actively working to
326
// initiate new channels.
327
type serializedPubKey [33]byte
328

329
// newSerializedKey creates a new serialized public key from an instance of a
330
// live pubkey object.
331
func newSerializedKey(pubKey *btcec.PublicKey) serializedPubKey {
381✔
332
        var s serializedPubKey
381✔
333
        copy(s[:], pubKey.SerializeCompressed())
381✔
334
        return s
381✔
335
}
381✔
336

337
// DevConfig specifies configs used for integration test only.
338
type DevConfig struct {
339
        // ProcessChannelReadyWait is the duration to sleep before processing
340
        // remote node's channel ready message once the channel as been marked
341
        // as `channelReadySent`.
342
        ProcessChannelReadyWait time.Duration
343

344
        // MaxWaitNumBlocksFundingConf is the maximum number of blocks
345
        // to wait for the funding transaction to be confirmed before forgetting
346
        // channels that aren't initiated by us.
347
        MaxWaitNumBlocksFundingConf int
348
}
349

350
// Config defines the configuration for the FundingManager. All elements
351
// within the configuration MUST be non-nil for the FundingManager to carry out
352
// its duties.
353
type Config struct {
354
        // Dev specifies config values used in integration test. For
355
        // production, this config will always be an empty struct.
356
        Dev *DevConfig
357

358
        // NoWumboChans indicates if we're to reject all incoming wumbo channel
359
        // requests, and also reject all outgoing wumbo channel requests.
360
        NoWumboChans bool
361

362
        // IDKey is the PublicKey that is used to identify this node within the
363
        // Lightning Network.
364
        IDKey *btcec.PublicKey
365

366
        // IDKeyLoc is the locator for the key that is used to identify this
367
        // node within the LightningNetwork.
368
        IDKeyLoc keychain.KeyLocator
369

370
        // Wallet handles the parts of the funding process that involves moving
371
        // funds from on-chain transaction outputs into Lightning channels.
372
        Wallet *lnwallet.LightningWallet
373

374
        // PublishTransaction facilitates the process of broadcasting a
375
        // transaction to the network.
376
        PublishTransaction func(*wire.MsgTx, string) error
377

378
        // UpdateLabel updates the label that a transaction has in our wallet,
379
        // overwriting any existing labels.
380
        UpdateLabel func(chainhash.Hash, string) error
381

382
        // FeeEstimator calculates appropriate fee rates based on historical
383
        // transaction information.
384
        FeeEstimator chainfee.Estimator
385

386
        // Notifier is used by the FundingManager to determine when the
387
        // channel's funding transaction has been confirmed on the blockchain
388
        // so that the channel creation process can be completed.
389
        Notifier chainntnfs.ChainNotifier
390

391
        // ChannelDB is the database that keeps track of all channel state.
392
        ChannelDB *channeldb.ChannelStateDB
393

394
        // SignMessage signs an arbitrary message with a given public key. The
395
        // actual digest signed is the double sha-256 of the message. In the
396
        // case that the private key corresponding to the passed public key
397
        // cannot be located, then an error is returned.
398
        //
399
        // TODO(roasbeef): should instead pass on this responsibility to a
400
        // distinct sub-system?
401
        SignMessage func(keyLoc keychain.KeyLocator,
402
                msg []byte, doubleHash bool) (*ecdsa.Signature, error)
403

404
        // CurrentNodeAnnouncement should return the latest, fully signed node
405
        // announcement from the backing Lightning Network node with a fresh
406
        // timestamp.
407
        CurrentNodeAnnouncement func() (lnwire.NodeAnnouncement, error)
408

409
        // SendAnnouncement is used by the FundingManager to send announcement
410
        // messages to the Gossiper to possibly broadcast to the greater
411
        // network. A set of optional message fields can be provided to populate
412
        // any information within the graph that is not included in the gossip
413
        // message.
414
        SendAnnouncement func(msg lnwire.Message,
415
                optionalFields ...discovery.OptionalMsgField) chan error
416

417
        // NotifyWhenOnline allows the FundingManager to register with a
418
        // subsystem that will notify it when the peer comes online. This is
419
        // used when sending the channelReady message, since it MUST be
420
        // delivered after the funding transaction is confirmed.
421
        //
422
        // NOTE: The peerChan channel must be buffered.
423
        NotifyWhenOnline func(peer [33]byte, peerChan chan<- lnpeer.Peer)
424

425
        // FindChannel queries the database for the channel with the given
426
        // channel ID. Providing the node's public key is an optimization that
427
        // prevents deserializing and scanning through all possible channels.
428
        FindChannel func(node *btcec.PublicKey,
429
                chanID lnwire.ChannelID) (*channeldb.OpenChannel, error)
430

431
        // TempChanIDSeed is a cryptographically random string of bytes that's
432
        // used as a seed to generate pending channel ID's.
433
        TempChanIDSeed [32]byte
434

435
        // DefaultRoutingPolicy is the default routing policy used when
436
        // initially announcing channels.
437
        DefaultRoutingPolicy models.ForwardingPolicy
438

439
        // DefaultMinHtlcIn is the default minimum incoming htlc value that is
440
        // set as a channel parameter.
441
        DefaultMinHtlcIn lnwire.MilliSatoshi
442

443
        // NumRequiredConfs is a function closure that helps the funding
444
        // manager decide how many confirmations it should require for a
445
        // channel extended to it. The function is able to take into account
446
        // the amount of the channel, and any funds we'll be pushed in the
447
        // process to determine how many confirmations we'll require.
448
        NumRequiredConfs func(btcutil.Amount, lnwire.MilliSatoshi) uint16
449

450
        // RequiredRemoteDelay is a function that maps the total amount in a
451
        // proposed channel to the CSV delay that we'll require for the remote
452
        // party. Naturally a larger channel should require a higher CSV delay
453
        // in order to give us more time to claim funds in the case of a
454
        // contract breach.
455
        RequiredRemoteDelay func(btcutil.Amount) uint16
456

457
        // RequiredRemoteChanReserve is a function closure that, given the
458
        // channel capacity and dust limit, will return an appropriate amount
459
        // for the remote peer's required channel reserve that is to be adhered
460
        // to at all times.
461
        RequiredRemoteChanReserve func(capacity,
462
                dustLimit btcutil.Amount) btcutil.Amount
463

464
        // RequiredRemoteMaxValue is a function closure that, given the channel
465
        // capacity, returns the amount of MilliSatoshis that our remote peer
466
        // can have in total outstanding HTLCs with us.
467
        RequiredRemoteMaxValue func(btcutil.Amount) lnwire.MilliSatoshi
468

469
        // RequiredRemoteMaxHTLCs is a function closure that, given the channel
470
        // capacity, returns the number of maximum HTLCs the remote peer can
471
        // offer us.
472
        RequiredRemoteMaxHTLCs func(btcutil.Amount) uint16
473

474
        // WatchNewChannel is to be called once a new channel enters the final
475
        // funding stage: waiting for on-chain confirmation. This method sends
476
        // the channel to the ChainArbitrator so it can watch for any on-chain
477
        // events related to the channel. We also provide the public key of the
478
        // node we're establishing a channel with for reconnection purposes.
479
        WatchNewChannel func(*channeldb.OpenChannel, *btcec.PublicKey) error
480

481
        // ReportShortChanID allows the funding manager to report the confirmed
482
        // short channel ID of a formerly pending zero-conf channel to outside
483
        // sub-systems.
484
        ReportShortChanID func(wire.OutPoint) error
485

486
        // ZombieSweeperInterval is the periodic time interval in which the
487
        // zombie sweeper is run.
488
        ZombieSweeperInterval time.Duration
489

490
        // ReservationTimeout is the length of idle time that must pass before
491
        // a reservation is considered a zombie.
492
        ReservationTimeout time.Duration
493

494
        // MinChanSize is the smallest channel size that we'll accept as an
495
        // inbound channel. We have such a parameter, as otherwise, nodes could
496
        // flood us with very small channels that would never really be usable
497
        // due to fees.
498
        MinChanSize btcutil.Amount
499

500
        // MaxChanSize is the largest channel size that we'll accept as an
501
        // inbound channel. We have such a parameter, so that you may decide how
502
        // WUMBO you would like your channel.
503
        MaxChanSize btcutil.Amount
504

505
        // MaxPendingChannels is the maximum number of pending channels we
506
        // allow for each peer.
507
        MaxPendingChannels int
508

509
        // RejectPush is set true if the fundingmanager should reject any
510
        // incoming channels having a non-zero push amount.
511
        RejectPush bool
512

513
        // MaxLocalCSVDelay is the maximum csv delay we will allow for our
514
        // commit output. Channels that exceed this value will be failed.
515
        MaxLocalCSVDelay uint16
516

517
        // NotifyOpenChannelEvent informs the ChannelNotifier when channels
518
        // transition from pending open to open.
519
        NotifyOpenChannelEvent func(wire.OutPoint)
520

521
        // OpenChannelPredicate is a predicate on the lnwire.OpenChannel message
522
        // and on the requesting node's public key that returns a bool which
523
        // tells the funding manager whether or not to accept the channel.
524
        OpenChannelPredicate chanacceptor.ChannelAcceptor
525

526
        // NotifyPendingOpenChannelEvent informs the ChannelNotifier when
527
        // channels enter a pending state.
528
        NotifyPendingOpenChannelEvent func(wire.OutPoint,
529
                *channeldb.OpenChannel)
530

531
        // EnableUpfrontShutdown specifies whether the upfront shutdown script
532
        // is enabled.
533
        EnableUpfrontShutdown bool
534

535
        // MaxAnchorsCommitFeeRate is the max commitment fee rate we'll use as
536
        // the initiator for channels of the anchor type.
537
        MaxAnchorsCommitFeeRate chainfee.SatPerKWeight
538

539
        // DeleteAliasEdge allows the Manager to delete an alias channel edge
540
        // from the graph. It also returns our local to-be-deleted policy.
541
        DeleteAliasEdge func(scid lnwire.ShortChannelID) (
542
                *models.ChannelEdgePolicy, error)
543

544
        // AliasManager is an implementation of the aliasHandler interface that
545
        // abstracts away the handling of many alias functions.
546
        AliasManager aliasHandler
547

548
        // IsSweeperOutpoint queries the sweeper store for successfully
549
        // published sweeps. This is useful to decide for the internal wallet
550
        // backed funding flow to not use utxos still being swept by the sweeper
551
        // subsystem.
552
        IsSweeperOutpoint func(wire.OutPoint) bool
553

554
        // AuxLeafStore is an optional store that can be used to store auxiliary
555
        // leaves for certain custom channel types.
556
        AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
557

558
        // AuxFundingController is an optional controller that can be used to
559
        // modify the way we handle certain custom channel types. It's also
560
        // able to automatically handle new custom protocol messages related to
561
        // the funding process.
562
        AuxFundingController fn.Option[AuxFundingController]
563

564
        // AuxSigner is an optional signer that can be used to sign auxiliary
565
        // leaves for certain custom channel types.
566
        AuxSigner fn.Option[lnwallet.AuxSigner]
567

568
        // AuxResolver is an optional interface that can be used to modify the
569
        // way contracts are resolved.
570
        AuxResolver fn.Option[lnwallet.AuxContractResolver]
571
}
572

573
// Manager acts as an orchestrator/bridge between the wallet's
574
// 'ChannelReservation' workflow, and the wire protocol's funding initiation
575
// messages. Any requests to initiate the funding workflow for a channel,
576
// either kicked-off locally or remotely are handled by the funding manager.
577
// Once a channel's funding workflow has been completed, any local callers, the
578
// local peer, and possibly the remote peer are notified of the completion of
579
// the channel workflow. Additionally, any temporary or permanent access
580
// controls between the wallet and remote peers are enforced via the funding
581
// manager.
582
type Manager struct {
583
        started sync.Once
584
        stopped sync.Once
585

586
        // cfg is a copy of the configuration struct that the FundingManager
587
        // was initialized with.
588
        cfg *Config
589

590
        // chanIDKey is a cryptographically random key that's used to generate
591
        // temporary channel ID's.
592
        chanIDKey [32]byte
593

594
        // chanIDNonce is a nonce that's incremented for each new funding
595
        // reservation created.
596
        chanIDNonce atomic.Uint64
597

598
        // nonceMtx is a mutex that guards the pendingMusigNonces.
599
        nonceMtx sync.RWMutex
600

601
        // pendingMusigNonces is used to store the musig2 nonce we generate to
602
        // send funding locked until we receive a funding locked message from
603
        // the remote party. We'll use this to keep track of the nonce we
604
        // generated, so we send the local+remote nonces to the peer state
605
        // machine.
606
        //
607
        // NOTE: This map is protected by the nonceMtx above.
608
        //
609
        // TODO(roasbeef): replace w/ generic concurrent map
610
        pendingMusigNonces map[lnwire.ChannelID]*musig2.Nonces
611

612
        // activeReservations is a map which houses the state of all pending
613
        // funding workflows.
614
        activeReservations map[serializedPubKey]pendingChannels
615

616
        // signedReservations is a utility map that maps the permanent channel
617
        // ID of a funding reservation to its temporary channel ID. This is
618
        // required as mid funding flow, we switch to referencing the channel
619
        // by its full channel ID once the commitment transactions have been
620
        // signed by both parties.
621
        signedReservations map[lnwire.ChannelID]PendingChanID
622

623
        // resMtx guards both of the maps above to ensure that all access is
624
        // goroutine safe.
625
        resMtx sync.RWMutex
626

627
        // fundingMsgs is a channel that relays fundingMsg structs from
628
        // external sub-systems using the ProcessFundingMsg call.
629
        fundingMsgs chan *fundingMsg
630

631
        // fundingRequests is a channel used to receive channel initiation
632
        // requests from a local subsystem within the daemon.
633
        fundingRequests chan *InitFundingMsg
634

635
        localDiscoverySignals *lnutils.SyncMap[lnwire.ChannelID, chan struct{}]
636

637
        handleChannelReadyBarriers *lnutils.SyncMap[lnwire.ChannelID, struct{}]
638

639
        quit chan struct{}
640
        wg   sync.WaitGroup
641
}
642

643
// channelOpeningState represents the different states a channel can be in
644
// between the funding transaction has been confirmed and the channel is
645
// announced to the network and ready to be used.
646
type channelOpeningState uint8
647

648
const (
649
        // markedOpen is the opening state of a channel if the funding
650
        // transaction is confirmed on-chain, but channelReady is not yet
651
        // successfully sent to the other peer.
652
        markedOpen channelOpeningState = iota
653

654
        // channelReadySent is the opening state of a channel if the
655
        // channelReady message has successfully been sent to the other peer,
656
        // but we still haven't announced the channel to the network.
657
        channelReadySent
658

659
        // addedToGraph is the opening state of a channel if the channel has
660
        // been successfully added to the graph immediately after the
661
        // channelReady message has been sent, but we still haven't announced
662
        // the channel to the network.
663
        addedToGraph
664
)
665

666
func (c channelOpeningState) String() string {
3✔
667
        switch c {
3✔
668
        case markedOpen:
3✔
669
                return "markedOpen"
3✔
670
        case channelReadySent:
3✔
671
                return "channelReadySent"
3✔
672
        case addedToGraph:
3✔
673
                return "addedToGraph"
3✔
674
        default:
×
675
                return "unknown"
×
676
        }
677
}
678

679
// NewFundingManager creates and initializes a new instance of the
680
// fundingManager.
681
func NewFundingManager(cfg Config) (*Manager, error) {
110✔
682
        return &Manager{
110✔
683
                cfg:       &cfg,
110✔
684
                chanIDKey: cfg.TempChanIDSeed,
110✔
685
                activeReservations: make(
110✔
686
                        map[serializedPubKey]pendingChannels,
110✔
687
                ),
110✔
688
                signedReservations: make(
110✔
689
                        map[lnwire.ChannelID][32]byte,
110✔
690
                ),
110✔
691
                fundingMsgs: make(
110✔
692
                        chan *fundingMsg, msgBufferSize,
110✔
693
                ),
110✔
694
                fundingRequests: make(
110✔
695
                        chan *InitFundingMsg, msgBufferSize,
110✔
696
                ),
110✔
697
                localDiscoverySignals: &lnutils.SyncMap[
110✔
698
                        lnwire.ChannelID, chan struct{},
110✔
699
                ]{},
110✔
700
                handleChannelReadyBarriers: &lnutils.SyncMap[
110✔
701
                        lnwire.ChannelID, struct{},
110✔
702
                ]{},
110✔
703
                pendingMusigNonces: make(
110✔
704
                        map[lnwire.ChannelID]*musig2.Nonces,
110✔
705
                ),
110✔
706
                quit: make(chan struct{}),
110✔
707
        }, nil
110✔
708
}
110✔
709

710
// Start launches all helper goroutines required for handling requests sent
711
// to the funding manager.
712
func (f *Manager) Start() error {
110✔
713
        var err error
110✔
714
        f.started.Do(func() {
220✔
715
                log.Info("Funding manager starting")
110✔
716
                err = f.start()
110✔
717
        })
110✔
718
        return err
110✔
719
}
720

721
func (f *Manager) start() error {
110✔
722
        // Upon restart, the Funding Manager will check the database to load any
110✔
723
        // channels that were  waiting for their funding transactions to be
110✔
724
        // confirmed on the blockchain at the time when the daemon last went
110✔
725
        // down.
110✔
726
        // TODO(roasbeef): store height that funding finished?
110✔
727
        //  * would then replace call below
110✔
728
        allChannels, err := f.cfg.ChannelDB.FetchAllChannels()
110✔
729
        if err != nil {
110✔
730
                return err
×
731
        }
×
732

733
        for _, channel := range allChannels {
122✔
734
                chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
12✔
735

12✔
736
                // For any channels that were in a pending state when the
12✔
737
                // daemon was last connected, the Funding Manager will
12✔
738
                // re-initialize the channel barriers, and republish the
12✔
739
                // funding transaction if we're the initiator.
12✔
740
                if channel.IsPending {
16✔
741
                        log.Tracef("Loading pending ChannelPoint(%v), "+
4✔
742
                                "creating chan barrier",
4✔
743
                                channel.FundingOutpoint)
4✔
744

4✔
745
                        f.localDiscoverySignals.Store(
4✔
746
                                chanID, make(chan struct{}),
4✔
747
                        )
4✔
748

4✔
749
                        // Rebroadcast the funding transaction for any pending
4✔
750
                        // channel that we initiated. No error will be returned
4✔
751
                        // if the transaction already has been broadcast.
4✔
752
                        chanType := channel.ChanType
4✔
753
                        if chanType.IsSingleFunder() &&
4✔
754
                                chanType.HasFundingTx() &&
4✔
755
                                channel.IsInitiator {
8✔
756

4✔
757
                                f.rebroadcastFundingTx(channel)
4✔
758
                        }
4✔
759
                } else if channel.ChanType.IsSingleFunder() &&
11✔
760
                        channel.ChanType.HasFundingTx() &&
11✔
761
                        channel.IsZeroConf() && channel.IsInitiator &&
11✔
762
                        !channel.ZeroConfConfirmed() {
13✔
763

2✔
764
                        // Rebroadcast the funding transaction for unconfirmed
2✔
765
                        // zero-conf channels if we have the funding tx and are
2✔
766
                        // also the initiator.
2✔
767
                        f.rebroadcastFundingTx(channel)
2✔
768
                }
2✔
769

770
                // We will restart the funding state machine for all channels,
771
                // which will wait for the channel's funding transaction to be
772
                // confirmed on the blockchain, and transmit the messages
773
                // necessary for the channel to be operational.
774
                f.wg.Add(1)
12✔
775
                go f.advanceFundingState(channel, chanID, nil)
12✔
776
        }
777

778
        f.wg.Add(1) // TODO(roasbeef): tune
110✔
779
        go f.reservationCoordinator()
110✔
780

110✔
781
        return nil
110✔
782
}
783

784
// Stop signals all helper goroutines to execute a graceful shutdown. This
785
// method will block until all goroutines have exited.
786
func (f *Manager) Stop() error {
107✔
787
        f.stopped.Do(func() {
213✔
788
                log.Info("Funding manager shutting down...")
106✔
789
                defer log.Debug("Funding manager shutdown complete")
106✔
790

106✔
791
                close(f.quit)
106✔
792
                f.wg.Wait()
106✔
793
        })
106✔
794

795
        return nil
107✔
796
}
797

798
// rebroadcastFundingTx publishes the funding tx on startup for each
799
// unconfirmed channel.
800
func (f *Manager) rebroadcastFundingTx(c *channeldb.OpenChannel) {
6✔
801
        var fundingTxBuf bytes.Buffer
6✔
802
        err := c.FundingTxn.Serialize(&fundingTxBuf)
6✔
803
        if err != nil {
6✔
804
                log.Errorf("Unable to serialize funding transaction %v: %v",
×
805
                        c.FundingTxn.TxHash(), err)
×
806

×
807
                // Clear the buffer of any bytes that were written before the
×
808
                // serialization error to prevent logging an incomplete
×
809
                // transaction.
×
810
                fundingTxBuf.Reset()
×
811
        } else {
6✔
812
                log.Debugf("Rebroadcasting funding tx for ChannelPoint(%v): "+
6✔
813
                        "%x", c.FundingOutpoint, fundingTxBuf.Bytes())
6✔
814
        }
6✔
815

816
        // Set a nil short channel ID at this stage because we do not know it
817
        // until our funding tx confirms.
818
        label := labels.MakeLabel(labels.LabelTypeChannelOpen, nil)
6✔
819

6✔
820
        err = f.cfg.PublishTransaction(c.FundingTxn, label)
6✔
821
        if err != nil {
6✔
822
                log.Errorf("Unable to rebroadcast funding tx %x for "+
×
823
                        "ChannelPoint(%v): %v", fundingTxBuf.Bytes(),
×
824
                        c.FundingOutpoint, err)
×
825
        }
×
826
}
827

828
// PendingChanID is a type that represents a pending channel ID. This might be
829
// selected by the caller, but if not, will be automatically selected.
830
type PendingChanID = [32]byte
831

832
// nextPendingChanID returns the next free pending channel ID to be used to
833
// identify a particular future channel funding workflow.
834
func (f *Manager) nextPendingChanID() PendingChanID {
59✔
835
        // Obtain a fresh nonce. We do this by encoding the incremented nonce.
59✔
836
        nextNonce := f.chanIDNonce.Add(1)
59✔
837

59✔
838
        var nonceBytes [8]byte
59✔
839
        binary.LittleEndian.PutUint64(nonceBytes[:], nextNonce)
59✔
840

59✔
841
        // We'll generate the next pending channelID by "encrypting" 32-bytes
59✔
842
        // of zeroes which'll extract 32 random bytes from our stream cipher.
59✔
843
        var (
59✔
844
                nextChanID PendingChanID
59✔
845
                zeroes     [32]byte
59✔
846
        )
59✔
847
        salsa20.XORKeyStream(
59✔
848
                nextChanID[:], zeroes[:], nonceBytes[:], &f.chanIDKey,
59✔
849
        )
59✔
850

59✔
851
        return nextChanID
59✔
852
}
59✔
853

854
// CancelPeerReservations cancels all active reservations associated with the
855
// passed node. This will ensure any outputs which have been pre committed,
856
// (and thus locked from coin selection), are properly freed.
857
func (f *Manager) CancelPeerReservations(nodePub [33]byte) {
3✔
858
        log.Debugf("Cancelling all reservations for peer %x", nodePub[:])
3✔
859

3✔
860
        f.resMtx.Lock()
3✔
861
        defer f.resMtx.Unlock()
3✔
862

3✔
863
        // We'll attempt to look up this node in the set of active
3✔
864
        // reservations.  If they don't have any, then there's no further work
3✔
865
        // to be done.
3✔
866
        nodeReservations, ok := f.activeReservations[nodePub]
3✔
867
        if !ok {
6✔
868
                log.Debugf("No active reservations for node: %x", nodePub[:])
3✔
869
                return
3✔
870
        }
3✔
871

872
        // If they do have any active reservations, then we'll cancel all of
873
        // them (which releases any locked UTXO's), and also delete it from the
874
        // reservation map.
875
        for pendingID, resCtx := range nodeReservations {
×
876
                if err := resCtx.reservation.Cancel(); err != nil {
×
877
                        log.Errorf("unable to cancel reservation for "+
×
878
                                "node=%x: %v", nodePub[:], err)
×
879
                }
×
880

881
                resCtx.err <- fmt.Errorf("peer disconnected")
×
882
                delete(nodeReservations, pendingID)
×
883
        }
884

885
        // Finally, we'll delete the node itself from the set of reservations.
886
        delete(f.activeReservations, nodePub)
×
887
}
888

889
// chanIdentifier wraps pending channel ID and channel ID into one struct so
890
// it's easier to identify a specific channel.
891
//
892
// TODO(yy): move to a different package to hide the private fields so direct
893
// access is disabled.
894
type chanIdentifier struct {
895
        // tempChanID is the pending channel ID created by the funder when
896
        // initializing the funding flow. For fundee, it's received from the
897
        // `open_channel` message.
898
        tempChanID lnwire.ChannelID
899

900
        // chanID is the channel ID created by the funder once the
901
        // `accept_channel` message is received. For fundee, it's received from
902
        // the `funding_created` message.
903
        chanID lnwire.ChannelID
904

905
        // chanIDSet is a boolean indicates whether the active channel ID is
906
        // set for this identifier. For zero conf channels, the `chanID` can be
907
        // all-zero, which is the same as the empty value of `ChannelID`. To
908
        // avoid the confusion, we use this boolean to explicitly signal
909
        // whether the `chanID` is set or not.
910
        chanIDSet bool
911
}
912

913
// newChanIdentifier creates a new chanIdentifier.
914
func newChanIdentifier(tempChanID lnwire.ChannelID) *chanIdentifier {
147✔
915
        return &chanIdentifier{
147✔
916
                tempChanID: tempChanID,
147✔
917
        }
147✔
918
}
147✔
919

920
// setChanID updates the `chanIdentifier` with the active channel ID.
921
func (c *chanIdentifier) setChanID(chanID lnwire.ChannelID) {
91✔
922
        c.chanID = chanID
91✔
923
        c.chanIDSet = true
91✔
924
}
91✔
925

926
// hasChanID returns true if the active channel ID has been set.
927
func (c *chanIdentifier) hasChanID() bool {
24✔
928
        return c.chanIDSet
24✔
929
}
24✔
930

931
// failFundingFlow will fail the active funding flow with the target peer,
932
// identified by its unique temporary channel ID. This method will send an
933
// error to the remote peer, and also remove the reservation from our set of
934
// pending reservations.
935
//
936
// TODO(roasbeef): if peer disconnects, and haven't yet broadcast funding
937
// transaction, then all reservations should be cleared.
938
func (f *Manager) failFundingFlow(peer lnpeer.Peer, cid *chanIdentifier,
939
        fundingErr error) {
24✔
940

24✔
941
        log.Debugf("Failing funding flow for pending_id=%v: %v",
24✔
942
                cid.tempChanID, fundingErr)
24✔
943

24✔
944
        // First, notify Brontide to remove the pending channel.
24✔
945
        //
24✔
946
        // NOTE: depending on where we fail the flow, we may not have the
24✔
947
        // active channel ID yet.
24✔
948
        if cid.hasChanID() {
32✔
949
                err := peer.RemovePendingChannel(cid.chanID)
8✔
950
                if err != nil {
8✔
951
                        log.Errorf("Unable to remove channel %v with peer %x: "+
×
952
                                "%v", cid,
×
953
                                peer.IdentityKey().SerializeCompressed(), err)
×
954
                }
×
955
        }
956

957
        ctx, err := f.cancelReservationCtx(
24✔
958
                peer.IdentityKey(), cid.tempChanID, false,
24✔
959
        )
24✔
960
        if err != nil {
36✔
961
                log.Errorf("unable to cancel reservation: %v", err)
12✔
962
        }
12✔
963

964
        // In case the case where the reservation existed, send the funding
965
        // error on the error channel.
966
        if ctx != nil {
39✔
967
                ctx.err <- fundingErr
15✔
968
        }
15✔
969

970
        // We only send the exact error if it is part of out whitelisted set of
971
        // errors (lnwire.FundingError or lnwallet.ReservationError).
972
        var msg lnwire.ErrorData
24✔
973
        switch e := fundingErr.(type) {
24✔
974
        // Let the actual error message be sent to the remote for the
975
        // whitelisted types.
976
        case lnwallet.ReservationError:
8✔
977
                msg = lnwire.ErrorData(e.Error())
8✔
978
        case lnwire.FundingError:
7✔
979
                msg = lnwire.ErrorData(e.Error())
7✔
980
        case chanacceptor.ChanAcceptError:
3✔
981
                msg = lnwire.ErrorData(e.Error())
3✔
982

983
        // For all other error types we just send a generic error.
984
        default:
15✔
985
                msg = lnwire.ErrorData("funding failed due to internal error")
15✔
986
        }
987

988
        errMsg := &lnwire.Error{
24✔
989
                ChanID: cid.tempChanID,
24✔
990
                Data:   msg,
24✔
991
        }
24✔
992

24✔
993
        log.Debugf("Sending funding error to peer (%x): %v",
24✔
994
                peer.IdentityKey().SerializeCompressed(), spew.Sdump(errMsg))
24✔
995
        if err := peer.SendMessage(false, errMsg); err != nil {
24✔
UNCOV
996
                log.Errorf("unable to send error message to peer %v", err)
×
UNCOV
997
        }
×
998
}
999

1000
// sendWarning sends a new warning message to the target peer, targeting the
1001
// specified cid with the passed funding error.
1002
func (f *Manager) sendWarning(peer lnpeer.Peer, cid *chanIdentifier,
1003
        fundingErr error) {
×
1004

×
1005
        msg := fundingErr.Error()
×
1006

×
1007
        errMsg := &lnwire.Warning{
×
1008
                ChanID: cid.tempChanID,
×
1009
                Data:   lnwire.WarningData(msg),
×
1010
        }
×
1011

×
1012
        log.Debugf("Sending funding warning to peer (%x): %v",
×
1013
                peer.IdentityKey().SerializeCompressed(),
×
1014
                spew.Sdump(errMsg),
×
1015
        )
×
1016

×
1017
        if err := peer.SendMessage(false, errMsg); err != nil {
×
1018
                log.Errorf("unable to send error message to peer %v", err)
×
1019
        }
×
1020
}
1021

1022
// reservationCoordinator is the primary goroutine tasked with progressing the
1023
// funding workflow between the wallet, and any outside peers or local callers.
1024
//
1025
// NOTE: This MUST be run as a goroutine.
1026
func (f *Manager) reservationCoordinator() {
110✔
1027
        defer f.wg.Done()
110✔
1028

110✔
1029
        zombieSweepTicker := time.NewTicker(f.cfg.ZombieSweeperInterval)
110✔
1030
        defer zombieSweepTicker.Stop()
110✔
1031

110✔
1032
        for {
485✔
1033
                select {
375✔
1034
                case fmsg := <-f.fundingMsgs:
212✔
1035
                        switch msg := fmsg.msg.(type) {
212✔
1036
                        case *lnwire.OpenChannel:
56✔
1037
                                f.fundeeProcessOpenChannel(fmsg.peer, msg)
56✔
1038

1039
                        case *lnwire.AcceptChannel:
35✔
1040
                                f.funderProcessAcceptChannel(fmsg.peer, msg)
35✔
1041

1042
                        case *lnwire.FundingCreated:
30✔
1043
                                f.fundeeProcessFundingCreated(fmsg.peer, msg)
30✔
1044

1045
                        case *lnwire.FundingSigned:
30✔
1046
                                f.funderProcessFundingSigned(fmsg.peer, msg)
30✔
1047

1048
                        case *lnwire.ChannelReady:
31✔
1049
                                f.wg.Add(1)
31✔
1050
                                go f.handleChannelReady(fmsg.peer, msg)
31✔
1051

1052
                        case *lnwire.Warning:
42✔
1053
                                f.handleWarningMsg(fmsg.peer, msg)
42✔
1054

1055
                        case *lnwire.Error:
3✔
1056
                                f.handleErrorMsg(fmsg.peer, msg)
3✔
1057
                        }
1058
                case req := <-f.fundingRequests:
59✔
1059
                        f.handleInitFundingMsg(req)
59✔
1060

1061
                case <-zombieSweepTicker.C:
3✔
1062
                        f.pruneZombieReservations()
3✔
1063

1064
                case <-f.quit:
106✔
1065
                        return
106✔
1066
                }
1067
        }
1068
}
1069

1070
// advanceFundingState will advance the channel through the steps after the
1071
// funding transaction is broadcasted, up until the point where the channel is
1072
// ready for operation. This includes waiting for the funding transaction to
1073
// confirm, sending channel_ready to the peer, adding the channel to the graph,
1074
// and announcing the channel. The updateChan can be set non-nil to get
1075
// OpenStatusUpdates.
1076
//
1077
// NOTE: This MUST be run as a goroutine.
1078
func (f *Manager) advanceFundingState(channel *channeldb.OpenChannel,
1079
        pendingChanID PendingChanID,
1080
        updateChan chan<- *lnrpc.OpenStatusUpdate) {
66✔
1081

66✔
1082
        defer f.wg.Done()
66✔
1083

66✔
1084
        // If the channel is still pending we must wait for the funding
66✔
1085
        // transaction to confirm.
66✔
1086
        if channel.IsPending {
124✔
1087
                err := f.advancePendingChannelState(channel, pendingChanID)
58✔
1088
                if err != nil {
82✔
1089
                        log.Errorf("Unable to advance pending state of "+
24✔
1090
                                "ChannelPoint(%v): %v",
24✔
1091
                                channel.FundingOutpoint, err)
24✔
1092
                        return
24✔
1093
                }
24✔
1094
        }
1095

1096
        var chanOpts []lnwallet.ChannelOpt
45✔
1097
        f.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
87✔
1098
                chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
42✔
1099
        })
42✔
1100
        f.cfg.AuxSigner.WhenSome(func(s lnwallet.AuxSigner) {
87✔
1101
                chanOpts = append(chanOpts, lnwallet.WithAuxSigner(s))
42✔
1102
        })
42✔
1103
        f.cfg.AuxResolver.WhenSome(func(s lnwallet.AuxContractResolver) {
45✔
1104
                chanOpts = append(chanOpts, lnwallet.WithAuxResolver(s))
×
1105
        })
×
1106

1107
        // We create the state-machine object which wraps the database state.
1108
        lnChannel, err := lnwallet.NewLightningChannel(
45✔
1109
                nil, channel, nil, chanOpts...,
45✔
1110
        )
45✔
1111
        if err != nil {
45✔
1112
                log.Errorf("Unable to create LightningChannel(%v): %v",
×
1113
                        channel.FundingOutpoint, err)
×
1114
                return
×
1115
        }
×
1116

1117
        for {
196✔
1118
                channelState, shortChanID, err := f.getChannelOpeningState(
151✔
1119
                        &channel.FundingOutpoint,
151✔
1120
                )
151✔
1121
                if err == channeldb.ErrChannelNotFound {
179✔
1122
                        // Channel not in fundingManager's opening database,
28✔
1123
                        // meaning it was successfully announced to the
28✔
1124
                        // network.
28✔
1125
                        // TODO(halseth): could do graph consistency check
28✔
1126
                        // here, and re-add the edge if missing.
28✔
1127
                        log.Debugf("ChannelPoint(%v) with chan_id=%x not "+
28✔
1128
                                "found in opening database, assuming already "+
28✔
1129
                                "announced to the network",
28✔
1130
                                channel.FundingOutpoint, pendingChanID)
28✔
1131
                        return
28✔
1132
                } else if err != nil {
154✔
1133
                        log.Errorf("Unable to query database for "+
×
1134
                                "channel opening state(%v): %v",
×
1135
                                channel.FundingOutpoint, err)
×
1136
                        return
×
1137
                }
×
1138

1139
                // If we did find the channel in the opening state database, we
1140
                // have seen the funding transaction being confirmed, but there
1141
                // are still steps left of the setup procedure. We continue the
1142
                // procedure where we left off.
1143
                err = f.stateStep(
126✔
1144
                        channel, lnChannel, shortChanID, pendingChanID,
126✔
1145
                        channelState, updateChan,
126✔
1146
                )
126✔
1147
                if err != nil {
146✔
1148
                        log.Errorf("Unable to advance state(%v): %v",
20✔
1149
                                channel.FundingOutpoint, err)
20✔
1150
                        return
20✔
1151
                }
20✔
1152
        }
1153
}
1154

1155
// stateStep advances the confirmed channel one step in the funding state
1156
// machine. This method is synchronous and the new channel opening state will
1157
// have been written to the database when it successfully returns. The
1158
// updateChan can be set non-nil to get OpenStatusUpdates.
1159
func (f *Manager) stateStep(channel *channeldb.OpenChannel,
1160
        lnChannel *lnwallet.LightningChannel,
1161
        shortChanID *lnwire.ShortChannelID, pendingChanID PendingChanID,
1162
        channelState channelOpeningState,
1163
        updateChan chan<- *lnrpc.OpenStatusUpdate) error {
126✔
1164

126✔
1165
        chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
126✔
1166
        log.Debugf("Channel(%v) with ShortChanID %v has opening state %v",
126✔
1167
                chanID, shortChanID, channelState)
126✔
1168

126✔
1169
        switch channelState {
126✔
1170
        // The funding transaction was confirmed, but we did not successfully
1171
        // send the channelReady message to the peer, so let's do that now.
1172
        case markedOpen:
38✔
1173
                err := f.sendChannelReady(channel, lnChannel)
38✔
1174
                if err != nil {
39✔
1175
                        return fmt.Errorf("failed sending channelReady: %w",
1✔
1176
                                err)
1✔
1177
                }
1✔
1178

1179
                // As the channelReady message is now sent to the peer, the
1180
                // channel is moved to the next state of the state machine. It
1181
                // will be moved to the last state (actually deleted from the
1182
                // database) after the channel is finally announced.
1183
                err = f.saveChannelOpeningState(
37✔
1184
                        &channel.FundingOutpoint, channelReadySent,
37✔
1185
                        shortChanID,
37✔
1186
                )
37✔
1187
                if err != nil {
37✔
1188
                        return fmt.Errorf("error setting channel state to"+
×
1189
                                " channelReadySent: %w", err)
×
1190
                }
×
1191

1192
                log.Debugf("Channel(%v) with ShortChanID %v: successfully "+
37✔
1193
                        "sent ChannelReady", chanID, shortChanID)
37✔
1194

37✔
1195
                return nil
37✔
1196

1197
        // channelReady was sent to peer, but the channel was not added to the
1198
        // graph and the channel announcement was not sent.
1199
        case channelReadySent:
63✔
1200
                // We must wait until we've received the peer's channel_ready
63✔
1201
                // before sending a channel_update according to BOLT#07.
63✔
1202
                received, err := f.receivedChannelReady(
63✔
1203
                        channel.IdentityPub, chanID,
63✔
1204
                )
63✔
1205
                if err != nil {
63✔
UNCOV
1206
                        return fmt.Errorf("failed to check if channel_ready "+
×
UNCOV
1207
                                "was received: %v", err)
×
UNCOV
1208
                }
×
1209

1210
                if !received {
102✔
1211
                        // We haven't received ChannelReady, so we'll continue
39✔
1212
                        // to the next iteration of the loop after sleeping for
39✔
1213
                        // checkPeerChannelReadyInterval.
39✔
1214
                        select {
39✔
1215
                        case <-time.After(checkPeerChannelReadyInterval):
27✔
1216
                        case <-f.quit:
15✔
1217
                                return ErrFundingManagerShuttingDown
15✔
1218
                        }
1219

1220
                        return nil
27✔
1221
                }
1222

1223
                return f.handleChannelReadyReceived(
27✔
1224
                        channel, shortChanID, pendingChanID, updateChan,
27✔
1225
                )
27✔
1226

1227
        // The channel was added to the Router's topology, but the channel
1228
        // announcement was not sent.
1229
        case addedToGraph:
31✔
1230
                if channel.IsZeroConf() {
40✔
1231
                        // If this is a zero-conf channel, then we will wait
9✔
1232
                        // for it to be confirmed before announcing it to the
9✔
1233
                        // greater network.
9✔
1234
                        err := f.waitForZeroConfChannel(channel)
9✔
1235
                        if err != nil {
14✔
1236
                                return fmt.Errorf("failed waiting for zero "+
5✔
1237
                                        "channel: %v", err)
5✔
1238
                        }
5✔
1239

1240
                        // Update the local shortChanID variable such that
1241
                        // annAfterSixConfs uses the confirmed SCID.
1242
                        confirmedScid := channel.ZeroConfRealScid()
7✔
1243
                        shortChanID = &confirmedScid
7✔
1244
                }
1245

1246
                err := f.annAfterSixConfs(channel, shortChanID)
29✔
1247
                if err != nil {
34✔
1248
                        return fmt.Errorf("error sending channel "+
5✔
1249
                                "announcement: %v", err)
5✔
1250
                }
5✔
1251

1252
                // We delete the channel opening state from our internal
1253
                // database as the opening process has succeeded. We can do
1254
                // this because we assume the AuthenticatedGossiper queues the
1255
                // announcement messages, and persists them in case of a daemon
1256
                // shutdown.
1257
                err = f.deleteChannelOpeningState(&channel.FundingOutpoint)
27✔
1258
                if err != nil {
27✔
1259
                        return fmt.Errorf("error deleting channel state: %w",
×
1260
                                err)
×
1261
                }
×
1262

1263
                // After the fee parameters have been stored in the
1264
                // announcement we can delete them from the database. For
1265
                // private channels we do not announce the channel policy to
1266
                // the network but still need to delete them from the database.
1267
                err = f.deleteInitialForwardingPolicy(chanID)
27✔
1268
                if err != nil {
27✔
1269
                        log.Infof("Could not delete initial policy for chanId "+
×
1270
                                "%x", chanID)
×
1271
                }
×
1272

1273
                log.Debugf("Channel(%v) with ShortChanID %v: successfully "+
27✔
1274
                        "announced", chanID, shortChanID)
27✔
1275

27✔
1276
                return nil
27✔
1277
        }
1278

1279
        return fmt.Errorf("undefined channelState: %v", channelState)
×
1280
}
1281

1282
// advancePendingChannelState waits for a pending channel's funding tx to
1283
// confirm, and marks it open in the database when that happens.
1284
func (f *Manager) advancePendingChannelState(channel *channeldb.OpenChannel,
1285
        pendingChanID PendingChanID) error {
58✔
1286

58✔
1287
        if channel.IsZeroConf() {
65✔
1288
                // Persist the alias to the alias database.
7✔
1289
                baseScid := channel.ShortChannelID
7✔
1290
                err := f.cfg.AliasManager.AddLocalAlias(
7✔
1291
                        baseScid, baseScid, true, false,
7✔
1292
                )
7✔
1293
                if err != nil {
7✔
1294
                        return fmt.Errorf("error adding local alias to "+
×
1295
                                "store: %v", err)
×
1296
                }
×
1297

1298
                // We don't wait for zero-conf channels to be confirmed and
1299
                // instead immediately proceed with the rest of the funding
1300
                // flow. The channel opening state is stored under the alias
1301
                // SCID.
1302
                err = f.saveChannelOpeningState(
7✔
1303
                        &channel.FundingOutpoint, markedOpen,
7✔
1304
                        &channel.ShortChannelID,
7✔
1305
                )
7✔
1306
                if err != nil {
7✔
1307
                        return fmt.Errorf("error setting zero-conf channel "+
×
1308
                                "state to markedOpen: %v", err)
×
1309
                }
×
1310

1311
                // The ShortChannelID is already set since it's an alias, but
1312
                // we still need to mark the channel as no longer pending.
1313
                err = channel.MarkAsOpen(channel.ShortChannelID)
7✔
1314
                if err != nil {
7✔
1315
                        return fmt.Errorf("error setting zero-conf channel's "+
×
1316
                                "pending flag to false: %v", err)
×
1317
                }
×
1318

1319
                // Inform the ChannelNotifier that the channel has transitioned
1320
                // from pending open to open.
1321
                f.cfg.NotifyOpenChannelEvent(channel.FundingOutpoint)
7✔
1322

7✔
1323
                // Find and close the discoverySignal for this channel such
7✔
1324
                // that ChannelReady messages will be processed.
7✔
1325
                chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
7✔
1326
                discoverySignal, ok := f.localDiscoverySignals.Load(chanID)
7✔
1327
                if ok {
14✔
1328
                        close(discoverySignal)
7✔
1329
                }
7✔
1330

1331
                return nil
7✔
1332
        }
1333

1334
        confChannel, err := f.waitForFundingWithTimeout(channel)
54✔
1335
        if err == ErrConfirmationTimeout {
59✔
1336
                return f.fundingTimeout(channel, pendingChanID)
5✔
1337
        } else if err != nil {
79✔
1338
                return fmt.Errorf("error waiting for funding "+
22✔
1339
                        "confirmation for ChannelPoint(%v): %v",
22✔
1340
                        channel.FundingOutpoint, err)
22✔
1341
        }
22✔
1342

1343
        if blockchain.IsCoinBaseTx(confChannel.fundingTx) {
35✔
1344
                // If it's a coinbase transaction, we need to wait for it to
2✔
1345
                // mature. We wait out an additional MinAcceptDepth on top of
2✔
1346
                // the coinbase maturity as an extra margin of safety.
2✔
1347
                maturity := f.cfg.Wallet.Cfg.NetParams.CoinbaseMaturity
2✔
1348
                numCoinbaseConfs := uint32(maturity)
2✔
1349

2✔
1350
                if channel.NumConfsRequired > maturity {
2✔
1351
                        numCoinbaseConfs = uint32(channel.NumConfsRequired)
×
1352
                }
×
1353

1354
                txid := &channel.FundingOutpoint.Hash
2✔
1355
                fundingScript, err := makeFundingScript(channel)
2✔
1356
                if err != nil {
2✔
1357
                        log.Errorf("unable to create funding script for "+
×
1358
                                "ChannelPoint(%v): %v",
×
1359
                                channel.FundingOutpoint, err)
×
1360

×
1361
                        return err
×
1362
                }
×
1363

1364
                confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(
2✔
1365
                        txid, fundingScript, numCoinbaseConfs,
2✔
1366
                        channel.BroadcastHeight(),
2✔
1367
                )
2✔
1368
                if err != nil {
2✔
1369
                        log.Errorf("Unable to register for confirmation of "+
×
1370
                                "ChannelPoint(%v): %v",
×
1371
                                channel.FundingOutpoint, err)
×
1372

×
1373
                        return err
×
1374
                }
×
1375

1376
                select {
2✔
1377
                case _, ok := <-confNtfn.Confirmed:
2✔
1378
                        if !ok {
2✔
1379
                                return fmt.Errorf("ChainNotifier shutting "+
×
1380
                                        "down, can't complete funding flow "+
×
1381
                                        "for ChannelPoint(%v)",
×
1382
                                        channel.FundingOutpoint)
×
1383
                        }
×
1384

1385
                case <-f.quit:
×
1386
                        return ErrFundingManagerShuttingDown
×
1387
                }
1388
        }
1389

1390
        // Success, funding transaction was confirmed.
1391
        chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
33✔
1392
        log.Debugf("ChannelID(%v) is now fully confirmed! "+
33✔
1393
                "(shortChanID=%v)", chanID, confChannel.shortChanID)
33✔
1394

33✔
1395
        err = f.handleFundingConfirmation(channel, confChannel)
33✔
1396
        if err != nil {
33✔
1397
                return fmt.Errorf("unable to handle funding "+
×
1398
                        "confirmation for ChannelPoint(%v): %v",
×
1399
                        channel.FundingOutpoint, err)
×
1400
        }
×
1401

1402
        return nil
33✔
1403
}
1404

1405
// ProcessFundingMsg sends a message to the internal fundingManager goroutine,
1406
// allowing it to handle the lnwire.Message.
1407
func (f *Manager) ProcessFundingMsg(msg lnwire.Message, peer lnpeer.Peer) {
213✔
1408
        select {
213✔
1409
        case f.fundingMsgs <- &fundingMsg{msg, peer}:
213✔
1410
        case <-f.quit:
×
1411
                return
×
1412
        }
1413
}
1414

1415
// fundeeProcessOpenChannel creates an initial 'ChannelReservation' within the
1416
// wallet, then responds to the source peer with an accept channel message
1417
// progressing the funding workflow.
1418
//
1419
// TODO(roasbeef): add error chan to all, let channelManager handle
1420
// error+propagate.
1421
//
1422
//nolint:funlen
1423
func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
1424
        msg *lnwire.OpenChannel) {
56✔
1425

56✔
1426
        // Check number of pending channels to be smaller than maximum allowed
56✔
1427
        // number and send ErrorGeneric to remote peer if condition is
56✔
1428
        // violated.
56✔
1429
        peerPubKey := peer.IdentityKey()
56✔
1430
        peerIDKey := newSerializedKey(peerPubKey)
56✔
1431

56✔
1432
        amt := msg.FundingAmount
56✔
1433

56✔
1434
        // We get all pending channels for this peer. This is the list of the
56✔
1435
        // active reservations and the channels pending open in the database.
56✔
1436
        f.resMtx.RLock()
56✔
1437
        reservations := f.activeReservations[peerIDKey]
56✔
1438

56✔
1439
        // We don't count reservations that were created from a canned funding
56✔
1440
        // shim. The user has registered the shim and therefore expects this
56✔
1441
        // channel to arrive.
56✔
1442
        numPending := 0
56✔
1443
        for _, res := range reservations {
68✔
1444
                if !res.reservation.IsCannedShim() {
24✔
1445
                        numPending++
12✔
1446
                }
12✔
1447
        }
1448
        f.resMtx.RUnlock()
56✔
1449

56✔
1450
        // Create the channel identifier.
56✔
1451
        cid := newChanIdentifier(msg.PendingChannelID)
56✔
1452

56✔
1453
        // Also count the channels that are already pending. There we don't know
56✔
1454
        // the underlying intent anymore, unfortunately.
56✔
1455
        channels, err := f.cfg.ChannelDB.FetchOpenChannels(peerPubKey)
56✔
1456
        if err != nil {
56✔
1457
                f.failFundingFlow(peer, cid, err)
×
1458
                return
×
1459
        }
×
1460

1461
        for _, c := range channels {
71✔
1462
                // Pending channels that have a non-zero thaw height were also
15✔
1463
                // created through a canned funding shim. Those also don't
15✔
1464
                // count towards the DoS protection limit.
15✔
1465
                //
15✔
1466
                // TODO(guggero): Properly store the funding type (wallet, shim,
15✔
1467
                // PSBT) on the channel so we don't need to use the thaw height.
15✔
1468
                if c.IsPending && c.ThawHeight == 0 {
26✔
1469
                        numPending++
11✔
1470
                }
11✔
1471
        }
1472

1473
        // TODO(roasbeef): modify to only accept a _single_ pending channel per
1474
        // block unless white listed
1475
        if numPending >= f.cfg.MaxPendingChannels {
63✔
1476
                f.failFundingFlow(peer, cid, lnwire.ErrMaxPendingChannels)
7✔
1477

7✔
1478
                return
7✔
1479
        }
7✔
1480

1481
        // Ensure that the pendingChansLimit is respected.
1482
        pendingChans, err := f.cfg.ChannelDB.FetchPendingChannels()
52✔
1483
        if err != nil {
52✔
1484
                f.failFundingFlow(peer, cid, err)
×
1485
                return
×
1486
        }
×
1487

1488
        if len(pendingChans) > pendingChansLimit {
52✔
1489
                f.failFundingFlow(peer, cid, lnwire.ErrMaxPendingChannels)
×
1490
                return
×
1491
        }
×
1492

1493
        // We'll also reject any requests to create channels until we're fully
1494
        // synced to the network as we won't be able to properly validate the
1495
        // confirmation of the funding transaction.
1496
        isSynced, _, err := f.cfg.Wallet.IsSynced()
52✔
1497
        if err != nil || !isSynced {
52✔
1498
                if err != nil {
×
1499
                        log.Errorf("unable to query wallet: %v", err)
×
1500
                }
×
1501
                err := errors.New("Synchronizing blockchain")
×
1502
                f.failFundingFlow(peer, cid, err)
×
1503
                return
×
1504
        }
1505

1506
        // Ensure that the remote party respects our maximum channel size.
1507
        if amt > f.cfg.MaxChanSize {
57✔
1508
                f.failFundingFlow(
5✔
1509
                        peer, cid,
5✔
1510
                        lnwallet.ErrChanTooLarge(amt, f.cfg.MaxChanSize),
5✔
1511
                )
5✔
1512
                return
5✔
1513
        }
5✔
1514

1515
        // We'll, also ensure that the remote party isn't attempting to propose
1516
        // a channel that's below our current min channel size.
1517
        if amt < f.cfg.MinChanSize {
53✔
1518
                f.failFundingFlow(
3✔
1519
                        peer, cid,
3✔
1520
                        lnwallet.ErrChanTooSmall(amt, f.cfg.MinChanSize),
3✔
1521
                )
3✔
1522
                return
3✔
1523
        }
3✔
1524

1525
        // If request specifies non-zero push amount and 'rejectpush' is set,
1526
        // signal an error.
1527
        if f.cfg.RejectPush && msg.PushAmount > 0 {
51✔
1528
                f.failFundingFlow(peer, cid, lnwallet.ErrNonZeroPushAmount())
1✔
1529
                return
1✔
1530
        }
1✔
1531

1532
        // Send the OpenChannel request to the ChannelAcceptor to determine
1533
        // whether this node will accept the channel.
1534
        chanReq := &chanacceptor.ChannelAcceptRequest{
49✔
1535
                Node:        peer.IdentityKey(),
49✔
1536
                OpenChanMsg: msg,
49✔
1537
        }
49✔
1538

49✔
1539
        // Query our channel acceptor to determine whether we should reject
49✔
1540
        // the channel.
49✔
1541
        acceptorResp := f.cfg.OpenChannelPredicate.Accept(chanReq)
49✔
1542
        if acceptorResp.RejectChannel() {
52✔
1543
                f.failFundingFlow(peer, cid, acceptorResp.ChanAcceptError)
3✔
1544
                return
3✔
1545
        }
3✔
1546

1547
        log.Infof("Recv'd fundingRequest(amt=%v, push=%v, delay=%v, "+
49✔
1548
                "pendingId=%x) from peer(%x)", amt, msg.PushAmount,
49✔
1549
                msg.CsvDelay, msg.PendingChannelID,
49✔
1550
                peer.IdentityKey().SerializeCompressed())
49✔
1551

49✔
1552
        // Attempt to initialize a reservation within the wallet. If the wallet
49✔
1553
        // has insufficient resources to create the channel, then the
49✔
1554
        // reservation attempt may be rejected. Note that since we're on the
49✔
1555
        // responding side of a single funder workflow, we don't commit any
49✔
1556
        // funds to the channel ourselves.
49✔
1557
        //
49✔
1558
        // Before we init the channel, we'll also check to see what commitment
49✔
1559
        // format we can use with this peer. This is dependent on *both* us and
49✔
1560
        // the remote peer are signaling the proper feature bit if we're using
49✔
1561
        // implicit negotiation, and simply the channel type sent over if we're
49✔
1562
        // using explicit negotiation.
49✔
1563
        chanType, commitType, err := negotiateCommitmentType(
49✔
1564
                msg.ChannelType, peer.LocalFeatures(), peer.RemoteFeatures(),
49✔
1565
        )
49✔
1566
        if err != nil {
49✔
1567
                // TODO(roasbeef): should be using soft errors
×
1568
                log.Errorf("channel type negotiation failed: %v", err)
×
1569
                f.failFundingFlow(peer, cid, err)
×
1570
                return
×
1571
        }
×
1572

1573
        var scidFeatureVal bool
49✔
1574
        if hasFeatures(
49✔
1575
                peer.LocalFeatures(), peer.RemoteFeatures(),
49✔
1576
                lnwire.ScidAliasOptional,
49✔
1577
        ) {
55✔
1578

6✔
1579
                scidFeatureVal = true
6✔
1580
        }
6✔
1581

1582
        var (
49✔
1583
                zeroConf bool
49✔
1584
                scid     bool
49✔
1585
        )
49✔
1586

49✔
1587
        // Only echo back a channel type in AcceptChannel if we actually used
49✔
1588
        // explicit negotiation above.
49✔
1589
        if chanType != nil {
56✔
1590
                // Check if the channel type includes the zero-conf or
7✔
1591
                // scid-alias bits.
7✔
1592
                featureVec := lnwire.RawFeatureVector(*chanType)
7✔
1593
                zeroConf = featureVec.IsSet(lnwire.ZeroConfRequired)
7✔
1594
                scid = featureVec.IsSet(lnwire.ScidAliasRequired)
7✔
1595

7✔
1596
                // If the zero-conf channel type was negotiated, ensure that
7✔
1597
                // the acceptor allows it.
7✔
1598
                if zeroConf && !acceptorResp.ZeroConf {
7✔
1599
                        // Fail the funding flow.
×
1600
                        flowErr := fmt.Errorf("channel acceptor blocked " +
×
1601
                                "zero-conf channel negotiation")
×
1602
                        log.Errorf("Cancelling funding flow for %v based on "+
×
1603
                                "channel acceptor response: %v", cid, flowErr)
×
1604
                        f.failFundingFlow(peer, cid, flowErr)
×
1605
                        return
×
1606
                }
×
1607

1608
                // If the zero-conf channel type wasn't negotiated and the
1609
                // fundee still wants a zero-conf channel, perform more checks.
1610
                // Require that both sides have the scid-alias feature bit set.
1611
                // We don't require anchors here - this is for compatibility
1612
                // with LDK.
1613
                if !zeroConf && acceptorResp.ZeroConf {
7✔
1614
                        if !scidFeatureVal {
×
1615
                                // Fail the funding flow.
×
1616
                                flowErr := fmt.Errorf("scid-alias feature " +
×
1617
                                        "must be negotiated for zero-conf")
×
1618
                                log.Errorf("Cancelling funding flow for "+
×
1619
                                        "zero-conf channel %v: %v", cid,
×
1620
                                        flowErr)
×
1621
                                f.failFundingFlow(peer, cid, flowErr)
×
1622
                                return
×
1623
                        }
×
1624

1625
                        // Set zeroConf to true to enable the zero-conf flow.
1626
                        zeroConf = true
×
1627
                }
1628
        }
1629

1630
        public := msg.ChannelFlags&lnwire.FFAnnounceChannel != 0
49✔
1631
        switch {
49✔
1632
        // Sending the option-scid-alias channel type for a public channel is
1633
        // disallowed.
1634
        case public && scid:
×
1635
                err = fmt.Errorf("option-scid-alias chantype for public " +
×
1636
                        "channel")
×
1637
                log.Errorf("Cancelling funding flow for public channel %v "+
×
1638
                        "with scid-alias: %v", cid, err)
×
1639
                f.failFundingFlow(peer, cid, err)
×
1640

×
1641
                return
×
1642

1643
        // The current variant of taproot channels can only be used with
1644
        // unadvertised channels for now.
1645
        case commitType.IsTaproot() && public:
×
1646
                err = fmt.Errorf("taproot channel type for public channel")
×
1647
                log.Errorf("Cancelling funding flow for public taproot "+
×
1648
                        "channel %v: %v", cid, err)
×
1649
                f.failFundingFlow(peer, cid, err)
×
1650

×
1651
                return
×
1652
        }
1653

1654
        // At this point, if we have an AuxFundingController active, we'll
1655
        // check to see if we have a special tapscript root to use in our
1656
        // MuSig funding output.
1657
        tapscriptRoot, err := fn.MapOptionZ(
49✔
1658
                f.cfg.AuxFundingController,
49✔
1659
                func(c AuxFundingController) AuxTapscriptResult {
49✔
1660
                        return c.DeriveTapscriptRoot(msg.PendingChannelID)
×
1661
                },
×
1662
        ).Unpack()
1663
        if err != nil {
49✔
1664
                err = fmt.Errorf("error deriving tapscript root: %w", err)
×
1665
                log.Error(err)
×
1666
                f.failFundingFlow(peer, cid, err)
×
1667

×
1668
                return
×
1669
        }
×
1670

1671
        req := &lnwallet.InitFundingReserveMsg{
49✔
1672
                ChainHash:        &msg.ChainHash,
49✔
1673
                PendingChanID:    msg.PendingChannelID,
49✔
1674
                NodeID:           peer.IdentityKey(),
49✔
1675
                NodeAddr:         peer.Address(),
49✔
1676
                LocalFundingAmt:  0,
49✔
1677
                RemoteFundingAmt: amt,
49✔
1678
                CommitFeePerKw:   chainfee.SatPerKWeight(msg.FeePerKiloWeight),
49✔
1679
                FundingFeePerKw:  0,
49✔
1680
                PushMSat:         msg.PushAmount,
49✔
1681
                Flags:            msg.ChannelFlags,
49✔
1682
                MinConfs:         1,
49✔
1683
                CommitType:       commitType,
49✔
1684
                ZeroConf:         zeroConf,
49✔
1685
                OptionScidAlias:  scid,
49✔
1686
                ScidAliasFeature: scidFeatureVal,
49✔
1687
                TapscriptRoot:    tapscriptRoot,
49✔
1688
        }
49✔
1689

49✔
1690
        reservation, err := f.cfg.Wallet.InitChannelReservation(req)
49✔
1691
        if err != nil {
49✔
1692
                log.Errorf("Unable to initialize reservation: %v", err)
×
1693
                f.failFundingFlow(peer, cid, err)
×
1694
                return
×
1695
        }
×
1696

1697
        log.Debugf("Initialized channel reservation: zeroConf=%v, psbt=%v, "+
49✔
1698
                "cannedShim=%v", reservation.IsZeroConf(),
49✔
1699
                reservation.IsPsbt(), reservation.IsCannedShim())
49✔
1700

49✔
1701
        if zeroConf {
54✔
1702
                // Store an alias for zero-conf channels. Other option-scid
5✔
1703
                // channels will do this at a later point.
5✔
1704
                aliasScid, err := f.cfg.AliasManager.RequestAlias()
5✔
1705
                if err != nil {
5✔
1706
                        log.Errorf("Unable to request alias: %v", err)
×
1707
                        f.failFundingFlow(peer, cid, err)
×
1708
                        return
×
1709
                }
×
1710

1711
                reservation.AddAlias(aliasScid)
5✔
1712
        }
1713

1714
        // As we're the responder, we get to specify the number of confirmations
1715
        // that we require before both of us consider the channel open. We'll
1716
        // use our mapping to derive the proper number of confirmations based on
1717
        // the amount of the channel, and also if any funds are being pushed to
1718
        // us. If a depth value was set by our channel acceptor, we will use
1719
        // that value instead.
1720
        numConfsReq := f.cfg.NumRequiredConfs(msg.FundingAmount, msg.PushAmount)
49✔
1721
        if acceptorResp.MinAcceptDepth != 0 {
49✔
1722
                numConfsReq = acceptorResp.MinAcceptDepth
×
1723
        }
×
1724

1725
        // We'll ignore the min_depth calculated above if this is a zero-conf
1726
        // channel.
1727
        if zeroConf {
54✔
1728
                numConfsReq = 0
5✔
1729
        }
5✔
1730

1731
        reservation.SetNumConfsRequired(numConfsReq)
49✔
1732

49✔
1733
        // We'll also validate and apply all the constraints the initiating
49✔
1734
        // party is attempting to dictate for our commitment transaction.
49✔
1735
        stateBounds := &channeldb.ChannelStateBounds{
49✔
1736
                ChanReserve:      msg.ChannelReserve,
49✔
1737
                MaxPendingAmount: msg.MaxValueInFlight,
49✔
1738
                MinHTLC:          msg.HtlcMinimum,
49✔
1739
                MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
49✔
1740
        }
49✔
1741
        commitParams := &channeldb.CommitmentParams{
49✔
1742
                DustLimit: msg.DustLimit,
49✔
1743
                CsvDelay:  msg.CsvDelay,
49✔
1744
        }
49✔
1745
        err = reservation.CommitConstraints(
49✔
1746
                stateBounds, commitParams, f.cfg.MaxLocalCSVDelay, true,
49✔
1747
        )
49✔
1748
        if err != nil {
49✔
UNCOV
1749
                log.Errorf("Unacceptable channel constraints: %v", err)
×
UNCOV
1750
                f.failFundingFlow(peer, cid, err)
×
UNCOV
1751
                return
×
UNCOV
1752
        }
×
1753

1754
        // Check whether the peer supports upfront shutdown, and get a new
1755
        // wallet address if our node is configured to set shutdown addresses by
1756
        // default. We use the upfront shutdown script provided by our channel
1757
        // acceptor (if any) in lieu of user input.
1758
        shutdown, err := getUpfrontShutdownScript(
49✔
1759
                f.cfg.EnableUpfrontShutdown, peer, acceptorResp.UpfrontShutdown,
49✔
1760
                f.selectShutdownScript,
49✔
1761
        )
49✔
1762
        if err != nil {
49✔
1763
                f.failFundingFlow(
×
1764
                        peer, cid,
×
1765
                        fmt.Errorf("getUpfrontShutdownScript error: %w", err),
×
1766
                )
×
1767
                return
×
1768
        }
×
1769
        reservation.SetOurUpfrontShutdown(shutdown)
49✔
1770

49✔
1771
        // If a script enforced channel lease is being proposed, we'll need to
49✔
1772
        // validate its custom TLV records.
49✔
1773
        if commitType == lnwallet.CommitmentTypeScriptEnforcedLease {
52✔
1774
                if msg.LeaseExpiry == nil {
3✔
1775
                        err := errors.New("missing lease expiry")
×
1776
                        f.failFundingFlow(peer, cid, err)
×
1777
                        return
×
1778
                }
×
1779

1780
                // If we had a shim registered for this channel prior to
1781
                // receiving its corresponding OpenChannel message, then we'll
1782
                // validate the proposed LeaseExpiry against what was registered
1783
                // in our shim.
1784
                if reservation.LeaseExpiry() != 0 {
6✔
1785
                        if uint32(*msg.LeaseExpiry) !=
3✔
1786
                                reservation.LeaseExpiry() {
3✔
1787

×
1788
                                err := errors.New("lease expiry mismatch")
×
1789
                                f.failFundingFlow(peer, cid, err)
×
1790
                                return
×
1791
                        }
×
1792
                }
1793
        }
1794

1795
        log.Infof("Requiring %v confirmations for pendingChan(%x): "+
49✔
1796
                "amt=%v, push_amt=%v, committype=%v, upfrontShutdown=%x",
49✔
1797
                numConfsReq, msg.PendingChannelID, amt, msg.PushAmount,
49✔
1798
                commitType, msg.UpfrontShutdownScript)
49✔
1799

49✔
1800
        // Generate our required constraints for the remote party, using the
49✔
1801
        // values provided by the channel acceptor if they are non-zero.
49✔
1802
        remoteCsvDelay := f.cfg.RequiredRemoteDelay(amt)
49✔
1803
        if acceptorResp.CSVDelay != 0 {
49✔
1804
                remoteCsvDelay = acceptorResp.CSVDelay
×
1805
        }
×
1806

1807
        // If our default dust limit was above their ChannelReserve, we change
1808
        // it to the ChannelReserve. We must make sure the ChannelReserve we
1809
        // send in the AcceptChannel message is above both dust limits.
1810
        // Therefore, take the maximum of msg.DustLimit and our dust limit.
1811
        //
1812
        // NOTE: Even with this bounding, the ChannelAcceptor may return an
1813
        // BOLT#02-invalid ChannelReserve.
1814
        maxDustLimit := reservation.OurContribution().DustLimit
49✔
1815
        if msg.DustLimit > maxDustLimit {
49✔
1816
                maxDustLimit = msg.DustLimit
×
1817
        }
×
1818

1819
        chanReserve := f.cfg.RequiredRemoteChanReserve(amt, maxDustLimit)
49✔
1820
        if acceptorResp.Reserve != 0 {
49✔
1821
                chanReserve = acceptorResp.Reserve
×
1822
        }
×
1823

1824
        remoteMaxValue := f.cfg.RequiredRemoteMaxValue(amt)
49✔
1825
        if acceptorResp.InFlightTotal != 0 {
49✔
1826
                remoteMaxValue = acceptorResp.InFlightTotal
×
1827
        }
×
1828

1829
        maxHtlcs := f.cfg.RequiredRemoteMaxHTLCs(amt)
49✔
1830
        if acceptorResp.HtlcLimit != 0 {
49✔
1831
                maxHtlcs = acceptorResp.HtlcLimit
×
1832
        }
×
1833

1834
        // Default to our default minimum hltc value, replacing it with the
1835
        // channel acceptor's value if it is set.
1836
        minHtlc := f.cfg.DefaultMinHtlcIn
49✔
1837
        if acceptorResp.MinHtlcIn != 0 {
49✔
1838
                minHtlc = acceptorResp.MinHtlcIn
×
1839
        }
×
1840

1841
        // If we are handling a FundingOpen request then we need to specify the
1842
        // default channel fees since they are not provided by the responder
1843
        // interactively.
1844
        ourContribution := reservation.OurContribution()
49✔
1845
        forwardingPolicy := f.defaultForwardingPolicy(
49✔
1846
                ourContribution.ChannelStateBounds,
49✔
1847
        )
49✔
1848

49✔
1849
        // Once the reservation has been created successfully, we add it to
49✔
1850
        // this peer's map of pending reservations to track this particular
49✔
1851
        // reservation until either abort or completion.
49✔
1852
        f.resMtx.Lock()
49✔
1853
        if _, ok := f.activeReservations[peerIDKey]; !ok {
94✔
1854
                f.activeReservations[peerIDKey] = make(pendingChannels)
45✔
1855
        }
45✔
1856
        resCtx := &reservationWithCtx{
49✔
1857
                reservation:       reservation,
49✔
1858
                chanAmt:           amt,
49✔
1859
                forwardingPolicy:  *forwardingPolicy,
49✔
1860
                remoteCsvDelay:    remoteCsvDelay,
49✔
1861
                remoteMinHtlc:     minHtlc,
49✔
1862
                remoteMaxValue:    remoteMaxValue,
49✔
1863
                remoteMaxHtlcs:    maxHtlcs,
49✔
1864
                remoteChanReserve: chanReserve,
49✔
1865
                maxLocalCsv:       f.cfg.MaxLocalCSVDelay,
49✔
1866
                channelType:       chanType,
49✔
1867
                err:               make(chan error, 1),
49✔
1868
                peer:              peer,
49✔
1869
        }
49✔
1870
        f.activeReservations[peerIDKey][msg.PendingChannelID] = resCtx
49✔
1871
        f.resMtx.Unlock()
49✔
1872

49✔
1873
        // Update the timestamp once the fundingOpenMsg has been handled.
49✔
1874
        defer resCtx.updateTimestamp()
49✔
1875

49✔
1876
        cfg := channeldb.ChannelConfig{
49✔
1877
                ChannelStateBounds: channeldb.ChannelStateBounds{
49✔
1878
                        MaxPendingAmount: remoteMaxValue,
49✔
1879
                        ChanReserve:      chanReserve,
49✔
1880
                        MinHTLC:          minHtlc,
49✔
1881
                        MaxAcceptedHtlcs: maxHtlcs,
49✔
1882
                },
49✔
1883
                CommitmentParams: channeldb.CommitmentParams{
49✔
1884
                        DustLimit: msg.DustLimit,
49✔
1885
                        CsvDelay:  remoteCsvDelay,
49✔
1886
                },
49✔
1887
                MultiSigKey: keychain.KeyDescriptor{
49✔
1888
                        PubKey: copyPubKey(msg.FundingKey),
49✔
1889
                },
49✔
1890
                RevocationBasePoint: keychain.KeyDescriptor{
49✔
1891
                        PubKey: copyPubKey(msg.RevocationPoint),
49✔
1892
                },
49✔
1893
                PaymentBasePoint: keychain.KeyDescriptor{
49✔
1894
                        PubKey: copyPubKey(msg.PaymentPoint),
49✔
1895
                },
49✔
1896
                DelayBasePoint: keychain.KeyDescriptor{
49✔
1897
                        PubKey: copyPubKey(msg.DelayedPaymentPoint),
49✔
1898
                },
49✔
1899
                HtlcBasePoint: keychain.KeyDescriptor{
49✔
1900
                        PubKey: copyPubKey(msg.HtlcPoint),
49✔
1901
                },
49✔
1902
        }
49✔
1903

49✔
1904
        // With our parameters set, we'll now process their contribution so we
49✔
1905
        // can move the funding workflow ahead.
49✔
1906
        remoteContribution := &lnwallet.ChannelContribution{
49✔
1907
                FundingAmount:        amt,
49✔
1908
                FirstCommitmentPoint: msg.FirstCommitmentPoint,
49✔
1909
                ChannelConfig:        &cfg,
49✔
1910
                UpfrontShutdown:      msg.UpfrontShutdownScript,
49✔
1911
        }
49✔
1912

49✔
1913
        if resCtx.reservation.IsTaproot() {
54✔
1914
                localNonce, err := msg.LocalNonce.UnwrapOrErrV(errNoLocalNonce)
5✔
1915
                if err != nil {
5✔
1916
                        log.Error(errNoLocalNonce)
×
1917

×
1918
                        f.failFundingFlow(resCtx.peer, cid, errNoLocalNonce)
×
1919

×
1920
                        return
×
1921
                }
×
1922

1923
                remoteContribution.LocalNonce = &musig2.Nonces{
5✔
1924
                        PubNonce: localNonce,
5✔
1925
                }
5✔
1926
        }
1927

1928
        err = reservation.ProcessSingleContribution(remoteContribution)
49✔
1929
        if err != nil {
55✔
1930
                log.Errorf("unable to add contribution reservation: %v", err)
6✔
1931
                f.failFundingFlow(peer, cid, err)
6✔
1932
                return
6✔
1933
        }
6✔
1934

1935
        log.Infof("Sending fundingResp for pending_id(%x)",
43✔
1936
                msg.PendingChannelID)
43✔
1937
        bounds := remoteContribution.ChannelConfig.ChannelStateBounds
43✔
1938
        log.Debugf("Remote party accepted channel state space bounds: %v",
43✔
1939
                lnutils.SpewLogClosure(bounds))
43✔
1940
        params := remoteContribution.ChannelConfig.CommitmentParams
43✔
1941
        log.Debugf("Remote party accepted commitment rendering params: %v",
43✔
1942
                lnutils.SpewLogClosure(params))
43✔
1943

43✔
1944
        reservation.SetState(lnwallet.SentAcceptChannel)
43✔
1945

43✔
1946
        // With the initiator's contribution recorded, respond with our
43✔
1947
        // contribution in the next message of the workflow.
43✔
1948
        fundingAccept := lnwire.AcceptChannel{
43✔
1949
                PendingChannelID:      msg.PendingChannelID,
43✔
1950
                DustLimit:             ourContribution.DustLimit,
43✔
1951
                MaxValueInFlight:      remoteMaxValue,
43✔
1952
                ChannelReserve:        chanReserve,
43✔
1953
                MinAcceptDepth:        uint32(numConfsReq),
43✔
1954
                HtlcMinimum:           minHtlc,
43✔
1955
                CsvDelay:              remoteCsvDelay,
43✔
1956
                MaxAcceptedHTLCs:      maxHtlcs,
43✔
1957
                FundingKey:            ourContribution.MultiSigKey.PubKey,
43✔
1958
                RevocationPoint:       ourContribution.RevocationBasePoint.PubKey,
43✔
1959
                PaymentPoint:          ourContribution.PaymentBasePoint.PubKey,
43✔
1960
                DelayedPaymentPoint:   ourContribution.DelayBasePoint.PubKey,
43✔
1961
                HtlcPoint:             ourContribution.HtlcBasePoint.PubKey,
43✔
1962
                FirstCommitmentPoint:  ourContribution.FirstCommitmentPoint,
43✔
1963
                UpfrontShutdownScript: ourContribution.UpfrontShutdown,
43✔
1964
                ChannelType:           chanType,
43✔
1965
                LeaseExpiry:           msg.LeaseExpiry,
43✔
1966
        }
43✔
1967

43✔
1968
        if commitType.IsTaproot() {
48✔
1969
                fundingAccept.LocalNonce = lnwire.SomeMusig2Nonce(
5✔
1970
                        ourContribution.LocalNonce.PubNonce,
5✔
1971
                )
5✔
1972
        }
5✔
1973

1974
        if err := peer.SendMessage(true, &fundingAccept); err != nil {
43✔
1975
                log.Errorf("unable to send funding response to peer: %v", err)
×
1976
                f.failFundingFlow(peer, cid, err)
×
1977
                return
×
1978
        }
×
1979
}
1980

1981
// funderProcessAcceptChannel processes a response to the workflow initiation
1982
// sent by the remote peer. This message then queues a message with the funding
1983
// outpoint, and a commitment signature to the remote peer.
1984
//
1985
//nolint:funlen
1986
func (f *Manager) funderProcessAcceptChannel(peer lnpeer.Peer,
1987
        msg *lnwire.AcceptChannel) {
35✔
1988

35✔
1989
        pendingChanID := msg.PendingChannelID
35✔
1990
        peerKey := peer.IdentityKey()
35✔
1991
        var peerKeyBytes []byte
35✔
1992
        if peerKey != nil {
70✔
1993
                peerKeyBytes = peerKey.SerializeCompressed()
35✔
1994
        }
35✔
1995

1996
        resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
35✔
1997
        if err != nil {
35✔
1998
                log.Warnf("Can't find reservation (peerKey:%x, chan_id:%v)",
×
1999
                        peerKeyBytes, pendingChanID)
×
2000
                return
×
2001
        }
×
2002

2003
        // Update the timestamp once the fundingAcceptMsg has been handled.
2004
        defer resCtx.updateTimestamp()
35✔
2005

35✔
2006
        if resCtx.reservation.State() != lnwallet.SentOpenChannel {
35✔
2007
                return
×
2008
        }
×
2009

2010
        log.Infof("Recv'd fundingResponse for pending_id(%x)",
35✔
2011
                pendingChanID[:])
35✔
2012

35✔
2013
        // Create the channel identifier.
35✔
2014
        cid := newChanIdentifier(msg.PendingChannelID)
35✔
2015

35✔
2016
        // Perform some basic validation of any custom TLV records included.
35✔
2017
        //
35✔
2018
        // TODO: Return errors as funding.Error to give context to remote peer?
35✔
2019
        if resCtx.channelType != nil {
42✔
2020
                // We'll want to quickly check that the ChannelType echoed by
7✔
2021
                // the channel request recipient matches what we proposed.
7✔
2022
                if msg.ChannelType == nil {
8✔
2023
                        err := errors.New("explicit channel type not echoed " +
1✔
2024
                                "back")
1✔
2025
                        f.failFundingFlow(peer, cid, err)
1✔
2026
                        return
1✔
2027
                }
1✔
2028
                proposedFeatures := lnwire.RawFeatureVector(*resCtx.channelType)
6✔
2029
                ackedFeatures := lnwire.RawFeatureVector(*msg.ChannelType)
6✔
2030
                if !proposedFeatures.Equals(&ackedFeatures) {
6✔
2031
                        err := errors.New("channel type mismatch")
×
2032
                        f.failFundingFlow(peer, cid, err)
×
2033
                        return
×
2034
                }
×
2035

2036
                // We'll want to do the same with the LeaseExpiry if one should
2037
                // be set.
2038
                if resCtx.reservation.LeaseExpiry() != 0 {
9✔
2039
                        if msg.LeaseExpiry == nil {
3✔
2040
                                err := errors.New("lease expiry not echoed " +
×
2041
                                        "back")
×
2042
                                f.failFundingFlow(peer, cid, err)
×
2043
                                return
×
2044
                        }
×
2045
                        if uint32(*msg.LeaseExpiry) !=
3✔
2046
                                resCtx.reservation.LeaseExpiry() {
3✔
2047

×
2048
                                err := errors.New("lease expiry mismatch")
×
2049
                                f.failFundingFlow(peer, cid, err)
×
2050
                                return
×
2051
                        }
×
2052
                }
2053
        } else if msg.ChannelType != nil {
28✔
2054
                // The spec isn't too clear about whether it's okay to set the
×
2055
                // channel type in the accept_channel response if we didn't
×
2056
                // explicitly set it in the open_channel message. For now, we
×
2057
                // check that it's the same type we'd have arrived through
×
2058
                // implicit negotiation. If it's another type, we fail the flow.
×
2059
                _, implicitCommitType := implicitNegotiateCommitmentType(
×
2060
                        peer.LocalFeatures(), peer.RemoteFeatures(),
×
2061
                )
×
2062

×
2063
                _, negotiatedCommitType, err := negotiateCommitmentType(
×
2064
                        msg.ChannelType, peer.LocalFeatures(),
×
2065
                        peer.RemoteFeatures(),
×
2066
                )
×
2067
                if err != nil {
×
2068
                        err := errors.New("received unexpected channel type")
×
2069
                        f.failFundingFlow(peer, cid, err)
×
2070
                        return
×
2071
                }
×
2072

2073
                if implicitCommitType != negotiatedCommitType {
×
2074
                        err := errors.New("negotiated unexpected channel type")
×
2075
                        f.failFundingFlow(peer, cid, err)
×
2076
                        return
×
2077
                }
×
2078
        }
2079

2080
        // The required number of confirmations should not be greater than the
2081
        // maximum number of confirmations required by the ChainNotifier to
2082
        // properly dispatch confirmations.
2083
        if msg.MinAcceptDepth > chainntnfs.MaxNumConfs {
35✔
2084
                err := lnwallet.ErrNumConfsTooLarge(
1✔
2085
                        msg.MinAcceptDepth, chainntnfs.MaxNumConfs,
1✔
2086
                )
1✔
2087
                log.Warnf("Unacceptable channel constraints: %v", err)
1✔
2088
                f.failFundingFlow(peer, cid, err)
1✔
2089
                return
1✔
2090
        }
1✔
2091

2092
        // Check that zero-conf channels have minimum depth set to 0.
2093
        if resCtx.reservation.IsZeroConf() && msg.MinAcceptDepth != 0 {
33✔
2094
                err = fmt.Errorf("zero-conf channel has min_depth non-zero")
×
2095
                log.Warn(err)
×
2096
                f.failFundingFlow(peer, cid, err)
×
2097
                return
×
2098
        }
×
2099

2100
        // If this is not a zero-conf channel but the peer responded with a
2101
        // min-depth of zero, we will use our minimum of 1 instead.
2102
        minDepth := msg.MinAcceptDepth
33✔
2103
        if !resCtx.reservation.IsZeroConf() && minDepth == 0 {
33✔
2104
                log.Infof("Responder to pending_id=%v sent a minimum "+
×
2105
                        "confirmation depth of 0 for non-zero-conf channel. "+
×
2106
                        "We will use a minimum depth of 1 instead.",
×
2107
                        cid.tempChanID)
×
2108

×
2109
                minDepth = 1
×
2110
        }
×
2111

2112
        // We'll also specify the responder's preference for the number of
2113
        // required confirmations, and also the set of channel constraints
2114
        // they've specified for commitment states we can create.
2115
        resCtx.reservation.SetNumConfsRequired(uint16(minDepth))
33✔
2116
        bounds := channeldb.ChannelStateBounds{
33✔
2117
                ChanReserve:      msg.ChannelReserve,
33✔
2118
                MaxPendingAmount: msg.MaxValueInFlight,
33✔
2119
                MinHTLC:          msg.HtlcMinimum,
33✔
2120
                MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
33✔
2121
        }
33✔
2122
        commitParams := channeldb.CommitmentParams{
33✔
2123
                DustLimit: msg.DustLimit,
33✔
2124
                CsvDelay:  msg.CsvDelay,
33✔
2125
        }
33✔
2126
        err = resCtx.reservation.CommitConstraints(
33✔
2127
                &bounds, &commitParams, resCtx.maxLocalCsv, false,
33✔
2128
        )
33✔
2129
        if err != nil {
34✔
2130
                log.Warnf("Unacceptable channel constraints: %v", err)
1✔
2131
                f.failFundingFlow(peer, cid, err)
1✔
2132
                return
1✔
2133
        }
1✔
2134

2135
        cfg := channeldb.ChannelConfig{
32✔
2136
                ChannelStateBounds: channeldb.ChannelStateBounds{
32✔
2137
                        MaxPendingAmount: resCtx.remoteMaxValue,
32✔
2138
                        ChanReserve:      resCtx.remoteChanReserve,
32✔
2139
                        MinHTLC:          resCtx.remoteMinHtlc,
32✔
2140
                        MaxAcceptedHtlcs: resCtx.remoteMaxHtlcs,
32✔
2141
                },
32✔
2142
                CommitmentParams: channeldb.CommitmentParams{
32✔
2143
                        DustLimit: msg.DustLimit,
32✔
2144
                        CsvDelay:  resCtx.remoteCsvDelay,
32✔
2145
                },
32✔
2146
                MultiSigKey: keychain.KeyDescriptor{
32✔
2147
                        PubKey: copyPubKey(msg.FundingKey),
32✔
2148
                },
32✔
2149
                RevocationBasePoint: keychain.KeyDescriptor{
32✔
2150
                        PubKey: copyPubKey(msg.RevocationPoint),
32✔
2151
                },
32✔
2152
                PaymentBasePoint: keychain.KeyDescriptor{
32✔
2153
                        PubKey: copyPubKey(msg.PaymentPoint),
32✔
2154
                },
32✔
2155
                DelayBasePoint: keychain.KeyDescriptor{
32✔
2156
                        PubKey: copyPubKey(msg.DelayedPaymentPoint),
32✔
2157
                },
32✔
2158
                HtlcBasePoint: keychain.KeyDescriptor{
32✔
2159
                        PubKey: copyPubKey(msg.HtlcPoint),
32✔
2160
                },
32✔
2161
        }
32✔
2162

32✔
2163
        // The remote node has responded with their portion of the channel
32✔
2164
        // contribution. At this point, we can process their contribution which
32✔
2165
        // allows us to construct and sign both the commitment transaction, and
32✔
2166
        // the funding transaction.
32✔
2167
        remoteContribution := &lnwallet.ChannelContribution{
32✔
2168
                FirstCommitmentPoint: msg.FirstCommitmentPoint,
32✔
2169
                ChannelConfig:        &cfg,
32✔
2170
                UpfrontShutdown:      msg.UpfrontShutdownScript,
32✔
2171
        }
32✔
2172

32✔
2173
        if resCtx.reservation.IsTaproot() {
37✔
2174
                localNonce, err := msg.LocalNonce.UnwrapOrErrV(errNoLocalNonce)
5✔
2175
                if err != nil {
5✔
2176
                        log.Error(errNoLocalNonce)
×
2177

×
2178
                        f.failFundingFlow(resCtx.peer, cid, errNoLocalNonce)
×
2179

×
2180
                        return
×
2181
                }
×
2182

2183
                remoteContribution.LocalNonce = &musig2.Nonces{
5✔
2184
                        PubNonce: localNonce,
5✔
2185
                }
5✔
2186
        }
2187

2188
        err = resCtx.reservation.ProcessContribution(remoteContribution)
32✔
2189

32✔
2190
        // The wallet has detected that a PSBT funding process was requested by
32✔
2191
        // the user and has halted the funding process after negotiating the
32✔
2192
        // multisig keys. We now have everything that is needed for the user to
32✔
2193
        // start constructing a PSBT that sends to the multisig funding address.
32✔
2194
        var psbtIntent *chanfunding.PsbtIntent
32✔
2195
        if psbtErr, ok := err.(*lnwallet.PsbtFundingRequired); ok {
35✔
2196
                // Return the information that is needed by the user to
3✔
2197
                // construct the PSBT back to the caller.
3✔
2198
                addr, amt, packet, err := psbtErr.Intent.FundingParams()
3✔
2199
                if err != nil {
3✔
2200
                        log.Errorf("Unable to process PSBT funding params "+
×
2201
                                "for contribution from %x: %v", peerKeyBytes,
×
2202
                                err)
×
2203
                        f.failFundingFlow(peer, cid, err)
×
2204
                        return
×
2205
                }
×
2206
                var buf bytes.Buffer
3✔
2207
                err = packet.Serialize(&buf)
3✔
2208
                if err != nil {
3✔
2209
                        log.Errorf("Unable to serialize PSBT for "+
×
2210
                                "contribution from %x: %v", peerKeyBytes, err)
×
2211
                        f.failFundingFlow(peer, cid, err)
×
2212
                        return
×
2213
                }
×
2214
                resCtx.updates <- &lnrpc.OpenStatusUpdate{
3✔
2215
                        PendingChanId: pendingChanID[:],
3✔
2216
                        Update: &lnrpc.OpenStatusUpdate_PsbtFund{
3✔
2217
                                PsbtFund: &lnrpc.ReadyForPsbtFunding{
3✔
2218
                                        FundingAddress: addr.EncodeAddress(),
3✔
2219
                                        FundingAmount:  amt,
3✔
2220
                                        Psbt:           buf.Bytes(),
3✔
2221
                                },
3✔
2222
                        },
3✔
2223
                }
3✔
2224
                psbtIntent = psbtErr.Intent
3✔
2225
        } else if err != nil {
32✔
2226
                log.Errorf("Unable to process contribution from %x: %v",
×
2227
                        peerKeyBytes, err)
×
2228
                f.failFundingFlow(peer, cid, err)
×
2229
                return
×
2230
        }
×
2231

2232
        log.Infof("pendingChan(%x): remote party proposes num_confs=%v, "+
32✔
2233
                "csv_delay=%v", pendingChanID[:], msg.MinAcceptDepth,
32✔
2234
                msg.CsvDelay)
32✔
2235
        bounds = remoteContribution.ChannelConfig.ChannelStateBounds
32✔
2236
        log.Debugf("Remote party accepted channel state space bounds: %v",
32✔
2237
                lnutils.SpewLogClosure(bounds))
32✔
2238
        commitParams = remoteContribution.ChannelConfig.CommitmentParams
32✔
2239
        log.Debugf("Remote party accepted commitment rendering params: %v",
32✔
2240
                lnutils.SpewLogClosure(commitParams))
32✔
2241

32✔
2242
        // If the user requested funding through a PSBT, we cannot directly
32✔
2243
        // continue now and need to wait for the fully funded and signed PSBT
32✔
2244
        // to arrive. To not block any other channels from opening, we wait in
32✔
2245
        // a separate goroutine.
32✔
2246
        if psbtIntent != nil {
35✔
2247
                f.wg.Add(1)
3✔
2248
                go func() {
6✔
2249
                        defer f.wg.Done()
3✔
2250

3✔
2251
                        f.waitForPsbt(psbtIntent, resCtx, cid)
3✔
2252
                }()
3✔
2253

2254
                // With the new goroutine spawned, we can now exit to unblock
2255
                // the main event loop.
2256
                return
3✔
2257
        }
2258

2259
        // In a normal, non-PSBT funding flow, we can jump directly to the next
2260
        // step where we expect our contribution to be finalized.
2261
        f.continueFundingAccept(resCtx, cid)
32✔
2262
}
2263

2264
// waitForPsbt blocks until either a signed PSBT arrives, an error occurs or
2265
// the funding manager shuts down. In the case of a valid PSBT, the funding flow
2266
// is continued.
2267
//
2268
// NOTE: This method must be called as a goroutine.
2269
func (f *Manager) waitForPsbt(intent *chanfunding.PsbtIntent,
2270
        resCtx *reservationWithCtx, cid *chanIdentifier) {
3✔
2271

3✔
2272
        // failFlow is a helper that logs an error message with the current
3✔
2273
        // context and then fails the funding flow.
3✔
2274
        peerKey := resCtx.peer.IdentityKey()
3✔
2275
        failFlow := func(errMsg string, cause error) {
6✔
2276
                log.Errorf("Unable to handle funding accept message "+
3✔
2277
                        "for peer_key=%x, pending_chan_id=%x: %s: %v",
3✔
2278
                        peerKey.SerializeCompressed(), cid.tempChanID, errMsg,
3✔
2279
                        cause)
3✔
2280
                f.failFundingFlow(resCtx.peer, cid, cause)
3✔
2281
        }
3✔
2282

2283
        // We'll now wait until the intent has received the final and complete
2284
        // funding transaction. If the channel is closed without any error being
2285
        // sent, we know everything's going as expected.
2286
        select {
3✔
2287
        case err := <-intent.PsbtReady:
3✔
2288
                switch err {
3✔
2289
                // If the user canceled the funding reservation, we need to
2290
                // inform the other peer about us canceling the reservation.
2291
                case chanfunding.ErrUserCanceled:
3✔
2292
                        failFlow("aborting PSBT flow", err)
3✔
2293
                        return
3✔
2294

2295
                // If the remote canceled the funding reservation, we don't need
2296
                // to send another fail message. But we want to inform the user
2297
                // about what happened.
2298
                case chanfunding.ErrRemoteCanceled:
3✔
2299
                        log.Infof("Remote canceled, aborting PSBT flow "+
3✔
2300
                                "for peer_key=%x, pending_chan_id=%x",
3✔
2301
                                peerKey.SerializeCompressed(), cid.tempChanID)
3✔
2302
                        return
3✔
2303

2304
                // Nil error means the flow continues normally now.
2305
                case nil:
3✔
2306

2307
                // For any other error, we'll fail the funding flow.
2308
                default:
×
2309
                        failFlow("error waiting for PSBT flow", err)
×
2310
                        return
×
2311
                }
2312

2313
                // At this point, we'll see if there's an AuxFundingDesc we
2314
                // need to deliver so the funding process can continue
2315
                // properly.
2316
                auxFundingDesc, err := fn.MapOptionZ(
3✔
2317
                        f.cfg.AuxFundingController,
3✔
2318
                        func(c AuxFundingController) AuxFundingDescResult {
3✔
2319
                                return c.DescFromPendingChanID(
×
2320
                                        cid.tempChanID,
×
2321
                                        lnwallet.NewAuxChanState(
×
2322
                                                resCtx.reservation.ChanState(),
×
2323
                                        ),
×
2324
                                        resCtx.reservation.CommitmentKeyRings(),
×
2325
                                        true,
×
2326
                                )
×
2327
                        },
×
2328
                ).Unpack()
2329
                if err != nil {
3✔
2330
                        failFlow("error continuing PSBT flow", err)
×
2331
                        return
×
2332
                }
×
2333

2334
                // A non-nil error means we can continue the funding flow.
2335
                // Notify the wallet so it can prepare everything we need to
2336
                // continue.
2337
                //
2338
                // We'll also pass along the aux funding controller as well,
2339
                // which may be used to help process the finalized PSBT.
2340
                err = resCtx.reservation.ProcessPsbt(auxFundingDesc)
3✔
2341
                if err != nil {
3✔
2342
                        failFlow("error continuing PSBT flow", err)
×
2343
                        return
×
2344
                }
×
2345

2346
                // We are now ready to continue the funding flow.
2347
                f.continueFundingAccept(resCtx, cid)
3✔
2348

2349
        // Handle a server shutdown as well because the reservation won't
2350
        // survive a restart as it's in memory only.
2351
        case <-f.quit:
×
2352
                log.Errorf("Unable to handle funding accept message "+
×
2353
                        "for peer_key=%x, pending_chan_id=%x: funding manager "+
×
2354
                        "shutting down", peerKey.SerializeCompressed(),
×
2355
                        cid.tempChanID)
×
2356
                return
×
2357
        }
2358
}
2359

2360
// continueFundingAccept continues the channel funding flow once our
2361
// contribution is finalized, the channel output is known and the funding
2362
// transaction is signed.
2363
func (f *Manager) continueFundingAccept(resCtx *reservationWithCtx,
2364
        cid *chanIdentifier) {
32✔
2365

32✔
2366
        // Now that we have their contribution, we can extract, then send over
32✔
2367
        // both the funding out point and our signature for their version of
32✔
2368
        // the commitment transaction to the remote peer.
32✔
2369
        outPoint := resCtx.reservation.FundingOutpoint()
32✔
2370
        _, sig := resCtx.reservation.OurSignatures()
32✔
2371

32✔
2372
        // A new channel has almost finished the funding process. In order to
32✔
2373
        // properly synchronize with the writeHandler goroutine, we add a new
32✔
2374
        // channel to the barriers map which will be closed once the channel is
32✔
2375
        // fully open.
32✔
2376
        channelID := lnwire.NewChanIDFromOutPoint(*outPoint)
32✔
2377
        log.Debugf("Creating chan barrier for ChanID(%v)", channelID)
32✔
2378

32✔
2379
        // The next message that advances the funding flow will reference the
32✔
2380
        // channel via its permanent channel ID, so we'll set up this mapping
32✔
2381
        // so we can retrieve the reservation context once we get the
32✔
2382
        // FundingSigned message.
32✔
2383
        f.resMtx.Lock()
32✔
2384
        f.signedReservations[channelID] = cid.tempChanID
32✔
2385
        f.resMtx.Unlock()
32✔
2386

32✔
2387
        log.Infof("Generated ChannelPoint(%v) for pending_id(%x)", outPoint,
32✔
2388
                cid.tempChanID[:])
32✔
2389

32✔
2390
        // Before sending FundingCreated sent, we notify Brontide to keep track
32✔
2391
        // of this pending open channel.
32✔
2392
        err := resCtx.peer.AddPendingChannel(channelID, f.quit)
32✔
2393
        if err != nil {
32✔
2394
                pubKey := resCtx.peer.IdentityKey().SerializeCompressed()
×
2395
                log.Errorf("Unable to add pending channel %v with peer %x: %v",
×
2396
                        channelID, pubKey, err)
×
2397
        }
×
2398

2399
        // Once Brontide is aware of this channel, we need to set it in
2400
        // chanIdentifier so this channel will be removed from Brontide if the
2401
        // funding flow fails.
2402
        cid.setChanID(channelID)
32✔
2403

32✔
2404
        // Send the FundingCreated msg.
32✔
2405
        fundingCreated := &lnwire.FundingCreated{
32✔
2406
                PendingChannelID: cid.tempChanID,
32✔
2407
                FundingPoint:     *outPoint,
32✔
2408
        }
32✔
2409

32✔
2410
        // If this is a taproot channel, then we'll need to populate the musig2
32✔
2411
        // partial sig field instead of the regular commit sig field.
32✔
2412
        if resCtx.reservation.IsTaproot() {
37✔
2413
                partialSig, ok := sig.(*lnwallet.MusigPartialSig)
5✔
2414
                if !ok {
5✔
2415
                        err := fmt.Errorf("expected musig partial sig, got %T",
×
2416
                                sig)
×
2417
                        log.Error(err)
×
2418
                        f.failFundingFlow(resCtx.peer, cid, err)
×
2419

×
2420
                        return
×
2421
                }
×
2422

2423
                fundingCreated.PartialSig = lnwire.MaybePartialSigWithNonce(
5✔
2424
                        partialSig.ToWireSig(),
5✔
2425
                )
5✔
2426
        } else {
30✔
2427
                fundingCreated.CommitSig, err = lnwire.NewSigFromSignature(sig)
30✔
2428
                if err != nil {
30✔
2429
                        log.Errorf("Unable to parse signature: %v", err)
×
2430
                        f.failFundingFlow(resCtx.peer, cid, err)
×
2431
                        return
×
2432
                }
×
2433
        }
2434

2435
        resCtx.reservation.SetState(lnwallet.SentFundingCreated)
32✔
2436

32✔
2437
        if err := resCtx.peer.SendMessage(true, fundingCreated); err != nil {
32✔
2438
                log.Errorf("Unable to send funding complete message: %v", err)
×
2439
                f.failFundingFlow(resCtx.peer, cid, err)
×
2440
                return
×
2441
        }
×
2442
}
2443

2444
// fundeeProcessFundingCreated progresses the funding workflow when the daemon
2445
// is on the responding side of a single funder workflow. Once this message has
2446
// been processed, a signature is sent to the remote peer allowing it to
2447
// broadcast the funding transaction, progressing the workflow into the final
2448
// stage.
2449
//
2450
//nolint:funlen
2451
func (f *Manager) fundeeProcessFundingCreated(peer lnpeer.Peer,
2452
        msg *lnwire.FundingCreated) {
30✔
2453

30✔
2454
        peerKey := peer.IdentityKey()
30✔
2455
        pendingChanID := msg.PendingChannelID
30✔
2456

30✔
2457
        resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
30✔
2458
        if err != nil {
30✔
2459
                log.Warnf("can't find reservation (peer_id:%v, chan_id:%x)",
×
2460
                        peerKey, pendingChanID[:])
×
2461
                return
×
2462
        }
×
2463

2464
        // The channel initiator has responded with the funding outpoint of the
2465
        // final funding transaction, as well as a signature for our version of
2466
        // the commitment transaction. So at this point, we can validate the
2467
        // initiator's commitment transaction, then send our own if it's valid.
2468
        fundingOut := msg.FundingPoint
30✔
2469
        log.Infof("completing pending_id(%x) with ChannelPoint(%v)",
30✔
2470
                pendingChanID[:], fundingOut)
30✔
2471

30✔
2472
        if resCtx.reservation.State() != lnwallet.SentAcceptChannel {
30✔
2473
                return
×
2474
        }
×
2475

2476
        // Create the channel identifier without setting the active channel ID.
2477
        cid := newChanIdentifier(pendingChanID)
30✔
2478

30✔
2479
        // For taproot channels, the commit signature is actually the partial
30✔
2480
        // signature. Otherwise, we can convert the ECDSA commit signature into
30✔
2481
        // our internal input.Signature type.
30✔
2482
        var commitSig input.Signature
30✔
2483
        if resCtx.reservation.IsTaproot() {
35✔
2484
                partialSig, err := msg.PartialSig.UnwrapOrErrV(errNoPartialSig)
5✔
2485
                if err != nil {
5✔
2486
                        f.failFundingFlow(peer, cid, err)
×
2487

×
2488
                        return
×
2489
                }
×
2490

2491
                commitSig = new(lnwallet.MusigPartialSig).FromWireSig(
5✔
2492
                        &partialSig,
5✔
2493
                )
5✔
2494
        } else {
28✔
2495
                commitSig, err = msg.CommitSig.ToSignature()
28✔
2496
                if err != nil {
28✔
2497
                        log.Errorf("unable to parse signature: %v", err)
×
2498
                        f.failFundingFlow(peer, cid, err)
×
2499
                        return
×
2500
                }
×
2501
        }
2502

2503
        // At this point, we'll see if there's an AuxFundingDesc we need to
2504
        // deliver so the funding process can continue properly.
2505
        auxFundingDesc, err := fn.MapOptionZ(
30✔
2506
                f.cfg.AuxFundingController,
30✔
2507
                func(c AuxFundingController) AuxFundingDescResult {
30✔
2508
                        return c.DescFromPendingChanID(
×
2509
                                cid.tempChanID, lnwallet.NewAuxChanState(
×
2510
                                        resCtx.reservation.ChanState(),
×
2511
                                ), resCtx.reservation.CommitmentKeyRings(),
×
2512
                                true,
×
2513
                        )
×
2514
                },
×
2515
        ).Unpack()
2516
        if err != nil {
30✔
2517
                log.Errorf("error continuing PSBT flow: %v", err)
×
2518
                f.failFundingFlow(peer, cid, err)
×
2519
                return
×
2520
        }
×
2521

2522
        // With all the necessary data available, attempt to advance the
2523
        // funding workflow to the next stage. If this succeeds then the
2524
        // funding transaction will broadcast after our next message.
2525
        // CompleteReservationSingle will also mark the channel as 'IsPending'
2526
        // in the database.
2527
        //
2528
        // We'll also directly pass in the AuxFunding controller as well,
2529
        // which may be used by the reservation system to finalize funding our
2530
        // side.
2531
        completeChan, err := resCtx.reservation.CompleteReservationSingle(
30✔
2532
                &fundingOut, commitSig, auxFundingDesc,
30✔
2533
        )
30✔
2534
        if err != nil {
30✔
2535
                log.Errorf("unable to complete single reservation: %v", err)
×
2536
                f.failFundingFlow(peer, cid, err)
×
2537
                return
×
2538
        }
×
2539

2540
        // Get forwarding policy before deleting the reservation context.
2541
        forwardingPolicy := resCtx.forwardingPolicy
30✔
2542

30✔
2543
        // The channel is marked IsPending in the database, and can be removed
30✔
2544
        // from the set of active reservations.
30✔
2545
        f.deleteReservationCtx(peerKey, cid.tempChanID)
30✔
2546

30✔
2547
        // If something goes wrong before the funding transaction is confirmed,
30✔
2548
        // we use this convenience method to delete the pending OpenChannel
30✔
2549
        // from the database.
30✔
2550
        deleteFromDatabase := func() {
30✔
2551
                localBalance := completeChan.LocalCommitment.LocalBalance.ToSatoshis()
×
2552
                closeInfo := &channeldb.ChannelCloseSummary{
×
2553
                        ChanPoint:               completeChan.FundingOutpoint,
×
2554
                        ChainHash:               completeChan.ChainHash,
×
2555
                        RemotePub:               completeChan.IdentityPub,
×
2556
                        CloseType:               channeldb.FundingCanceled,
×
2557
                        Capacity:                completeChan.Capacity,
×
2558
                        SettledBalance:          localBalance,
×
2559
                        RemoteCurrentRevocation: completeChan.RemoteCurrentRevocation,
×
2560
                        RemoteNextRevocation:    completeChan.RemoteNextRevocation,
×
2561
                        LocalChanConfig:         completeChan.LocalChanCfg,
×
2562
                }
×
2563

×
2564
                // Close the channel with us as the initiator because we are
×
2565
                // deciding to exit the funding flow due to an internal error.
×
2566
                if err := completeChan.CloseChannel(
×
2567
                        closeInfo, channeldb.ChanStatusLocalCloseInitiator,
×
2568
                ); err != nil {
×
2569
                        log.Errorf("Failed closing channel %v: %v",
×
2570
                                completeChan.FundingOutpoint, err)
×
2571
                }
×
2572
        }
2573

2574
        // A new channel has almost finished the funding process. In order to
2575
        // properly synchronize with the writeHandler goroutine, we add a new
2576
        // channel to the barriers map which will be closed once the channel is
2577
        // fully open.
2578
        channelID := lnwire.NewChanIDFromOutPoint(fundingOut)
30✔
2579
        log.Debugf("Creating chan barrier for ChanID(%v)", channelID)
30✔
2580

30✔
2581
        fundingSigned := &lnwire.FundingSigned{}
30✔
2582

30✔
2583
        // For taproot channels, we'll need to send over a partial signature
30✔
2584
        // that includes the nonce along side the signature.
30✔
2585
        _, sig := resCtx.reservation.OurSignatures()
30✔
2586
        if resCtx.reservation.IsTaproot() {
35✔
2587
                partialSig, ok := sig.(*lnwallet.MusigPartialSig)
5✔
2588
                if !ok {
5✔
2589
                        err := fmt.Errorf("expected musig partial sig, got %T",
×
2590
                                sig)
×
2591
                        log.Error(err)
×
2592
                        f.failFundingFlow(resCtx.peer, cid, err)
×
2593
                        deleteFromDatabase()
×
2594

×
2595
                        return
×
2596
                }
×
2597

2598
                fundingSigned.PartialSig = lnwire.MaybePartialSigWithNonce(
5✔
2599
                        partialSig.ToWireSig(),
5✔
2600
                )
5✔
2601
        } else {
28✔
2602
                fundingSigned.CommitSig, err = lnwire.NewSigFromSignature(sig)
28✔
2603
                if err != nil {
28✔
2604
                        log.Errorf("unable to parse signature: %v", err)
×
2605
                        f.failFundingFlow(peer, cid, err)
×
2606
                        deleteFromDatabase()
×
2607

×
2608
                        return
×
2609
                }
×
2610
        }
2611

2612
        // Before sending FundingSigned, we notify Brontide first to keep track
2613
        // of this pending open channel.
2614
        if err := peer.AddPendingChannel(channelID, f.quit); err != nil {
30✔
2615
                pubKey := peer.IdentityKey().SerializeCompressed()
×
2616
                log.Errorf("Unable to add pending channel %v with peer %x: %v",
×
2617
                        cid.chanID, pubKey, err)
×
2618
        }
×
2619

2620
        // Once Brontide is aware of this channel, we need to set it in
2621
        // chanIdentifier so this channel will be removed from Brontide if the
2622
        // funding flow fails.
2623
        cid.setChanID(channelID)
30✔
2624

30✔
2625
        fundingSigned.ChanID = cid.chanID
30✔
2626

30✔
2627
        log.Infof("sending FundingSigned for pending_id(%x) over "+
30✔
2628
                "ChannelPoint(%v)", pendingChanID[:], fundingOut)
30✔
2629

30✔
2630
        // With their signature for our version of the commitment transaction
30✔
2631
        // verified, we can now send over our signature to the remote peer.
30✔
2632
        if err := peer.SendMessage(true, fundingSigned); err != nil {
30✔
2633
                log.Errorf("unable to send FundingSigned message: %v", err)
×
2634
                f.failFundingFlow(peer, cid, err)
×
2635
                deleteFromDatabase()
×
2636
                return
×
2637
        }
×
2638

2639
        // With a permanent channel id established we can save the respective
2640
        // forwarding policy in the database. In the channel announcement phase
2641
        // this forwarding policy is retrieved and applied.
2642
        err = f.saveInitialForwardingPolicy(cid.chanID, &forwardingPolicy)
30✔
2643
        if err != nil {
30✔
2644
                log.Errorf("Unable to store the forwarding policy: %v", err)
×
2645
        }
×
2646

2647
        // Now that we've sent over our final signature for this channel, we'll
2648
        // send it to the ChainArbitrator so it can watch for any on-chain
2649
        // actions during this final confirmation stage.
2650
        if err := f.cfg.WatchNewChannel(completeChan, peerKey); err != nil {
30✔
2651
                log.Errorf("Unable to send new ChannelPoint(%v) for "+
×
2652
                        "arbitration: %v", fundingOut, err)
×
2653
        }
×
2654

2655
        // Create an entry in the local discovery map so we can ensure that we
2656
        // process the channel confirmation fully before we receive a
2657
        // channel_ready message.
2658
        f.localDiscoverySignals.Store(cid.chanID, make(chan struct{}))
30✔
2659

30✔
2660
        // Inform the ChannelNotifier that the channel has entered
30✔
2661
        // pending open state.
30✔
2662
        f.cfg.NotifyPendingOpenChannelEvent(fundingOut, completeChan)
30✔
2663

30✔
2664
        // At this point we have sent our last funding message to the
30✔
2665
        // initiating peer before the funding transaction will be broadcast.
30✔
2666
        // With this last message, our job as the responder is now complete.
30✔
2667
        // We'll wait for the funding transaction to reach the specified number
30✔
2668
        // of confirmations, then start normal operations.
30✔
2669
        //
30✔
2670
        // When we get to this point we have sent the signComplete message to
30✔
2671
        // the channel funder, and BOLT#2 specifies that we MUST remember the
30✔
2672
        // channel for reconnection. The channel is already marked
30✔
2673
        // as pending in the database, so in case of a disconnect or restart,
30✔
2674
        // we will continue waiting for the confirmation the next time we start
30✔
2675
        // the funding manager. In case the funding transaction never appears
30✔
2676
        // on the blockchain, we must forget this channel. We therefore
30✔
2677
        // completely forget about this channel if we haven't seen the funding
30✔
2678
        // transaction in 288 blocks (~ 48 hrs), by canceling the reservation
30✔
2679
        // and canceling the wait for the funding confirmation.
30✔
2680
        f.wg.Add(1)
30✔
2681
        go f.advanceFundingState(completeChan, pendingChanID, nil)
30✔
2682
}
2683

2684
// funderProcessFundingSigned processes the final message received in a single
2685
// funder workflow. Once this message is processed, the funding transaction is
2686
// broadcast. Once the funding transaction reaches a sufficient number of
2687
// confirmations, a message is sent to the responding peer along with a compact
2688
// encoding of the location of the channel within the blockchain.
2689
func (f *Manager) funderProcessFundingSigned(peer lnpeer.Peer,
2690
        msg *lnwire.FundingSigned) {
30✔
2691

30✔
2692
        // As the funding signed message will reference the reservation by its
30✔
2693
        // permanent channel ID, we'll need to perform an intermediate look up
30✔
2694
        // before we can obtain the reservation.
30✔
2695
        f.resMtx.Lock()
30✔
2696
        pendingChanID, ok := f.signedReservations[msg.ChanID]
30✔
2697
        delete(f.signedReservations, msg.ChanID)
30✔
2698
        f.resMtx.Unlock()
30✔
2699

30✔
2700
        // Create the channel identifier and set the channel ID.
30✔
2701
        //
30✔
2702
        // NOTE: we may get an empty pending channel ID here if the key cannot
30✔
2703
        // be found, which means when we cancel the reservation context in
30✔
2704
        // `failFundingFlow`, we will get an error. In this case, we will send
30✔
2705
        // an error msg to our peer using the active channel ID.
30✔
2706
        //
30✔
2707
        // TODO(yy): refactor the funding flow to fix this case.
30✔
2708
        cid := newChanIdentifier(pendingChanID)
30✔
2709
        cid.setChanID(msg.ChanID)
30✔
2710

30✔
2711
        // If the pending channel ID is not found, fail the funding flow.
30✔
2712
        if !ok {
30✔
2713
                // NOTE: we directly overwrite the pending channel ID here for
×
2714
                // this rare case since we don't have a valid pending channel
×
2715
                // ID.
×
2716
                cid.tempChanID = msg.ChanID
×
2717

×
2718
                err := fmt.Errorf("unable to find signed reservation for "+
×
2719
                        "chan_id=%x", msg.ChanID)
×
2720
                log.Warnf(err.Error())
×
2721
                f.failFundingFlow(peer, cid, err)
×
2722
                return
×
2723
        }
×
2724

2725
        peerKey := peer.IdentityKey()
30✔
2726
        resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
30✔
2727
        if err != nil {
30✔
2728
                log.Warnf("Unable to find reservation (peer_id:%v, "+
×
2729
                        "chan_id:%x)", peerKey, pendingChanID[:])
×
2730
                // TODO: add ErrChanNotFound?
×
2731
                f.failFundingFlow(peer, cid, err)
×
2732
                return
×
2733
        }
×
2734

2735
        if resCtx.reservation.State() != lnwallet.SentFundingCreated {
30✔
2736
                err := fmt.Errorf("unable to find reservation for chan_id=%x",
×
2737
                        msg.ChanID)
×
2738
                f.failFundingFlow(peer, cid, err)
×
2739

×
2740
                return
×
2741
        }
×
2742

2743
        // Create an entry in the local discovery map so we can ensure that we
2744
        // process the channel confirmation fully before we receive a
2745
        // channel_ready message.
2746
        fundingPoint := resCtx.reservation.FundingOutpoint()
30✔
2747
        permChanID := lnwire.NewChanIDFromOutPoint(*fundingPoint)
30✔
2748
        f.localDiscoverySignals.Store(permChanID, make(chan struct{}))
30✔
2749

30✔
2750
        // We have to store the forwardingPolicy before the reservation context
30✔
2751
        // is deleted. The policy will then be read and applied in
30✔
2752
        // newChanAnnouncement.
30✔
2753
        err = f.saveInitialForwardingPolicy(
30✔
2754
                permChanID, &resCtx.forwardingPolicy,
30✔
2755
        )
30✔
2756
        if err != nil {
30✔
2757
                log.Errorf("Unable to store the forwarding policy: %v", err)
×
2758
        }
×
2759

2760
        // For taproot channels, the commit signature is actually the partial
2761
        // signature. Otherwise, we can convert the ECDSA commit signature into
2762
        // our internal input.Signature type.
2763
        var commitSig input.Signature
30✔
2764
        if resCtx.reservation.IsTaproot() {
35✔
2765
                partialSig, err := msg.PartialSig.UnwrapOrErrV(errNoPartialSig)
5✔
2766
                if err != nil {
5✔
2767
                        f.failFundingFlow(peer, cid, err)
×
2768

×
2769
                        return
×
2770
                }
×
2771

2772
                commitSig = new(lnwallet.MusigPartialSig).FromWireSig(
5✔
2773
                        &partialSig,
5✔
2774
                )
5✔
2775
        } else {
28✔
2776
                commitSig, err = msg.CommitSig.ToSignature()
28✔
2777
                if err != nil {
28✔
2778
                        log.Errorf("unable to parse signature: %v", err)
×
2779
                        f.failFundingFlow(peer, cid, err)
×
2780
                        return
×
2781
                }
×
2782
        }
2783

2784
        completeChan, err := resCtx.reservation.CompleteReservation(
30✔
2785
                nil, commitSig,
30✔
2786
        )
30✔
2787
        if err != nil {
30✔
2788
                log.Errorf("Unable to complete reservation sign "+
×
2789
                        "complete: %v", err)
×
2790
                f.failFundingFlow(peer, cid, err)
×
2791
                return
×
2792
        }
×
2793

2794
        // The channel is now marked IsPending in the database, and we can
2795
        // delete it from our set of active reservations.
2796
        f.deleteReservationCtx(peerKey, pendingChanID)
30✔
2797

30✔
2798
        // Broadcast the finalized funding transaction to the network, but only
30✔
2799
        // if we actually have the funding transaction.
30✔
2800
        if completeChan.ChanType.HasFundingTx() {
59✔
2801
                fundingTx := completeChan.FundingTxn
29✔
2802
                var fundingTxBuf bytes.Buffer
29✔
2803
                if err := fundingTx.Serialize(&fundingTxBuf); err != nil {
29✔
2804
                        log.Errorf("Unable to serialize funding "+
×
2805
                                "transaction %v: %v", fundingTx.TxHash(), err)
×
2806

×
2807
                        // Clear the buffer of any bytes that were written
×
2808
                        // before the serialization error to prevent logging an
×
2809
                        // incomplete transaction.
×
2810
                        fundingTxBuf.Reset()
×
2811
                }
×
2812

2813
                log.Infof("Broadcasting funding tx for ChannelPoint(%v): %x",
29✔
2814
                        completeChan.FundingOutpoint, fundingTxBuf.Bytes())
29✔
2815

29✔
2816
                // Set a nil short channel ID at this stage because we do not
29✔
2817
                // know it until our funding tx confirms.
29✔
2818
                label := labels.MakeLabel(
29✔
2819
                        labels.LabelTypeChannelOpen, nil,
29✔
2820
                )
29✔
2821

29✔
2822
                err = f.cfg.PublishTransaction(fundingTx, label)
29✔
2823
                if err != nil {
29✔
2824
                        log.Errorf("Unable to broadcast funding tx %x for "+
×
2825
                                "ChannelPoint(%v): %v", fundingTxBuf.Bytes(),
×
2826
                                completeChan.FundingOutpoint, err)
×
2827

×
2828
                        // We failed to broadcast the funding transaction, but
×
2829
                        // watch the channel regardless, in case the
×
2830
                        // transaction made it to the network. We will retry
×
2831
                        // broadcast at startup.
×
2832
                        //
×
2833
                        // TODO(halseth): retry more often? Handle with CPFP?
×
2834
                        // Just delete from the DB?
×
2835
                }
×
2836
        }
2837

2838
        // Before we proceed, if we have a funding hook that wants a
2839
        // notification that it's safe to broadcast the funding transaction,
2840
        // then we'll send that now.
2841
        err = fn.MapOptionZ(
30✔
2842
                f.cfg.AuxFundingController,
30✔
2843
                func(controller AuxFundingController) error {
30✔
2844
                        return controller.ChannelFinalized(cid.tempChanID)
×
2845
                },
×
2846
        )
2847
        if err != nil {
30✔
2848
                log.Errorf("Failed to inform aux funding controller about "+
×
2849
                        "ChannelPoint(%v) being finalized: %v", fundingPoint,
×
2850
                        err)
×
2851
        }
×
2852

2853
        // Now that we have a finalized reservation for this funding flow,
2854
        // we'll send the to be active channel to the ChainArbitrator so it can
2855
        // watch for any on-chain actions before the channel has fully
2856
        // confirmed.
2857
        if err := f.cfg.WatchNewChannel(completeChan, peerKey); err != nil {
30✔
2858
                log.Errorf("Unable to send new ChannelPoint(%v) for "+
×
2859
                        "arbitration: %v", fundingPoint, err)
×
2860
        }
×
2861

2862
        log.Infof("Finalizing pending_id(%x) over ChannelPoint(%v), "+
30✔
2863
                "waiting for channel open on-chain", pendingChanID[:],
30✔
2864
                fundingPoint)
30✔
2865

30✔
2866
        // Send an update to the upstream client that the negotiation process
30✔
2867
        // is over.
30✔
2868
        upd := &lnrpc.OpenStatusUpdate{
30✔
2869
                Update: &lnrpc.OpenStatusUpdate_ChanPending{
30✔
2870
                        ChanPending: &lnrpc.PendingUpdate{
30✔
2871
                                Txid:        fundingPoint.Hash[:],
30✔
2872
                                OutputIndex: fundingPoint.Index,
30✔
2873
                        },
30✔
2874
                },
30✔
2875
                PendingChanId: pendingChanID[:],
30✔
2876
        }
30✔
2877

30✔
2878
        select {
30✔
2879
        case resCtx.updates <- upd:
30✔
2880
                // Inform the ChannelNotifier that the channel has entered
30✔
2881
                // pending open state.
30✔
2882
                f.cfg.NotifyPendingOpenChannelEvent(*fundingPoint, completeChan)
30✔
2883
        case <-f.quit:
×
2884
                return
×
2885
        }
2886

2887
        // At this point we have broadcast the funding transaction and done all
2888
        // necessary processing.
2889
        f.wg.Add(1)
30✔
2890
        go f.advanceFundingState(completeChan, pendingChanID, resCtx.updates)
30✔
2891
}
2892

2893
// confirmedChannel wraps a confirmed funding transaction, as well as the short
2894
// channel ID which identifies that channel into a single struct. We'll use
2895
// this to pass around the final state of a channel after it has been
2896
// confirmed.
2897
type confirmedChannel struct {
2898
        // shortChanID expresses where in the block the funding transaction was
2899
        // located.
2900
        shortChanID lnwire.ShortChannelID
2901

2902
        // fundingTx is the funding transaction that created the channel.
2903
        fundingTx *wire.MsgTx
2904
}
2905

2906
// fundingTimeout is called when callers of waitForFundingWithTimeout receive
2907
// an ErrConfirmationTimeout. It is used to clean-up channel state and mark the
2908
// channel as closed. The error is only returned for the responder of the
2909
// channel flow.
2910
func (f *Manager) fundingTimeout(c *channeldb.OpenChannel,
2911
        pendingID PendingChanID) error {
5✔
2912

5✔
2913
        // We'll get a timeout if the number of blocks mined since the channel
5✔
2914
        // was initiated reaches MaxWaitNumBlocksFundingConf and we are not the
5✔
2915
        // channel initiator.
5✔
2916
        localBalance := c.LocalCommitment.LocalBalance.ToSatoshis()
5✔
2917
        closeInfo := &channeldb.ChannelCloseSummary{
5✔
2918
                ChainHash:               c.ChainHash,
5✔
2919
                ChanPoint:               c.FundingOutpoint,
5✔
2920
                RemotePub:               c.IdentityPub,
5✔
2921
                Capacity:                c.Capacity,
5✔
2922
                SettledBalance:          localBalance,
5✔
2923
                CloseType:               channeldb.FundingCanceled,
5✔
2924
                RemoteCurrentRevocation: c.RemoteCurrentRevocation,
5✔
2925
                RemoteNextRevocation:    c.RemoteNextRevocation,
5✔
2926
                LocalChanConfig:         c.LocalChanCfg,
5✔
2927
        }
5✔
2928

5✔
2929
        // Close the channel with us as the initiator because we are timing the
5✔
2930
        // channel out.
5✔
2931
        if err := c.CloseChannel(
5✔
2932
                closeInfo, channeldb.ChanStatusLocalCloseInitiator,
5✔
2933
        ); err != nil {
5✔
2934
                return fmt.Errorf("failed closing channel %v: %w",
×
2935
                        c.FundingOutpoint, err)
×
2936
        }
×
2937

2938
        timeoutErr := fmt.Errorf("timeout waiting for funding tx (%v) to "+
5✔
2939
                "confirm", c.FundingOutpoint)
5✔
2940

5✔
2941
        // When the peer comes online, we'll notify it that we are now
5✔
2942
        // considering the channel flow canceled.
5✔
2943
        f.wg.Add(1)
5✔
2944
        go func() {
10✔
2945
                defer f.wg.Done()
5✔
2946

5✔
2947
                peer, err := f.waitForPeerOnline(c.IdentityPub)
5✔
2948
                switch err {
5✔
2949
                // We're already shutting down, so we can just return.
2950
                case ErrFundingManagerShuttingDown:
×
2951
                        return
×
2952

2953
                // nil error means we continue on.
2954
                case nil:
5✔
2955

2956
                // For unexpected errors, we print the error and still try to
2957
                // fail the funding flow.
2958
                default:
×
2959
                        log.Errorf("Unexpected error while waiting for peer "+
×
2960
                                "to come online: %v", err)
×
2961
                }
2962

2963
                // Create channel identifier and set the channel ID.
2964
                cid := newChanIdentifier(pendingID)
5✔
2965
                cid.setChanID(lnwire.NewChanIDFromOutPoint(c.FundingOutpoint))
5✔
2966

5✔
2967
                // TODO(halseth): should this send be made
5✔
2968
                // reliable?
5✔
2969

5✔
2970
                // The reservation won't exist at this point, but we'll send an
5✔
2971
                // Error message over anyways with ChanID set to pendingID.
5✔
2972
                f.failFundingFlow(peer, cid, timeoutErr)
5✔
2973
        }()
2974

2975
        return timeoutErr
5✔
2976
}
2977

2978
// waitForFundingWithTimeout is a wrapper around waitForFundingConfirmation and
2979
// waitForTimeout that will return ErrConfirmationTimeout if we are not the
2980
// channel initiator and the MaxWaitNumBlocksFundingConf has passed from the
2981
// funding broadcast height. In case of confirmation, the short channel ID of
2982
// the channel and the funding transaction will be returned.
2983
func (f *Manager) waitForFundingWithTimeout(
2984
        ch *channeldb.OpenChannel) (*confirmedChannel, error) {
60✔
2985

60✔
2986
        confChan := make(chan *confirmedChannel)
60✔
2987
        timeoutChan := make(chan error, 1)
60✔
2988
        cancelChan := make(chan struct{})
60✔
2989

60✔
2990
        f.wg.Add(1)
60✔
2991
        go f.waitForFundingConfirmation(ch, cancelChan, confChan)
60✔
2992

60✔
2993
        // If we are not the initiator, we have no money at stake and will
60✔
2994
        // timeout waiting for the funding transaction to confirm after a
60✔
2995
        // while.
60✔
2996
        if !ch.IsInitiator && !ch.IsZeroConf() {
88✔
2997
                f.wg.Add(1)
28✔
2998
                go f.waitForTimeout(ch, cancelChan, timeoutChan)
28✔
2999
        }
28✔
3000
        defer close(cancelChan)
60✔
3001

60✔
3002
        select {
60✔
3003
        case err := <-timeoutChan:
5✔
3004
                if err != nil {
5✔
3005
                        return nil, err
×
3006
                }
×
3007
                return nil, ErrConfirmationTimeout
5✔
3008

3009
        case <-f.quit:
24✔
3010
                // The fundingManager is shutting down, and will resume wait on
24✔
3011
                // startup.
24✔
3012
                return nil, ErrFundingManagerShuttingDown
24✔
3013

3014
        case confirmedChannel, ok := <-confChan:
37✔
3015
                if !ok {
37✔
3016
                        return nil, fmt.Errorf("waiting for funding" +
×
3017
                                "confirmation failed")
×
3018
                }
×
3019
                return confirmedChannel, nil
37✔
3020
        }
3021
}
3022

3023
// makeFundingScript re-creates the funding script for the funding transaction
3024
// of the target channel.
3025
func makeFundingScript(channel *channeldb.OpenChannel) ([]byte, error) {
80✔
3026
        localKey := channel.LocalChanCfg.MultiSigKey.PubKey
80✔
3027
        remoteKey := channel.RemoteChanCfg.MultiSigKey.PubKey
80✔
3028

80✔
3029
        if channel.ChanType.IsTaproot() {
88✔
3030
                pkScript, _, err := input.GenTaprootFundingScript(
8✔
3031
                        localKey, remoteKey, int64(channel.Capacity),
8✔
3032
                        channel.TapscriptRoot,
8✔
3033
                )
8✔
3034
                if err != nil {
8✔
3035
                        return nil, err
×
3036
                }
×
3037

3038
                return pkScript, nil
8✔
3039
        }
3040

3041
        multiSigScript, err := input.GenMultiSigScript(
75✔
3042
                localKey.SerializeCompressed(),
75✔
3043
                remoteKey.SerializeCompressed(),
75✔
3044
        )
75✔
3045
        if err != nil {
75✔
3046
                return nil, err
×
3047
        }
×
3048

3049
        return input.WitnessScriptHash(multiSigScript)
75✔
3050
}
3051

3052
// waitForFundingConfirmation handles the final stages of the channel funding
3053
// process once the funding transaction has been broadcast. The primary
3054
// function of waitForFundingConfirmation is to wait for blockchain
3055
// confirmation, and then to notify the other systems that must be notified
3056
// when a channel has become active for lightning transactions.
3057
// The wait can be canceled by closing the cancelChan. In case of success,
3058
// a *lnwire.ShortChannelID will be passed to confChan.
3059
//
3060
// NOTE: This MUST be run as a goroutine.
3061
func (f *Manager) waitForFundingConfirmation(
3062
        completeChan *channeldb.OpenChannel, cancelChan <-chan struct{},
3063
        confChan chan<- *confirmedChannel) {
60✔
3064

60✔
3065
        defer f.wg.Done()
60✔
3066
        defer close(confChan)
60✔
3067

60✔
3068
        // Register with the ChainNotifier for a notification once the funding
60✔
3069
        // transaction reaches `numConfs` confirmations.
60✔
3070
        txid := completeChan.FundingOutpoint.Hash
60✔
3071
        fundingScript, err := makeFundingScript(completeChan)
60✔
3072
        if err != nil {
60✔
3073
                log.Errorf("unable to create funding script for "+
×
3074
                        "ChannelPoint(%v): %v", completeChan.FundingOutpoint,
×
3075
                        err)
×
3076
                return
×
3077
        }
×
3078
        numConfs := uint32(completeChan.NumConfsRequired)
60✔
3079

60✔
3080
        // If the underlying channel is a zero-conf channel, we'll set numConfs
60✔
3081
        // to 6, since it will be zero here.
60✔
3082
        if completeChan.IsZeroConf() {
69✔
3083
                numConfs = 6
9✔
3084
        }
9✔
3085

3086
        confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(
60✔
3087
                &txid, fundingScript, numConfs,
60✔
3088
                completeChan.BroadcastHeight(),
60✔
3089
        )
60✔
3090
        if err != nil {
60✔
3091
                log.Errorf("Unable to register for confirmation of "+
×
3092
                        "ChannelPoint(%v): %v", completeChan.FundingOutpoint,
×
3093
                        err)
×
3094
                return
×
3095
        }
×
3096

3097
        log.Infof("Waiting for funding tx (%v) to reach %v confirmations",
60✔
3098
                txid, numConfs)
60✔
3099

60✔
3100
        var confDetails *chainntnfs.TxConfirmation
60✔
3101
        var ok bool
60✔
3102

60✔
3103
        // Wait until the specified number of confirmations has been reached,
60✔
3104
        // we get a cancel signal, or the wallet signals a shutdown.
60✔
3105
        select {
60✔
3106
        case confDetails, ok = <-confNtfn.Confirmed:
37✔
3107
                // fallthrough
3108

3109
        case <-cancelChan:
7✔
3110
                log.Warnf("canceled waiting for funding confirmation, "+
7✔
3111
                        "stopping funding flow for ChannelPoint(%v)",
7✔
3112
                        completeChan.FundingOutpoint)
7✔
3113
                return
7✔
3114

3115
        case <-f.quit:
22✔
3116
                log.Warnf("fundingManager shutting down, stopping funding "+
22✔
3117
                        "flow for ChannelPoint(%v)",
22✔
3118
                        completeChan.FundingOutpoint)
22✔
3119
                return
22✔
3120
        }
3121

3122
        if !ok {
37✔
3123
                log.Warnf("ChainNotifier shutting down, cannot complete "+
×
3124
                        "funding flow for ChannelPoint(%v)",
×
3125
                        completeChan.FundingOutpoint)
×
3126
                return
×
3127
        }
×
3128

3129
        fundingPoint := completeChan.FundingOutpoint
37✔
3130
        log.Infof("ChannelPoint(%v) is now active: ChannelID(%v)",
37✔
3131
                fundingPoint, lnwire.NewChanIDFromOutPoint(fundingPoint))
37✔
3132

37✔
3133
        // With the block height and the transaction index known, we can
37✔
3134
        // construct the compact chanID which is used on the network to unique
37✔
3135
        // identify channels.
37✔
3136
        shortChanID := lnwire.ShortChannelID{
37✔
3137
                BlockHeight: confDetails.BlockHeight,
37✔
3138
                TxIndex:     confDetails.TxIndex,
37✔
3139
                TxPosition:  uint16(fundingPoint.Index),
37✔
3140
        }
37✔
3141

37✔
3142
        select {
37✔
3143
        case confChan <- &confirmedChannel{
3144
                shortChanID: shortChanID,
3145
                fundingTx:   confDetails.Tx,
3146
        }:
37✔
3147
        case <-f.quit:
×
3148
                return
×
3149
        }
3150
}
3151

3152
// waitForTimeout will close the timeout channel if MaxWaitNumBlocksFundingConf
3153
// has passed from the broadcast height of the given channel. In case of error,
3154
// the error is sent on timeoutChan. The wait can be canceled by closing the
3155
// cancelChan.
3156
//
3157
// NOTE: timeoutChan MUST be buffered.
3158
// NOTE: This MUST be run as a goroutine.
3159
func (f *Manager) waitForTimeout(completeChan *channeldb.OpenChannel,
3160
        cancelChan <-chan struct{}, timeoutChan chan<- error) {
28✔
3161

28✔
3162
        defer f.wg.Done()
28✔
3163

28✔
3164
        epochClient, err := f.cfg.Notifier.RegisterBlockEpochNtfn(nil)
28✔
3165
        if err != nil {
28✔
3166
                timeoutChan <- fmt.Errorf("unable to register for epoch "+
×
3167
                        "notification: %v", err)
×
3168
                return
×
3169
        }
×
3170

3171
        defer epochClient.Cancel()
28✔
3172

28✔
3173
        // For the waitBlocksForFundingConf different values are set
28✔
3174
        // in case we are in a dev environment so enhance test
28✔
3175
        // capabilities.
28✔
3176
        waitBlocksForFundingConf := MaxWaitNumBlocksFundingConf
28✔
3177

28✔
3178
        // Get the waitBlocksForFundingConf. If we are not in
28✔
3179
        // development mode, this would be nil.
28✔
3180
        if lncfg.IsDevBuild() {
31✔
3181
                waitBlocksForFundingConf = f.cfg.Dev.MaxWaitNumBlocksFundingConf
3✔
3182
        }
3✔
3183

3184
        // On block maxHeight we will cancel the funding confirmation wait.
3185
        broadcastHeight := completeChan.BroadcastHeight()
28✔
3186
        maxHeight := broadcastHeight + uint32(waitBlocksForFundingConf)
28✔
3187
        for {
58✔
3188
                select {
30✔
3189
                case epoch, ok := <-epochClient.Epochs:
7✔
3190
                        if !ok {
7✔
3191
                                timeoutChan <- fmt.Errorf("epoch client " +
×
3192
                                        "shutting down")
×
3193
                                return
×
3194
                        }
×
3195

3196
                        // Close the timeout channel and exit if the block is
3197
                        // above the max height.
3198
                        if uint32(epoch.Height) >= maxHeight {
12✔
3199
                                log.Warnf("Waited for %v blocks without "+
5✔
3200
                                        "seeing funding transaction confirmed,"+
5✔
3201
                                        " cancelling.",
5✔
3202
                                        waitBlocksForFundingConf)
5✔
3203

5✔
3204
                                // Notify the caller of the timeout.
5✔
3205
                                close(timeoutChan)
5✔
3206
                                return
5✔
3207
                        }
5✔
3208

3209
                        // TODO: If we are the channel initiator implement
3210
                        // a method for recovering the funds from the funding
3211
                        // transaction
3212

3213
                case <-cancelChan:
18✔
3214
                        return
18✔
3215

3216
                case <-f.quit:
11✔
3217
                        // The fundingManager is shutting down, will resume
11✔
3218
                        // waiting for the funding transaction on startup.
11✔
3219
                        return
11✔
3220
                }
3221
        }
3222
}
3223

3224
// makeLabelForTx updates the label for the confirmed funding transaction. If
3225
// we opened the channel, and lnd's wallet published our funding tx (which is
3226
// not the case for some channels) then we update our transaction label with
3227
// our short channel ID, which is known now that our funding transaction has
3228
// confirmed. We do not label transactions we did not publish, because our
3229
// wallet has no knowledge of them.
3230
func (f *Manager) makeLabelForTx(c *channeldb.OpenChannel) {
37✔
3231
        if c.IsInitiator && c.ChanType.HasFundingTx() {
56✔
3232
                shortChanID := c.ShortChanID()
19✔
3233

19✔
3234
                // For zero-conf channels, we'll use the actually-confirmed
19✔
3235
                // short channel id.
19✔
3236
                if c.IsZeroConf() {
24✔
3237
                        shortChanID = c.ZeroConfRealScid()
5✔
3238
                }
5✔
3239

3240
                label := labels.MakeLabel(
19✔
3241
                        labels.LabelTypeChannelOpen, &shortChanID,
19✔
3242
                )
19✔
3243

19✔
3244
                err := f.cfg.UpdateLabel(c.FundingOutpoint.Hash, label)
19✔
3245
                if err != nil {
19✔
3246
                        log.Errorf("unable to update label: %v", err)
×
3247
                }
×
3248
        }
3249
}
3250

3251
// handleFundingConfirmation marks a channel as open in the database, and set
3252
// the channelOpeningState markedOpen. In addition it will report the now
3253
// decided short channel ID to the switch, and close the local discovery signal
3254
// for this channel.
3255
func (f *Manager) handleFundingConfirmation(
3256
        completeChan *channeldb.OpenChannel,
3257
        confChannel *confirmedChannel) error {
33✔
3258

33✔
3259
        fundingPoint := completeChan.FundingOutpoint
33✔
3260
        chanID := lnwire.NewChanIDFromOutPoint(fundingPoint)
33✔
3261

33✔
3262
        // TODO(roasbeef): ideally persistent state update for chan above
33✔
3263
        // should be abstracted
33✔
3264

33✔
3265
        // Now that that the channel has been fully confirmed, we'll request
33✔
3266
        // that the wallet fully verify this channel to ensure that it can be
33✔
3267
        // used.
33✔
3268
        err := f.cfg.Wallet.ValidateChannel(completeChan, confChannel.fundingTx)
33✔
3269
        if err != nil {
33✔
3270
                // TODO(roasbeef): delete chan state?
×
3271
                return fmt.Errorf("unable to validate channel: %w", err)
×
3272
        }
×
3273

3274
        // Now that the channel has been validated, we'll persist an alias for
3275
        // this channel if the option-scid-alias feature-bit was negotiated.
3276
        if completeChan.NegotiatedAliasFeature() {
38✔
3277
                aliasScid, err := f.cfg.AliasManager.RequestAlias()
5✔
3278
                if err != nil {
5✔
3279
                        return fmt.Errorf("unable to request alias: %w", err)
×
3280
                }
×
3281

3282
                err = f.cfg.AliasManager.AddLocalAlias(
5✔
3283
                        aliasScid, confChannel.shortChanID, true, false,
5✔
3284
                )
5✔
3285
                if err != nil {
5✔
3286
                        return fmt.Errorf("unable to request alias: %w", err)
×
3287
                }
×
3288
        }
3289

3290
        // The funding transaction now being confirmed, we add this channel to
3291
        // the fundingManager's internal persistent state machine that we use
3292
        // to track the remaining process of the channel opening. This is
3293
        // useful to resume the opening process in case of restarts. We set the
3294
        // opening state before we mark the channel opened in the database,
3295
        // such that we can receover from one of the db writes failing.
3296
        err = f.saveChannelOpeningState(
33✔
3297
                &fundingPoint, markedOpen, &confChannel.shortChanID,
33✔
3298
        )
33✔
3299
        if err != nil {
33✔
3300
                return fmt.Errorf("error setting channel state to "+
×
3301
                        "markedOpen: %v", err)
×
3302
        }
×
3303

3304
        // Now that the channel has been fully confirmed and we successfully
3305
        // saved the opening state, we'll mark it as open within the database.
3306
        err = completeChan.MarkAsOpen(confChannel.shortChanID)
33✔
3307
        if err != nil {
33✔
3308
                return fmt.Errorf("error setting channel pending flag to "+
×
3309
                        "false:        %v", err)
×
3310
        }
×
3311

3312
        // Update the confirmed funding transaction label.
3313
        f.makeLabelForTx(completeChan)
33✔
3314

33✔
3315
        // Inform the ChannelNotifier that the channel has transitioned from
33✔
3316
        // pending open to open.
33✔
3317
        f.cfg.NotifyOpenChannelEvent(completeChan.FundingOutpoint)
33✔
3318

33✔
3319
        // Close the discoverySignal channel, indicating to a separate
33✔
3320
        // goroutine that the channel now is marked as open in the database
33✔
3321
        // and that it is acceptable to process channel_ready messages
33✔
3322
        // from the peer.
33✔
3323
        if discoverySignal, ok := f.localDiscoverySignals.Load(chanID); ok {
66✔
3324
                close(discoverySignal)
33✔
3325
        }
33✔
3326

3327
        return nil
33✔
3328
}
3329

3330
// sendChannelReady creates and sends the channelReady message.
3331
// This should be called after the funding transaction has been confirmed,
3332
// and the channelState is 'markedOpen'.
3333
func (f *Manager) sendChannelReady(completeChan *channeldb.OpenChannel,
3334
        channel *lnwallet.LightningChannel) error {
38✔
3335

38✔
3336
        chanID := lnwire.NewChanIDFromOutPoint(completeChan.FundingOutpoint)
38✔
3337

38✔
3338
        var peerKey [33]byte
38✔
3339
        copy(peerKey[:], completeChan.IdentityPub.SerializeCompressed())
38✔
3340

38✔
3341
        // Next, we'll send over the channel_ready message which marks that we
38✔
3342
        // consider the channel open by presenting the remote party with our
38✔
3343
        // next revocation key. Without the revocation key, the remote party
38✔
3344
        // will be unable to propose state transitions.
38✔
3345
        nextRevocation, err := channel.NextRevocationKey()
38✔
3346
        if err != nil {
38✔
3347
                return fmt.Errorf("unable to create next revocation: %w", err)
×
3348
        }
×
3349
        channelReadyMsg := lnwire.NewChannelReady(chanID, nextRevocation)
38✔
3350

38✔
3351
        // If this is a taproot channel, then we also need to send along our
38✔
3352
        // set of musig2 nonces as well.
38✔
3353
        if completeChan.ChanType.IsTaproot() {
45✔
3354
                log.Infof("ChanID(%v): generating musig2 nonces...",
7✔
3355
                        chanID)
7✔
3356

7✔
3357
                f.nonceMtx.Lock()
7✔
3358
                localNonce, ok := f.pendingMusigNonces[chanID]
7✔
3359
                if !ok {
14✔
3360
                        // If we don't have any nonces generated yet for this
7✔
3361
                        // first state, then we'll generate them now and stow
7✔
3362
                        // them away.  When we receive the funding locked
7✔
3363
                        // message, we'll then pass along this same set of
7✔
3364
                        // nonces.
7✔
3365
                        newNonce, err := channel.GenMusigNonces()
7✔
3366
                        if err != nil {
7✔
3367
                                f.nonceMtx.Unlock()
×
3368
                                return err
×
3369
                        }
×
3370

3371
                        // Now that we've generated the nonce for this channel,
3372
                        // we'll store it in the set of pending nonces.
3373
                        localNonce = newNonce
7✔
3374
                        f.pendingMusigNonces[chanID] = localNonce
7✔
3375
                }
3376
                f.nonceMtx.Unlock()
7✔
3377

7✔
3378
                channelReadyMsg.NextLocalNonce = lnwire.SomeMusig2Nonce(
7✔
3379
                        localNonce.PubNonce,
7✔
3380
                )
7✔
3381
        }
3382

3383
        // If the channel negotiated the option-scid-alias feature bit, we'll
3384
        // send a TLV segment that includes an alias the peer can use in their
3385
        // invoice hop hints. We'll send the first alias we find for the
3386
        // channel since it does not matter which alias we send. We'll error
3387
        // out in the odd case that no aliases are found.
3388
        if completeChan.NegotiatedAliasFeature() {
47✔
3389
                aliases := f.cfg.AliasManager.GetAliases(
9✔
3390
                        completeChan.ShortChanID(),
9✔
3391
                )
9✔
3392
                if len(aliases) == 0 {
9✔
3393
                        return fmt.Errorf("no aliases found")
×
3394
                }
×
3395

3396
                // We can use a pointer to aliases since GetAliases returns a
3397
                // copy of the alias slice.
3398
                channelReadyMsg.AliasScid = &aliases[0]
9✔
3399
        }
3400

3401
        // If the peer has disconnected before we reach this point, we will need
3402
        // to wait for him to come back online before sending the channelReady
3403
        // message. This is special for channelReady, since failing to send any
3404
        // of the previous messages in the funding flow just cancels the flow.
3405
        // But now the funding transaction is confirmed, the channel is open
3406
        // and we have to make sure the peer gets the channelReady message when
3407
        // it comes back online. This is also crucial during restart of lnd,
3408
        // where we might try to resend the channelReady message before the
3409
        // server has had the time to connect to the peer. We keep trying to
3410
        // send channelReady until we succeed, or the fundingManager is shut
3411
        // down.
3412
        for {
76✔
3413
                peer, err := f.waitForPeerOnline(completeChan.IdentityPub)
38✔
3414
                if err != nil {
39✔
3415
                        return err
1✔
3416
                }
1✔
3417

3418
                localAlias := peer.LocalFeatures().HasFeature(
37✔
3419
                        lnwire.ScidAliasOptional,
37✔
3420
                )
37✔
3421
                remoteAlias := peer.RemoteFeatures().HasFeature(
37✔
3422
                        lnwire.ScidAliasOptional,
37✔
3423
                )
37✔
3424

37✔
3425
                // We could also refresh the channel state instead of checking
37✔
3426
                // whether the feature was negotiated, but this saves us a
37✔
3427
                // database read.
37✔
3428
                if channelReadyMsg.AliasScid == nil && localAlias &&
37✔
3429
                        remoteAlias {
37✔
3430

×
3431
                        // If an alias was not assigned above and the scid
×
3432
                        // alias feature was negotiated, check if we already
×
3433
                        // have an alias stored in case handleChannelReady was
×
3434
                        // called before this. If an alias exists, use that in
×
3435
                        // channel_ready. Otherwise, request and store an
×
3436
                        // alias and use that.
×
3437
                        aliases := f.cfg.AliasManager.GetAliases(
×
3438
                                completeChan.ShortChannelID,
×
3439
                        )
×
3440
                        if len(aliases) == 0 {
×
3441
                                // No aliases were found.
×
3442
                                alias, err := f.cfg.AliasManager.RequestAlias()
×
3443
                                if err != nil {
×
3444
                                        return err
×
3445
                                }
×
3446

3447
                                err = f.cfg.AliasManager.AddLocalAlias(
×
3448
                                        alias, completeChan.ShortChannelID,
×
3449
                                        false, false,
×
3450
                                )
×
3451
                                if err != nil {
×
3452
                                        return err
×
3453
                                }
×
3454

3455
                                channelReadyMsg.AliasScid = &alias
×
3456
                        } else {
×
3457
                                channelReadyMsg.AliasScid = &aliases[0]
×
3458
                        }
×
3459
                }
3460

3461
                log.Infof("Peer(%x) is online, sending ChannelReady "+
37✔
3462
                        "for ChannelID(%v)", peerKey, chanID)
37✔
3463

37✔
3464
                if err := peer.SendMessage(true, channelReadyMsg); err == nil {
74✔
3465
                        // Sending succeeded, we can break out and continue the
37✔
3466
                        // funding flow.
37✔
3467
                        break
37✔
3468
                }
3469

3470
                log.Warnf("Unable to send channelReady to peer %x: %v. "+
×
3471
                        "Will retry when online", peerKey, err)
×
3472
        }
3473

3474
        return nil
37✔
3475
}
3476

3477
// receivedChannelReady checks whether or not we've received a ChannelReady
3478
// from the remote peer. If we have, RemoteNextRevocation will be set.
3479
func (f *Manager) receivedChannelReady(node *btcec.PublicKey,
3480
        chanID lnwire.ChannelID) (bool, error) {
63✔
3481

63✔
3482
        // If the funding manager has exited, return an error to stop looping.
63✔
3483
        // Note that the peer may appear as online while the funding manager
63✔
3484
        // has stopped due to the shutdown order in the server.
63✔
3485
        select {
63✔
UNCOV
3486
        case <-f.quit:
×
UNCOV
3487
                return false, ErrFundingManagerShuttingDown
×
3488
        default:
63✔
3489
        }
3490

3491
        // Avoid a tight loop if peer is offline.
3492
        if _, err := f.waitForPeerOnline(node); err != nil {
63✔
3493
                log.Errorf("Wait for peer online failed: %v", err)
×
3494
                return false, err
×
3495
        }
×
3496

3497
        // If we cannot find the channel, then we haven't processed the
3498
        // remote's channelReady message.
3499
        channel, err := f.cfg.FindChannel(node, chanID)
63✔
3500
        if err != nil {
63✔
3501
                log.Errorf("Unable to locate ChannelID(%v) to determine if "+
×
3502
                        "ChannelReady was received", chanID)
×
3503
                return false, err
×
3504
        }
×
3505

3506
        // If we haven't insert the next revocation point, we haven't finished
3507
        // processing the channel ready message.
3508
        if channel.RemoteNextRevocation == nil {
101✔
3509
                return false, nil
38✔
3510
        }
38✔
3511

3512
        // Finally, the barrier signal is removed once we finish
3513
        // `handleChannelReady`. If we can still find the signal, we haven't
3514
        // finished processing it yet.
3515
        _, loaded := f.handleChannelReadyBarriers.Load(chanID)
28✔
3516

28✔
3517
        return !loaded, nil
28✔
3518
}
3519

3520
// extractAnnounceParams extracts the various channel announcement and update
3521
// parameters that will be needed to construct a ChannelAnnouncement and a
3522
// ChannelUpdate.
3523
func (f *Manager) extractAnnounceParams(c *channeldb.OpenChannel) (
3524
        lnwire.MilliSatoshi, lnwire.MilliSatoshi) {
29✔
3525

29✔
3526
        // We'll obtain the min HTLC value we can forward in our direction, as
29✔
3527
        // we'll use this value within our ChannelUpdate. This constraint is
29✔
3528
        // originally set by the remote node, as it will be the one that will
29✔
3529
        // need to determine the smallest HTLC it deems economically relevant.
29✔
3530
        fwdMinHTLC := c.LocalChanCfg.MinHTLC
29✔
3531

29✔
3532
        // We don't necessarily want to go as low as the remote party allows.
29✔
3533
        // Check it against our default forwarding policy.
29✔
3534
        if fwdMinHTLC < f.cfg.DefaultRoutingPolicy.MinHTLCOut {
32✔
3535
                fwdMinHTLC = f.cfg.DefaultRoutingPolicy.MinHTLCOut
3✔
3536
        }
3✔
3537

3538
        // We'll obtain the max HTLC value we can forward in our direction, as
3539
        // we'll use this value within our ChannelUpdate. This value must be <=
3540
        // channel capacity and <= the maximum in-flight msats set by the peer.
3541
        fwdMaxHTLC := c.LocalChanCfg.MaxPendingAmount
29✔
3542
        capacityMSat := lnwire.NewMSatFromSatoshis(c.Capacity)
29✔
3543
        if fwdMaxHTLC > capacityMSat {
29✔
3544
                fwdMaxHTLC = capacityMSat
×
3545
        }
×
3546

3547
        return fwdMinHTLC, fwdMaxHTLC
29✔
3548
}
3549

3550
// addToGraph sends a ChannelAnnouncement and a ChannelUpdate to the
3551
// gossiper so that the channel is added to the graph builder's internal graph.
3552
// These announcement messages are NOT broadcasted to the greater network,
3553
// only to the channel counter party. The proofs required to announce the
3554
// channel to the greater network will be created and sent in annAfterSixConfs.
3555
// The peerAlias is used for zero-conf channels to give the counter-party a
3556
// ChannelUpdate they understand. ourPolicy may be set for various
3557
// option-scid-alias channels to re-use the same policy.
3558
func (f *Manager) addToGraph(completeChan *channeldb.OpenChannel,
3559
        shortChanID *lnwire.ShortChannelID,
3560
        peerAlias *lnwire.ShortChannelID,
3561
        ourPolicy *models.ChannelEdgePolicy) error {
29✔
3562

29✔
3563
        chanID := lnwire.NewChanIDFromOutPoint(completeChan.FundingOutpoint)
29✔
3564

29✔
3565
        fwdMinHTLC, fwdMaxHTLC := f.extractAnnounceParams(completeChan)
29✔
3566

29✔
3567
        ann, err := f.newChanAnnouncement(
29✔
3568
                f.cfg.IDKey, completeChan.IdentityPub,
29✔
3569
                &completeChan.LocalChanCfg.MultiSigKey,
29✔
3570
                completeChan.RemoteChanCfg.MultiSigKey.PubKey, *shortChanID,
29✔
3571
                chanID, fwdMinHTLC, fwdMaxHTLC, ourPolicy,
29✔
3572
                completeChan.ChanType,
29✔
3573
        )
29✔
3574
        if err != nil {
29✔
3575
                return fmt.Errorf("error generating channel "+
×
3576
                        "announcement: %v", err)
×
3577
        }
×
3578

3579
        // Send ChannelAnnouncement and ChannelUpdate to the gossiper to add
3580
        // to the Router's topology.
3581
        errChan := f.cfg.SendAnnouncement(
29✔
3582
                ann.chanAnn, discovery.ChannelCapacity(completeChan.Capacity),
29✔
3583
                discovery.ChannelPoint(completeChan.FundingOutpoint),
29✔
3584
                discovery.TapscriptRoot(completeChan.TapscriptRoot),
29✔
3585
        )
29✔
3586
        select {
29✔
3587
        case err := <-errChan:
29✔
3588
                if err != nil {
29✔
3589
                        if graph.IsError(err, graph.ErrOutdated,
×
3590
                                graph.ErrIgnored) {
×
3591

×
3592
                                log.Debugf("Graph rejected "+
×
3593
                                        "ChannelAnnouncement: %v", err)
×
3594
                        } else {
×
3595
                                return fmt.Errorf("error sending channel "+
×
3596
                                        "announcement: %v", err)
×
3597
                        }
×
3598
                }
3599
        case <-f.quit:
×
3600
                return ErrFundingManagerShuttingDown
×
3601
        }
3602

3603
        errChan = f.cfg.SendAnnouncement(
29✔
3604
                ann.chanUpdateAnn, discovery.RemoteAlias(peerAlias),
29✔
3605
        )
29✔
3606
        select {
29✔
3607
        case err := <-errChan:
29✔
3608
                if err != nil {
29✔
3609
                        if graph.IsError(err, graph.ErrOutdated,
×
3610
                                graph.ErrIgnored) {
×
3611

×
3612
                                log.Debugf("Graph rejected "+
×
3613
                                        "ChannelUpdate: %v", err)
×
3614
                        } else {
×
3615
                                return fmt.Errorf("error sending channel "+
×
3616
                                        "update: %v", err)
×
3617
                        }
×
3618
                }
3619
        case <-f.quit:
×
3620
                return ErrFundingManagerShuttingDown
×
3621
        }
3622

3623
        return nil
29✔
3624
}
3625

3626
// annAfterSixConfs broadcasts the necessary channel announcement messages to
3627
// the network after 6 confs. Should be called after the channelReady message
3628
// is sent and the channel is added to the graph (channelState is
3629
// 'addedToGraph') and the channel is ready to be used. This is the last
3630
// step in the channel opening process, and the opening state will be deleted
3631
// from the database if successful.
3632
func (f *Manager) annAfterSixConfs(completeChan *channeldb.OpenChannel,
3633
        shortChanID *lnwire.ShortChannelID) error {
29✔
3634

29✔
3635
        // If this channel is not meant to be announced to the greater network,
29✔
3636
        // we'll only send our NodeAnnouncement to our counterparty to ensure we
29✔
3637
        // don't leak any of our information.
29✔
3638
        announceChan := completeChan.ChannelFlags&lnwire.FFAnnounceChannel != 0
29✔
3639
        if !announceChan {
40✔
3640
                log.Debugf("Will not announce private channel %v.",
11✔
3641
                        shortChanID.ToUint64())
11✔
3642

11✔
3643
                peer, err := f.waitForPeerOnline(completeChan.IdentityPub)
11✔
3644
                if err != nil {
11✔
3645
                        return err
×
3646
                }
×
3647

3648
                nodeAnn, err := f.cfg.CurrentNodeAnnouncement()
11✔
3649
                if err != nil {
11✔
3650
                        return fmt.Errorf("unable to retrieve current node "+
×
3651
                                "announcement: %v", err)
×
3652
                }
×
3653

3654
                chanID := lnwire.NewChanIDFromOutPoint(
11✔
3655
                        completeChan.FundingOutpoint,
11✔
3656
                )
11✔
3657
                pubKey := peer.PubKey()
11✔
3658
                log.Debugf("Sending our NodeAnnouncement for "+
11✔
3659
                        "ChannelID(%v) to %x", chanID, pubKey)
11✔
3660

11✔
3661
                // TODO(halseth): make reliable. If the peer is not online this
11✔
3662
                // will fail, and the opening process will stop. Should instead
11✔
3663
                // block here, waiting for the peer to come online.
11✔
3664
                if err := peer.SendMessage(true, &nodeAnn); err != nil {
11✔
3665
                        return fmt.Errorf("unable to send node announcement "+
×
3666
                                "to peer %x: %v", pubKey, err)
×
3667
                }
×
3668
        } else {
21✔
3669
                // Otherwise, we'll wait until the funding transaction has
21✔
3670
                // reached 6 confirmations before announcing it.
21✔
3671
                numConfs := uint32(completeChan.NumConfsRequired)
21✔
3672
                if numConfs < 6 {
42✔
3673
                        numConfs = 6
21✔
3674
                }
21✔
3675
                txid := completeChan.FundingOutpoint.Hash
21✔
3676
                log.Debugf("Will announce channel %v after ChannelPoint"+
21✔
3677
                        "(%v) has gotten %d confirmations",
21✔
3678
                        shortChanID.ToUint64(), completeChan.FundingOutpoint,
21✔
3679
                        numConfs)
21✔
3680

21✔
3681
                fundingScript, err := makeFundingScript(completeChan)
21✔
3682
                if err != nil {
21✔
3683
                        return fmt.Errorf("unable to create funding script "+
×
3684
                                "for ChannelPoint(%v): %v",
×
3685
                                completeChan.FundingOutpoint, err)
×
3686
                }
×
3687

3688
                // Register with the ChainNotifier for a notification once the
3689
                // funding transaction reaches at least 6 confirmations.
3690
                confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(
21✔
3691
                        &txid, fundingScript, numConfs,
21✔
3692
                        completeChan.BroadcastHeight(),
21✔
3693
                )
21✔
3694
                if err != nil {
21✔
3695
                        return fmt.Errorf("unable to register for "+
×
3696
                                "confirmation of ChannelPoint(%v): %v",
×
3697
                                completeChan.FundingOutpoint, err)
×
3698
                }
×
3699

3700
                // Wait until 6 confirmations has been reached or the wallet
3701
                // signals a shutdown.
3702
                select {
21✔
3703
                case _, ok := <-confNtfn.Confirmed:
19✔
3704
                        if !ok {
19✔
3705
                                return fmt.Errorf("ChainNotifier shutting "+
×
3706
                                        "down, cannot complete funding flow "+
×
3707
                                        "for ChannelPoint(%v)",
×
3708
                                        completeChan.FundingOutpoint)
×
3709
                        }
×
3710
                        // Fallthrough.
3711

3712
                case <-f.quit:
5✔
3713
                        return fmt.Errorf("%v, stopping funding flow for "+
5✔
3714
                                "ChannelPoint(%v)",
5✔
3715
                                ErrFundingManagerShuttingDown,
5✔
3716
                                completeChan.FundingOutpoint)
5✔
3717
                }
3718

3719
                fundingPoint := completeChan.FundingOutpoint
19✔
3720
                chanID := lnwire.NewChanIDFromOutPoint(fundingPoint)
19✔
3721

19✔
3722
                log.Infof("Announcing ChannelPoint(%v), short_chan_id=%v",
19✔
3723
                        &fundingPoint, shortChanID)
19✔
3724

19✔
3725
                // If this is a non-zero-conf option-scid-alias channel, we'll
19✔
3726
                // delete the mappings the gossiper uses so that ChannelUpdates
19✔
3727
                // with aliases won't be accepted. This is done elsewhere for
19✔
3728
                // zero-conf channels.
19✔
3729
                isScidFeature := completeChan.NegotiatedAliasFeature()
19✔
3730
                isZeroConf := completeChan.IsZeroConf()
19✔
3731
                if isScidFeature && !isZeroConf {
22✔
3732
                        baseScid := completeChan.ShortChanID()
3✔
3733
                        err := f.cfg.AliasManager.DeleteSixConfs(baseScid)
3✔
3734
                        if err != nil {
3✔
3735
                                return fmt.Errorf("failed deleting six confs "+
×
3736
                                        "maps: %v", err)
×
3737
                        }
×
3738

3739
                        // We'll delete the edge and add it again via
3740
                        // addToGraph. This is because the peer may have
3741
                        // sent us a ChannelUpdate with an alias and we don't
3742
                        // want to relay this.
3743
                        ourPolicy, err := f.cfg.DeleteAliasEdge(baseScid)
3✔
3744
                        if err != nil {
3✔
3745
                                return fmt.Errorf("failed deleting real edge "+
×
3746
                                        "for alias channel from graph: %v",
×
3747
                                        err)
×
3748
                        }
×
3749

3750
                        err = f.addToGraph(
3✔
3751
                                completeChan, &baseScid, nil, ourPolicy,
3✔
3752
                        )
3✔
3753
                        if err != nil {
3✔
3754
                                return fmt.Errorf("failed to re-add to "+
×
3755
                                        "graph: %v", err)
×
3756
                        }
×
3757
                }
3758

3759
                // Create and broadcast the proofs required to make this channel
3760
                // public and usable for other nodes for routing.
3761
                err = f.announceChannel(
19✔
3762
                        f.cfg.IDKey, completeChan.IdentityPub,
19✔
3763
                        &completeChan.LocalChanCfg.MultiSigKey,
19✔
3764
                        completeChan.RemoteChanCfg.MultiSigKey.PubKey,
19✔
3765
                        *shortChanID, chanID, completeChan.ChanType,
19✔
3766
                )
19✔
3767
                if err != nil {
22✔
3768
                        return fmt.Errorf("channel announcement failed: %w",
3✔
3769
                                err)
3✔
3770
                }
3✔
3771

3772
                log.Debugf("Channel with ChannelPoint(%v), short_chan_id=%v "+
19✔
3773
                        "sent to gossiper", &fundingPoint, shortChanID)
19✔
3774
        }
3775

3776
        return nil
27✔
3777
}
3778

3779
// waitForZeroConfChannel is called when the state is addedToGraph with
3780
// a zero-conf channel. This will wait for the real confirmation, add the
3781
// confirmed SCID to the router graph, and then announce after six confs.
3782
func (f *Manager) waitForZeroConfChannel(c *channeldb.OpenChannel) error {
9✔
3783
        // First we'll check whether the channel is confirmed on-chain. If it
9✔
3784
        // is already confirmed, the chainntnfs subsystem will return with the
9✔
3785
        // confirmed tx. Otherwise, we'll wait here until confirmation occurs.
9✔
3786
        confChan, err := f.waitForFundingWithTimeout(c)
9✔
3787
        if err != nil {
14✔
3788
                return fmt.Errorf("error waiting for zero-conf funding "+
5✔
3789
                        "confirmation for ChannelPoint(%v): %v",
5✔
3790
                        c.FundingOutpoint, err)
5✔
3791
        }
5✔
3792

3793
        // We'll need to refresh the channel state so that things are properly
3794
        // populated when validating the channel state. Otherwise, a panic may
3795
        // occur due to inconsistency in the OpenChannel struct.
3796
        err = c.Refresh()
7✔
3797
        if err != nil {
10✔
3798
                return fmt.Errorf("unable to refresh channel state: %w", err)
3✔
3799
        }
3✔
3800

3801
        // Now that we have the confirmed transaction and the proper SCID,
3802
        // we'll call ValidateChannel to ensure the confirmed tx is properly
3803
        // formatted.
3804
        err = f.cfg.Wallet.ValidateChannel(c, confChan.fundingTx)
7✔
3805
        if err != nil {
7✔
3806
                return fmt.Errorf("unable to validate zero-conf channel: "+
×
3807
                        "%v", err)
×
3808
        }
×
3809

3810
        // Once we know the confirmed ShortChannelID, we'll need to save it to
3811
        // the database and refresh the OpenChannel struct with it.
3812
        err = c.MarkRealScid(confChan.shortChanID)
7✔
3813
        if err != nil {
7✔
3814
                return fmt.Errorf("unable to set confirmed SCID for zero "+
×
3815
                        "channel: %v", err)
×
3816
        }
×
3817

3818
        // Six confirmations have been reached. If this channel is public,
3819
        // we'll delete some of the alias mappings the gossiper uses.
3820
        isPublic := c.ChannelFlags&lnwire.FFAnnounceChannel != 0
7✔
3821
        if isPublic {
12✔
3822
                err = f.cfg.AliasManager.DeleteSixConfs(c.ShortChannelID)
5✔
3823
                if err != nil {
5✔
3824
                        return fmt.Errorf("unable to delete base alias after "+
×
3825
                                "six confirmations: %v", err)
×
3826
                }
×
3827

3828
                // TODO: Make this atomic!
3829
                ourPolicy, err := f.cfg.DeleteAliasEdge(c.ShortChanID())
5✔
3830
                if err != nil {
5✔
3831
                        return fmt.Errorf("unable to delete alias edge from "+
×
3832
                                "graph: %v", err)
×
3833
                }
×
3834

3835
                // We'll need to update the graph with the new ShortChannelID
3836
                // via an addToGraph call. We don't pass in the peer's
3837
                // alias since we'll be using the confirmed SCID from now on
3838
                // regardless if it's public or not.
3839
                err = f.addToGraph(
5✔
3840
                        c, &confChan.shortChanID, nil, ourPolicy,
5✔
3841
                )
5✔
3842
                if err != nil {
5✔
3843
                        return fmt.Errorf("failed adding confirmed zero-conf "+
×
3844
                                "SCID to graph: %v", err)
×
3845
                }
×
3846
        }
3847

3848
        // Since we have now marked down the confirmed SCID, we'll also need to
3849
        // tell the Switch to refresh the relevant ChannelLink so that forwards
3850
        // under the confirmed SCID are possible if this is a public channel.
3851
        err = f.cfg.ReportShortChanID(c.FundingOutpoint)
7✔
3852
        if err != nil {
7✔
3853
                // This should only fail if the link is not found in the
×
3854
                // Switch's linkIndex map. If this is the case, then the peer
×
3855
                // has gone offline and the next time the link is loaded, it
×
3856
                // will have a refreshed state. Just log an error here.
×
3857
                log.Errorf("unable to report scid for zero-conf channel "+
×
3858
                        "channel: %v", err)
×
3859
        }
×
3860

3861
        // Update the confirmed transaction's label.
3862
        f.makeLabelForTx(c)
7✔
3863

7✔
3864
        return nil
7✔
3865
}
3866

3867
// genFirstStateMusigNonce generates a nonces for the "first" local state. This
3868
// is the verification nonce for the state created for us after the initial
3869
// commitment transaction signed as part of the funding flow.
3870
func genFirstStateMusigNonce(channel *channeldb.OpenChannel,
3871
) (*musig2.Nonces, error) {
7✔
3872

7✔
3873
        musig2ShaChain, err := channeldb.DeriveMusig2Shachain(
7✔
3874
                channel.RevocationProducer,
7✔
3875
        )
7✔
3876
        if err != nil {
7✔
3877
                return nil, fmt.Errorf("unable to generate musig channel "+
×
3878
                        "nonces: %v", err)
×
3879
        }
×
3880

3881
        // We use the _next_ commitment height here as we need to generate the
3882
        // nonce for the next state the remote party will sign for us.
3883
        verNonce, err := channeldb.NewMusigVerificationNonce(
7✔
3884
                channel.LocalChanCfg.MultiSigKey.PubKey,
7✔
3885
                channel.LocalCommitment.CommitHeight+1,
7✔
3886
                musig2ShaChain,
7✔
3887
        )
7✔
3888
        if err != nil {
7✔
3889
                return nil, fmt.Errorf("unable to generate musig channel "+
×
3890
                        "nonces: %v", err)
×
3891
        }
×
3892

3893
        return verNonce, nil
7✔
3894
}
3895

3896
// handleChannelReady finalizes the channel funding process and enables the
3897
// channel to enter normal operating mode.
3898
func (f *Manager) handleChannelReady(peer lnpeer.Peer, //nolint:funlen
3899
        msg *lnwire.ChannelReady) {
31✔
3900

31✔
3901
        defer f.wg.Done()
31✔
3902

31✔
3903
        // If we are in development mode, we'll wait for specified duration
31✔
3904
        // before processing the channel ready message.
31✔
3905
        if f.cfg.Dev != nil {
34✔
3906
                duration := f.cfg.Dev.ProcessChannelReadyWait
3✔
3907
                log.Warnf("Channel(%v): sleeping %v before processing "+
3✔
3908
                        "channel_ready", msg.ChanID, duration)
3✔
3909

3✔
3910
                select {
3✔
3911
                case <-time.After(duration):
3✔
3912
                        log.Warnf("Channel(%v): slept %v before processing "+
3✔
3913
                                "channel_ready", msg.ChanID, duration)
3✔
3914
                case <-f.quit:
×
3915
                        log.Warnf("Channel(%v): quit sleeping", msg.ChanID)
×
3916
                        return
×
3917
                }
3918
        }
3919

3920
        log.Debugf("Received ChannelReady for ChannelID(%v) from "+
31✔
3921
                "peer %x", msg.ChanID,
31✔
3922
                peer.IdentityKey().SerializeCompressed())
31✔
3923

31✔
3924
        // We now load or create a new channel barrier for this channel.
31✔
3925
        _, loaded := f.handleChannelReadyBarriers.LoadOrStore(
31✔
3926
                msg.ChanID, struct{}{},
31✔
3927
        )
31✔
3928

31✔
3929
        // If we are currently in the process of handling a channel_ready
31✔
3930
        // message for this channel, ignore.
31✔
3931
        if loaded {
35✔
3932
                log.Infof("Already handling channelReady for "+
4✔
3933
                        "ChannelID(%v), ignoring.", msg.ChanID)
4✔
3934
                return
4✔
3935
        }
4✔
3936

3937
        // If not already handling channelReady for this channel, then the
3938
        // `LoadOrStore` has set up a barrier, and it will be removed once this
3939
        // function exits.
3940
        defer f.handleChannelReadyBarriers.Delete(msg.ChanID)
30✔
3941

30✔
3942
        localDiscoverySignal, ok := f.localDiscoverySignals.Load(msg.ChanID)
30✔
3943
        if ok {
58✔
3944
                // Before we proceed with processing the channel_ready
28✔
3945
                // message, we'll wait for the local waitForFundingConfirmation
28✔
3946
                // goroutine to signal that it has the necessary state in
28✔
3947
                // place. Otherwise, we may be missing critical information
28✔
3948
                // required to handle forwarded HTLC's.
28✔
3949
                select {
28✔
3950
                case <-localDiscoverySignal:
28✔
3951
                        // Fallthrough
3952
                case <-f.quit:
3✔
3953
                        return
3✔
3954
                }
3955

3956
                // With the signal received, we can now safely delete the entry
3957
                // from the map.
3958
                f.localDiscoverySignals.Delete(msg.ChanID)
28✔
3959
        }
3960

3961
        // First, we'll attempt to locate the channel whose funding workflow is
3962
        // being finalized by this message. We go to the database rather than
3963
        // our reservation map as we may have restarted, mid funding flow. Also
3964
        // provide the node's public key to make the search faster.
3965
        chanID := msg.ChanID
30✔
3966
        channel, err := f.cfg.FindChannel(peer.IdentityKey(), chanID)
30✔
3967
        if err != nil {
30✔
3968
                log.Errorf("Unable to locate ChannelID(%v), cannot complete "+
×
3969
                        "funding", chanID)
×
3970
                return
×
3971
        }
×
3972

3973
        // If this is a taproot channel, then we can generate the set of nonces
3974
        // the remote party needs to send the next remote commitment here.
3975
        var firstVerNonce *musig2.Nonces
30✔
3976
        if channel.ChanType.IsTaproot() {
37✔
3977
                firstVerNonce, err = genFirstStateMusigNonce(channel)
7✔
3978
                if err != nil {
7✔
3979
                        log.Error(err)
×
3980
                        return
×
3981
                }
×
3982
        }
3983

3984
        // We'll need to store the received TLV alias if the option_scid_alias
3985
        // feature was negotiated. This will be used to provide route hints
3986
        // during invoice creation. In the zero-conf case, it is also used to
3987
        // provide a ChannelUpdate to the remote peer. This is done before the
3988
        // call to InsertNextRevocation in case the call to PutPeerAlias fails.
3989
        // If it were to fail on the first call to handleChannelReady, we
3990
        // wouldn't want the channel to be usable yet.
3991
        if channel.NegotiatedAliasFeature() {
39✔
3992
                // If the AliasScid field is nil, we must fail out. We will
9✔
3993
                // most likely not be able to route through the peer.
9✔
3994
                if msg.AliasScid == nil {
9✔
3995
                        log.Debugf("Consider closing ChannelID(%v), peer "+
×
3996
                                "does not implement the option-scid-alias "+
×
3997
                                "feature properly", chanID)
×
3998
                        return
×
3999
                }
×
4000

4001
                // We'll store the AliasScid so that invoice creation can use
4002
                // it.
4003
                err = f.cfg.AliasManager.PutPeerAlias(chanID, *msg.AliasScid)
9✔
4004
                if err != nil {
9✔
4005
                        log.Errorf("unable to store peer's alias: %v", err)
×
4006
                        return
×
4007
                }
×
4008

4009
                // If we do not have an alias stored, we'll create one now.
4010
                // This is only used in the upgrade case where a user toggles
4011
                // the option-scid-alias feature-bit to on. We'll also send the
4012
                // channel_ready message here in case the link is created
4013
                // before sendChannelReady is called.
4014
                aliases := f.cfg.AliasManager.GetAliases(
9✔
4015
                        channel.ShortChannelID,
9✔
4016
                )
9✔
4017
                if len(aliases) == 0 {
9✔
4018
                        // No aliases were found so we'll request and store an
×
4019
                        // alias and use it in the channel_ready message.
×
4020
                        alias, err := f.cfg.AliasManager.RequestAlias()
×
4021
                        if err != nil {
×
4022
                                log.Errorf("unable to request alias: %v", err)
×
4023
                                return
×
4024
                        }
×
4025

4026
                        err = f.cfg.AliasManager.AddLocalAlias(
×
4027
                                alias, channel.ShortChannelID, false, false,
×
4028
                        )
×
4029
                        if err != nil {
×
4030
                                log.Errorf("unable to add local alias: %v",
×
4031
                                        err)
×
4032
                                return
×
4033
                        }
×
4034

4035
                        secondPoint, err := channel.SecondCommitmentPoint()
×
4036
                        if err != nil {
×
4037
                                log.Errorf("unable to fetch second "+
×
4038
                                        "commitment point: %v", err)
×
4039
                                return
×
4040
                        }
×
4041

4042
                        channelReadyMsg := lnwire.NewChannelReady(
×
4043
                                chanID, secondPoint,
×
4044
                        )
×
4045
                        channelReadyMsg.AliasScid = &alias
×
4046

×
4047
                        if firstVerNonce != nil {
×
4048
                                channelReadyMsg.NextLocalNonce = lnwire.SomeMusig2Nonce( //nolint:ll
×
4049
                                        firstVerNonce.PubNonce,
×
4050
                                )
×
4051
                        }
×
4052

4053
                        err = peer.SendMessage(true, channelReadyMsg)
×
4054
                        if err != nil {
×
4055
                                log.Errorf("unable to send channel_ready: %v",
×
4056
                                        err)
×
4057
                                return
×
4058
                        }
×
4059
                }
4060
        }
4061

4062
        // If the RemoteNextRevocation is non-nil, it means that we have
4063
        // already processed channelReady for this channel, so ignore. This
4064
        // check is after the alias logic so we store the peer's most recent
4065
        // alias. The spec requires us to validate that subsequent
4066
        // channel_ready messages use the same per commitment point (the
4067
        // second), but it is not actually necessary since we'll just end up
4068
        // ignoring it. We are, however, required to *send* the same per
4069
        // commitment point, since another pedantic implementation might
4070
        // verify it.
4071
        if channel.RemoteNextRevocation != nil {
34✔
4072
                log.Infof("Received duplicate channelReady for "+
4✔
4073
                        "ChannelID(%v), ignoring.", chanID)
4✔
4074
                return
4✔
4075
        }
4✔
4076

4077
        // If this is a taproot channel, then we'll need to map the received
4078
        // nonces to a nonce pair, and also fetch our pending nonces, which are
4079
        // required in order to make the channel whole.
4080
        var chanOpts []lnwallet.ChannelOpt
29✔
4081
        if channel.ChanType.IsTaproot() {
36✔
4082
                f.nonceMtx.Lock()
7✔
4083
                localNonce, ok := f.pendingMusigNonces[chanID]
7✔
4084
                if !ok {
10✔
4085
                        // If there's no pending nonce for this channel ID,
3✔
4086
                        // we'll use the one generated above.
3✔
4087
                        localNonce = firstVerNonce
3✔
4088
                        f.pendingMusigNonces[chanID] = firstVerNonce
3✔
4089
                }
3✔
4090
                f.nonceMtx.Unlock()
7✔
4091

7✔
4092
                log.Infof("ChanID(%v): applying local+remote musig2 nonces",
7✔
4093
                        chanID)
7✔
4094

7✔
4095
                remoteNonce, err := msg.NextLocalNonce.UnwrapOrErrV(
7✔
4096
                        errNoLocalNonce,
7✔
4097
                )
7✔
4098
                if err != nil {
7✔
4099
                        cid := newChanIdentifier(msg.ChanID)
×
4100
                        f.sendWarning(peer, cid, err)
×
4101

×
4102
                        return
×
4103
                }
×
4104

4105
                chanOpts = append(
7✔
4106
                        chanOpts,
7✔
4107
                        lnwallet.WithLocalMusigNonces(localNonce),
7✔
4108
                        lnwallet.WithRemoteMusigNonces(&musig2.Nonces{
7✔
4109
                                PubNonce: remoteNonce,
7✔
4110
                        }),
7✔
4111
                )
7✔
4112

7✔
4113
                // Inform the aux funding controller that the liquidity in the
7✔
4114
                // custom channel is now ready to be advertised. We potentially
7✔
4115
                // haven't sent our own channel ready message yet, but other
7✔
4116
                // than that the channel is ready to count toward available
7✔
4117
                // liquidity.
7✔
4118
                err = fn.MapOptionZ(
7✔
4119
                        f.cfg.AuxFundingController,
7✔
4120
                        func(controller AuxFundingController) error {
7✔
4121
                                return controller.ChannelReady(
×
4122
                                        lnwallet.NewAuxChanState(channel),
×
4123
                                )
×
4124
                        },
×
4125
                )
4126
                if err != nil {
7✔
4127
                        cid := newChanIdentifier(msg.ChanID)
×
4128
                        f.sendWarning(peer, cid, err)
×
4129

×
4130
                        return
×
4131
                }
×
4132
        }
4133

4134
        // The channel_ready message contains the next commitment point we'll
4135
        // need to create the next commitment state for the remote party. So
4136
        // we'll insert that into the channel now before passing it along to
4137
        // other sub-systems.
4138
        err = channel.InsertNextRevocation(msg.NextPerCommitmentPoint)
29✔
4139
        if err != nil {
29✔
4140
                log.Errorf("unable to insert next commitment point: %v", err)
×
4141
                return
×
4142
        }
×
4143

4144
        // Before we can add the channel to the peer, we'll need to ensure that
4145
        // we have an initial forwarding policy set.
4146
        if err := f.ensureInitialForwardingPolicy(chanID, channel); err != nil {
29✔
4147
                log.Errorf("Unable to ensure initial forwarding policy: %v",
×
4148
                        err)
×
4149
        }
×
4150

4151
        err = peer.AddNewChannel(&lnpeer.NewChannel{
29✔
4152
                OpenChannel: channel,
29✔
4153
                ChanOpts:    chanOpts,
29✔
4154
        }, f.quit)
29✔
4155
        if err != nil {
29✔
4156
                log.Errorf("Unable to add new channel %v with peer %x: %v",
×
4157
                        channel.FundingOutpoint,
×
4158
                        peer.IdentityKey().SerializeCompressed(), err,
×
4159
                )
×
4160
        }
×
4161
}
4162

4163
// handleChannelReadyReceived is called once the remote's channelReady message
4164
// is received and processed. At this stage, we must have sent out our
4165
// channelReady message, once the remote's channelReady is processed, the
4166
// channel is now active, thus we change its state to `addedToGraph` to
4167
// let the channel start handling routing.
4168
func (f *Manager) handleChannelReadyReceived(channel *channeldb.OpenChannel,
4169
        scid *lnwire.ShortChannelID, pendingChanID PendingChanID,
4170
        updateChan chan<- *lnrpc.OpenStatusUpdate) error {
27✔
4171

27✔
4172
        chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
27✔
4173

27✔
4174
        // Since we've sent+received funding locked at this point, we
27✔
4175
        // can clean up the pending musig2 nonce state.
27✔
4176
        f.nonceMtx.Lock()
27✔
4177
        delete(f.pendingMusigNonces, chanID)
27✔
4178
        f.nonceMtx.Unlock()
27✔
4179

27✔
4180
        var peerAlias *lnwire.ShortChannelID
27✔
4181
        if channel.IsZeroConf() {
34✔
4182
                // We'll need to wait until channel_ready has been received and
7✔
4183
                // the peer lets us know the alias they want to use for the
7✔
4184
                // channel. With this information, we can then construct a
7✔
4185
                // ChannelUpdate for them.  If an alias does not yet exist,
7✔
4186
                // we'll just return, letting the next iteration of the loop
7✔
4187
                // check again.
7✔
4188
                var defaultAlias lnwire.ShortChannelID
7✔
4189
                chanID := lnwire.NewChanIDFromOutPoint(channel.FundingOutpoint)
7✔
4190
                foundAlias, _ := f.cfg.AliasManager.GetPeerAlias(chanID)
7✔
4191
                if foundAlias == defaultAlias {
7✔
4192
                        return nil
×
4193
                }
×
4194

4195
                peerAlias = &foundAlias
7✔
4196
        }
4197

4198
        err := f.addToGraph(channel, scid, peerAlias, nil)
27✔
4199
        if err != nil {
27✔
4200
                return fmt.Errorf("failed adding to graph: %w", err)
×
4201
        }
×
4202

4203
        // As the channel is now added to the ChannelRouter's topology, the
4204
        // channel is moved to the next state of the state machine. It will be
4205
        // moved to the last state (actually deleted from the database) after
4206
        // the channel is finally announced.
4207
        err = f.saveChannelOpeningState(
27✔
4208
                &channel.FundingOutpoint, addedToGraph, scid,
27✔
4209
        )
27✔
4210
        if err != nil {
27✔
4211
                return fmt.Errorf("error setting channel state to"+
×
4212
                        " addedToGraph: %w", err)
×
4213
        }
×
4214

4215
        log.Debugf("Channel(%v) with ShortChanID %v: successfully "+
27✔
4216
                "added to graph", chanID, scid)
27✔
4217

27✔
4218
        err = fn.MapOptionZ(
27✔
4219
                f.cfg.AuxFundingController,
27✔
4220
                func(controller AuxFundingController) error {
27✔
4221
                        return controller.ChannelReady(
×
4222
                                lnwallet.NewAuxChanState(channel),
×
4223
                        )
×
4224
                },
×
4225
        )
4226
        if err != nil {
27✔
4227
                return fmt.Errorf("failed notifying aux funding controller "+
×
4228
                        "about channel ready: %w", err)
×
4229
        }
×
4230

4231
        // Give the caller a final update notifying them that the channel is
4232
        fundingPoint := channel.FundingOutpoint
27✔
4233
        cp := &lnrpc.ChannelPoint{
27✔
4234
                FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
27✔
4235
                        FundingTxidBytes: fundingPoint.Hash[:],
27✔
4236
                },
27✔
4237
                OutputIndex: fundingPoint.Index,
27✔
4238
        }
27✔
4239

27✔
4240
        if updateChan != nil {
40✔
4241
                upd := &lnrpc.OpenStatusUpdate{
13✔
4242
                        Update: &lnrpc.OpenStatusUpdate_ChanOpen{
13✔
4243
                                ChanOpen: &lnrpc.ChannelOpenUpdate{
13✔
4244
                                        ChannelPoint: cp,
13✔
4245
                                },
13✔
4246
                        },
13✔
4247
                        PendingChanId: pendingChanID[:],
13✔
4248
                }
13✔
4249

13✔
4250
                select {
13✔
4251
                case updateChan <- upd:
13✔
4252
                case <-f.quit:
×
4253
                        return ErrFundingManagerShuttingDown
×
4254
                }
4255
        }
4256

4257
        return nil
27✔
4258
}
4259

4260
// ensureInitialForwardingPolicy ensures that we have an initial forwarding
4261
// policy set for the given channel. If we don't, we'll fall back to the default
4262
// values.
4263
func (f *Manager) ensureInitialForwardingPolicy(chanID lnwire.ChannelID,
4264
        channel *channeldb.OpenChannel) error {
29✔
4265

29✔
4266
        // Before we can add the channel to the peer, we'll need to ensure that
29✔
4267
        // we have an initial forwarding policy set. This should always be the
29✔
4268
        // case except for a channel that was created with lnd <= 0.15.5 and
29✔
4269
        // is still pending while updating to this version.
29✔
4270
        var needDBUpdate bool
29✔
4271
        forwardingPolicy, err := f.getInitialForwardingPolicy(chanID)
29✔
4272
        if err != nil {
29✔
4273
                log.Errorf("Unable to fetch initial forwarding policy, "+
×
4274
                        "falling back to default values: %v", err)
×
4275

×
4276
                forwardingPolicy = f.defaultForwardingPolicy(
×
4277
                        channel.LocalChanCfg.ChannelStateBounds,
×
4278
                )
×
4279
                needDBUpdate = true
×
4280
        }
×
4281

4282
        // We only started storing the actual values for MinHTLCOut and MaxHTLC
4283
        // after 0.16.x, so if a channel was opened with such a version and is
4284
        // still pending while updating to this version, we'll need to set the
4285
        // values to the default values.
4286
        if forwardingPolicy.MinHTLCOut == 0 {
45✔
4287
                forwardingPolicy.MinHTLCOut = channel.LocalChanCfg.MinHTLC
16✔
4288
                needDBUpdate = true
16✔
4289
        }
16✔
4290
        if forwardingPolicy.MaxHTLC == 0 {
45✔
4291
                forwardingPolicy.MaxHTLC = channel.LocalChanCfg.MaxPendingAmount
16✔
4292
                needDBUpdate = true
16✔
4293
        }
16✔
4294

4295
        // And finally, if we found that the values currently stored aren't
4296
        // sufficient for the link, we'll update the database.
4297
        if needDBUpdate {
45✔
4298
                err := f.saveInitialForwardingPolicy(chanID, forwardingPolicy)
16✔
4299
                if err != nil {
16✔
4300
                        return fmt.Errorf("unable to update initial "+
×
4301
                                "forwarding policy: %v", err)
×
4302
                }
×
4303
        }
4304

4305
        return nil
29✔
4306
}
4307

4308
// chanAnnouncement encapsulates the two authenticated announcements that we
4309
// send out to the network after a new channel has been created locally.
4310
type chanAnnouncement struct {
4311
        chanAnn       *lnwire.ChannelAnnouncement1
4312
        chanUpdateAnn *lnwire.ChannelUpdate1
4313
        chanProof     *lnwire.AnnounceSignatures1
4314
}
4315

4316
// newChanAnnouncement creates the authenticated channel announcement messages
4317
// required to broadcast a newly created channel to the network. The
4318
// announcement is two part: the first part authenticates the existence of the
4319
// channel and contains four signatures binding the funding pub keys and
4320
// identity pub keys of both parties to the channel, and the second segment is
4321
// authenticated only by us and contains our directional routing policy for the
4322
// channel. ourPolicy may be set in order to re-use an existing, non-default
4323
// policy.
4324
func (f *Manager) newChanAnnouncement(localPubKey,
4325
        remotePubKey *btcec.PublicKey, localFundingKey *keychain.KeyDescriptor,
4326
        remoteFundingKey *btcec.PublicKey, shortChanID lnwire.ShortChannelID,
4327
        chanID lnwire.ChannelID, fwdMinHTLC, fwdMaxHTLC lnwire.MilliSatoshi,
4328
        ourPolicy *models.ChannelEdgePolicy,
4329
        chanType channeldb.ChannelType) (*chanAnnouncement, error) {
45✔
4330

45✔
4331
        chainHash := *f.cfg.Wallet.Cfg.NetParams.GenesisHash
45✔
4332

45✔
4333
        // The unconditional section of the announcement is the ShortChannelID
45✔
4334
        // itself which compactly encodes the location of the funding output
45✔
4335
        // within the blockchain.
45✔
4336
        chanAnn := &lnwire.ChannelAnnouncement1{
45✔
4337
                ShortChannelID: shortChanID,
45✔
4338
                Features:       lnwire.NewRawFeatureVector(),
45✔
4339
                ChainHash:      chainHash,
45✔
4340
        }
45✔
4341

45✔
4342
        // If this is a taproot channel, then we'll set a special bit in the
45✔
4343
        // feature vector to indicate to the routing layer that this needs a
45✔
4344
        // slightly different type of validation.
45✔
4345
        //
45✔
4346
        // TODO(roasbeef): temp, remove after gossip 1.5
45✔
4347
        if chanType.IsTaproot() {
52✔
4348
                log.Debugf("Applying taproot feature bit to "+
7✔
4349
                        "ChannelAnnouncement for %v", chanID)
7✔
4350

7✔
4351
                chanAnn.Features.Set(
7✔
4352
                        lnwire.SimpleTaprootChannelsRequiredStaging,
7✔
4353
                )
7✔
4354
        }
7✔
4355

4356
        // The chanFlags field indicates which directed edge of the channel is
4357
        // being updated within the ChannelUpdateAnnouncement announcement
4358
        // below. A value of zero means it's the edge of the "first" node and 1
4359
        // being the other node.
4360
        var chanFlags lnwire.ChanUpdateChanFlags
45✔
4361

45✔
4362
        // The lexicographical ordering of the two identity public keys of the
45✔
4363
        // nodes indicates which of the nodes is "first". If our serialized
45✔
4364
        // identity key is lower than theirs then we're the "first" node and
45✔
4365
        // second otherwise.
45✔
4366
        selfBytes := localPubKey.SerializeCompressed()
45✔
4367
        remoteBytes := remotePubKey.SerializeCompressed()
45✔
4368
        if bytes.Compare(selfBytes, remoteBytes) == -1 {
69✔
4369
                copy(chanAnn.NodeID1[:], localPubKey.SerializeCompressed())
24✔
4370
                copy(chanAnn.NodeID2[:], remotePubKey.SerializeCompressed())
24✔
4371
                copy(
24✔
4372
                        chanAnn.BitcoinKey1[:],
24✔
4373
                        localFundingKey.PubKey.SerializeCompressed(),
24✔
4374
                )
24✔
4375
                copy(
24✔
4376
                        chanAnn.BitcoinKey2[:],
24✔
4377
                        remoteFundingKey.SerializeCompressed(),
24✔
4378
                )
24✔
4379

24✔
4380
                // If we're the first node then update the chanFlags to
24✔
4381
                // indicate the "direction" of the update.
24✔
4382
                chanFlags = 0
24✔
4383
        } else {
48✔
4384
                copy(chanAnn.NodeID1[:], remotePubKey.SerializeCompressed())
24✔
4385
                copy(chanAnn.NodeID2[:], localPubKey.SerializeCompressed())
24✔
4386
                copy(
24✔
4387
                        chanAnn.BitcoinKey1[:],
24✔
4388
                        remoteFundingKey.SerializeCompressed(),
24✔
4389
                )
24✔
4390
                copy(
24✔
4391
                        chanAnn.BitcoinKey2[:],
24✔
4392
                        localFundingKey.PubKey.SerializeCompressed(),
24✔
4393
                )
24✔
4394

24✔
4395
                // If we're the second node then update the chanFlags to
24✔
4396
                // indicate the "direction" of the update.
24✔
4397
                chanFlags = 1
24✔
4398
        }
24✔
4399

4400
        // Our channel update message flags will signal that we support the
4401
        // max_htlc field.
4402
        msgFlags := lnwire.ChanUpdateRequiredMaxHtlc
45✔
4403

45✔
4404
        // We announce the channel with the default values. Some of
45✔
4405
        // these values can later be changed by crafting a new ChannelUpdate.
45✔
4406
        chanUpdateAnn := &lnwire.ChannelUpdate1{
45✔
4407
                ShortChannelID: shortChanID,
45✔
4408
                ChainHash:      chainHash,
45✔
4409
                Timestamp:      uint32(time.Now().Unix()),
45✔
4410
                MessageFlags:   msgFlags,
45✔
4411
                ChannelFlags:   chanFlags,
45✔
4412
                TimeLockDelta: uint16(
45✔
4413
                        f.cfg.DefaultRoutingPolicy.TimeLockDelta,
45✔
4414
                ),
45✔
4415
                HtlcMinimumMsat: fwdMinHTLC,
45✔
4416
                HtlcMaximumMsat: fwdMaxHTLC,
45✔
4417
        }
45✔
4418

45✔
4419
        // The caller of newChanAnnouncement is expected to provide the initial
45✔
4420
        // forwarding policy to be announced. If no persisted initial policy
45✔
4421
        // values are found, then we will use the default policy values in the
45✔
4422
        // channel announcement.
45✔
4423
        storedFwdingPolicy, err := f.getInitialForwardingPolicy(chanID)
45✔
4424
        if err != nil && !errors.Is(err, channeldb.ErrChannelNotFound) {
45✔
4425
                return nil, errors.Errorf("unable to generate channel "+
×
4426
                        "update announcement: %v", err)
×
4427
        }
×
4428

4429
        switch {
45✔
4430
        case ourPolicy != nil:
3✔
4431
                // If ourPolicy is non-nil, modify the default parameters of the
3✔
4432
                // ChannelUpdate.
3✔
4433
                chanUpdateAnn.MessageFlags = ourPolicy.MessageFlags
3✔
4434
                chanUpdateAnn.ChannelFlags = ourPolicy.ChannelFlags
3✔
4435
                chanUpdateAnn.TimeLockDelta = ourPolicy.TimeLockDelta
3✔
4436
                chanUpdateAnn.HtlcMinimumMsat = ourPolicy.MinHTLC
3✔
4437
                chanUpdateAnn.HtlcMaximumMsat = ourPolicy.MaxHTLC
3✔
4438
                chanUpdateAnn.BaseFee = uint32(ourPolicy.FeeBaseMSat)
3✔
4439
                chanUpdateAnn.FeeRate = uint32(
3✔
4440
                        ourPolicy.FeeProportionalMillionths,
3✔
4441
                )
3✔
4442

4443
        case storedFwdingPolicy != nil:
45✔
4444
                chanUpdateAnn.BaseFee = uint32(storedFwdingPolicy.BaseFee)
45✔
4445
                chanUpdateAnn.FeeRate = uint32(storedFwdingPolicy.FeeRate)
45✔
4446

4447
        default:
×
4448
                log.Infof("No channel forwarding policy specified for channel "+
×
4449
                        "announcement of ChannelID(%v). "+
×
4450
                        "Assuming default fee parameters.", chanID)
×
4451
                chanUpdateAnn.BaseFee = uint32(
×
4452
                        f.cfg.DefaultRoutingPolicy.BaseFee,
×
4453
                )
×
4454
                chanUpdateAnn.FeeRate = uint32(
×
4455
                        f.cfg.DefaultRoutingPolicy.FeeRate,
×
4456
                )
×
4457
        }
4458

4459
        // With the channel update announcement constructed, we'll generate a
4460
        // signature that signs a double-sha digest of the announcement.
4461
        // This'll serve to authenticate this announcement and any other future
4462
        // updates we may send.
4463
        chanUpdateMsg, err := chanUpdateAnn.DataToSign()
45✔
4464
        if err != nil {
45✔
4465
                return nil, err
×
4466
        }
×
4467
        sig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanUpdateMsg, true)
45✔
4468
        if err != nil {
45✔
4469
                return nil, errors.Errorf("unable to generate channel "+
×
4470
                        "update announcement signature: %v", err)
×
4471
        }
×
4472
        chanUpdateAnn.Signature, err = lnwire.NewSigFromSignature(sig)
45✔
4473
        if err != nil {
45✔
4474
                return nil, errors.Errorf("unable to generate channel "+
×
4475
                        "update announcement signature: %v", err)
×
4476
        }
×
4477

4478
        // The channel existence proofs itself is currently announced in
4479
        // distinct message. In order to properly authenticate this message, we
4480
        // need two signatures: one under the identity public key used which
4481
        // signs the message itself and another signature of the identity
4482
        // public key under the funding key itself.
4483
        //
4484
        // TODO(roasbeef): use SignAnnouncement here instead?
4485
        chanAnnMsg, err := chanAnn.DataToSign()
45✔
4486
        if err != nil {
45✔
4487
                return nil, err
×
4488
        }
×
4489
        nodeSig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanAnnMsg, true)
45✔
4490
        if err != nil {
45✔
4491
                return nil, errors.Errorf("unable to generate node "+
×
4492
                        "signature for channel announcement: %v", err)
×
4493
        }
×
4494
        bitcoinSig, err := f.cfg.SignMessage(
45✔
4495
                localFundingKey.KeyLocator, chanAnnMsg, true,
45✔
4496
        )
45✔
4497
        if err != nil {
45✔
4498
                return nil, errors.Errorf("unable to generate bitcoin "+
×
4499
                        "signature for node public key: %v", err)
×
4500
        }
×
4501

4502
        // Finally, we'll generate the announcement proof which we'll use to
4503
        // provide the other side with the necessary signatures required to
4504
        // allow them to reconstruct the full channel announcement.
4505
        proof := &lnwire.AnnounceSignatures1{
45✔
4506
                ChannelID:      chanID,
45✔
4507
                ShortChannelID: shortChanID,
45✔
4508
        }
45✔
4509
        proof.NodeSignature, err = lnwire.NewSigFromSignature(nodeSig)
45✔
4510
        if err != nil {
45✔
4511
                return nil, err
×
4512
        }
×
4513
        proof.BitcoinSignature, err = lnwire.NewSigFromSignature(bitcoinSig)
45✔
4514
        if err != nil {
45✔
4515
                return nil, err
×
4516
        }
×
4517

4518
        return &chanAnnouncement{
45✔
4519
                chanAnn:       chanAnn,
45✔
4520
                chanUpdateAnn: chanUpdateAnn,
45✔
4521
                chanProof:     proof,
45✔
4522
        }, nil
45✔
4523
}
4524

4525
// announceChannel announces a newly created channel to the rest of the network
4526
// by crafting the two authenticated announcements required for the peers on
4527
// the network to recognize the legitimacy of the channel. The crafted
4528
// announcements are then sent to the channel router to handle broadcasting to
4529
// the network during its next trickle.
4530
// This method is synchronous and will return when all the network requests
4531
// finish, either successfully or with an error.
4532
func (f *Manager) announceChannel(localIDKey, remoteIDKey *btcec.PublicKey,
4533
        localFundingKey *keychain.KeyDescriptor,
4534
        remoteFundingKey *btcec.PublicKey, shortChanID lnwire.ShortChannelID,
4535
        chanID lnwire.ChannelID, chanType channeldb.ChannelType) error {
19✔
4536

19✔
4537
        // First, we'll create the batch of announcements to be sent upon
19✔
4538
        // initial channel creation. This includes the channel announcement
19✔
4539
        // itself, the channel update announcement, and our half of the channel
19✔
4540
        // proof needed to fully authenticate the channel.
19✔
4541
        //
19✔
4542
        // We can pass in zeroes for the min and max htlc policy, because we
19✔
4543
        // only use the channel announcement message from the returned struct.
19✔
4544
        ann, err := f.newChanAnnouncement(
19✔
4545
                localIDKey, remoteIDKey, localFundingKey, remoteFundingKey,
19✔
4546
                shortChanID, chanID, 0, 0, nil, chanType,
19✔
4547
        )
19✔
4548
        if err != nil {
19✔
4549
                log.Errorf("can't generate channel announcement: %v", err)
×
4550
                return err
×
4551
        }
×
4552

4553
        // We only send the channel proof announcement and the node announcement
4554
        // because addToGraph previously sent the ChannelAnnouncement and
4555
        // the ChannelUpdate announcement messages. The channel proof and node
4556
        // announcements are broadcast to the greater network.
4557
        errChan := f.cfg.SendAnnouncement(ann.chanProof)
19✔
4558
        select {
19✔
4559
        case err := <-errChan:
19✔
4560
                if err != nil {
22✔
4561
                        if graph.IsError(err, graph.ErrOutdated,
3✔
4562
                                graph.ErrIgnored) {
3✔
4563

×
4564
                                log.Debugf("Graph rejected "+
×
4565
                                        "AnnounceSignatures: %v", err)
×
4566
                        } else {
3✔
4567
                                log.Errorf("Unable to send channel "+
3✔
4568
                                        "proof: %v", err)
3✔
4569
                                return err
3✔
4570
                        }
3✔
4571
                }
4572

4573
        case <-f.quit:
×
4574
                return ErrFundingManagerShuttingDown
×
4575
        }
4576

4577
        // Now that the channel is announced to the network, we will also
4578
        // obtain and send a node announcement. This is done since a node
4579
        // announcement is only accepted after a channel is known for that
4580
        // particular node, and this might be our first channel.
4581
        nodeAnn, err := f.cfg.CurrentNodeAnnouncement()
19✔
4582
        if err != nil {
19✔
4583
                log.Errorf("can't generate node announcement: %v", err)
×
4584
                return err
×
4585
        }
×
4586

4587
        errChan = f.cfg.SendAnnouncement(&nodeAnn)
19✔
4588
        select {
19✔
4589
        case err := <-errChan:
19✔
4590
                if err != nil {
22✔
4591
                        if graph.IsError(err, graph.ErrOutdated,
3✔
4592
                                graph.ErrIgnored) {
6✔
4593

3✔
4594
                                log.Debugf("Graph rejected "+
3✔
4595
                                        "NodeAnnouncement: %v", err)
3✔
4596
                        } else {
3✔
4597
                                log.Errorf("Unable to send node "+
×
4598
                                        "announcement: %v", err)
×
4599
                                return err
×
4600
                        }
×
4601
                }
4602

4603
        case <-f.quit:
×
4604
                return ErrFundingManagerShuttingDown
×
4605
        }
4606

4607
        return nil
19✔
4608
}
4609

4610
// InitFundingWorkflow sends a message to the funding manager instructing it
4611
// to initiate a single funder workflow with the source peer.
4612
func (f *Manager) InitFundingWorkflow(msg *InitFundingMsg) {
59✔
4613
        f.fundingRequests <- msg
59✔
4614
}
59✔
4615

4616
// getUpfrontShutdownScript takes a user provided script and a getScript
4617
// function which can be used to generate an upfront shutdown script. If our
4618
// peer does not support the feature, this function will error if a non-zero
4619
// script was provided by the user, and return an empty script otherwise. If
4620
// our peer does support the feature, we will return the user provided script
4621
// if non-zero, or a freshly generated script if our node is configured to set
4622
// upfront shutdown scripts automatically.
4623
func getUpfrontShutdownScript(enableUpfrontShutdown bool, peer lnpeer.Peer,
4624
        script lnwire.DeliveryAddress,
4625
        getScript func(bool) (lnwire.DeliveryAddress, error)) (lnwire.DeliveryAddress,
4626
        error) {
111✔
4627

111✔
4628
        // Check whether the remote peer supports upfront shutdown scripts.
111✔
4629
        remoteUpfrontShutdown := peer.RemoteFeatures().HasFeature(
111✔
4630
                lnwire.UpfrontShutdownScriptOptional,
111✔
4631
        )
111✔
4632

111✔
4633
        // If the peer does not support upfront shutdown scripts, and one has been
111✔
4634
        // provided, return an error because the feature is not supported.
111✔
4635
        if !remoteUpfrontShutdown && len(script) != 0 {
112✔
4636
                return nil, errUpfrontShutdownScriptNotSupported
1✔
4637
        }
1✔
4638

4639
        // If the peer does not support upfront shutdown, return an empty address.
4640
        if !remoteUpfrontShutdown {
213✔
4641
                return nil, nil
103✔
4642
        }
103✔
4643

4644
        // If the user has provided an script and the peer supports the feature,
4645
        // return it. Note that user set scripts override the enable upfront
4646
        // shutdown flag.
4647
        if len(script) > 0 {
12✔
4648
                return script, nil
5✔
4649
        }
5✔
4650

4651
        // If we do not have setting of upfront shutdown script enabled, return
4652
        // an empty script.
4653
        if !enableUpfrontShutdown {
9✔
4654
                return nil, nil
4✔
4655
        }
4✔
4656

4657
        // We can safely send a taproot address iff, both sides have negotiated
4658
        // the shutdown-any-segwit feature.
4659
        taprootOK := peer.RemoteFeatures().HasFeature(lnwire.ShutdownAnySegwitOptional) &&
1✔
4660
                peer.LocalFeatures().HasFeature(lnwire.ShutdownAnySegwitOptional)
1✔
4661

1✔
4662
        return getScript(taprootOK)
1✔
4663
}
4664

4665
// handleInitFundingMsg creates a channel reservation within the daemon's
4666
// wallet, then sends a funding request to the remote peer kicking off the
4667
// funding workflow.
4668
func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
59✔
4669
        var (
59✔
4670
                peerKey        = msg.Peer.IdentityKey()
59✔
4671
                localAmt       = msg.LocalFundingAmt
59✔
4672
                baseFee        = msg.BaseFee
59✔
4673
                feeRate        = msg.FeeRate
59✔
4674
                minHtlcIn      = msg.MinHtlcIn
59✔
4675
                remoteCsvDelay = msg.RemoteCsvDelay
59✔
4676
                maxValue       = msg.MaxValueInFlight
59✔
4677
                maxHtlcs       = msg.MaxHtlcs
59✔
4678
                maxCSV         = msg.MaxLocalCsv
59✔
4679
                chanReserve    = msg.RemoteChanReserve
59✔
4680
                outpoints      = msg.Outpoints
59✔
4681
        )
59✔
4682

59✔
4683
        // If no maximum CSV delay was set for this channel, we use our default
59✔
4684
        // value.
59✔
4685
        if maxCSV == 0 {
118✔
4686
                maxCSV = f.cfg.MaxLocalCSVDelay
59✔
4687
        }
59✔
4688

4689
        log.Infof("Initiating fundingRequest(local_amt=%v "+
59✔
4690
                "(subtract_fees=%v), push_amt=%v, chain_hash=%v, peer=%x, "+
59✔
4691
                "min_confs=%v)", localAmt, msg.SubtractFees, msg.PushAmt,
59✔
4692
                msg.ChainHash, peerKey.SerializeCompressed(), msg.MinConfs)
59✔
4693

59✔
4694
        // We set the channel flags to indicate whether we want this channel to
59✔
4695
        // be announced to the network.
59✔
4696
        var channelFlags lnwire.FundingFlag
59✔
4697
        if !msg.Private {
113✔
4698
                // This channel will be announced.
54✔
4699
                channelFlags = lnwire.FFAnnounceChannel
54✔
4700
        }
54✔
4701

4702
        // If the caller specified their own channel ID, then we'll use that.
4703
        // Otherwise we'll generate a fresh one as normal.  This will be used
4704
        // to track this reservation throughout its lifetime.
4705
        var chanID PendingChanID
59✔
4706
        if msg.PendingChanID == zeroID {
118✔
4707
                chanID = f.nextPendingChanID()
59✔
4708
        } else {
62✔
4709
                // If the user specified their own pending channel ID, then
3✔
4710
                // we'll ensure it doesn't collide with any existing pending
3✔
4711
                // channel ID.
3✔
4712
                chanID = msg.PendingChanID
3✔
4713
                if _, err := f.getReservationCtx(peerKey, chanID); err == nil {
3✔
4714
                        msg.Err <- fmt.Errorf("pendingChannelID(%x) "+
×
4715
                                "already present", chanID[:])
×
4716
                        return
×
4717
                }
×
4718
        }
4719

4720
        // Check whether the peer supports upfront shutdown, and get an address
4721
        // which should be used (either a user specified address or a new
4722
        // address from the wallet if our node is configured to set shutdown
4723
        // address by default).
4724
        shutdown, err := getUpfrontShutdownScript(
59✔
4725
                f.cfg.EnableUpfrontShutdown, msg.Peer, msg.ShutdownScript,
59✔
4726
                f.selectShutdownScript,
59✔
4727
        )
59✔
4728
        if err != nil {
59✔
4729
                msg.Err <- err
×
4730
                return
×
4731
        }
×
4732

4733
        // Initialize a funding reservation with the local wallet. If the
4734
        // wallet doesn't have enough funds to commit to this channel, then the
4735
        // request will fail, and be aborted.
4736
        //
4737
        // Before we init the channel, we'll also check to see what commitment
4738
        // format we can use with this peer. This is dependent on *both* us and
4739
        // the remote peer are signaling the proper feature bit.
4740
        chanType, commitType, err := negotiateCommitmentType(
59✔
4741
                msg.ChannelType, msg.Peer.LocalFeatures(),
59✔
4742
                msg.Peer.RemoteFeatures(),
59✔
4743
        )
59✔
4744
        if err != nil {
62✔
4745
                log.Errorf("channel type negotiation failed: %v", err)
3✔
4746
                msg.Err <- err
3✔
4747
                return
3✔
4748
        }
3✔
4749

4750
        var (
59✔
4751
                zeroConf bool
59✔
4752
                scid     bool
59✔
4753
        )
59✔
4754

59✔
4755
        if chanType != nil {
66✔
4756
                // Check if the returned chanType includes either the zero-conf
7✔
4757
                // or scid-alias bits.
7✔
4758
                featureVec := lnwire.RawFeatureVector(*chanType)
7✔
4759
                zeroConf = featureVec.IsSet(lnwire.ZeroConfRequired)
7✔
4760
                scid = featureVec.IsSet(lnwire.ScidAliasRequired)
7✔
4761

7✔
4762
                // The option-scid-alias channel type for a public channel is
7✔
4763
                // disallowed.
7✔
4764
                if scid && !msg.Private {
7✔
4765
                        err = fmt.Errorf("option-scid-alias chantype for " +
×
4766
                                "public channel")
×
4767
                        log.Error(err)
×
4768
                        msg.Err <- err
×
4769

×
4770
                        return
×
4771
                }
×
4772
        }
4773

4774
        // First, we'll query the fee estimator for a fee that should get the
4775
        // commitment transaction confirmed by the next few blocks (conf target
4776
        // of 3). We target the near blocks here to ensure that we'll be able
4777
        // to execute a timely unilateral channel closure if needed.
4778
        commitFeePerKw, err := f.cfg.FeeEstimator.EstimateFeePerKW(3)
59✔
4779
        if err != nil {
59✔
4780
                msg.Err <- err
×
4781
                return
×
4782
        }
×
4783

4784
        // For anchor channels cap the initial commit fee rate at our defined
4785
        // maximum.
4786
        if commitType.HasAnchors() &&
59✔
4787
                commitFeePerKw > f.cfg.MaxAnchorsCommitFeeRate {
66✔
4788

7✔
4789
                commitFeePerKw = f.cfg.MaxAnchorsCommitFeeRate
7✔
4790
        }
7✔
4791

4792
        var scidFeatureVal bool
59✔
4793
        if hasFeatures(
59✔
4794
                msg.Peer.LocalFeatures(), msg.Peer.RemoteFeatures(),
59✔
4795
                lnwire.ScidAliasOptional,
59✔
4796
        ) {
65✔
4797

6✔
4798
                scidFeatureVal = true
6✔
4799
        }
6✔
4800

4801
        // At this point, if we have an AuxFundingController active, we'll check
4802
        // to see if we have a special tapscript root to use in our MuSig2
4803
        // funding output.
4804
        tapscriptRoot, err := fn.MapOptionZ(
59✔
4805
                f.cfg.AuxFundingController,
59✔
4806
                func(c AuxFundingController) AuxTapscriptResult {
59✔
4807
                        return c.DeriveTapscriptRoot(chanID)
×
4808
                },
×
4809
        ).Unpack()
4810
        if err != nil {
59✔
4811
                err = fmt.Errorf("error deriving tapscript root: %w", err)
×
4812
                log.Error(err)
×
4813
                msg.Err <- err
×
4814

×
4815
                return
×
4816
        }
×
4817

4818
        req := &lnwallet.InitFundingReserveMsg{
59✔
4819
                ChainHash:         &msg.ChainHash,
59✔
4820
                PendingChanID:     chanID,
59✔
4821
                NodeID:            peerKey,
59✔
4822
                NodeAddr:          msg.Peer.Address(),
59✔
4823
                SubtractFees:      msg.SubtractFees,
59✔
4824
                LocalFundingAmt:   localAmt,
59✔
4825
                RemoteFundingAmt:  0,
59✔
4826
                FundUpToMaxAmt:    msg.FundUpToMaxAmt,
59✔
4827
                MinFundAmt:        msg.MinFundAmt,
59✔
4828
                RemoteChanReserve: chanReserve,
59✔
4829
                Outpoints:         outpoints,
59✔
4830
                CommitFeePerKw:    commitFeePerKw,
59✔
4831
                FundingFeePerKw:   msg.FundingFeePerKw,
59✔
4832
                PushMSat:          msg.PushAmt,
59✔
4833
                Flags:             channelFlags,
59✔
4834
                MinConfs:          msg.MinConfs,
59✔
4835
                CommitType:        commitType,
59✔
4836
                ChanFunder:        msg.ChanFunder,
59✔
4837
                // Unconfirmed Utxos which are marked by the sweeper subsystem
59✔
4838
                // are excluded from the coin selection because they are not
59✔
4839
                // final and can be RBFed by the sweeper subsystem.
59✔
4840
                AllowUtxoForFunding: func(u lnwallet.Utxo) bool {
119✔
4841
                        // Utxos with at least 1 confirmation are safe to use
60✔
4842
                        // for channel openings because they don't bare the risk
60✔
4843
                        // of being replaced (BIP 125 RBF).
60✔
4844
                        if u.Confirmations > 0 {
63✔
4845
                                return true
3✔
4846
                        }
3✔
4847

4848
                        // Query the sweeper storage to make sure we don't use
4849
                        // an unconfirmed utxo still in use by the sweeper
4850
                        // subsystem.
4851
                        return !f.cfg.IsSweeperOutpoint(u.OutPoint)
60✔
4852
                },
4853
                ZeroConf:         zeroConf,
4854
                OptionScidAlias:  scid,
4855
                ScidAliasFeature: scidFeatureVal,
4856
                Memo:             msg.Memo,
4857
                TapscriptRoot:    tapscriptRoot,
4858
        }
4859

4860
        reservation, err := f.cfg.Wallet.InitChannelReservation(req)
59✔
4861
        if err != nil {
62✔
4862
                msg.Err <- err
3✔
4863
                return
3✔
4864
        }
3✔
4865

4866
        if zeroConf {
64✔
4867
                // Store the alias for zero-conf channels in the underlying
5✔
4868
                // partial channel state.
5✔
4869
                aliasScid, err := f.cfg.AliasManager.RequestAlias()
5✔
4870
                if err != nil {
5✔
4871
                        msg.Err <- err
×
4872
                        return
×
4873
                }
×
4874

4875
                reservation.AddAlias(aliasScid)
5✔
4876
        }
4877

4878
        // Set our upfront shutdown address in the existing reservation.
4879
        reservation.SetOurUpfrontShutdown(shutdown)
59✔
4880

59✔
4881
        // Now that we have successfully reserved funds for this channel in the
59✔
4882
        // wallet, we can fetch the final channel capacity. This is done at
59✔
4883
        // this point since the final capacity might change in case of
59✔
4884
        // SubtractFees=true.
59✔
4885
        capacity := reservation.Capacity()
59✔
4886

59✔
4887
        log.Infof("Target commit tx sat/kw for pendingID(%x): %v", chanID,
59✔
4888
                int64(commitFeePerKw))
59✔
4889

59✔
4890
        // If the remote CSV delay was not set in the open channel request,
59✔
4891
        // we'll use the RequiredRemoteDelay closure to compute the delay we
59✔
4892
        // require given the total amount of funds within the channel.
59✔
4893
        if remoteCsvDelay == 0 {
117✔
4894
                remoteCsvDelay = f.cfg.RequiredRemoteDelay(capacity)
58✔
4895
        }
58✔
4896

4897
        // If no minimum HTLC value was specified, use the default one.
4898
        if minHtlcIn == 0 {
117✔
4899
                minHtlcIn = f.cfg.DefaultMinHtlcIn
58✔
4900
        }
58✔
4901

4902
        // If no max value was specified, use the default one.
4903
        if maxValue == 0 {
117✔
4904
                maxValue = f.cfg.RequiredRemoteMaxValue(capacity)
58✔
4905
        }
58✔
4906

4907
        if maxHtlcs == 0 {
118✔
4908
                maxHtlcs = f.cfg.RequiredRemoteMaxHTLCs(capacity)
59✔
4909
        }
59✔
4910

4911
        // Once the reservation has been created, and indexed, queue a funding
4912
        // request to the remote peer, kicking off the funding workflow.
4913
        ourContribution := reservation.OurContribution()
59✔
4914

59✔
4915
        // Prepare the optional channel fee values from the initFundingMsg. If
59✔
4916
        // useBaseFee or useFeeRate are false the client did not provide fee
59✔
4917
        // values hence we assume default fee settings from the config.
59✔
4918
        forwardingPolicy := f.defaultForwardingPolicy(
59✔
4919
                ourContribution.ChannelStateBounds,
59✔
4920
        )
59✔
4921
        if baseFee != nil {
63✔
4922
                forwardingPolicy.BaseFee = lnwire.MilliSatoshi(*baseFee)
4✔
4923
        }
4✔
4924

4925
        if feeRate != nil {
63✔
4926
                forwardingPolicy.FeeRate = lnwire.MilliSatoshi(*feeRate)
4✔
4927
        }
4✔
4928

4929
        // Fetch our dust limit which is part of the default channel
4930
        // constraints, and log it.
4931
        ourDustLimit := ourContribution.DustLimit
59✔
4932

59✔
4933
        log.Infof("Dust limit for pendingID(%x): %v", chanID, ourDustLimit)
59✔
4934

59✔
4935
        // If the channel reserve is not specified, then we calculate an
59✔
4936
        // appropriate amount here.
59✔
4937
        if chanReserve == 0 {
114✔
4938
                chanReserve = f.cfg.RequiredRemoteChanReserve(
55✔
4939
                        capacity, ourDustLimit,
55✔
4940
                )
55✔
4941
        }
55✔
4942

4943
        // If a pending channel map for this peer isn't already created, then
4944
        // we create one, ultimately allowing us to track this pending
4945
        // reservation within the target peer.
4946
        peerIDKey := newSerializedKey(peerKey)
59✔
4947
        f.resMtx.Lock()
59✔
4948
        if _, ok := f.activeReservations[peerIDKey]; !ok {
111✔
4949
                f.activeReservations[peerIDKey] = make(pendingChannels)
52✔
4950
        }
52✔
4951

4952
        resCtx := &reservationWithCtx{
59✔
4953
                chanAmt:           capacity,
59✔
4954
                forwardingPolicy:  *forwardingPolicy,
59✔
4955
                remoteCsvDelay:    remoteCsvDelay,
59✔
4956
                remoteMinHtlc:     minHtlcIn,
59✔
4957
                remoteMaxValue:    maxValue,
59✔
4958
                remoteMaxHtlcs:    maxHtlcs,
59✔
4959
                remoteChanReserve: chanReserve,
59✔
4960
                maxLocalCsv:       maxCSV,
59✔
4961
                channelType:       chanType,
59✔
4962
                reservation:       reservation,
59✔
4963
                peer:              msg.Peer,
59✔
4964
                updates:           msg.Updates,
59✔
4965
                err:               msg.Err,
59✔
4966
        }
59✔
4967
        f.activeReservations[peerIDKey][chanID] = resCtx
59✔
4968
        f.resMtx.Unlock()
59✔
4969

59✔
4970
        // Update the timestamp once the InitFundingMsg has been handled.
59✔
4971
        defer resCtx.updateTimestamp()
59✔
4972

59✔
4973
        // Check the sanity of the selected channel constraints.
59✔
4974
        bounds := &channeldb.ChannelStateBounds{
59✔
4975
                ChanReserve:      chanReserve,
59✔
4976
                MaxPendingAmount: maxValue,
59✔
4977
                MinHTLC:          minHtlcIn,
59✔
4978
                MaxAcceptedHtlcs: maxHtlcs,
59✔
4979
        }
59✔
4980
        commitParams := &channeldb.CommitmentParams{
59✔
4981
                DustLimit: ourDustLimit,
59✔
4982
                CsvDelay:  remoteCsvDelay,
59✔
4983
        }
59✔
4984
        err = lnwallet.VerifyConstraints(
59✔
4985
                bounds, commitParams, resCtx.maxLocalCsv, capacity,
59✔
4986
        )
59✔
4987
        if err != nil {
61✔
4988
                _, reserveErr := f.cancelReservationCtx(peerKey, chanID, false)
2✔
4989
                if reserveErr != nil {
2✔
4990
                        log.Errorf("unable to cancel reservation: %v",
×
4991
                                reserveErr)
×
4992
                }
×
4993

4994
                msg.Err <- err
2✔
4995
                return
2✔
4996
        }
4997

4998
        // When opening a script enforced channel lease, include the required
4999
        // expiry TLV record in our proposal.
5000
        var leaseExpiry *lnwire.LeaseExpiry
57✔
5001
        if commitType == lnwallet.CommitmentTypeScriptEnforcedLease {
60✔
5002
                leaseExpiry = new(lnwire.LeaseExpiry)
3✔
5003
                *leaseExpiry = lnwire.LeaseExpiry(reservation.LeaseExpiry())
3✔
5004
        }
3✔
5005

5006
        log.Infof("Starting funding workflow with %v for pending_id(%x), "+
57✔
5007
                "committype=%v", msg.Peer.Address(), chanID, commitType)
57✔
5008

57✔
5009
        reservation.SetState(lnwallet.SentOpenChannel)
57✔
5010

57✔
5011
        fundingOpen := lnwire.OpenChannel{
57✔
5012
                ChainHash:             *f.cfg.Wallet.Cfg.NetParams.GenesisHash,
57✔
5013
                PendingChannelID:      chanID,
57✔
5014
                FundingAmount:         capacity,
57✔
5015
                PushAmount:            msg.PushAmt,
57✔
5016
                DustLimit:             ourDustLimit,
57✔
5017
                MaxValueInFlight:      maxValue,
57✔
5018
                ChannelReserve:        chanReserve,
57✔
5019
                HtlcMinimum:           minHtlcIn,
57✔
5020
                FeePerKiloWeight:      uint32(commitFeePerKw),
57✔
5021
                CsvDelay:              remoteCsvDelay,
57✔
5022
                MaxAcceptedHTLCs:      maxHtlcs,
57✔
5023
                FundingKey:            ourContribution.MultiSigKey.PubKey,
57✔
5024
                RevocationPoint:       ourContribution.RevocationBasePoint.PubKey,
57✔
5025
                PaymentPoint:          ourContribution.PaymentBasePoint.PubKey,
57✔
5026
                HtlcPoint:             ourContribution.HtlcBasePoint.PubKey,
57✔
5027
                DelayedPaymentPoint:   ourContribution.DelayBasePoint.PubKey,
57✔
5028
                FirstCommitmentPoint:  ourContribution.FirstCommitmentPoint,
57✔
5029
                ChannelFlags:          channelFlags,
57✔
5030
                UpfrontShutdownScript: shutdown,
57✔
5031
                ChannelType:           chanType,
57✔
5032
                LeaseExpiry:           leaseExpiry,
57✔
5033
        }
57✔
5034

57✔
5035
        if commitType.IsTaproot() {
62✔
5036
                fundingOpen.LocalNonce = lnwire.SomeMusig2Nonce(
5✔
5037
                        ourContribution.LocalNonce.PubNonce,
5✔
5038
                )
5✔
5039
        }
5✔
5040

5041
        if err := msg.Peer.SendMessage(true, &fundingOpen); err != nil {
57✔
5042
                e := fmt.Errorf("unable to send funding request message: %w",
×
5043
                        err)
×
5044
                log.Errorf(e.Error())
×
5045

×
5046
                // Since we were unable to send the initial message to the peer
×
5047
                // and start the funding flow, we'll cancel this reservation.
×
5048
                _, err := f.cancelReservationCtx(peerKey, chanID, false)
×
5049
                if err != nil {
×
5050
                        log.Errorf("unable to cancel reservation: %v", err)
×
5051
                }
×
5052

5053
                msg.Err <- e
×
5054
                return
×
5055
        }
5056
}
5057

5058
// handleWarningMsg processes the warning which was received from remote peer.
5059
func (f *Manager) handleWarningMsg(peer lnpeer.Peer, msg *lnwire.Warning) {
42✔
5060
        log.Warnf("received warning message from peer %x: %v",
42✔
5061
                peer.IdentityKey().SerializeCompressed(), msg.Warning())
42✔
5062
}
42✔
5063

5064
// handleErrorMsg processes the error which was received from remote peer,
5065
// depending on the type of error we should do different clean up steps and
5066
// inform the user about it.
5067
func (f *Manager) handleErrorMsg(peer lnpeer.Peer, msg *lnwire.Error) {
3✔
5068
        chanID := msg.ChanID
3✔
5069
        peerKey := peer.IdentityKey()
3✔
5070

3✔
5071
        // First, we'll attempt to retrieve and cancel the funding workflow
3✔
5072
        // that this error was tied to. If we're unable to do so, then we'll
3✔
5073
        // exit early as this was an unwarranted error.
3✔
5074
        resCtx, err := f.cancelReservationCtx(peerKey, chanID, true)
3✔
5075
        if err != nil {
3✔
5076
                log.Warnf("Received error for non-existent funding "+
×
5077
                        "flow: %v (%v)", err, msg.Error())
×
5078
                return
×
5079
        }
×
5080

5081
        // If we did indeed find the funding workflow, then we'll return the
5082
        // error back to the caller (if any), and cancel the workflow itself.
5083
        fundingErr := fmt.Errorf("received funding error from %x: %v",
3✔
5084
                peerKey.SerializeCompressed(), msg.Error(),
3✔
5085
        )
3✔
5086
        log.Errorf(fundingErr.Error())
3✔
5087

3✔
5088
        // If this was a PSBT funding flow, the remote likely timed out because
3✔
5089
        // we waited too long. Return a nice error message to the user in that
3✔
5090
        // case so the user knows what's the problem.
3✔
5091
        if resCtx.reservation.IsPsbt() {
6✔
5092
                fundingErr = fmt.Errorf("%w: %v", chanfunding.ErrRemoteCanceled,
3✔
5093
                        fundingErr)
3✔
5094
        }
3✔
5095

5096
        resCtx.err <- fundingErr
3✔
5097
}
5098

5099
// pruneZombieReservations loops through all pending reservations and fails the
5100
// funding flow for any reservations that have not been updated since the
5101
// ReservationTimeout and are not locked waiting for the funding transaction.
5102
func (f *Manager) pruneZombieReservations() {
6✔
5103
        zombieReservations := make(pendingChannels)
6✔
5104

6✔
5105
        f.resMtx.RLock()
6✔
5106
        for _, pendingReservations := range f.activeReservations {
12✔
5107
                for pendingChanID, resCtx := range pendingReservations {
12✔
5108
                        if resCtx.isLocked() {
6✔
5109
                                continue
×
5110
                        }
5111

5112
                        // We don't want to expire PSBT funding reservations.
5113
                        // These reservations are always initiated by us and the
5114
                        // remote peer is likely going to cancel them after some
5115
                        // idle time anyway. So no need for us to also prune
5116
                        // them.
5117
                        sinceLastUpdate := time.Since(resCtx.lastUpdated)
6✔
5118
                        isExpired := sinceLastUpdate > f.cfg.ReservationTimeout
6✔
5119
                        if !resCtx.reservation.IsPsbt() && isExpired {
12✔
5120
                                zombieReservations[pendingChanID] = resCtx
6✔
5121
                        }
6✔
5122
                }
5123
        }
5124
        f.resMtx.RUnlock()
6✔
5125

6✔
5126
        for pendingChanID, resCtx := range zombieReservations {
12✔
5127
                err := fmt.Errorf("reservation timed out waiting for peer "+
6✔
5128
                        "(peer_id:%x, chan_id:%x)",
6✔
5129
                        resCtx.peer.IdentityKey().SerializeCompressed(),
6✔
5130
                        pendingChanID[:])
6✔
5131
                log.Warnf(err.Error())
6✔
5132

6✔
5133
                chanID := lnwire.NewChanIDFromOutPoint(
6✔
5134
                        *resCtx.reservation.FundingOutpoint(),
6✔
5135
                )
6✔
5136

6✔
5137
                // Create channel identifier and set the channel ID.
6✔
5138
                cid := newChanIdentifier(pendingChanID)
6✔
5139
                cid.setChanID(chanID)
6✔
5140

6✔
5141
                f.failFundingFlow(resCtx.peer, cid, err)
6✔
5142
        }
6✔
5143
}
5144

5145
// cancelReservationCtx does all needed work in order to securely cancel the
5146
// reservation.
5147
func (f *Manager) cancelReservationCtx(peerKey *btcec.PublicKey,
5148
        pendingChanID PendingChanID,
5149
        byRemote bool) (*reservationWithCtx, error) {
26✔
5150

26✔
5151
        log.Infof("Cancelling funding reservation for node_key=%x, "+
26✔
5152
                "chan_id=%x", peerKey.SerializeCompressed(), pendingChanID[:])
26✔
5153

26✔
5154
        peerIDKey := newSerializedKey(peerKey)
26✔
5155
        f.resMtx.Lock()
26✔
5156
        defer f.resMtx.Unlock()
26✔
5157

26✔
5158
        nodeReservations, ok := f.activeReservations[peerIDKey]
26✔
5159
        if !ok {
36✔
5160
                // No reservations for this node.
10✔
5161
                return nil, errors.Errorf("no active reservations for peer(%x)",
10✔
5162
                        peerIDKey[:])
10✔
5163
        }
10✔
5164

5165
        ctx, ok := nodeReservations[pendingChanID]
19✔
5166
        if !ok {
21✔
5167
                return nil, errors.Errorf("unknown channel (id: %x) for "+
2✔
5168
                        "peer(%x)", pendingChanID[:], peerIDKey[:])
2✔
5169
        }
2✔
5170

5171
        // If the reservation was a PSBT funding flow and it was canceled by the
5172
        // remote peer, then we need to thread through a different error message
5173
        // to the subroutine that's waiting for the user input so it can return
5174
        // a nice error message to the user.
5175
        if ctx.reservation.IsPsbt() && byRemote {
20✔
5176
                ctx.reservation.RemoteCanceled()
3✔
5177
        }
3✔
5178

5179
        if err := ctx.reservation.Cancel(); err != nil {
17✔
5180
                return nil, errors.Errorf("unable to cancel reservation: %v",
×
5181
                        err)
×
5182
        }
×
5183

5184
        delete(nodeReservations, pendingChanID)
17✔
5185

17✔
5186
        // If this was the last active reservation for this peer, delete the
17✔
5187
        // peer's entry altogether.
17✔
5188
        if len(nodeReservations) == 0 {
34✔
5189
                delete(f.activeReservations, peerIDKey)
17✔
5190
        }
17✔
5191
        return ctx, nil
17✔
5192
}
5193

5194
// deleteReservationCtx deletes the reservation uniquely identified by the
5195
// target public key of the peer, and the specified pending channel ID.
5196
func (f *Manager) deleteReservationCtx(peerKey *btcec.PublicKey,
5197
        pendingChanID PendingChanID) {
57✔
5198

57✔
5199
        peerIDKey := newSerializedKey(peerKey)
57✔
5200
        f.resMtx.Lock()
57✔
5201
        defer f.resMtx.Unlock()
57✔
5202

57✔
5203
        nodeReservations, ok := f.activeReservations[peerIDKey]
57✔
5204
        if !ok {
57✔
5205
                // No reservations for this node.
×
5206
                return
×
5207
        }
×
5208
        delete(nodeReservations, pendingChanID)
57✔
5209

57✔
5210
        // If this was the last active reservation for this peer, delete the
57✔
5211
        // peer's entry altogether.
57✔
5212
        if len(nodeReservations) == 0 {
107✔
5213
                delete(f.activeReservations, peerIDKey)
50✔
5214
        }
50✔
5215
}
5216

5217
// getReservationCtx returns the reservation context for a particular pending
5218
// channel ID for a target peer.
5219
func (f *Manager) getReservationCtx(peerKey *btcec.PublicKey,
5220
        pendingChanID PendingChanID) (*reservationWithCtx, error) {
91✔
5221

91✔
5222
        peerIDKey := newSerializedKey(peerKey)
91✔
5223
        f.resMtx.RLock()
91✔
5224
        resCtx, ok := f.activeReservations[peerIDKey][pendingChanID]
91✔
5225
        f.resMtx.RUnlock()
91✔
5226

91✔
5227
        if !ok {
94✔
5228
                return nil, errors.Errorf("unknown channel (id: %x) for "+
3✔
5229
                        "peer(%x)", pendingChanID[:], peerIDKey[:])
3✔
5230
        }
3✔
5231

5232
        return resCtx, nil
91✔
5233
}
5234

5235
// IsPendingChannel returns a boolean indicating whether the channel identified
5236
// by the pendingChanID and given peer is pending, meaning it is in the process
5237
// of being funded. After the funding transaction has been confirmed, the
5238
// channel will receive a new, permanent channel ID, and will no longer be
5239
// considered pending.
5240
func (f *Manager) IsPendingChannel(pendingChanID PendingChanID,
5241
        peer lnpeer.Peer) bool {
3✔
5242

3✔
5243
        peerIDKey := newSerializedKey(peer.IdentityKey())
3✔
5244
        f.resMtx.RLock()
3✔
5245
        _, ok := f.activeReservations[peerIDKey][pendingChanID]
3✔
5246
        f.resMtx.RUnlock()
3✔
5247

3✔
5248
        return ok
3✔
5249
}
3✔
5250

5251
func copyPubKey(pub *btcec.PublicKey) *btcec.PublicKey {
378✔
5252
        var tmp btcec.JacobianPoint
378✔
5253
        pub.AsJacobian(&tmp)
378✔
5254
        tmp.ToAffine()
378✔
5255
        return btcec.NewPublicKey(&tmp.X, &tmp.Y)
378✔
5256
}
378✔
5257

5258
// defaultForwardingPolicy returns the default forwarding policy based on the
5259
// default routing policy and our local channel constraints.
5260
func (f *Manager) defaultForwardingPolicy(
5261
        bounds channeldb.ChannelStateBounds) *models.ForwardingPolicy {
105✔
5262

105✔
5263
        return &models.ForwardingPolicy{
105✔
5264
                MinHTLCOut:    bounds.MinHTLC,
105✔
5265
                MaxHTLC:       bounds.MaxPendingAmount,
105✔
5266
                BaseFee:       f.cfg.DefaultRoutingPolicy.BaseFee,
105✔
5267
                FeeRate:       f.cfg.DefaultRoutingPolicy.FeeRate,
105✔
5268
                TimeLockDelta: f.cfg.DefaultRoutingPolicy.TimeLockDelta,
105✔
5269
        }
105✔
5270
}
105✔
5271

5272
// saveInitialForwardingPolicy saves the forwarding policy for the provided
5273
// chanPoint in the channelOpeningStateBucket.
5274
func (f *Manager) saveInitialForwardingPolicy(chanID lnwire.ChannelID,
5275
        forwardingPolicy *models.ForwardingPolicy) error {
70✔
5276

70✔
5277
        return f.cfg.ChannelDB.SaveInitialForwardingPolicy(
70✔
5278
                chanID, forwardingPolicy,
70✔
5279
        )
70✔
5280
}
70✔
5281

5282
// getInitialForwardingPolicy fetches the initial forwarding policy for a given
5283
// channel id from the database which will be applied during the channel
5284
// announcement phase.
5285
func (f *Manager) getInitialForwardingPolicy(
5286
        chanID lnwire.ChannelID) (*models.ForwardingPolicy, error) {
97✔
5287

97✔
5288
        return f.cfg.ChannelDB.GetInitialForwardingPolicy(chanID)
97✔
5289
}
97✔
5290

5291
// deleteInitialForwardingPolicy removes channel fees for this chanID from
5292
// the database.
5293
func (f *Manager) deleteInitialForwardingPolicy(chanID lnwire.ChannelID) error {
27✔
5294
        return f.cfg.ChannelDB.DeleteInitialForwardingPolicy(chanID)
27✔
5295
}
27✔
5296

5297
// saveChannelOpeningState saves the channelOpeningState for the provided
5298
// chanPoint to the channelOpeningStateBucket.
5299
func (f *Manager) saveChannelOpeningState(chanPoint *wire.OutPoint,
5300
        state channelOpeningState, shortChanID *lnwire.ShortChannelID) error {
95✔
5301

95✔
5302
        var outpointBytes bytes.Buffer
95✔
5303
        if err := WriteOutpoint(&outpointBytes, chanPoint); err != nil {
95✔
5304
                return err
×
5305
        }
×
5306

5307
        // Save state and the uint64 representation of the shortChanID
5308
        // for later use.
5309
        scratch := make([]byte, 10)
95✔
5310
        byteOrder.PutUint16(scratch[:2], uint16(state))
95✔
5311
        byteOrder.PutUint64(scratch[2:], shortChanID.ToUint64())
95✔
5312

95✔
5313
        return f.cfg.ChannelDB.SaveChannelOpeningState(
95✔
5314
                outpointBytes.Bytes(), scratch,
95✔
5315
        )
95✔
5316
}
5317

5318
// getChannelOpeningState fetches the channelOpeningState for the provided
5319
// chanPoint from the database, or returns ErrChannelNotFound if the channel
5320
// is not found.
5321
func (f *Manager) getChannelOpeningState(chanPoint *wire.OutPoint) (
5322
        channelOpeningState, *lnwire.ShortChannelID, error) {
254✔
5323

254✔
5324
        var outpointBytes bytes.Buffer
254✔
5325
        if err := WriteOutpoint(&outpointBytes, chanPoint); err != nil {
254✔
5326
                return 0, nil, err
×
5327
        }
×
5328

5329
        value, err := f.cfg.ChannelDB.GetChannelOpeningState(
254✔
5330
                outpointBytes.Bytes(),
254✔
5331
        )
254✔
5332
        if err != nil {
304✔
5333
                return 0, nil, err
50✔
5334
        }
50✔
5335

5336
        state := channelOpeningState(byteOrder.Uint16(value[:2]))
207✔
5337
        shortChanID := lnwire.NewShortChanIDFromInt(byteOrder.Uint64(value[2:]))
207✔
5338
        return state, &shortChanID, nil
207✔
5339
}
5340

5341
// deleteChannelOpeningState removes any state for chanPoint from the database.
5342
func (f *Manager) deleteChannelOpeningState(chanPoint *wire.OutPoint) error {
27✔
5343
        var outpointBytes bytes.Buffer
27✔
5344
        if err := WriteOutpoint(&outpointBytes, chanPoint); err != nil {
27✔
5345
                return err
×
5346
        }
×
5347

5348
        return f.cfg.ChannelDB.DeleteChannelOpeningState(
27✔
5349
                outpointBytes.Bytes(),
27✔
5350
        )
27✔
5351
}
5352

5353
// selectShutdownScript selects the shutdown script we should send to the peer.
5354
// If we can use taproot, then we prefer that, otherwise we'll use a p2wkh
5355
// script.
5356
func (f *Manager) selectShutdownScript(taprootOK bool,
5357
) (lnwire.DeliveryAddress, error) {
×
5358

×
5359
        addrType := lnwallet.WitnessPubKey
×
5360
        if taprootOK {
×
5361
                addrType = lnwallet.TaprootPubkey
×
5362
        }
×
5363

5364
        addr, err := f.cfg.Wallet.NewAddress(
×
5365
                addrType, false, lnwallet.DefaultAccountName,
×
5366
        )
×
5367
        if err != nil {
×
5368
                return nil, err
×
5369
        }
×
5370

5371
        return txscript.PayToAddrScript(addr)
×
5372
}
5373

5374
// waitForPeerOnline blocks until the peer specified by peerPubkey comes online
5375
// and then returns the online peer.
5376
func (f *Manager) waitForPeerOnline(peerPubkey *btcec.PublicKey) (lnpeer.Peer,
5377
        error) {
108✔
5378

108✔
5379
        peerChan := make(chan lnpeer.Peer, 1)
108✔
5380

108✔
5381
        var peerKey [33]byte
108✔
5382
        copy(peerKey[:], peerPubkey.SerializeCompressed())
108✔
5383

108✔
5384
        f.cfg.NotifyWhenOnline(peerKey, peerChan)
108✔
5385

108✔
5386
        var peer lnpeer.Peer
108✔
5387
        select {
108✔
5388
        case peer = <-peerChan:
107✔
5389
        case <-f.quit:
1✔
5390
                return peer, ErrFundingManagerShuttingDown
1✔
5391
        }
5392
        return peer, nil
107✔
5393
}
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