Skip to content

Commit

Permalink
cleanup uncommitted htlcs if their cltv stops being valid.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jul 15, 2022
1 parent 697c640 commit 8d7e962
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/main/scala/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,29 @@ class Channel(master: ChannelMaster, peerId: ByteVector) {
val requiredFee = MilliSatoshi(
master.config.feeBase.toLong + (master.config.feeProportionalMillionths * htlc.amountMsat.toLong / 1000000L)
)

if (
(htlc.cltvExpiry.blockHeight - master.currentBlock).toInt < master.config.cltvExpiryDelta.toInt ||
(htlc.cltvExpiry.blockHeight - master.currentBlock).toInt < master.config.cltvExpiryDelta.toInt
)
promise.success(
Some(
Left(
Some(
NormalFailureMessage(
IncorrectOrUnknownPaymentDetails(
htlc.amountMsat,
master.currentBlock.toLong
)
)
)
)
)
)
else if (
(incomingAmount - htlc.amountMsat) < requiredFee ||
updated.lcssNext.localBalanceMsat < MilliSatoshi(0L) ||
updated.lcssNext.remoteBalanceMsat < MilliSatoshi(0L)
) {
)
promise.success(
Some(
Left(
Expand All @@ -209,7 +226,7 @@ class Channel(master: ChannelMaster, peerId: ByteVector) {
)
)
)
} else {
else {
// will send update_add_htlc to hosted client
// and we update the state to include this uncommitted htlc
state = updated
Expand Down Expand Up @@ -1236,6 +1253,31 @@ class Channel(master: ChannelMaster, peerId: ByteVector) {

}
}

// cleanup uncommitted htlcs that may be pending for so long they're now inviable
state.uncommittedUpdates.collect {
case m @ FromLocal(htlc: UpdateAddHtlc, _)
if (htlc.cltvExpiry.blockHeight - master.currentBlock).toInt < master.config.cltvExpiryDelta.toInt => {
state = state.removeUncommitedUpdate(m)

// and fail them upstream
provideHtlcResult(
htlc.id,
Some(
Left(
Some(
NormalFailureMessage(
IncorrectOrUnknownPaymentDetails(
htlc.amountMsat,
master.currentBlock.toLong
)
)
)
)
)
)
}
}
}

// opening a channel, as a client, to another hosted channel provider
Expand Down

0 comments on commit 8d7e962

Please sign in to comment.