Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
It's a weird hierarchy to have `SendPaymentResult` be a
`ProcessFailureResult`. We instead return an `Either`
and upcast.

The logger belongs to the parent function and can simply
be provided in the function call.
  • Loading branch information
t-bast committed Oct 17, 2024
1 parent 07da618 commit d612732
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import fr.acinq.lightning.wire.UnknownNextPeer

class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: WalletParams, val db: OutgoingPaymentsDb) {

interface SendPaymentResult: ProcessFailureResult
interface SendPaymentResult
interface ProcessFailureResult
interface ProcessFulfillResult

Expand Down Expand Up @@ -71,8 +71,7 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle

private fun getPaymentAttempt(childId: UUID): PaymentAttempt? = childToPaymentId[childId]?.let { pending[it] }

private suspend fun sendPaymentInternal(request: PayInvoice, failures: List<Either<ChannelException, FailureMessage>>, channels: Map<ByteVector32, ChannelState>, currentBlockHeight: Int): SendPaymentResult {
val logger = MDCLogger(logger, staticMdc = request.mdc())
private suspend fun sendPaymentInternal(request: PayInvoice, failures: List<Either<ChannelException, FailureMessage>>, channels: Map<ByteVector32, ChannelState>, currentBlockHeight: Int, logger: MDCLogger): Either<Failure, Progress> {
val attemptNumber = failures.size
val trampolineFees = (request.trampolineFeesOverride ?: walletParams.trampolineFees)[attemptNumber]
logger.info { "trying payment with fee_base=${trampolineFees.feeBase}, fee_proportional=${trampolineFees.feeProportional}" }
Expand All @@ -85,7 +84,7 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle
}
db.completeOutgoingPaymentOffchain(request.paymentId, result.value)
removeFromState(request.paymentId)
Failure(request, OutgoingPaymentFailure(result.value, failures))
Either.Left(Failure(request, OutgoingPaymentFailure(result.value, failures)))
}
is Either.Right -> {
val hop = NodeHop(walletParams.trampolineNode.id, request.recipient, trampolineFees.cltvExpiryDelta, trampolineFees.calculateFees(request.amount))
Expand All @@ -96,8 +95,8 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle
db.addOutgoingLightningParts(request.paymentId, listOf(childPayment))
}
val payment = PaymentAttempt(request, attemptNumber, childPayment, sharedSecrets, failures)
pending[payment.request.paymentId] = payment
Progress(payment.request, payment.fees, listOf(cmd))
pending[request.paymentId] = payment
Either.Right(Progress(request, payment.fees, listOf(cmd)))
}
}
}
Expand All @@ -121,7 +120,7 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle
logger.error { "invoice has already been paid" }
return Failure(request, FinalFailure.AlreadyPaid.toPaymentFailure())
}
return sendPaymentInternal(request, listOf(), channels, currentBlockHeight)
return sendPaymentInternal(request, listOf(), channels, currentBlockHeight, logger).fold({ it }, { it })
}

/**
Expand Down Expand Up @@ -196,7 +195,7 @@ class OutgoingPaymentHandler(val nodeParams: NodeParams, val walletParams: Walle
Failure(payment.request, OutgoingPaymentFailure(finalError, payment.failures + failure))
} else {
// The trampoline node is asking us to retry the payment with more fees or a larger expiry delta.
sendPaymentInternal(payment.request, payment.failures + failure, channels, currentBlockHeight)
sendPaymentInternal(payment.request, payment.failures + failure, channels, currentBlockHeight, logger).fold({ it }, { it })
}
}

Expand Down

0 comments on commit d612732

Please sign in to comment.