Browse Source

code refactoring: _maybe_fullfill_htlc, _maybe_forward_htlc

regtest_lnd
ThomasV 6 years ago
committed by SomberNight
parent
commit
6d25670356
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 24
      electrum/lnpeer.py

24
electrum/lnpeer.py

@ -917,14 +917,16 @@ class Peer(PrintError):
htlc = {'amount_msat': amount_msat_htlc, 'payment_hash':payment_hash, 'cltv_expiry':cltv_expiry} htlc = {'amount_msat': amount_msat_htlc, 'payment_hash':payment_hash, 'cltv_expiry':cltv_expiry}
htlc_id = chan.receive_htlc(htlc) htlc_id = chan.receive_htlc(htlc)
local_ctn = chan.get_current_ctn(LOCAL) local_ctn = chan.get_current_ctn(LOCAL)
asyncio.ensure_future(self._on_update_add_htlc(chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion)) if processed_onion.are_we_final:
asyncio.ensure_future(self._maybe_fulfill_htlc(chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion))
else:
asyncio.ensure_future(self._maybe_forward_htlc(chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion))
@log_exceptions @log_exceptions
async def _on_update_add_htlc(self, chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion): async def _maybe_forward_htlc(self, chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion):
await self.await_local(chan, local_ctn) await self.await_local(chan, local_ctn)
# Forward HTLC # Forward HTLC
# FIXME: this is not robust to us going offline before payment is fulfilled # FIXME: this is not robust to us going offline before payment is fulfilled
if not processed_onion.are_we_final:
dph = processed_onion.hop_data.per_hop dph = processed_onion.hop_data.per_hop
next_chan = self.lnworker.get_channel_by_short_id(dph.short_channel_id) next_chan = self.lnworker.get_channel_by_short_id(dph.short_channel_id)
next_peer = self.lnworker.peers[next_chan.node_id] next_peer = self.lnworker.peers[next_chan.node_id]
@ -953,9 +955,12 @@ class Peer(PrintError):
# wait until we get paid # wait until we get paid
preimage = await next_peer.payment_preimages[payment_hash].get() preimage = await next_peer.payment_preimages[payment_hash].get()
# fulfill the original htlc # fulfill the original htlc
await self.fulfill_htlc(chan, htlc_id, preimage) await self._fulfill_htlc(chan, htlc_id, preimage)
self.print_error("htlc forwarded successfully") self.print_error("htlc forwarded successfully")
return
@log_exceptions
async def _maybe_fulfill_htlc(self, chan, local_ctn, htlc_id, htlc, payment_hash, cltv_expiry, amount_msat_htlc, processed_onion):
await self.await_local(chan, local_ctn)
try: try:
preimage, invoice = self.lnworker.get_invoice(payment_hash) preimage, invoice = self.lnworker.get_invoice(payment_hash)
except UnknownPaymentHash: except UnknownPaymentHash:
@ -986,12 +991,11 @@ class Peer(PrintError):
await self.fail_htlc(chan, htlc_id, onion_packet, reason) await self.fail_htlc(chan, htlc_id, onion_packet, reason)
return return
self.network.trigger_callback('htlc_added', UpdateAddHtlc(**htlc, htlc_id=htlc_id), invoice, RECEIVED) self.network.trigger_callback('htlc_added', UpdateAddHtlc(**htlc, htlc_id=htlc_id), invoice, RECEIVED)
# settle htlc if self.network.config.debug_lightning_do_not_settle:
if not self.network.config.debug_lightning_do_not_settle: return
# settle htlc await self._fulfill_htlc(chan, htlc_id, preimage)
await self.fulfill_htlc(chan, htlc_id, preimage)
async def fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes): async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
chan.settle_htlc(preimage, htlc_id) chan.settle_htlc(preimage, htlc_id)
remote_ctn = chan.get_current_ctn(REMOTE) remote_ctn = chan.get_current_ctn(REMOTE)
self.send_message("update_fulfill_htlc", self.send_message("update_fulfill_htlc",

Loading…
Cancel
Save