Browse Source

split lnpeer.fail_htlc into two methods with less parameters

master
ThomasV 5 years ago
parent
commit
47b3c49b25
  1. 58
      electrum/lnpeer.py

58
electrum/lnpeer.py

@ -1314,28 +1314,29 @@ class Peer(Logger):
id=htlc_id, id=htlc_id,
payment_preimage=preimage) payment_preimage=preimage)
def fail_htlc(self, *, chan: Channel, htlc_id: int, onion_packet: Optional[OnionPacket], def fail_htlc(self, *, chan: Channel, htlc_id: int, error_bytes: bytes):
reason: Optional[OnionRoutingFailureMessage], error_bytes: Optional[bytes]): self.logger.info(f"fail_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}.")
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)
if onion_packet and reason: self.send_message(
error_bytes = construct_onion_error(reason, onion_packet, our_onion_private_key=self.privkey) "update_fail_htlc",
if error_bytes: channel_id=chan.channel_id,
self.send_message("update_fail_htlc", id=htlc_id,
channel_id=chan.channel_id, len=len(error_bytes),
id=htlc_id, reason=error_bytes)
len=len(error_bytes),
reason=error_bytes) def fail_malformed_htlc(self, *, chan: Channel, htlc_id: int, reason: OnionRoutingFailureMessage):
else: self.logger.info(f"fail_malformed_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}.")
assert reason is not None assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
if not (reason.code & OnionFailureCodeMetaFlag.BADONION and len(reason.data) == 32): chan.fail_htlc(htlc_id)
raise Exception(f"unexpected reason when sending 'update_fail_malformed_htlc': {reason!r}") if not (reason.code & OnionFailureCodeMetaFlag.BADONION and len(reason.data) == 32):
self.send_message("update_fail_malformed_htlc", raise Exception(f"unexpected reason when sending 'update_fail_malformed_htlc': {reason!r}")
channel_id=chan.channel_id, self.send_message(
id=htlc_id, "update_fail_malformed_htlc",
sha256_of_onion=reason.data, channel_id=chan.channel_id,
failure_code=reason.code) id=htlc_id,
sha256_of_onion=reason.data,
failure_code=reason.code)
def on_revoke_and_ack(self, chan: Channel, payload): def on_revoke_and_ack(self, chan: Channel, payload):
if chan.peer_state == PeerState.BAD: if chan.peer_state == PeerState.BAD:
@ -1574,11 +1575,18 @@ class Peer(Logger):
self.fulfill_htlc(chan, htlc.htlc_id, preimage) self.fulfill_htlc(chan, htlc.htlc_id, preimage)
done.add(htlc_id) done.add(htlc_id)
if error_reason or error_bytes: if error_reason or error_bytes:
self.fail_htlc(chan=chan, if onion_packet and error_reason:
htlc_id=htlc.htlc_id, error_bytes = construct_onion_error(error_reason, onion_packet, our_onion_private_key=self.privkey)
onion_packet=onion_packet, if error_bytes:
reason=error_reason, self.fail_htlc(
error_bytes=error_bytes) chan=chan,
htlc_id=htlc.htlc_id,
error_bytes=error_bytes)
else:
self.fail_malformed_htlc(
chan=chan,
htlc_id=htlc.htlc_id,
reason=error_reason)
done.add(htlc_id) done.add(htlc_id)
# cleanup # cleanup
for htlc_id in done: for htlc_id in done:

Loading…
Cancel
Save