tlnchannel: pass reference to lnworker - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 3e443535a27e121d428ee184734be3bb28129100 DIR parent a8e2f79563c579280ca971aecb558e5070b7a2a7 HTML Author: ThomasV <thomasv@electrum.org> Date: Wed, 27 Feb 2019 10:51:40 +0100 lnchannel: pass reference to lnworker Diffstat: M electrum/lnchannel.py | 31 +++++++++++-------------------- M electrum/lnpeer.py | 8 ++------ M electrum/lnsweep.py | 4 ++-- M electrum/lnworker.py | 4 +--- 4 files changed, 16 insertions(+), 31 deletions(-) --- DIR diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py t@@ -120,12 +120,9 @@ class Channel(PrintError): except: return super().diagnostic_name() - def __init__(self, state, *, sweep_address=None, name=None, - payment_completed: Optional[Callable[['Channel', Direction, UpdateAddHtlc], None]] = None): - if not payment_completed: - payment_completed = lambda this, x, y: None + def __init__(self, state, *, sweep_address=None, name=None, lnworker=None): + self.lnworker = lnworker self.sweep_address = sweep_address - self.payment_completed = payment_completed assert 'local_state' not in state self.config = {} self.config[LOCAL] = state["local_config"] t@@ -437,10 +434,11 @@ class Channel(PrintError): received = self.hm.received_in_ctn(self.config[LOCAL].ctn) sent = self.hm.sent_in_ctn(self.config[LOCAL].ctn) - for htlc in received: - self.payment_completed(self, RECEIVED, htlc) - for htlc in sent: - self.payment_completed(self, SENT, htlc) + if self.lnworker: + for htlc in received: + self.lnworker.payment_completed(self, RECEIVED, htlc) + for htlc in sent: + self.lnworker.payment_completed(self, SENT, htlc) received_this_batch = htlcsum(received) sent_this_batch = htlcsum(sent) t@@ -616,11 +614,8 @@ class Channel(PrintError): assert htlc_id not in log['settles'] self.hm.send_settle(htlc_id) # save timestamp in LNWorker.preimages - try: - self.save_preimage(htlc.payment_hash, preimage, timestamp=int(time.time())) - except: - import traceback - traceback.print_exc() + if self.lnworker: + self.lnworker.save_preimage(htlc.payment_hash, preimage, timestamp=int(time.time())) def receive_htlc_settle(self, preimage, htlc_id): self.print_error("receive_htlc_settle") t@@ -629,12 +624,8 @@ class Channel(PrintError): assert htlc.payment_hash == sha256(preimage) assert htlc_id not in log['settles'] self.hm.recv_settle(htlc_id) - try: - self.save_preimage(htlc.payment_hash, preimage, timestamp=int(time.time())) - except AttributeError as e: - # save_preimage is not defined in the unit tests... this is ugly as hell. FIXME - import traceback - traceback.print_exc() + if self.lnworker: + self.lnworker.save_preimage(htlc.payment_hash, preimage, timestamp=int(time.time())) def fail_htlc(self, htlc_id): self.print_error("fail_htlc") DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py t@@ -376,10 +376,8 @@ class Peer(PrintError): } chan = Channel(chan_dict, sweep_address=self.lnworker.sweep_address, - payment_completed=self.lnworker.payment_completed) + lnworker=self.lnworker) chan.lnwatcher = self.lnwatcher - chan.get_preimage = self.lnworker.get_preimage # FIXME hack. - chan.save_preimage = self.lnworker.save_preimage # FIXME hack. sig_64, _ = chan.sign_next_commitment() self.send_message("funding_created", temporary_channel_id=temp_channel_id, t@@ -470,10 +468,8 @@ class Peer(PrintError): } chan = Channel(chan_dict, sweep_address=self.lnworker.sweep_address, - payment_completed=self.lnworker.payment_completed) + lnworker=self.lnworker) chan.lnwatcher = self.lnwatcher - chan.get_preimage = self.lnworker.get_preimage # FIXME hack. - chan.save_preimage = self.lnworker.save_preimage # FIXME hack. remote_sig = funding_created['signature'] chan.receive_new_commitment(remote_sig, []) sig_64, _ = chan.sign_next_commitment() DIR diff --git a/electrum/lnsweep.py b/electrum/lnsweep.py t@@ -157,7 +157,7 @@ def create_sweeptxs_for_our_latest_ctx(chan: 'Channel', ctx: Transaction, def create_txns_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool) -> Tuple[Optional[Transaction], Optional[Transaction]]: if is_received_htlc: try: - preimage = chan.get_preimage(htlc.payment_hash) + preimage = chan.lnworker.get_preimage(htlc.payment_hash) except UnknownPaymentHash as e: print_error(f'trying to sweep htlc from our latest ctx but getting {repr(e)}') return None, None t@@ -260,7 +260,7 @@ def create_sweeptxs_for_their_latest_ctx(chan: 'Channel', ctx: Transaction, def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool) -> Optional[Transaction]: if not is_received_htlc: try: - preimage = chan.get_preimage(htlc.payment_hash) + preimage = chan.lnworker.get_preimage(htlc.payment_hash) except UnknownPaymentHash as e: print_error(f'trying to sweep htlc from their latest ctx but getting {repr(e)}') return None DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py t@@ -78,9 +78,7 @@ class LNWorker(PrintError): self.peers = {} # type: Dict[bytes, Peer] # pubkey -> Peer self.channels = {} # type: Dict[bytes, Channel] for x in wallet.storage.get("channels", []): - c = Channel(x, sweep_address=self.sweep_address, payment_completed=self.payment_completed) - c.get_preimage = self.get_preimage # FIXME hack. - c.save_preimage = self.save_preimage # FIXME hack. + c = Channel(x, sweep_address=self.sweep_address, lnworker=self) self.channels[c.channel_id] = c c.set_remote_commitment() c.set_local_commitment(c.current_commitment(LOCAL))