Skip to content

Commit fcb0fe4

Browse files
committed
Refactor funding tx confirmation skip
And address PR comments.
1 parent d9ec2b1 commit fcb0fe4

3 files changed

Lines changed: 19 additions & 23 deletions

File tree

eclair-core/src/main/scala/fr/acinq/eclair/channel/Helpers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ object Helpers {
304304
* we make sure the cumulative block reward largely exceeds the channel size.
305305
*
306306
* @param fundingSatoshis funding amount of the channel
307-
* @return number of confirmations needed
307+
* @return number of confirmations needed, if any
308308
*/
309309
def minDepthFundee(channelConf: ChannelConf, channelFeatures: ChannelFeatures, fundingSatoshis: Satoshi): Option[Long] = fundingSatoshis match {
310310
case _ if channelFeatures.hasFeature(Features.ZeroConf) => None // zero-conf stay zero-conf, whatever the funding amount is

eclair-core/src/main/scala/fr/acinq/eclair/channel/fsm/ChannelOpenSingleFunder.scala

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,7 @@ trait ChannelOpenSingleFunder extends FundingHandlers with ErrorHandlers {
261261
blockchain ! WatchFundingConfirmed(self, commitInput.outPoint.txid, fundingMinDepth)
262262
goto(WAIT_FOR_FUNDING_CONFIRMED) using DATA_WAIT_FOR_FUNDING_CONFIRMED(commitments, None, nodeParams.currentBlockHeight, None, Right(fundingSigned)) storing() sending fundingSigned
263263
case None =>
264-
blockchain ! WatchFundingLost(self, commitments.commitInput.outPoint.txid, nodeParams.channelConf.minDepthBlocks)
265-
val channelKeyPath = keyManager.keyPath(commitments.localParams, commitments.channelConfig)
266-
val nextPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1)
267-
val shortIds = ShortIds(RealScidStatus.Unknown, ShortChannelId.generateLocalAlias(), None)
268-
context.system.eventStream.publish(ShortChannelIdAssigned(self, d.channelId, shortIds, remoteNodeId))
269-
val channelReady = ChannelReady(d.channelId, nextPerCommitmentPoint, TlvStream(ChannelReadyTlv.ShortChannelIdTlv(shortIds.localAlias)))
264+
val (shortIds, channelReady) = skipFundingConfirmation(commitments, None, emitEvent = true)
270265
goto(WAIT_FOR_CHANNEL_READY) using DATA_WAIT_FOR_CHANNEL_READY(commitments, shortIds, channelReady) storing() sending Seq(fundingSigned, channelReady)
271266
}
272267
}
@@ -326,15 +321,9 @@ trait ChannelOpenSingleFunder extends FundingHandlers with ErrorHandlers {
326321
Funding.minDepthFunder(commitments.channelFeatures) match {
327322
case Some(fundingMinDepth) =>
328323
blockchain ! WatchFundingConfirmed(self, commitInput.outPoint.txid, fundingMinDepth)
329-
log.info(s"committing txid=${fundingTx.txid}")
330324
goto(WAIT_FOR_FUNDING_CONFIRMED) using DATA_WAIT_FOR_FUNDING_CONFIRMED(commitments, Some(fundingTx), blockHeight, None, Left(fundingCreated)) storing() calling publishFundingTx()
331325
case None =>
332-
blockchain ! WatchFundingLost(self, commitments.commitInput.outPoint.txid, nodeParams.channelConf.minDepthBlocks)
333-
val channelKeyPath = keyManager.keyPath(commitments.localParams, commitments.channelConfig)
334-
val nextPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1)
335-
val shortIds = ShortIds(RealScidStatus.Unknown, ShortChannelId.generateLocalAlias(), None)
336-
context.system.eventStream.publish(ShortChannelIdAssigned(self, d.channelId, shortIds, remoteNodeId))
337-
val channelReady = ChannelReady(d.channelId, nextPerCommitmentPoint, TlvStream(ChannelReadyTlv.ShortChannelIdTlv(shortIds.localAlias)))
326+
val (shortIds, channelReady) = skipFundingConfirmation(commitments, None, emitEvent = true)
338327
goto(WAIT_FOR_CHANNEL_READY) using DATA_WAIT_FOR_CHANNEL_READY(commitments, shortIds, channelReady) storing() sending channelReady calling publishFundingTx()
339328
}
340329
}
@@ -368,14 +357,10 @@ trait ChannelOpenSingleFunder extends FundingHandlers with ErrorHandlers {
368357
case Event(remoteChannelReady: ChannelReady, d: DATA_WAIT_FOR_FUNDING_CONFIRMED) =>
369358
if (remoteChannelReady.alias_opt.isDefined && d.commitments.localParams.isInitiator) {
370359
log.info("this chanel isn't zero-conf, but we are funder and they sent an early channel_ready with an alias: no need to wait for confirmations")
371-
// NB: we will receive a WatchFundingConfirmedTriggered that will simply be ignored
372-
blockchain ! WatchFundingLost(self, d.commitments.commitInput.outPoint.txid, nodeParams.channelConf.minDepthBlocks)
373-
val channelKeyPath = keyManager.keyPath(d.commitments.localParams, d.commitments.channelConfig)
374-
val nextPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1)
375360
// No need to emit ShortChannelIdAssigned: we will emit it when handling their channel_ready in WAIT_FOR_CHANNEL_READY
376-
val shortIds = ShortIds(RealScidStatus.Unknown, ShortChannelId.generateLocalAlias(), remoteChannelReady.alias_opt)
377-
val localChannelReady = ChannelReady(d.channelId, nextPerCommitmentPoint, TlvStream(ChannelReadyTlv.ShortChannelIdTlv(shortIds.localAlias)))
361+
val (shortIds, localChannelReady) = skipFundingConfirmation(d.commitments, remoteChannelReady.alias_opt, emitEvent = false)
378362
self ! remoteChannelReady
363+
// NB: we will receive a WatchFundingConfirmedTriggered later that will simply be ignored
379364
goto(WAIT_FOR_CHANNEL_READY) using DATA_WAIT_FOR_CHANNEL_READY(d.commitments, shortIds, localChannelReady) storing() sending localChannelReady
380365
} else {
381366
log.info("received their channel_ready, deferring message")

eclair-core/src/main/scala/fr/acinq/eclair/channel/fsm/FundingHandlers.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package fr.acinq.eclair.channel.fsm
1919
import akka.actor.Status
2020
import akka.actor.typed.scaladsl.adapter.{TypedActorRefOps, actorRefAdapter}
2121
import fr.acinq.bitcoin.scalacompat.{ByteVector32, SatoshiLong, Transaction}
22-
import fr.acinq.eclair.BlockHeight
23-
import fr.acinq.eclair.blockchain.bitcoind.ZmqWatcher.{GetTxWithMeta, GetTxWithMetaResponse, WatchFundingSpent}
22+
import fr.acinq.eclair.{Alias, BlockHeight, ShortChannelId}
23+
import fr.acinq.eclair.blockchain.bitcoind.ZmqWatcher.{GetTxWithMeta, GetTxWithMetaResponse, WatchFundingLost, WatchFundingSpent}
2424
import fr.acinq.eclair.channel._
2525
import fr.acinq.eclair.channel.fsm.Channel.{BITCOIN_FUNDING_PUBLISH_FAILED, BITCOIN_FUNDING_TIMEOUT, FUNDING_TIMEOUT_FUNDEE}
2626
import fr.acinq.eclair.channel.publish.TxPublisher.PublishFinalTx
27-
import fr.acinq.eclair.wire.protocol.Error
27+
import fr.acinq.eclair.wire.protocol.{ChannelReady, ChannelReadyTlv, Error, TlvStream}
2828

2929
import scala.concurrent.duration.DurationInt
3030
import scala.util.{Failure, Success}
@@ -60,6 +60,17 @@ trait FundingHandlers extends CommonHandlers {
6060
//blockchain ! WatchLost(self, commitments.commitInput.outPoint.txid, nodeParams.channelConf.minDepthBlocks, BITCOIN_FUNDING_LOST)
6161
}
6262

63+
/** When using 0-conf, we don't wait for the funding tx to confirm and instantly send channel_ready. */
64+
def skipFundingConfirmation(commitments: Commitments, remoteAlias_opt: Option[Alias], emitEvent: Boolean): (ShortIds, ChannelReady) = {
65+
blockchain ! WatchFundingLost(self, commitments.commitInput.outPoint.txid, nodeParams.channelConf.minDepthBlocks)
66+
val channelKeyPath = keyManager.keyPath(commitments.localParams, commitments.channelConfig)
67+
val nextPerCommitmentPoint = keyManager.commitmentPoint(channelKeyPath, 1)
68+
val shortIds = ShortIds(RealScidStatus.Unknown, ShortChannelId.generateLocalAlias(), remoteAlias_opt)
69+
if (emitEvent) context.system.eventStream.publish(ShortChannelIdAssigned(self, commitments.channelId, shortIds, remoteNodeId))
70+
val channelReady = ChannelReady(commitments.channelId, nextPerCommitmentPoint, TlvStream(ChannelReadyTlv.ShortChannelIdTlv(shortIds.localAlias)))
71+
(shortIds, channelReady)
72+
}
73+
6374
/**
6475
* When we are funder, we use this function to detect when our funding tx has been double-spent (by another transaction
6576
* that we made for some reason). If the funding tx has been double spent we can forget about the channel.

0 commit comments

Comments
 (0)