Browse Source

small clean-up re max CLTV delta for LN

hard-fail-on-bad-server-string
SomberNight 5 years ago
parent
commit
90f3b667aa
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 5
      electrum/lnpeer.py
  2. 4
      electrum/lnrouter.py
  3. 2
      electrum/lnutil.py
  4. 2
      electrum/lnworker.py

5
electrum/lnpeer.py

@ -1027,13 +1027,16 @@ class Peer(Logger):
assert amount_msat > 0, "amount_msat is not greater zero"
if not chan.can_send_update_add_htlc():
raise PaymentFailure("Channel cannot send update_add_htlc")
local_height = self.network.get_local_height()
# create onion packet
final_cltv = self.network.get_local_height() + min_final_cltv_expiry
final_cltv = local_height + min_final_cltv_expiry
hops_data, amount_msat, cltv = calc_hops_data_for_payment(route, amount_msat, final_cltv)
assert final_cltv <= cltv, (final_cltv, cltv)
secret_key = os.urandom(32)
onion = new_onion_packet([x.node_id for x in route], secret_key, hops_data, associated_data=payment_hash)
# create htlc
if cltv > local_height + lnutil.NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE:
raise PaymentFailure(f"htlc expiry too far into future. (in {cltv-local_height} blocks)")
htlc = UpdateAddHtlc(amount_msat=amount_msat, payment_hash=payment_hash, cltv_expiry=cltv, timestamp=int(time.time()))
htlc = chan.add_htlc(htlc)
chan.set_onion_key(htlc.htlc_id, secret_key)

4
electrum/lnrouter.py

@ -31,6 +31,7 @@ from .util import bh2u, profiler
from .logging import Logger
from .lnutil import NUM_MAX_EDGES_IN_PAYMENT_PATH, ShortChannelID
from .channel_db import ChannelDB, Policy
from .lnutil import NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE
if TYPE_CHECKING:
from .lnchannel import Channel
@ -99,8 +100,7 @@ def is_route_sane_to_use(route: LNPaymentRoute, invoice_amount_msat: int, min_fi
cltv += route_edge.cltv_expiry_delta
total_fee = amt - invoice_amount_msat
# TODO revise ad-hoc heuristics
# cltv cannot be more than 2 months
if cltv > 60 * 144:
if cltv > NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE:
return False
if not is_fee_sane(total_fee, payment_amount_msat=invoice_amount_msat):
return False

2
electrum/lnutil.py

@ -193,7 +193,7 @@ NBLOCK_OUR_CLTV_EXPIRY_DELTA = 144
OUR_FEE_BASE_MSAT = 1000
OUR_FEE_PROPORTIONAL_MILLIONTHS = 1
NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE = 4032
NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE = 28 * 144
# When we open a channel, the remote peer has to support at least this

2
electrum/lnworker.py

@ -1040,7 +1040,7 @@ class LNWallet(LNWorker):
addr.amount = Decimal(amount_sat) / COIN
if addr.amount is None:
raise InvoiceError(_("Missing amount"))
if addr.get_min_final_cltv_expiry() > 60 * 144:
if addr.get_min_final_cltv_expiry() > lnutil.NBLOCK_CLTV_EXPIRY_TOO_FAR_INTO_FUTURE:
raise InvoiceError("{}\n{}".format(
_("Invoice wants us to risk locking funds for unreasonably long."),
f"min_final_cltv_expiry: {addr.get_min_final_cltv_expiry()}"))

Loading…
Cancel
Save