-
Notifications
You must be signed in to change notification settings - Fork 264
/
ChannelOpenDualFunded.scala
759 lines (710 loc) · 47.9 KB
/
ChannelOpenDualFunded.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
* Copyright 2022 ACINQ SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.acinq.eclair.channel.fsm
import akka.actor.typed.scaladsl.adapter.{ClassicActorContextOps, actorRefAdapter}
import com.softwaremill.quicklens.{ModifyPimp, QuicklensAt}
import fr.acinq.bitcoin.scalacompat.SatoshiLong
import fr.acinq.eclair.blockchain.bitcoind.ZmqWatcher._
import fr.acinq.eclair.channel._
import fr.acinq.eclair.channel.fsm.Channel._
import fr.acinq.eclair.channel.fund.InteractiveTxBuilder.{FullySignedSharedTransaction, InteractiveTxParams, PartiallySignedSharedTransaction, RequireConfirmedInputs}
import fr.acinq.eclair.channel.fund.{InteractiveTxBuilder, InteractiveTxSigningSession}
import fr.acinq.eclair.channel.publish.TxPublisher.SetChannelId
import fr.acinq.eclair.crypto.ShaChain
import fr.acinq.eclair.io.Peer.OpenChannelResponse
import fr.acinq.eclair.wire.protocol._
import fr.acinq.eclair.{MilliSatoshiLong, RealShortChannelId, ToMilliSatoshiConversion, UInt64, randomBytes32}
/**
* Created by t-bast on 19/04/2022.
*/
trait ChannelOpenDualFunded extends DualFundingHandlers with ErrorHandlers {
this: Channel =>
/*
INITIATOR NON_INITIATOR
| |
| open_channel2 | WAIT_FOR_OPEN_DUAL_FUNDED_CHANNEL
|-------------------------------->|
WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL | |
| accept_channel2 |
|<--------------------------------|
WAIT_FOR_DUAL_FUNDING_CREATED | | WAIT_FOR_DUAL_FUNDING_CREATED
| <interactive-tx protocol> |
| . |
| . |
| . |
| tx_complete |
|-------------------------------->|
| tx_complete |
|<--------------------------------|
WAIT_FOR_DUAL_FUNDING_SIGNED | | WAIT_FOR_DUAL_FUNDING_SIGNED
| commitment_signed |
|-------------------------------->|
| commitment_signed |
|<--------------------------------|
| tx_signatures |
|<--------------------------------|
| | WAIT_FOR_DUAL_FUNDING_CONFIRMED
| tx_signatures |
|-------------------------------->|
WAIT_FOR_DUAL_FUNDING_CONFIRMED | |
| tx_init_rbf |
|-------------------------------->|
| tx_ack_rbf |
|<--------------------------------|
| |
| <interactive-tx protocol> |
| . |
| . |
| . |
| tx_complete |
|-------------------------------->|
| tx_complete |
|<--------------------------------|
| |
| commitment_signed |
|-------------------------------->|
| commitment_signed |
|<--------------------------------|
| tx_signatures |
|<--------------------------------|
| tx_signatures |
|-------------------------------->|
| |
| <other rbf attempts> |
| . |
| . |
| . |
WAIT_FOR_DUAL_FUNDING_LOCKED | | WAIT_FOR_DUAL_FUNDING_LOCKED
| funding_locked funding_locked |
|---------------- ---------------|
| \/ |
| /\ |
|<--------------- -------------->|
NORMAL | | NORMAL
*/
when(WAIT_FOR_INIT_DUAL_FUNDED_CHANNEL)(handleExceptions {
case Event(input: INPUT_INIT_CHANNEL_INITIATOR, _) =>
val fundingPubKey = keyManager.fundingPublicKey(input.localParams.fundingKeyPath, fundingTxIndex = 0).publicKey
val channelKeyPath = keyManager.keyPath(input.localParams, input.channelConfig)
val upfrontShutdownScript_opt = input.localParams.upfrontShutdownScript_opt.map(scriptPubKey => ChannelTlv.UpfrontShutdownScriptTlv(scriptPubKey))
val tlvs: Set[OpenDualFundedChannelTlv] = Set(
upfrontShutdownScript_opt,
Some(ChannelTlv.ChannelTypeTlv(input.channelType)),
input.pushAmount_opt.map(amount => ChannelTlv.PushAmountTlv(amount)),
if (input.requireConfirmedInputs) Some(ChannelTlv.RequireConfirmedInputsTlv()) else None,
).flatten
val open = OpenDualFundedChannel(
chainHash = nodeParams.chainHash,
temporaryChannelId = input.temporaryChannelId,
fundingFeerate = input.fundingTxFeerate,
commitmentFeerate = input.commitTxFeerate,
fundingAmount = input.fundingAmount,
dustLimit = input.localParams.dustLimit,
maxHtlcValueInFlightMsat = UInt64(input.localParams.maxHtlcValueInFlightMsat.toLong),
htlcMinimum = input.localParams.htlcMinimum,
toSelfDelay = input.localParams.toSelfDelay,
maxAcceptedHtlcs = input.localParams.maxAcceptedHtlcs,
lockTime = nodeParams.currentBlockHeight.toLong,
fundingPubkey = fundingPubKey,
revocationBasepoint = keyManager.revocationPoint(channelKeyPath).publicKey,
paymentBasepoint = input.localParams.walletStaticPaymentBasepoint.getOrElse(keyManager.paymentPoint(channelKeyPath).publicKey),
delayedPaymentBasepoint = keyManager.delayedPaymentPoint(channelKeyPath).publicKey,
htlcBasepoint = keyManager.htlcPoint(channelKeyPath).publicKey,
firstPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 0),
secondPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1),
channelFlags = input.channelFlags,
tlvStream = TlvStream(tlvs))
goto(WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) using DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL(input, open) sending open
})
when(WAIT_FOR_OPEN_DUAL_FUNDED_CHANNEL)(handleExceptions {
case Event(open: OpenDualFundedChannel, d: DATA_WAIT_FOR_OPEN_DUAL_FUNDED_CHANNEL) =>
import d.init.{localParams, remoteInit}
Helpers.validateParamsDualFundedNonInitiator(nodeParams, d.init.channelType, open, remoteNodeId, localParams.initFeatures, remoteInit.features) match {
case Left(t) => handleLocalError(t, d, Some(open))
case Right((channelFeatures, remoteShutdownScript)) =>
context.system.eventStream.publish(ChannelCreated(self, peer, remoteNodeId, isInitiator = false, open.temporaryChannelId, open.commitmentFeerate, Some(open.fundingFeerate)))
val remoteParams = RemoteParams(
nodeId = remoteNodeId,
dustLimit = open.dustLimit,
maxHtlcValueInFlightMsat = open.maxHtlcValueInFlightMsat,
initialRequestedChannelReserve_opt = None, // channel reserve will be computed based on channel capacity
htlcMinimum = open.htlcMinimum,
toSelfDelay = open.toSelfDelay,
maxAcceptedHtlcs = open.maxAcceptedHtlcs,
revocationBasepoint = open.revocationBasepoint,
paymentBasepoint = open.paymentBasepoint,
delayedPaymentBasepoint = open.delayedPaymentBasepoint,
htlcBasepoint = open.htlcBasepoint,
initFeatures = remoteInit.features,
upfrontShutdownScript_opt = remoteShutdownScript)
log.debug("remote params: {}", remoteParams)
val localFundingPubkey = keyManager.fundingPublicKey(localParams.fundingKeyPath, fundingTxIndex = 0).publicKey
val channelKeyPath = keyManager.keyPath(localParams, d.init.channelConfig)
val revocationBasePoint = keyManager.revocationPoint(channelKeyPath).publicKey
// We've exchanged open_channel2 and accept_channel2, we now know the final channelId.
val channelId = Helpers.computeChannelId(open.revocationBasepoint, revocationBasePoint)
val channelParams = ChannelParams(channelId, d.init.channelConfig, channelFeatures, localParams, remoteParams, open.channelFlags)
val localAmount = d.init.fundingContribution_opt.getOrElse(0 sat)
val remoteAmount = open.fundingAmount
// At this point, the min_depth is an estimate and may change after we know exactly how our peer contributes
// to the funding transaction. Maybe they will contribute 0 satoshis to the shared output, but still add inputs
// and outputs.
val minDepth_opt = channelParams.minDepthFundee(nodeParams.channelConf.minDepthBlocks, localAmount + remoteAmount)
val upfrontShutdownScript_opt = localParams.upfrontShutdownScript_opt.map(scriptPubKey => ChannelTlv.UpfrontShutdownScriptTlv(scriptPubKey))
val tlvs: Set[AcceptDualFundedChannelTlv] = Set(
upfrontShutdownScript_opt,
Some(ChannelTlv.ChannelTypeTlv(d.init.channelType)),
d.init.pushAmount_opt.map(amount => ChannelTlv.PushAmountTlv(amount)),
if (nodeParams.channelConf.requireConfirmedInputsForDualFunding) Some(ChannelTlv.RequireConfirmedInputsTlv()) else None,
).flatten
val accept = AcceptDualFundedChannel(
temporaryChannelId = open.temporaryChannelId,
fundingAmount = localAmount,
dustLimit = localParams.dustLimit,
maxHtlcValueInFlightMsat = UInt64(localParams.maxHtlcValueInFlightMsat.toLong),
htlcMinimum = localParams.htlcMinimum,
minimumDepth = minDepth_opt.getOrElse(0),
toSelfDelay = localParams.toSelfDelay,
maxAcceptedHtlcs = localParams.maxAcceptedHtlcs,
fundingPubkey = localFundingPubkey,
revocationBasepoint = revocationBasePoint,
paymentBasepoint = localParams.walletStaticPaymentBasepoint.getOrElse(keyManager.paymentPoint(channelKeyPath).publicKey),
delayedPaymentBasepoint = keyManager.delayedPaymentPoint(channelKeyPath).publicKey,
htlcBasepoint = keyManager.htlcPoint(channelKeyPath).publicKey,
firstPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 0),
secondPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1),
tlvStream = TlvStream(tlvs))
peer ! ChannelIdAssigned(self, remoteNodeId, accept.temporaryChannelId, channelId) // we notify the peer asap so it knows how to route messages
txPublisher ! SetChannelId(remoteNodeId, channelId)
context.system.eventStream.publish(ChannelIdAssigned(self, remoteNodeId, accept.temporaryChannelId, channelId))
// We start the interactive-tx funding protocol.
val fundingParams = InteractiveTxParams(
channelId = channelId,
isInitiator = localParams.isInitiator,
localContribution = accept.fundingAmount,
remoteContribution = open.fundingAmount,
sharedInput_opt = None,
remoteFundingPubKey = open.fundingPubkey,
localOutputs = Nil,
lockTime = open.lockTime,
dustLimit = open.dustLimit.max(accept.dustLimit),
targetFeerate = open.fundingFeerate,
requireConfirmedInputs = RequireConfirmedInputs(forLocal = open.requireConfirmedInputs, forRemote = accept.requireConfirmedInputs)
)
val purpose = InteractiveTxBuilder.FundingTx(open.commitmentFeerate, open.firstPerCommitmentPoint, feeBudget_opt = None)
val txBuilder = context.spawnAnonymous(InteractiveTxBuilder(
randomBytes32(),
nodeParams, fundingParams,
channelParams, purpose,
localPushAmount = accept.pushAmount, remotePushAmount = open.pushAmount,
wallet))
txBuilder ! InteractiveTxBuilder.Start(self)
goto(WAIT_FOR_DUAL_FUNDING_CREATED) using DATA_WAIT_FOR_DUAL_FUNDING_CREATED(channelId, channelParams, open.secondPerCommitmentPoint, accept.pushAmount, open.pushAmount, txBuilder, deferred = None, replyTo_opt = None) sending accept
}
case Event(c: CloseCommand, d) => handleFastClose(c, d.channelId)
case Event(e: Error, d: DATA_WAIT_FOR_OPEN_DUAL_FUNDED_CHANNEL) => handleRemoteError(e, d)
case Event(INPUT_DISCONNECTED, _) => goto(CLOSED)
})
when(WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL)(handleExceptions {
case Event(accept: AcceptDualFundedChannel, d: DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) =>
import d.init.{localParams, remoteInit}
Helpers.validateParamsDualFundedInitiator(nodeParams, d.init.channelType, localParams.initFeatures, remoteInit.features, d.lastSent, accept) match {
case Left(t) =>
d.init.replyTo ! OpenChannelResponse.Rejected(t.getMessage)
handleLocalError(t, d, Some(accept))
case Right((channelFeatures, remoteShutdownScript)) =>
// We've exchanged open_channel2 and accept_channel2, we now know the final channelId.
val channelId = Helpers.computeChannelId(d.lastSent.revocationBasepoint, accept.revocationBasepoint)
peer ! ChannelIdAssigned(self, remoteNodeId, accept.temporaryChannelId, channelId) // we notify the peer asap so it knows how to route messages
txPublisher ! SetChannelId(remoteNodeId, channelId)
context.system.eventStream.publish(ChannelIdAssigned(self, remoteNodeId, accept.temporaryChannelId, channelId))
val remoteParams = RemoteParams(
nodeId = remoteNodeId,
dustLimit = accept.dustLimit,
maxHtlcValueInFlightMsat = accept.maxHtlcValueInFlightMsat,
initialRequestedChannelReserve_opt = None, // channel reserve will be computed based on channel capacity
htlcMinimum = accept.htlcMinimum,
toSelfDelay = accept.toSelfDelay,
maxAcceptedHtlcs = accept.maxAcceptedHtlcs,
revocationBasepoint = accept.revocationBasepoint,
paymentBasepoint = accept.paymentBasepoint,
delayedPaymentBasepoint = accept.delayedPaymentBasepoint,
htlcBasepoint = accept.htlcBasepoint,
initFeatures = remoteInit.features,
upfrontShutdownScript_opt = remoteShutdownScript)
log.debug("remote params: {}", remoteParams)
// We start the interactive-tx funding protocol.
val channelParams = ChannelParams(channelId, d.init.channelConfig, channelFeatures, localParams, remoteParams, d.lastSent.channelFlags)
val localAmount = d.lastSent.fundingAmount
val remoteAmount = accept.fundingAmount
val fundingParams = InteractiveTxParams(
channelId = channelId,
isInitiator = localParams.isInitiator,
localContribution = localAmount,
remoteContribution = remoteAmount,
sharedInput_opt = None,
remoteFundingPubKey = accept.fundingPubkey,
localOutputs = Nil,
lockTime = d.lastSent.lockTime,
dustLimit = d.lastSent.dustLimit.max(accept.dustLimit),
targetFeerate = d.lastSent.fundingFeerate,
requireConfirmedInputs = RequireConfirmedInputs(forLocal = accept.requireConfirmedInputs, forRemote = d.lastSent.requireConfirmedInputs)
)
val purpose = InteractiveTxBuilder.FundingTx(d.lastSent.commitmentFeerate, accept.firstPerCommitmentPoint, feeBudget_opt = d.init.fundingTxFeeBudget_opt)
val txBuilder = context.spawnAnonymous(InteractiveTxBuilder(
randomBytes32(),
nodeParams, fundingParams,
channelParams, purpose,
localPushAmount = d.lastSent.pushAmount, remotePushAmount = accept.pushAmount,
wallet))
txBuilder ! InteractiveTxBuilder.Start(self)
goto(WAIT_FOR_DUAL_FUNDING_CREATED) using DATA_WAIT_FOR_DUAL_FUNDING_CREATED(channelId, channelParams, accept.secondPerCommitmentPoint, d.lastSent.pushAmount, accept.pushAmount, txBuilder, deferred = None, replyTo_opt = Some(d.init.replyTo))
}
case Event(c: CloseCommand, d: DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) =>
d.init.replyTo ! OpenChannelResponse.Cancelled
handleFastClose(c, d.channelId)
case Event(e: Error, d: DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) =>
d.init.replyTo ! OpenChannelResponse.RemoteError(e.toAscii)
handleRemoteError(e, d)
case Event(INPUT_DISCONNECTED, d: DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) =>
d.init.replyTo ! OpenChannelResponse.Disconnected
goto(CLOSED)
case Event(TickChannelOpenTimeout, d: DATA_WAIT_FOR_ACCEPT_DUAL_FUNDED_CHANNEL) =>
d.init.replyTo ! OpenChannelResponse.TimedOut
goto(CLOSED)
})
when(WAIT_FOR_DUAL_FUNDING_CREATED)(handleExceptions {
case Event(msg: InteractiveTxMessage, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
msg match {
case msg: InteractiveTxConstructionMessage =>
d.txBuilder ! InteractiveTxBuilder.ReceiveMessage(msg)
stay()
case msg: TxAbort =>
log.info("our peer aborted the dual funding flow: ascii='{}' bin={}", msg.toAscii, msg.data)
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.RemoteError(msg.toAscii))
goto(CLOSED) sending TxAbort(d.channelId, DualFundingAborted(d.channelId).getMessage)
case _: TxSignatures =>
log.info("received unexpected tx_signatures")
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.Rejected(UnexpectedFundingSignatures(d.channelId).getMessage))
goto(CLOSED) sending TxAbort(d.channelId, UnexpectedFundingSignatures(d.channelId).getMessage)
case _: TxInitRbf =>
log.info("ignoring unexpected tx_init_rbf message")
stay() sending Warning(d.channelId, InvalidRbfAttempt(d.channelId).getMessage)
case _: TxAckRbf =>
log.info("ignoring unexpected tx_ack_rbf message")
stay() sending Warning(d.channelId, InvalidRbfAttempt(d.channelId).getMessage)
}
case Event(commitSig: CommitSig, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
log.debug("received their commit_sig, deferring message")
stay() using d.copy(deferred = Some(commitSig))
case Event(msg: InteractiveTxBuilder.Response, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) => msg match {
case InteractiveTxBuilder.SendMessage(_, msg) => stay() sending msg
case InteractiveTxBuilder.Succeeded(status, commitSig) =>
d.deferred.foreach(self ! _)
d.replyTo_opt.foreach(_ ! OpenChannelResponse.Created(d.channelId, status.fundingTx.txId, status.fundingTx.tx.localFees.truncateToSatoshi))
val d1 = DATA_WAIT_FOR_DUAL_FUNDING_SIGNED(d.channelParams, d.secondRemotePerCommitmentPoint, d.localPushAmount, d.remotePushAmount, status, None)
goto(WAIT_FOR_DUAL_FUNDING_SIGNED) using d1 storing() sending commitSig
case f: InteractiveTxBuilder.Failed =>
d.replyTo_opt.foreach(_ ! OpenChannelResponse.Rejected(f.cause.getMessage))
goto(CLOSED) sending TxAbort(d.channelId, f.cause.getMessage)
}
case Event(c: CloseCommand, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.Cancelled)
handleFastClose(c, d.channelId)
case Event(e: Error, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.RemoteError(e.toAscii))
handleRemoteError(e, d)
case Event(INPUT_DISCONNECTED, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.Disconnected)
goto(CLOSED)
case Event(TickChannelOpenTimeout, d: DATA_WAIT_FOR_DUAL_FUNDING_CREATED) =>
d.txBuilder ! InteractiveTxBuilder.Abort
d.replyTo_opt.foreach(_ ! OpenChannelResponse.TimedOut)
goto(CLOSED)
})
when(WAIT_FOR_DUAL_FUNDING_SIGNED)(handleExceptions {
case Event(commitSig: CommitSig, d: DATA_WAIT_FOR_DUAL_FUNDING_SIGNED) =>
d.signingSession.receiveCommitSig(nodeParams, d.channelParams, commitSig) match {
case Left(f) =>
rollbackFundingAttempt(d.signingSession.fundingTx.tx, Nil)
goto(CLOSED) sending Error(d.channelId, f.getMessage)
case Right(signingSession1) => signingSession1 match {
case signingSession1: InteractiveTxSigningSession.WaitingForSigs =>
// In theory we don't have to store their commit_sig here, as they would re-send it if we disconnect, but
// it is more consistent with the case where we send our tx_signatures first.
val d1 = d.copy(signingSession = signingSession1)
stay() using d1 storing()
case signingSession1: InteractiveTxSigningSession.SendingSigs =>
// We don't have their tx_sigs, but they have ours, and could publish the funding tx without telling us.
// That's why we move on immediately to the next step, and will update our unsigned funding tx when we
// receive their tx_sigs.
val minDepth_opt = d.channelParams.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, signingSession1.fundingTx.sharedTx.tx)
watchFundingConfirmed(d.signingSession.fundingTx.txId, minDepth_opt, delay_opt = None)
val commitments = Commitments(
params = d.channelParams,
changes = CommitmentChanges.init(),
active = List(signingSession1.commitment),
remoteNextCommitInfo = Right(d.secondRemotePerCommitmentPoint),
remotePerCommitmentSecrets = ShaChain.init,
originChannels = Map.empty
)
val d1 = DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED(commitments, d.localPushAmount, d.remotePushAmount, nodeParams.currentBlockHeight, nodeParams.currentBlockHeight, RbfStatus.NoRbf, None)
goto(WAIT_FOR_DUAL_FUNDING_CONFIRMED) using d1 storing() sending signingSession1.localSigs
}
}
case Event(msg: InteractiveTxMessage, d: DATA_WAIT_FOR_DUAL_FUNDING_SIGNED) =>
msg match {
case txSigs: TxSignatures =>
d.signingSession.receiveTxSigs(nodeParams, d.channelParams, txSigs) match {
case Left(f) =>
rollbackFundingAttempt(d.signingSession.fundingTx.tx, Nil)
goto(CLOSED) sending Error(d.channelId, f.getMessage)
case Right(signingSession) =>
val minDepth_opt = d.channelParams.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, signingSession.fundingTx.sharedTx.tx)
watchFundingConfirmed(d.signingSession.fundingTx.txId, minDepth_opt, delay_opt = None)
val commitments = Commitments(
params = d.channelParams,
changes = CommitmentChanges.init(),
active = List(signingSession.commitment),
remoteNextCommitInfo = Right(d.secondRemotePerCommitmentPoint),
remotePerCommitmentSecrets = ShaChain.init,
originChannels = Map.empty
)
val d1 = DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED(commitments, d.localPushAmount, d.remotePushAmount, nodeParams.currentBlockHeight, nodeParams.currentBlockHeight, RbfStatus.NoRbf, None)
goto(WAIT_FOR_DUAL_FUNDING_CONFIRMED) using d1 storing() sending signingSession.localSigs calling publishFundingTx(signingSession.fundingTx)
}
case msg: TxAbort =>
log.info("our peer aborted the dual funding flow: ascii='{}' bin={}", msg.toAscii, msg.data)
rollbackFundingAttempt(d.signingSession.fundingTx.tx, Nil)
goto(CLOSED) sending TxAbort(d.channelId, DualFundingAborted(d.channelId).getMessage)
case msg: InteractiveTxConstructionMessage =>
log.info("received unexpected interactive-tx message: {}", msg.getClass.getSimpleName)
stay() sending Warning(d.channelId, UnexpectedInteractiveTxMessage(d.channelId, msg).getMessage)
case _: TxInitRbf =>
log.info("ignoring unexpected tx_init_rbf message")
stay() sending Warning(d.channelId, InvalidRbfAttempt(d.channelId).getMessage)
case _: TxAckRbf =>
log.info("ignoring unexpected tx_ack_rbf message")
stay() sending Warning(d.channelId, InvalidRbfAttempt(d.channelId).getMessage)
}
case Event(c: CloseCommand, d: DATA_WAIT_FOR_DUAL_FUNDING_SIGNED) =>
rollbackFundingAttempt(d.signingSession.fundingTx.tx, Nil)
handleFastClose(c, d.channelId) sending Error(d.channelId, DualFundingAborted(d.channelId).getMessage)
case Event(e: Error, d: DATA_WAIT_FOR_DUAL_FUNDING_SIGNED) =>
rollbackFundingAttempt(d.signingSession.fundingTx.tx, Nil)
handleRemoteError(e, d)
case Event(INPUT_DISCONNECTED, d: DATA_WAIT_FOR_DUAL_FUNDING_SIGNED) =>
// We should be able to complete the channel open when reconnecting.
goto(OFFLINE) using d
})
when(WAIT_FOR_DUAL_FUNDING_CONFIRMED)(handleExceptions {
case Event(txSigs: TxSignatures, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.latestFundingTx.sharedTx match {
case fundingTx: PartiallySignedSharedTransaction => InteractiveTxSigningSession.addRemoteSigs(keyManager, d.commitments.params, d.latestFundingTx.fundingParams, fundingTx, txSigs) match {
case Left(cause) =>
val unsignedFundingTx = fundingTx.tx.buildUnsignedTx()
log.warning("received invalid tx_signatures for txid={} (current funding txid={}): {}", txSigs.txId, unsignedFundingTx.txid, cause.getMessage)
// The funding transaction may still confirm (since our peer should be able to generate valid signatures),
// so we cannot close the channel yet.
stay() sending Error(d.channelId, InvalidFundingSignature(d.channelId, Some(unsignedFundingTx.txid)).getMessage)
case Right(fundingTx) =>
log.info("publishing funding tx for channelId={} fundingTxId={}", d.channelId, fundingTx.signedTx.txid)
val dfu1 = d.latestFundingTx.copy(sharedTx = fundingTx)
val d1 = d.modify(_.commitments.active.at(0).localFundingStatus).setTo(dfu1)
stay() using d1 storing() calling publishFundingTx(dfu1)
}
case _: FullySignedSharedTransaction =>
d.rbfStatus match {
case RbfStatus.RbfWaitingForSigs(signingSession) =>
signingSession.receiveTxSigs(nodeParams, d.commitments.params, txSigs) match {
case Left(f) =>
rollbackRbfAttempt(signingSession, d)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, f.getMessage)
case Right(signingSession1) =>
val minDepth_opt = d.commitments.params.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, signingSession1.fundingTx.sharedTx.tx)
watchFundingConfirmed(signingSession.fundingTx.txId, minDepth_opt, delay_opt = None)
val commitments1 = d.commitments.add(signingSession1.commitment)
val d1 = DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED(commitments1, d.localPushAmount, d.remotePushAmount, d.waitingSince, d.lastChecked, RbfStatus.NoRbf, d.deferred)
stay() using d1 storing() sending signingSession1.localSigs calling publishFundingTx(signingSession1.fundingTx)
}
case _ if d.commitments.all.exists(_.fundingTxId == txSigs.txId) =>
log.debug("ignoring tx_signatures that we already received for txId={}", txSigs.txId)
stay()
case _ =>
log.debug("rejecting unexpected tx_signatures for txId={}", txSigs.txId)
reportRbfFailure(d.rbfStatus, UnexpectedFundingSignatures(d.channelId))
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, UnexpectedFundingSignatures(d.channelId).getMessage)
}
}
case Event(cmd: CMD_BUMP_FUNDING_FEE, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
val zeroConf = d.commitments.params.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, d.latestFundingTx.sharedTx.tx).isEmpty
if (!d.latestFundingTx.fundingParams.isInitiator) {
cmd.replyTo ! RES_FAILURE(cmd, InvalidRbfNonInitiator(d.channelId))
stay()
} else if (zeroConf) {
cmd.replyTo ! RES_FAILURE(cmd, InvalidRbfZeroConf(d.channelId))
stay()
} else {
d.rbfStatus match {
case RbfStatus.NoRbf =>
val minNextFeerate = d.latestFundingTx.fundingParams.minNextFeerate
if (cmd.targetFeerate < minNextFeerate) {
cmd.replyTo ! RES_FAILURE(cmd, InvalidRbfFeerate(d.channelId, cmd.targetFeerate, minNextFeerate))
stay()
} else {
stay() using d.copy(rbfStatus = RbfStatus.RbfRequested(cmd)) sending TxInitRbf(d.channelId, cmd.lockTime, cmd.targetFeerate, d.latestFundingTx.fundingParams.localContribution)
}
case _ =>
log.warning("cannot initiate rbf, another one is already in progress")
cmd.replyTo ! RES_FAILURE(cmd, InvalidRbfAlreadyInProgress(d.channelId))
stay()
}
}
case Event(msg: TxInitRbf, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
val zeroConf = d.commitments.params.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, d.latestFundingTx.sharedTx.tx).isEmpty
if (d.latestFundingTx.fundingParams.isInitiator) {
// Only the initiator is allowed to initiate RBF.
log.info("rejecting tx_init_rbf, we're the initiator, not them!")
stay() sending Error(d.channelId, InvalidRbfNonInitiator(d.channelId).getMessage)
} else if (zeroConf) {
log.info("rejecting tx_init_rbf, we're using zero-conf")
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, InvalidRbfZeroConf(d.channelId).getMessage)
} else {
val minNextFeerate = d.latestFundingTx.fundingParams.minNextFeerate
d.rbfStatus match {
case RbfStatus.NoRbf =>
val remainingRbfAttempts = nodeParams.channelConf.remoteRbfLimits.maxAttempts - d.previousFundingTxs.length
if (msg.feerate < minNextFeerate) {
log.info("rejecting rbf attempt: the new feerate must be at least {} (proposed={})", minNextFeerate, msg.feerate)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, InvalidRbfFeerate(d.channelId, msg.feerate, minNextFeerate).getMessage)
} else if (d.remotePushAmount > msg.fundingContribution) {
log.info("rejecting rbf attempt: invalid amount pushed (fundingAmount={}, pushAmount={})", msg.fundingContribution, d.remotePushAmount)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, InvalidPushAmount(d.channelId, d.remotePushAmount, msg.fundingContribution.toMilliSatoshi).getMessage)
} else if (remainingRbfAttempts <= 0) {
log.info("rejecting rbf attempt: maximum number of attempts reached (max={})", nodeParams.channelConf.remoteRbfLimits.maxAttempts)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, InvalidRbfAttemptsExhausted(d.channelId, nodeParams.channelConf.remoteRbfLimits.maxAttempts).getMessage)
} else if (nodeParams.currentBlockHeight < d.latestFundingTx.createdAt + nodeParams.channelConf.remoteRbfLimits.attemptDeltaBlocks) {
log.info("rejecting rbf attempt: last attempt was less than {} blocks ago", nodeParams.channelConf.remoteRbfLimits.attemptDeltaBlocks)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, InvalidRbfAttemptTooSoon(d.channelId, d.latestFundingTx.createdAt, d.latestFundingTx.createdAt + nodeParams.channelConf.remoteRbfLimits.attemptDeltaBlocks).getMessage)
} else {
log.info("our peer wants to raise the feerate of the funding transaction (previous={} target={})", d.latestFundingTx.fundingParams.targetFeerate, msg.feerate)
val fundingParams = d.latestFundingTx.fundingParams.copy(
// we don't change our funding contribution
remoteContribution = msg.fundingContribution,
lockTime = msg.lockTime,
targetFeerate = msg.feerate
)
val txBuilder = context.spawnAnonymous(InteractiveTxBuilder(
randomBytes32(),
nodeParams, fundingParams,
channelParams = d.commitments.params,
purpose = InteractiveTxBuilder.PreviousTxRbf(d.commitments.active.head, 0 msat, 0 msat, previousTransactions = d.allFundingTxs.map(_.sharedTx), feeBudget_opt = None),
localPushAmount = d.localPushAmount, remotePushAmount = d.remotePushAmount,
wallet))
txBuilder ! InteractiveTxBuilder.Start(self)
val toSend = Seq(
Some(TxAckRbf(d.channelId, fundingParams.localContribution)),
if (remainingRbfAttempts <= 3) Some(Warning(d.channelId, s"will accept at most ${remainingRbfAttempts - 1} future rbf attempts")) else None,
).flatten
stay() using d.copy(rbfStatus = RbfStatus.RbfInProgress(cmd_opt = None, txBuilder, remoteCommitSig = None)) sending toSend
}
case RbfStatus.RbfAborted =>
log.info("rejecting rbf attempt: our previous tx_abort was not acked")
stay() sending Warning(d.channelId, InvalidRbfTxAbortNotAcked(d.channelId).getMessage)
case _: RbfStatus.RbfRequested | _: RbfStatus.RbfInProgress | _: RbfStatus.RbfWaitingForSigs =>
log.info("rejecting rbf attempt: the current rbf attempt must be completed or aborted first")
stay() sending Warning(d.channelId, InvalidRbfAlreadyInProgress(d.channelId).getMessage)
}
}
case Event(msg: TxAckRbf, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.rbfStatus match {
case RbfStatus.RbfRequested(cmd) if d.remotePushAmount > msg.fundingContribution =>
log.info("rejecting rbf attempt: invalid amount pushed (fundingAmount={}, pushAmount={})", msg.fundingContribution, d.remotePushAmount)
val error = InvalidPushAmount(d.channelId, d.remotePushAmount, msg.fundingContribution.toMilliSatoshi)
cmd.replyTo ! RES_FAILURE(cmd, error)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, error.getMessage)
case RbfStatus.RbfRequested(cmd) =>
log.info("our peer accepted our rbf attempt and will contribute {} to the funding transaction", msg.fundingContribution)
val fundingParams = d.latestFundingTx.fundingParams.copy(
// we don't change our funding contribution
remoteContribution = msg.fundingContribution,
lockTime = cmd.lockTime,
targetFeerate = cmd.targetFeerate,
)
val txBuilder = context.spawnAnonymous(InteractiveTxBuilder(
randomBytes32(),
nodeParams, fundingParams,
channelParams = d.commitments.params,
purpose = InteractiveTxBuilder.PreviousTxRbf(d.commitments.active.head, 0 msat, 0 msat, previousTransactions = d.allFundingTxs.map(_.sharedTx), feeBudget_opt = Some(cmd.fundingFeeBudget)),
localPushAmount = d.localPushAmount, remotePushAmount = d.remotePushAmount,
wallet))
txBuilder ! InteractiveTxBuilder.Start(self)
stay() using d.copy(rbfStatus = RbfStatus.RbfInProgress(cmd_opt = Some(cmd), txBuilder, remoteCommitSig = None))
case _ =>
log.info("ignoring unexpected tx_ack_rbf")
stay() sending Warning(d.channelId, UnexpectedInteractiveTxMessage(d.channelId, msg).getMessage)
}
case Event(msg: InteractiveTxConstructionMessage, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.rbfStatus match {
case RbfStatus.RbfInProgress(_, txBuilder, _) =>
txBuilder ! InteractiveTxBuilder.ReceiveMessage(msg)
stay()
case _ =>
log.info("ignoring unexpected interactive-tx message: {}", msg.getClass.getSimpleName)
stay() sending Warning(d.channelId, UnexpectedInteractiveTxMessage(d.channelId, msg).getMessage)
}
case Event(commitSig: CommitSig, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.rbfStatus match {
case s: RbfStatus.RbfInProgress =>
log.debug("received their commit_sig, deferring message")
stay() using d.copy(rbfStatus = s.copy(remoteCommitSig = Some(commitSig)))
case RbfStatus.RbfWaitingForSigs(signingSession) =>
signingSession.receiveCommitSig(nodeParams, d.commitments.params, commitSig) match {
case Left(f) =>
rollbackRbfAttempt(signingSession, d)
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, f.getMessage)
case Right(signingSession1) => signingSession1 match {
case signingSession1: InteractiveTxSigningSession.WaitingForSigs =>
// No need to store their commit_sig, they will re-send it if we disconnect.
stay() using d.copy(rbfStatus = RbfStatus.RbfWaitingForSigs(signingSession1))
case signingSession1: InteractiveTxSigningSession.SendingSigs =>
val minDepth_opt = d.commitments.params.minDepthDualFunding(nodeParams.channelConf.minDepthBlocks, signingSession1.fundingTx.sharedTx.tx)
watchFundingConfirmed(signingSession.fundingTx.txId, minDepth_opt, delay_opt = None)
val commitments1 = d.commitments.add(signingSession1.commitment)
val d1 = DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED(commitments1, d.localPushAmount, d.remotePushAmount, d.waitingSince, d.lastChecked, RbfStatus.NoRbf, d.deferred)
stay() using d1 storing() sending signingSession1.localSigs
}
}
case _ =>
log.info("ignoring redundant commit_sig")
stay()
}
case Event(msg: TxAbort, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.rbfStatus match {
case RbfStatus.RbfInProgress(cmd_opt, txBuilder, _) =>
log.info("our peer aborted the rbf attempt: ascii='{}' bin={}", msg.toAscii, msg.data)
cmd_opt.foreach(cmd => cmd.replyTo ! RES_FAILURE(cmd, RbfAttemptAborted(d.channelId)))
txBuilder ! InteractiveTxBuilder.Abort
stay() using d.copy(rbfStatus = RbfStatus.NoRbf) sending TxAbort(d.channelId, RbfAttemptAborted(d.channelId).getMessage)
case RbfStatus.RbfWaitingForSigs(signingSession) =>
log.info("our peer aborted the rbf attempt: ascii='{}' bin={}", msg.toAscii, msg.data)
rollbackRbfAttempt(signingSession, d)
stay() using d.copy(rbfStatus = RbfStatus.NoRbf) sending TxAbort(d.channelId, RbfAttemptAborted(d.channelId).getMessage)
case RbfStatus.RbfRequested(cmd) =>
log.info("our peer rejected our rbf attempt: ascii='{}' bin={}", msg.toAscii, msg.data)
cmd.replyTo ! RES_FAILURE(cmd, new RuntimeException(s"rbf attempt rejected by our peer: ${msg.toAscii}"))
stay() using d.copy(rbfStatus = RbfStatus.NoRbf) sending TxAbort(d.channelId, RbfAttemptAborted(d.channelId).getMessage)
case RbfStatus.RbfAborted =>
log.debug("our peer acked our previous tx_abort")
stay() using d.copy(rbfStatus = RbfStatus.NoRbf)
case RbfStatus.NoRbf =>
log.info("our peer wants to abort the dual funding flow, but we've already negotiated a funding transaction: ascii='{}' bin={}", msg.toAscii, msg.data)
// We ack their tx_abort but we keep monitoring the funding transaction until it's confirmed or double-spent.
stay() sending TxAbort(d.channelId, DualFundingAborted(d.channelId).getMessage)
}
case Event(msg: InteractiveTxBuilder.Response, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
d.rbfStatus match {
case RbfStatus.RbfInProgress(cmd_opt, _, remoteCommitSig_opt) =>
msg match {
case InteractiveTxBuilder.SendMessage(_, msg) => stay() sending msg
case InteractiveTxBuilder.Succeeded(signingSession, commitSig) =>
cmd_opt.foreach(cmd => cmd.replyTo ! RES_BUMP_FUNDING_FEE(rbfIndex = d.previousFundingTxs.length, signingSession.fundingTx.txId, signingSession.fundingTx.tx.localFees.truncateToSatoshi))
remoteCommitSig_opt.foreach(self ! _)
val d1 = d.copy(rbfStatus = RbfStatus.RbfWaitingForSigs(signingSession))
stay() using d1 storing() sending commitSig
case f: InteractiveTxBuilder.Failed =>
log.info("rbf attempt failed: {}", f.cause.getMessage)
cmd_opt.foreach(cmd => cmd.replyTo ! RES_FAILURE(cmd, f.cause))
stay() using d.copy(rbfStatus = RbfStatus.RbfAborted) sending TxAbort(d.channelId, f.cause.getMessage)
}
case _ =>
// This can happen if we received a tx_abort right before receiving the interactive-tx result.
log.warning("ignoring interactive-tx result with rbfStatus={}", d.rbfStatus.getClass.getSimpleName)
stay()
}
case Event(w: WatchPublishedTriggered, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
log.info("funding txid={} was successfully published for zero-conf channelId={}", w.tx.txid, d.channelId)
val fundingStatus = LocalFundingStatus.ZeroconfPublishedFundingTx(w.tx, d.commitments.localFundingSigs(w.tx.txid))
d.commitments.updateLocalFundingStatus(w.tx.txid, fundingStatus) match {
case Right((commitments1, _)) =>
// we still watch the funding tx for confirmation even if we can use the zero-conf channel right away
watchFundingConfirmed(w.tx.txid, Some(nodeParams.channelConf.minDepthBlocks), delay_opt = None)
val realScidStatus = RealScidStatus.Unknown
val shortIds = createShortIds(d.channelId, realScidStatus)
val channelReady = createChannelReady(shortIds, d.commitments.params)
d.deferred.foreach(self ! _)
goto(WAIT_FOR_DUAL_FUNDING_READY) using DATA_WAIT_FOR_DUAL_FUNDING_READY(commitments1, shortIds) storing() sending channelReady
case Left(_) => stay()
}
case Event(w: WatchFundingConfirmedTriggered, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
acceptFundingTxConfirmed(w, d) match {
case Right((commitments1, commitment)) =>
val realScidStatus = RealScidStatus.Temporary(RealShortChannelId(w.blockHeight, w.txIndex, commitment.commitInput.outPoint.index.toInt))
val shortIds = createShortIds(d.channelId, realScidStatus)
val channelReady = createChannelReady(shortIds, d.commitments.params)
reportRbfFailure(d.rbfStatus, InvalidRbfTxConfirmed(d.channelId))
val toSend = d.rbfStatus match {
case RbfStatus.NoRbf | RbfStatus.RbfAborted => Seq(channelReady)
case _: RbfStatus.RbfRequested | _: RbfStatus.RbfInProgress | _: RbfStatus.RbfWaitingForSigs => Seq(TxAbort(d.channelId, InvalidRbfTxConfirmed(d.channelId).getMessage), channelReady)
}
d.deferred.foreach(self ! _)
goto(WAIT_FOR_DUAL_FUNDING_READY) using DATA_WAIT_FOR_DUAL_FUNDING_READY(commitments1, shortIds) storing() sending toSend
case Left(_) => stay()
}
case Event(ProcessCurrentBlockHeight(c), d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) => handleNewBlockDualFundingUnconfirmed(c, d)
case Event(e: BITCOIN_FUNDING_DOUBLE_SPENT, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
reportRbfFailure(d.rbfStatus, FundingTxDoubleSpent(d.channelId))
handleDualFundingDoubleSpent(e, d)
case Event(remoteChannelReady: ChannelReady, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
if (switchToZeroConf(remoteChannelReady, d)) {
log.info("this channel isn't zero-conf, but they sent an early channel_ready with an alias: no need to wait for confirmations")
blockchain ! WatchPublished(self, d.commitments.latest.fundingTxId)
}
log.debug("received their channel_ready, deferring message")
stay() using d.copy(deferred = Some(remoteChannelReady)) // no need to store, they will re-send if we get disconnected
case Event(remoteAnnSigs: AnnouncementSignatures, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) if d.commitments.announceChannel =>
delayEarlyAnnouncementSigs(remoteAnnSigs)
stay()
case Event(INPUT_DISCONNECTED, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
reportRbfFailure(d.rbfStatus, new RuntimeException("rbf attempt failed: disconnected"))
val d1 = d.rbfStatus match {
// We keep track of the RBF status: we should be able to complete the signature steps on reconnection.
case _: RbfStatus.RbfWaitingForSigs => d
case _ => d.copy(rbfStatus = RbfStatus.NoRbf)
}
goto(OFFLINE) using d1
case Event(e: Error, d: DATA_WAIT_FOR_DUAL_FUNDING_CONFIRMED) =>
reportRbfFailure(d.rbfStatus, new RuntimeException(s"remote error: ${e.toAscii}"))
handleRemoteError(e, d)
})
when(WAIT_FOR_DUAL_FUNDING_READY)(handleExceptions {
case Event(channelReady: ChannelReady, d: DATA_WAIT_FOR_DUAL_FUNDING_READY) =>
val d1 = receiveChannelReady(d.shortIds, channelReady, d.commitments)
goto(NORMAL) using d1 storing()
case Event(_: TxInitRbf, d: DATA_WAIT_FOR_DUAL_FUNDING_READY) =>
// Our peer may not have received the funding transaction confirmation.
stay() sending TxAbort(d.channelId, InvalidRbfTxConfirmed(d.channelId).getMessage)
case Event(remoteAnnSigs: AnnouncementSignatures, d: DATA_WAIT_FOR_DUAL_FUNDING_READY) if d.commitments.announceChannel =>
delayEarlyAnnouncementSigs(remoteAnnSigs)
stay()
case Event(e: Error, d: DATA_WAIT_FOR_DUAL_FUNDING_READY) => handleRemoteError(e, d)
})
}