htlc_switch: decouple maybe_send_commitment from htlc processing
This commit is contained in:
@@ -1239,32 +1239,28 @@ class Peer(Logger):
|
|||||||
# all good
|
# all good
|
||||||
return preimage, None
|
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}")
|
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}"
|
assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
|
||||||
chan.settle_htlc(preimage, htlc_id)
|
chan.settle_htlc(preimage, htlc_id)
|
||||||
payment_hash = sha256(preimage)
|
payment_hash = sha256(preimage)
|
||||||
self.lnworker.payment_received(payment_hash)
|
self.lnworker.payment_received(payment_hash)
|
||||||
remote_ctn = chan.get_latest_ctn(REMOTE)
|
|
||||||
self.send_message("update_fulfill_htlc",
|
self.send_message("update_fulfill_htlc",
|
||||||
channel_id=chan.channel_id,
|
channel_id=chan.channel_id,
|
||||||
id=htlc_id,
|
id=htlc_id,
|
||||||
payment_preimage=preimage)
|
payment_preimage=preimage)
|
||||||
await self.await_remote(chan, remote_ctn)
|
|
||||||
|
|
||||||
async def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
||||||
reason: OnionRoutingFailureMessage):
|
reason: OnionRoutingFailureMessage):
|
||||||
self.logger.info(f"fail_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}. reason: {reason}")
|
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}"
|
assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
|
||||||
chan.fail_htlc(htlc_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)
|
error_packet = construct_onion_error(reason, onion_packet, our_onion_private_key=self.privkey)
|
||||||
self.send_message("update_fail_htlc",
|
self.send_message("update_fail_htlc",
|
||||||
channel_id=chan.channel_id,
|
channel_id=chan.channel_id,
|
||||||
id=htlc_id,
|
id=htlc_id,
|
||||||
len=len(error_packet),
|
len=len(error_packet),
|
||||||
reason=error_packet)
|
reason=error_packet)
|
||||||
await self.await_remote(chan, remote_ctn)
|
|
||||||
|
|
||||||
def on_revoke_and_ack(self, payload):
|
def on_revoke_and_ack(self, payload):
|
||||||
channel_id = payload["channel_id"]
|
channel_id = payload["channel_id"]
|
||||||
|
|||||||
@@ -1335,12 +1335,14 @@ class LNWallet(LNWorker):
|
|||||||
if not chan.can_send_ctx_updates():
|
if not chan.can_send_ctx_updates():
|
||||||
continue
|
continue
|
||||||
peer = self.peers[chan.node_id]
|
peer = self.peers[chan.node_id]
|
||||||
|
peer.maybe_send_commitment(chan)
|
||||||
done = set()
|
done = set()
|
||||||
unfulfilled = chan.hm.log.get('unfulfilled_htlcs', {})
|
unfulfilled = chan.hm.log.get('unfulfilled_htlcs', {})
|
||||||
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarded) in unfulfilled.items():
|
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarded) in unfulfilled.items():
|
||||||
# todo: decouple this from processing.
|
if chan.get_oldest_unrevoked_ctn(LOCAL) <= local_ctn:
|
||||||
await peer.await_local(chan, local_ctn)
|
continue
|
||||||
await peer.await_remote(chan, remote_ctn)
|
if chan.get_oldest_unrevoked_ctn(REMOTE) <= remote_ctn:
|
||||||
|
continue
|
||||||
chan.logger.info(f'found unfulfilled htlc: {htlc_id}')
|
chan.logger.info(f'found unfulfilled htlc: {htlc_id}')
|
||||||
onion_packet = OnionPacket.from_bytes(bytes.fromhex(onion_packet_hex))
|
onion_packet = OnionPacket.from_bytes(bytes.fromhex(onion_packet_hex))
|
||||||
htlc = chan.hm.log[REMOTE]['adds'][htlc_id]
|
htlc = chan.hm.log[REMOTE]['adds'][htlc_id]
|
||||||
@@ -1362,18 +1364,16 @@ class LNWallet(LNWorker):
|
|||||||
processed_onion=processed_onion)
|
processed_onion=processed_onion)
|
||||||
if not error:
|
if not error:
|
||||||
unfulfilled[htlc_id] = local_ctn, remote_ctn, onion_packet_hex, True
|
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:
|
else:
|
||||||
f = self.pending_payments[payment_hash]
|
f = self.pending_payments[payment_hash]
|
||||||
if f.done():
|
if f.done():
|
||||||
success, preimage, error = f.result()
|
success, preimage, error = f.result()
|
||||||
if preimage:
|
if preimage:
|
||||||
await self.enable_htlc_settle.wait()
|
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)
|
done.add(htlc_id)
|
||||||
if error:
|
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)
|
done.add(htlc_id)
|
||||||
# cleanup
|
# cleanup
|
||||||
for htlc_id in done:
|
for htlc_id in done:
|
||||||
|
|||||||
Reference in New Issue
Block a user