electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 44412335961d56b771b1ad648f24f8add04d1f12
parent 242ab5ae563c433d528650a61bd3b1c50f7ddcb2
Author: ThomasV <thomasv@electrum.org>
Date:   Mon,  8 Oct 2018 18:33:23 +0200

get rid of callbacks in lnwatcher, use network events instead

Diffstat:
Melectrum/lnwatcher.py | 14++++----------
Melectrum/lnworker.py | 18+++++++++++++-----
2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py @@ -27,10 +27,7 @@ class LNWatcher(PrintError): self.addr_sync.start_network(network) self.lock = threading.RLock() self.watched_addresses = set() - self.channel_info = storage.get('channel_info', {}) # access with 'lock' - self.funding_txo_spent_callback = {} # funding_outpoint -> callback - # TODO structure will need to change when we handle HTLCs...... # [funding_outpoint_str][ctx_txid] -> set of EncumberedTransaction # access with 'lock' @@ -58,12 +55,11 @@ class LNWatcher(PrintError): storage.put('sweepstore', sweepstore) storage.write() - def watch_channel(self, address, outpoint, callback_funding_txo_spent): + def watch_channel(self, address, outpoint): self.watch_address(address) with self.lock: if address not in self.channel_info: self.channel_info[address] = outpoint - self.funding_txo_spent_callback[outpoint] = callback_funding_txo_spent self.write_to_disk() @aiosafe @@ -90,11 +86,9 @@ class LNWatcher(PrintError): async def check_onchain_situation(self, funding_outpoint): txid, index = funding_outpoint.split(':') ctx_candidate_txid = self.addr_sync.spent_outpoints[txid].get(int(index)) - # call funding_txo_spent_callback if there is one - is_funding_txo_spent = ctx_candidate_txid is not None - cb = self.funding_txo_spent_callback.get(funding_outpoint) - if cb: cb(is_funding_txo_spent) - if not is_funding_txo_spent: + is_spent = ctx_candidate_txid is not None + self.network.trigger_callback('channel_txo', funding_outpoint, is_spent) + if not is_spent: return ctx_candidate = self.addr_sync.transactions.get(ctx_candidate_txid) if ctx_candidate is None: diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -56,11 +56,12 @@ class LNWorker(PrintError): c.sweep_address = self.sweep_address self.invoices = wallet.storage.get('lightning_invoices', {}) for chan_id, chan in self.channels.items(): - self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str(), partial(self.on_channel_utxos, chan)) + self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str()) self._last_tried_peer = {} # LNPeerAddr -> unix timestamp self._add_peers_from_config() # wait until we see confirmations self.network.register_callback(self.on_network_update, ['network_updated', 'verified', 'fee']) # thread safe + self.network.register_callback(self.on_channel_txo, ['channel_txo']) asyncio.run_coroutine_threadsafe(self.network.main_taskgroup.spawn(self.main_loop()), self.network.asyncio_loop) def _read_ln_keystore(self) -> BIP32_KeyStore: @@ -145,9 +146,16 @@ class LNWorker(PrintError): return True, conf return False, conf - def on_channel_utxos(self, chan, is_funding_txo_spent: bool): - chan.set_funding_txo_spentness(is_funding_txo_spent) - if is_funding_txo_spent: + def on_channel_txo(self, event, txo, is_spent: bool): + with self.lock: + channels = list(self.channels.values()) + for chan in channels: + if chan.funding_outpoint.to_str() == txo: + break + else: + return + chan.set_funding_txo_spentness(is_spent) + if is_spent: chan.set_state("CLOSED") self.channel_db.remove_channel(chan.short_channel_id) self.network.trigger_callback('channel', chan) @@ -188,7 +196,7 @@ class LNWorker(PrintError): push_msat=push_sat * 1000, temp_channel_id=os.urandom(32)) self.save_channel(chan) - self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str(), partial(self.on_channel_utxos, chan)) + self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str()) self.on_channels_updated() def on_channels_updated(self):