Skip to content

Commit

Permalink
lnpeer.maybe_fulfill_htlc: follow BOLTs re some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SomberNight committed Feb 25, 2021
1 parent 61e7f7e commit 31bdb5c
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions electrum/lnpeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,13 +1507,21 @@ async def forward_trampoline_payment():

asyncio.ensure_future(forward_trampoline_payment())


def maybe_fulfill_htlc(
self, *,
chan: Channel,
htlc: UpdateAddHtlc,
processed_onion: ProcessedOnionPacket,
is_trampoline:bool = False) -> Optional[bytes]:
is_trampoline: bool = False,
) -> Optional[bytes]:
"""As a final recipient of an HTLC, decide if we should fulfill it.
Returns the preimage if yes, or None.
"""

try:
amt_to_forward = processed_onion.hop_data.payload["amt_to_forward"]["amt_to_forward"]
except:
raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')

# Check that our blockchain tip is sufficiently recent so that we have an approx idea of the height.
# We should not release the preimage for an HTLC that its sender could already time out as
Expand All @@ -1522,8 +1530,11 @@ def maybe_fulfill_htlc(
if chain.is_tip_stale():
raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
local_height = chain.height()
exc_incorrect_or_unknown_pd = OnionRoutingFailure(
code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
data=amt_to_forward.to_bytes(8, byteorder="big") + local_height.to_bytes(4, byteorder="big"))
if local_height + MIN_FINAL_CLTV_EXPIRY_ACCEPTED > htlc.cltv_expiry:
raise OnionRoutingFailure(code=OnionFailureCode.FINAL_EXPIRY_TOO_SOON, data=b'')
raise exc_incorrect_or_unknown_pd
try:
cltv_from_onion = processed_onion.hop_data.payload["outgoing_cltv_value"]["outgoing_cltv_value"]
except:
Expand All @@ -1534,10 +1545,6 @@ def maybe_fulfill_htlc(
raise OnionRoutingFailure(
code=OnionFailureCode.FINAL_INCORRECT_CLTV_EXPIRY,
data=htlc.cltv_expiry.to_bytes(4, byteorder="big"))
try:
amt_to_forward = processed_onion.hop_data.payload["amt_to_forward"]["amt_to_forward"]
except:
raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
try:
total_msat = processed_onion.hop_data.payload["payment_data"]["total_msat"]
except:
Expand All @@ -1554,21 +1561,21 @@ def maybe_fulfill_htlc(

info = self.lnworker.get_payment_info(htlc.payment_hash)
if info is None:
raise OnionRoutingFailure(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'')
raise exc_incorrect_or_unknown_pd
preimage = self.lnworker.get_preimage(htlc.payment_hash)
try:
payment_secret_from_onion = processed_onion.hop_data.payload["payment_data"]["payment_secret"]
except:
if total_msat > amt_to_forward:
# payment_secret is required for MPP
raise OnionRoutingFailure(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'')
raise exc_incorrect_or_unknown_pd
# TODO fail here if invoice has set PAYMENT_SECRET_REQ
else:
if payment_secret_from_onion != derive_payment_secret_from_payment_preimage(preimage):
raise OnionRoutingFailure(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'')
raise exc_incorrect_or_unknown_pd
invoice_msat = info.amount_msat
if not (invoice_msat is None or invoice_msat <= total_msat <= 2 * invoice_msat):
raise OnionRoutingFailure(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'')
raise exc_incorrect_or_unknown_pd
accepted, expired = self.lnworker.htlc_received(chan.short_channel_id, htlc, total_msat)
if accepted:
return preimage
Expand Down

0 comments on commit 31bdb5c

Please sign in to comment.