Skip to content

Commit

Permalink
Abort splice attempts when balance is too low
Browse files Browse the repository at this point in the history
If our balance, combined with our fee credit, doesn't allow us to pay
the liquidity fees with the payment type we chose, we immediately abort
the splice.
  • Loading branch information
t-bast committed Sep 25, 2024
1 parent 38ee3dc commit a8b05fe
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 45 deletions.
12 changes: 10 additions & 2 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/ChannelCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ sealed class ChannelCommand {
data class UpdateFee(val feerate: FeeratePerKw, val commit: Boolean = false) : Commitment(), ForbiddenDuringSplice, ForbiddenDuringQuiescence
data object CheckHtlcTimeout : Commitment()
sealed class Splice : Commitment() {
data class Request(val replyTo: CompletableDeferred<Response>, val spliceIn: SpliceIn?, val spliceOut: SpliceOut?, val requestRemoteFunding: LiquidityAds.RequestFunding?, val feerate: FeeratePerKw, val origins: List<Origin>) : Splice() {
data class Request(
val replyTo: CompletableDeferred<Response>,
val spliceIn: SpliceIn?,
val spliceOut: SpliceOut?,
val requestRemoteFunding: LiquidityAds.RequestFunding?,
val currentFeeCredit: MilliSatoshi,
val feerate: FeeratePerKw,
val origins: List<Origin>
) : Splice() {
val pushAmount: MilliSatoshi = spliceIn?.pushAmount ?: 0.msat
val spliceOutputs: List<TxOut> = spliceOut?.let { listOf(TxOut(it.amount, it.scriptPubKey)) } ?: emptyList()

Expand All @@ -110,7 +118,7 @@ sealed class ChannelCommand {
) : Response()

sealed class Failure : Response() {
data object InsufficientFunds : Failure()
data class InsufficientFunds(val balanceAfterFees: MilliSatoshi, val liquidityFees: MilliSatoshi, val currentFeeCredit: MilliSatoshi) : Failure()
data object InvalidSpliceOutPubKeyScript : Failure()
data object SpliceAlreadyInProgress : Failure()
data object ConcurrentRemoteSplice : Failure()
Expand Down
31 changes: 19 additions & 12 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/states/Normal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,29 @@ data class Normal(
paysCommitTxFees -> Transactions.commitTxFee(commitments.params.remoteParams.dustLimit, parentCommitment.remoteCommit.spec)
else -> 0.sat
}
if (parentCommitment.localCommit.spec.toLocal + fundingContribution.toMilliSatoshi() < parentCommitment.localChannelReserve(commitments.params).max(commitTxFees)) {
logger.warning { "cannot do splice: insufficient funds" }
spliceStatus.command.replyTo.complete(ChannelCommand.Commitment.Splice.Response.Failure.InsufficientFunds)
val actions = buildList {
add(ChannelAction.Message.Send(Warning(channelId, InvalidSpliceRequest(channelId).message)))
add(ChannelAction.Disconnect)
val liquidityFees = when (val requestRemoteFunding = spliceStatus.command.requestRemoteFunding) {
null -> 0.msat
else -> when (requestRemoteFunding.paymentDetails.paymentType) {
LiquidityAds.PaymentType.FromChannelBalance -> requestRemoteFunding.fees(spliceStatus.command.feerate, isChannelCreation = false).total.toMilliSatoshi()
LiquidityAds.PaymentType.FromChannelBalanceForFutureHtlc -> requestRemoteFunding.fees(spliceStatus.command.feerate, isChannelCreation = false).total.toMilliSatoshi()
// Liquidity fees will be deducted from future HTLCs instead of being paid immediately.
LiquidityAds.PaymentType.FromFutureHtlc -> 0.msat
LiquidityAds.PaymentType.FromFutureHtlcWithPreimage -> 0.msat
is LiquidityAds.PaymentType.Unknown -> 0.msat
}
Pair(this@Normal.copy(spliceStatus = SpliceStatus.None), actions)
}
val liquidityFeesOwed = (liquidityFees - spliceStatus.command.currentFeeCredit).max(0.msat)
val balanceAfterFees = parentCommitment.localCommit.spec.toLocal + fundingContribution.toMilliSatoshi() - liquidityFeesOwed
if (balanceAfterFees < parentCommitment.localChannelReserve(commitments.params).max(commitTxFees)) {
logger.warning { "cannot do splice: insufficient funds (balanceAfterFees=$balanceAfterFees, liquidityFees=$liquidityFees, feeCredit=${spliceStatus.command.currentFeeCredit})" }
spliceStatus.command.replyTo.complete(ChannelCommand.Commitment.Splice.Response.Failure.InsufficientFunds(balanceAfterFees, liquidityFees, spliceStatus.command.currentFeeCredit))
val action = listOf(ChannelAction.Message.Send(TxAbort(channelId, InvalidSpliceRequest(channelId).message)))
Pair(this@Normal.copy(spliceStatus = SpliceStatus.Aborted), action)
} else if (spliceStatus.command.spliceOut?.scriptPubKey?.let { Helpers.Closing.isValidFinalScriptPubkey(it, allowAnySegwit = true) } == false) {
logger.warning { "cannot do splice: invalid splice-out script" }
spliceStatus.command.replyTo.complete(ChannelCommand.Commitment.Splice.Response.Failure.InvalidSpliceOutPubKeyScript)
val actions = buildList {
add(ChannelAction.Message.Send(Warning(channelId, InvalidSpliceRequest(channelId).message)))
add(ChannelAction.Disconnect)
}
Pair(this@Normal.copy(spliceStatus = SpliceStatus.None), actions)
val action = listOf(ChannelAction.Message.Send(TxAbort(channelId, InvalidSpliceRequest(channelId).message)))
Pair(this@Normal.copy(spliceStatus = SpliceStatus.Aborted), action)
} else {
val spliceInit = SpliceInit(
channelId,
Expand Down
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ class Peer(
spliceIn = null,
spliceOut = ChannelCommand.Commitment.Splice.Request.SpliceOut(amount, scriptPubKey),
requestRemoteFunding = null,
currentFeeCredit = feeCreditFlow.value,
feerate = feerate,
origins = listOf(),
)
Expand All @@ -662,6 +663,7 @@ class Peer(
spliceIn = null,
spliceOut = null,
requestRemoteFunding = null,
currentFeeCredit = feeCreditFlow.value,
feerate = feerate,
origins = listOf(),
)
Expand All @@ -680,6 +682,7 @@ class Peer(
spliceIn = null,
spliceOut = null,
requestRemoteFunding = LiquidityAds.RequestFunding(amount, fundingRate, LiquidityAds.PaymentDetails.FromChannelBalance),
currentFeeCredit = feeCreditFlow.value,
feerate = feerate,
origins = listOf(),
)
Expand Down Expand Up @@ -1285,6 +1288,7 @@ class Peer(
spliceIn = ChannelCommand.Commitment.Splice.Request.SpliceIn(cmd.walletInputs),
spliceOut = null,
requestRemoteFunding = null,
currentFeeCredit = feeCreditFlow.value,
feerate = feerate,
origins = listOf(Origin.OnChainWallet(cmd.walletInputs.map { it.outPoint }.toSet(), cmd.totalAmount.toMilliSatoshi(), ChannelManagementFees(fee, 0.sat)))
)
Expand Down Expand Up @@ -1425,6 +1429,7 @@ class Peer(
spliceIn = null,
spliceOut = null,
requestRemoteFunding = LiquidityAds.RequestFunding(cmd.requestedAmount, cmd.fundingRate, paymentDetails),
currentFeeCredit = currentFeeCredit,
feerate = targetFeerate,
origins = listOf(Origin.OffChainPayment(cmd.preimage, cmd.paymentAmount, totalFees))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ class QuiescenceTestsCommon : LightningTestSuite() {
spliceOut = spliceOut?.let { ChannelCommand.Commitment.Splice.Request.SpliceOut(it, Script.write(Script.pay2wpkh(Lightning.randomKey().publicKey())).byteVector()) },
feerate = FeeratePerKw(253.sat),
requestRemoteFunding = null,
currentFeeCredit = 0.msat,
origins = listOf(),
)
}
Expand Down
Loading

0 comments on commit a8b05fe

Please sign in to comment.