From baf8ce2c2f98c196b41f3202d2b7b6b49d2bf1a8 Mon Sep 17 00:00:00 2001 From: t-bast Date: Wed, 16 Oct 2024 05:31:46 +0200 Subject: [PATCH] Clarify why we still need a `childId` Each outgoing HTLC will have its own ID in the payments DB. Since we support retries, we have a 1-to-N mapping from a payment ID to its child IDs. --- .../lightning/payment/OutgoingPaymentHandler.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/payment/OutgoingPaymentHandler.kt b/src/commonMain/kotlin/fr/acinq/lightning/payment/OutgoingPaymentHandler.kt index f6d53fc75..2194f0a11 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/payment/OutgoingPaymentHandler.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/payment/OutgoingPaymentHandler.kt @@ -41,12 +41,14 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle data class Success(val request: PayInvoice, val payment: LightningOutgoingPayment, val preimage: ByteVector32) : ProcessFailureResult, ProcessFulfillResult private val logger = nodeParams.loggerFactory.newLogger(this::class) - private val childToParentId = mutableMapOf() + // Each outgoing HTLC will have its own ID, because its status will be recorded in the payments DB. + // Since we automatically retry on failure, we may have multiple child attempts for each payment. + private val childToPaymentId = mutableMapOf() private val pending = mutableMapOf() /** * While a payment is in progress, we wait for the outgoing HTLC to settle. - * When we receive a failure, we retry with a different channel or fees. + * When we receive a failure, we may retry with a different fee or expiry. * * @param request payment request containing the total amount to send. * @param attemptNumber number of failed previous payment attempts. @@ -65,9 +67,9 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle } // NB: this function should only be used in tests. - fun getPendingPayment(parentId: UUID): PaymentAttempt? = pending[parentId] + fun getPendingPayment(paymentId: UUID): PaymentAttempt? = pending[paymentId] - private fun getPaymentAttempt(childId: UUID): PaymentAttempt? = childToParentId[childId]?.let { pending[it] } + private fun getPaymentAttempt(childId: UUID): PaymentAttempt? = childToPaymentId[childId]?.let { pending[it] } suspend fun sendPayment(request: PayInvoice, channels: Map, currentBlockHeight: Int): SendPaymentResult { val logger = MDCLogger(logger, staticMdc = request.mdc()) @@ -291,8 +293,8 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle } private fun removeFromState(paymentId: UUID) { - val children = childToParentId.filterValues { it == paymentId }.keys - children.forEach { childToParentId.remove(it) } + val children = childToPaymentId.filterValues { it == paymentId }.keys + children.forEach { childToPaymentId.remove(it) } pending.remove(paymentId) } @@ -322,7 +324,7 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle private fun createOutgoingPayment(request: PayInvoice, channel: Normal, hop: NodeHop, currentBlockHeight: Int): Triple { val logger = MDCLogger(logger, staticMdc = request.mdc()) val childId = UUID.randomUUID() - childToParentId[childId] = request.paymentId + childToPaymentId[childId] = request.paymentId val (amount, expiry, onion) = createPaymentOnion(request, hop, currentBlockHeight) val outgoingPayment = LightningOutgoingPayment.Part( id = childId,