mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-03 02:35:20 +00:00
htlc_switch: decouple maybe_send_commitment from htlc processing
This commit is contained in:
parent
cfc20845a2
commit
5d3bca7bb8
2 changed files with 10 additions and 14 deletions
|
@ -1239,32 +1239,28 @@ class Peer(Logger):
|
|||
# all good
|
||||
return preimage, None
|
||||
|
||||
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
|
||||
def fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
|
||||
self.logger.info(f"_fulfill_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
|
||||
assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
|
||||
chan.settle_htlc(preimage, htlc_id)
|
||||
payment_hash = sha256(preimage)
|
||||
self.lnworker.payment_received(payment_hash)
|
||||
remote_ctn = chan.get_latest_ctn(REMOTE)
|
||||
self.send_message("update_fulfill_htlc",
|
||||
channel_id=chan.channel_id,
|
||||
id=htlc_id,
|
||||
payment_preimage=preimage)
|
||||
await self.await_remote(chan, remote_ctn)
|
||||
|
||||
async def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
||||
reason: OnionRoutingFailureMessage):
|
||||
def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
||||
reason: OnionRoutingFailureMessage):
|
||||
self.logger.info(f"fail_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}. reason: {reason}")
|
||||
assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
|
||||
chan.fail_htlc(htlc_id)
|
||||
remote_ctn = chan.get_latest_ctn(REMOTE)
|
||||
error_packet = construct_onion_error(reason, onion_packet, our_onion_private_key=self.privkey)
|
||||
self.send_message("update_fail_htlc",
|
||||
channel_id=chan.channel_id,
|
||||
id=htlc_id,
|
||||
len=len(error_packet),
|
||||
reason=error_packet)
|
||||
await self.await_remote(chan, remote_ctn)
|
||||
|
||||
def on_revoke_and_ack(self, payload):
|
||||
channel_id = payload["channel_id"]
|
||||
|
|
|
@ -1335,12 +1335,14 @@ class LNWallet(LNWorker):
|
|||
if not chan.can_send_ctx_updates():
|
||||
continue
|
||||
peer = self.peers[chan.node_id]
|
||||
peer.maybe_send_commitment(chan)
|
||||
done = set()
|
||||
unfulfilled = chan.hm.log.get('unfulfilled_htlcs', {})
|
||||
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarded) in unfulfilled.items():
|
||||
# todo: decouple this from processing.
|
||||
await peer.await_local(chan, local_ctn)
|
||||
await peer.await_remote(chan, remote_ctn)
|
||||
if chan.get_oldest_unrevoked_ctn(LOCAL) <= local_ctn:
|
||||
continue
|
||||
if chan.get_oldest_unrevoked_ctn(REMOTE) <= remote_ctn:
|
||||
continue
|
||||
chan.logger.info(f'found unfulfilled htlc: {htlc_id}')
|
||||
onion_packet = OnionPacket.from_bytes(bytes.fromhex(onion_packet_hex))
|
||||
htlc = chan.hm.log[REMOTE]['adds'][htlc_id]
|
||||
|
@ -1362,18 +1364,16 @@ class LNWallet(LNWorker):
|
|||
processed_onion=processed_onion)
|
||||
if not error:
|
||||
unfulfilled[htlc_id] = local_ctn, remote_ctn, onion_packet_hex, True
|
||||
next_remote_ctn = next_chan.get_latest_ctn(REMOTE)
|
||||
await next_peer.await_remote(next_chan, next_remote_ctn)
|
||||
else:
|
||||
f = self.pending_payments[payment_hash]
|
||||
if f.done():
|
||||
success, preimage, error = f.result()
|
||||
if preimage:
|
||||
await self.enable_htlc_settle.wait()
|
||||
await peer._fulfill_htlc(chan, htlc.htlc_id, preimage)
|
||||
peer.fulfill_htlc(chan, htlc.htlc_id, preimage)
|
||||
done.add(htlc_id)
|
||||
if error:
|
||||
await peer.fail_htlc(chan, htlc.htlc_id, onion_packet, error)
|
||||
peer.fail_htlc(chan, htlc.htlc_id, onion_packet, error)
|
||||
done.add(htlc_id)
|
||||
# cleanup
|
||||
for htlc_id in done:
|
||||
|
|
Loading…
Add table
Reference in a new issue