electrum

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

commit 61983c222ae64073152e7237555e1a3464700640
parent 3fd3b2a74daa4edbc20bf13482d7e13826f0cbb6
Author: ThomasV <thomasv@electrum.org>
Date:   Tue, 26 Jun 2018 12:10:03 +0200

lightning: single shared instance of Watcher, ChannelDB and PathFinder

Diffstat:
Melectrum/network.py | 9+++++++++
Mlib/lnbase.py | 6+++---
Mlib/lnwatcher.py | 2+-
Mlib/lnworker.py | 9++-------
4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/electrum/network.py b/electrum/network.py @@ -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 @@ -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) diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -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 @@ -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 @@ -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 diff --git a/lib/lnwatcher.py b/lib/lnwatcher.py @@ -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 = {} diff --git a/lib/lnworker.py b/lib/lnworker.py @@ -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") @@ -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 @@ -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]