mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-28 16:01:30 +00:00
lnpeer: receive_and_revoke, send_and_revoke
This commit is contained in:
parent
f4b2644620
commit
fa96efabb5
1 changed files with 35 additions and 28 deletions
|
@ -916,10 +916,7 @@ class Peer(PrintError):
|
||||||
# process update_fail_htlc on channel
|
# process update_fail_htlc on channel
|
||||||
chan = self.channels[channel_id]
|
chan = self.channels[channel_id]
|
||||||
chan.receive_fail_htlc(htlc_id)
|
chan.receive_fail_htlc(htlc_id)
|
||||||
await self.receive_commitment(chan)
|
await self.receive_and_revoke(chan)
|
||||||
self.revoke(chan)
|
|
||||||
self.send_commitment(chan) # htlc will be removed
|
|
||||||
await self.receive_revoke(chan)
|
|
||||||
self.network.trigger_callback('ln_message', self.lnworker, 'Payment failed', htlc_id)
|
self.network.trigger_callback('ln_message', self.lnworker, 'Payment failed', htlc_id)
|
||||||
|
|
||||||
async def _handle_error_code_from_failed_htlc(self, error_reason, route: List['RouteEdge'], channel_id, htlc_id):
|
async def _handle_error_code_from_failed_htlc(self, error_reason, route: List['RouteEdge'], channel_id, htlc_id):
|
||||||
|
@ -968,14 +965,19 @@ class Peer(PrintError):
|
||||||
self.send_message("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs))
|
self.send_message("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs))
|
||||||
return len(htlc_sigs)
|
return len(htlc_sigs)
|
||||||
|
|
||||||
async def update_channel(self, chan: Channel, message_name: str, **kwargs):
|
async def send_and_revoke(self, chan: Channel):
|
||||||
""" generic channel update flow """
|
""" generic channel update flow """
|
||||||
self.send_message(message_name, **kwargs)
|
|
||||||
self.send_commitment(chan)
|
self.send_commitment(chan)
|
||||||
await self.receive_revoke(chan)
|
await self.receive_revoke(chan)
|
||||||
await self.receive_commitment(chan)
|
await self.receive_commitment(chan)
|
||||||
self.revoke(chan)
|
self.revoke(chan)
|
||||||
|
|
||||||
|
async def receive_and_revoke(self, chan: Channel):
|
||||||
|
await self.receive_commitment(chan)
|
||||||
|
self.revoke(chan)
|
||||||
|
self.send_commitment(chan)
|
||||||
|
await self.receive_revoke(chan)
|
||||||
|
|
||||||
async def pay(self, route: List['RouteEdge'], chan: Channel, amount_msat: int,
|
async def pay(self, route: List['RouteEdge'], chan: Channel, amount_msat: int,
|
||||||
payment_hash: bytes, min_final_cltv_expiry: int):
|
payment_hash: bytes, min_final_cltv_expiry: int):
|
||||||
assert chan.get_state() == "OPEN", chan.get_state()
|
assert chan.get_state() == "OPEN", chan.get_state()
|
||||||
|
@ -992,7 +994,14 @@ class Peer(PrintError):
|
||||||
chan.onion_keys[htlc_id] = secret_key
|
chan.onion_keys[htlc_id] = secret_key
|
||||||
self.attempted_route[(chan.channel_id, htlc_id)] = route
|
self.attempted_route[(chan.channel_id, htlc_id)] = route
|
||||||
self.print_error(f"starting payment. route: {route}")
|
self.print_error(f"starting payment. route: {route}")
|
||||||
await self.update_channel(chan, "update_add_htlc", channel_id=chan.channel_id, id=htlc_id, cltv_expiry=cltv, amount_msat=amount_msat, payment_hash=payment_hash, onion_routing_packet=onion.to_bytes())
|
self.send_message("update_add_htlc",
|
||||||
|
channel_id=chan.channel_id,
|
||||||
|
id=htlc_id,
|
||||||
|
cltv_expiry=cltv,
|
||||||
|
amount_msat=amount_msat,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
onion_routing_packet=onion.to_bytes())
|
||||||
|
await self.send_and_revoke(chan)
|
||||||
return UpdateAddHtlc(**htlc, htlc_id=htlc_id)
|
return UpdateAddHtlc(**htlc, htlc_id=htlc_id)
|
||||||
|
|
||||||
async def receive_revoke(self, chan: Channel):
|
async def receive_revoke(self, chan: Channel):
|
||||||
|
@ -1028,12 +1037,8 @@ class Peer(PrintError):
|
||||||
preimage = update_fulfill_htlc_msg["payment_preimage"]
|
preimage = update_fulfill_htlc_msg["payment_preimage"]
|
||||||
htlc_id = int.from_bytes(update_fulfill_htlc_msg["id"], "big")
|
htlc_id = int.from_bytes(update_fulfill_htlc_msg["id"], "big")
|
||||||
chan.receive_htlc_settle(preimage, htlc_id)
|
chan.receive_htlc_settle(preimage, htlc_id)
|
||||||
await self.receive_commitment(chan)
|
await self.receive_and_revoke(chan)
|
||||||
self.revoke(chan)
|
|
||||||
self.send_commitment(chan) # htlc will be removed
|
|
||||||
await self.receive_revoke(chan)
|
|
||||||
self.network.trigger_callback('ln_message', self.lnworker, 'Payment sent', htlc_id)
|
self.network.trigger_callback('ln_message', self.lnworker, 'Payment sent', htlc_id)
|
||||||
|
|
||||||
# used in lightning-integration
|
# used in lightning-integration
|
||||||
self.payment_preimages[sha256(preimage)].put_nowait(preimage)
|
self.payment_preimages[sha256(preimage)].put_nowait(preimage)
|
||||||
|
|
||||||
|
@ -1060,10 +1065,7 @@ class Peer(PrintError):
|
||||||
# add htlc
|
# add htlc
|
||||||
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)
|
||||||
await self.receive_commitment(chan)
|
await self.receive_and_revoke(chan)
|
||||||
self.revoke(chan)
|
|
||||||
self.send_commitment(chan)
|
|
||||||
await self.receive_revoke(chan)
|
|
||||||
# maybe fail htlc
|
# maybe fail htlc
|
||||||
if not processed_onion.are_we_final:
|
if not processed_onion.are_we_final:
|
||||||
# no forwarding for now
|
# no forwarding for now
|
||||||
|
@ -1103,14 +1105,15 @@ class Peer(PrintError):
|
||||||
# settle htlc
|
# settle htlc
|
||||||
if not self.network.config.debug_lightning_do_not_settle:
|
if not self.network.config.debug_lightning_do_not_settle:
|
||||||
# settle htlc
|
# settle htlc
|
||||||
await self.settle_htlc(chan, htlc_id, preimage)
|
await self.fulfill_htlc(chan, htlc_id, preimage)
|
||||||
|
|
||||||
async def settle_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)
|
||||||
await self.update_channel(chan, "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.send_and_revoke(chan)
|
||||||
self.network.trigger_callback('ln_message', self.lnworker, 'Payment received', htlc_id)
|
self.network.trigger_callback('ln_message', self.lnworker, 'Payment received', htlc_id)
|
||||||
|
|
||||||
async def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
async def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket,
|
||||||
|
@ -1118,11 +1121,12 @@ class Peer(PrintError):
|
||||||
self.print_error(f"failing received htlc {(bh2u(chan.channel_id), htlc_id)}. reason: {reason}")
|
self.print_error(f"failing received htlc {(bh2u(chan.channel_id), htlc_id)}. reason: {reason}")
|
||||||
chan.fail_htlc(htlc_id)
|
chan.fail_htlc(htlc_id)
|
||||||
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)
|
||||||
await self.update_channel(chan, "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.send_and_revoke(chan)
|
||||||
|
|
||||||
def on_revoke_and_ack(self, payload):
|
def on_revoke_and_ack(self, payload):
|
||||||
self.print_error("got revoke_and_ack")
|
self.print_error("got revoke_and_ack")
|
||||||
|
@ -1152,7 +1156,10 @@ class Peer(PrintError):
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
chan.update_fee(feerate_per_kw, True)
|
chan.update_fee(feerate_per_kw, True)
|
||||||
await self.update_channel(chan, "update_fee", channel_id=chan.channel_id, feerate_per_kw=feerate_per_kw)
|
self.send_message("update_fee",
|
||||||
|
channel_id=chan.channel_id,
|
||||||
|
feerate_per_kw=feerate_per_kw)
|
||||||
|
await self.send_and_revoke(chan)
|
||||||
|
|
||||||
def on_closing_signed(self, payload):
|
def on_closing_signed(self, payload):
|
||||||
chan_id = payload["channel_id"]
|
chan_id = payload["channel_id"]
|
||||||
|
|
Loading…
Add table
Reference in a new issue