|
|
@ -917,14 +917,16 @@ class Peer(PrintError): |
|
|
|
htlc = {'amount_msat': amount_msat_htlc, 'payment_hash':payment_hash, 'cltv_expiry':cltv_expiry} |
|
|
|
htlc_id = chan.receive_htlc(htlc) |
|
|
|
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 |
|
|
|
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) |
|
|
|
# Forward HTLC |
|
|
|
# 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 |
|
|
|
next_chan = self.lnworker.get_channel_by_short_id(dph.short_channel_id) |
|
|
|
next_peer = self.lnworker.peers[next_chan.node_id] |
|
|
@ -953,9 +955,12 @@ class Peer(PrintError): |
|
|
|
# wait until we get paid |
|
|
|
preimage = await next_peer.payment_preimages[payment_hash].get() |
|
|
|
# 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") |
|
|
|
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: |
|
|
|
preimage, invoice = self.lnworker.get_invoice(payment_hash) |
|
|
|
except UnknownPaymentHash: |
|
|
@ -986,12 +991,11 @@ class Peer(PrintError): |
|
|
|
await self.fail_htlc(chan, htlc_id, onion_packet, reason) |
|
|
|
return |
|
|
|
self.network.trigger_callback('htlc_added', UpdateAddHtlc(**htlc, htlc_id=htlc_id), invoice, RECEIVED) |
|
|
|
# settle htlc |
|
|
|
if not self.network.config.debug_lightning_do_not_settle: |
|
|
|
# settle htlc |
|
|
|
await self.fulfill_htlc(chan, htlc_id, preimage) |
|
|
|
if self.network.config.debug_lightning_do_not_settle: |
|
|
|
return |
|
|
|
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) |
|
|
|
remote_ctn = chan.get_current_ctn(REMOTE) |
|
|
|
self.send_message("update_fulfill_htlc", |
|
|
|