URI: 
       tlightning: single shared instance of Watcher, ChannelDB and PathFinder - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 61983c222ae64073152e7237555e1a3464700640
   DIR parent 3fd3b2a74daa4edbc20bf13482d7e13826f0cbb6
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Tue, 26 Jun 2018 12:10:03 +0200
       
       lightning: single shared instance of Watcher, ChannelDB and PathFinder
       
       Diffstat:
         M electrum/network.py                 |       9 +++++++++
         M lib/lnbase.py                       |       6 +++---
         M lib/lnwatcher.py                    |       2 +-
         M lib/lnworker.py                     |       9 ++-------
       
       4 files changed, 15 insertions(+), 11 deletions(-)
       ---
   DIR diff --git a/electrum/network.py b/electrum/network.py
       t@@ -64,6 +64,10 @@ from .logging import get_logger, Logger
        _logger = get_logger(__name__)
        
        
       +# lightning network
       +from . import lnwatcher
       +from . import lnrouter
       +
        NODES_RETRY_INTERVAL = 60
        SERVER_RETRY_INTERVAL = 10
        NUM_TARGET_CONNECTED_SERVERS = 10
       t@@ -295,6 +299,11 @@ class Network(Logger):
        
                self._set_status('disconnected')
        
       +        # lightning network
       +        self.channel_db = lnrouter.ChannelDB()
       +        self.path_finder = lnrouter.LNPathFinder(self.channel_db)
       +        self.lnwatcher = lnwatcher.LNWatcher(self)
       +
            def run_from_another_thread(self, coro):
                assert self._loop_thread != threading.current_thread(), 'must not be called from network thread'
                fut = asyncio.run_coroutine_threadsafe(coro, self.asyncio_loop)
   DIR diff --git a/lib/lnbase.py b/lib/lnbase.py
       t@@ -592,7 +592,7 @@ class Peer(PrintError):
                self.lnworker = lnworker
                self.privkey = lnworker.privkey
                self.network = lnworker.network
       -        self.channel_db = lnworker.channel_db
       +        self.channel_db = lnworker.network.channel_db
                self.channel_state = lnworker.channel_state
                self.read_buffer = b''
                self.ping_time = 0
       t@@ -1118,7 +1118,7 @@ class Peer(PrintError):
                except IndexError:
                    print("payment destination reported error")
        
       -        self.lnworker.path_finder.blacklist.add(short_chan_id)
       +        self.network.path_finder.blacklist.add(short_chan_id)
                self.update_fail_htlc[payload["channel_id"]].put_nowait("HTLC failure with code {} (categories {})".format(code, codes))
        
            @aiosafe
       t@@ -1126,7 +1126,7 @@ class Peer(PrintError):
                assert self.channel_state[chan.channel_id] == "OPEN"
                assert amount_msat > 0, "amount_msat is not greater zero"
                height = self.network.get_local_height()
       -        route = self.lnworker.path_finder.create_route_from_path(path, self.lnworker.pubkey)
       +        route = self.network.path_finder.create_route_from_path(path, self.lnworker.pubkey)
                hops_data = []
                sum_of_deltas = sum(route_edge.channel_policy.cltv_expiry_delta for route_edge in route[1:])
                total_fee = 0
   DIR diff --git a/lib/lnwatcher.py b/lib/lnwatcher.py
       t@@ -4,7 +4,7 @@ from .bitcoin import redeem_script_to_address
        
        class LNWatcher(PrintError):
        
       -    def __init__(self, network, channel_state):
       +    def __init__(self, network):
                self.network = network
                self.watched_channels = {}
        
   DIR diff --git a/lib/lnworker.py b/lib/lnworker.py
       t@@ -12,10 +12,8 @@ from .util import bh2u, bfh, PrintError
        from .constants import set_testnet, set_simnet
        from .lnbase import Peer, Outpoint, ChannelConfig, LocalState, RemoteState, Keypair, OnlyPubkeyKeypair, OpenChannel, ChannelConstraints, RevocationStore, calc_short_channel_id, privkey_to_pubkey
        from .lightning_payencode.lnaddr import lnencode, LnAddr, lndecode
       -from . import lnrouter
        from .ecc import ECPrivkey, CURVE_ORDER, der_sig_from_sig_string
        from .transaction import Transaction
       -from .lnwatcher import LNWatcher
        
        is_key = lambda k: k.endswith("_basepoint") or k.endswith("_key")
        
       t@@ -94,15 +92,12 @@ class LNWorker(PrintError):
                self.peers = {}
                # view of the network
                self.nodes = {}  # received node announcements
       -        self.channel_db = lnrouter.ChannelDB()
       -        self.path_finder = lnrouter.LNPathFinder(self.channel_db)
                self.channels = {x.channel_id: x for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))}
                self.invoices = wallet.storage.get('lightning_invoices', {})
                peer_list = network.config.get('lightning_peers', node_list)
                self.channel_state = {chan.channel_id: "DISCONNECTED" for chan in self.channels.values()}
       -        self.lnwatcher = LNWatcher(network, self.channel_state)
                for chan_id, chan in self.channels.items():
       -            self.lnwatcher.watch_channel(chan, self.on_channel_utxos)
       +            self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos)
                for host, port, pubkey in peer_list:
                    self.add_peer(host, int(port), pubkey)
                # wait until we see confirmations
       t@@ -199,7 +194,7 @@ class LNWorker(PrintError):
                payment_hash = addr.paymenthash
                invoice_pubkey = addr.pubkey.serialize()
                amount_msat = int(addr.amount * COIN * 1000)
       -        path = self.path_finder.find_path_for_payment(self.pubkey, invoice_pubkey, amount_msat)
       +        path = self.network.path_finder.find_path_for_payment(self.pubkey, invoice_pubkey, amount_msat)
                if path is None:
                    raise Exception("No path found")
                node_id, short_channel_id = path[0]