From eca10eb04d91b5e2d2122bb281a15bebbe202dfe Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 18 Oct 2023 19:22:52 +0000 Subject: [PATCH] lnpeer.maybe_forward_trampoline: fix cltv calculation follow-up b0401a6386bb229435f786e050ba75e5eedd335c --- electrum/lnpeer.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py index 6e249ad88..0f9ea9511 100644 --- a/electrum/lnpeer.py +++ b/electrum/lnpeer.py @@ -1780,7 +1780,16 @@ class Peer(Logger): trampoline_cltv_delta = cltv_expiry - cltv_from_onion # cltv budget total_msat = outer_onion.hop_data.payload["payment_data"]["total_msat"] trampoline_fee = total_msat - amt_to_forward - self.logger.info(f'trampoline cltv and fee: {trampoline_cltv_delta, trampoline_fee}') + self.logger.info(f'trampoline forwarding. fee_budget={trampoline_fee}') + self.logger.info(f'trampoline forwarding. cltv_budget={trampoline_cltv_delta}. (inc={cltv_expiry}. out={cltv_from_onion})') + # To convert abs vs rel cltvs, we need to guess blockheight used by original sender as "current blockheight". + # Blocks might have been mined since. + # - if we skew towards the past, we decrease our own cltv_budget accordingly (which is ok) + # - if we skew towards the future, we decrease the cltv_budget for the subsequent nodes in the path, + # which can result in them failing the payment. + # So we skew towards the past and guess that there has been 1 new block mined since the payment began: + local_height_of_onion_creator = self.network.get_local_height() - 1 + cltv_budget_for_rest_of_route = cltv_from_onion - local_height_of_onion_creator try: await self.lnworker.pay_to_node( @@ -1788,10 +1797,10 @@ class Peer(Logger): payment_hash=payment_hash, payment_secret=payment_secret, amount_to_pay=amt_to_forward, - # FIXME this API (min_cltv_expiry) is bad. This is setting the cltv-delta for the last edge - # on the path to the *next trampoline* node. We should just let lnrouter set this. - # Instead, we should rewrite pay_to_node to operate on a fee-budget and cltv-budget. - min_cltv_expiry=lnutil.MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE, + # FIXME this API (min_cltv_expiry) is confusing. The value will be added to local_height + # to form the abs cltv used on the last edge on the path to the *next trampoline* node. + # We should rewrite pay_to_node to operate on a cltv-budget (and fee-budget). + min_cltv_expiry=cltv_budget_for_rest_of_route, r_tags=r_tags, invoice_features=invoice_features, fwd_trampoline_onion=next_trampoline_onion,