Skip to content

Commit 61983c2

Browse files
committed
lightning: single shared instance of Watcher, ChannelDB and PathFinder
1 parent 3fd3b2a commit 61983c2

4 files changed

Lines changed: 15 additions & 11 deletions

File tree

electrum/network.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
_logger = get_logger(__name__)
6565

6666

67+
# lightning network
68+
from . import lnwatcher
69+
from . import lnrouter
70+
6771
NODES_RETRY_INTERVAL = 60
6872
SERVER_RETRY_INTERVAL = 10
6973
NUM_TARGET_CONNECTED_SERVERS = 10
@@ -295,6 +299,11 @@ def __init__(self, config: SimpleConfig=None):
295299

296300
self._set_status('disconnected')
297301

302+
# lightning network
303+
self.channel_db = lnrouter.ChannelDB()
304+
self.path_finder = lnrouter.LNPathFinder(self.channel_db)
305+
self.lnwatcher = lnwatcher.LNWatcher(self)
306+
298307
def run_from_another_thread(self, coro):
299308
assert self._loop_thread != threading.current_thread(), 'must not be called from network thread'
300309
fut = asyncio.run_coroutine_threadsafe(coro, self.asyncio_loop)

lib/lnbase.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def __init__(self, lnworker, host, port, pubkey, request_initial_sync=False):
592592
self.lnworker = lnworker
593593
self.privkey = lnworker.privkey
594594
self.network = lnworker.network
595-
self.channel_db = lnworker.channel_db
595+
self.channel_db = lnworker.network.channel_db
596596
self.channel_state = lnworker.channel_state
597597
self.read_buffer = b''
598598
self.ping_time = 0
@@ -1118,15 +1118,15 @@ def on_update_fail_htlc(self, payload):
11181118
except IndexError:
11191119
print("payment destination reported error")
11201120

1121-
self.lnworker.path_finder.blacklist.add(short_chan_id)
1121+
self.network.path_finder.blacklist.add(short_chan_id)
11221122
self.update_fail_htlc[payload["channel_id"]].put_nowait("HTLC failure with code {} (categories {})".format(code, codes))
11231123

11241124
@aiosafe
11251125
async def pay(self, path, chan, amount_msat, payment_hash, pubkey_in_invoice, min_final_cltv_expiry):
11261126
assert self.channel_state[chan.channel_id] == "OPEN"
11271127
assert amount_msat > 0, "amount_msat is not greater zero"
11281128
height = self.network.get_local_height()
1129-
route = self.lnworker.path_finder.create_route_from_path(path, self.lnworker.pubkey)
1129+
route = self.network.path_finder.create_route_from_path(path, self.lnworker.pubkey)
11301130
hops_data = []
11311131
sum_of_deltas = sum(route_edge.channel_policy.cltv_expiry_delta for route_edge in route[1:])
11321132
total_fee = 0

lib/lnwatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class LNWatcher(PrintError):
66

7-
def __init__(self, network, channel_state):
7+
def __init__(self, network):
88
self.network = network
99
self.watched_channels = {}
1010

lib/lnworker.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
from .constants import set_testnet, set_simnet
1313
from .lnbase import Peer, Outpoint, ChannelConfig, LocalState, RemoteState, Keypair, OnlyPubkeyKeypair, OpenChannel, ChannelConstraints, RevocationStore, calc_short_channel_id, privkey_to_pubkey
1414
from .lightning_payencode.lnaddr import lnencode, LnAddr, lndecode
15-
from . import lnrouter
1615
from .ecc import ECPrivkey, CURVE_ORDER, der_sig_from_sig_string
1716
from .transaction import Transaction
18-
from .lnwatcher import LNWatcher
1917

2018
is_key = lambda k: k.endswith("_basepoint") or k.endswith("_key")
2119

@@ -94,15 +92,12 @@ def __init__(self, wallet, network):
9492
self.peers = {}
9593
# view of the network
9694
self.nodes = {} # received node announcements
97-
self.channel_db = lnrouter.ChannelDB()
98-
self.path_finder = lnrouter.LNPathFinder(self.channel_db)
9995
self.channels = {x.channel_id: x for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))}
10096
self.invoices = wallet.storage.get('lightning_invoices', {})
10197
peer_list = network.config.get('lightning_peers', node_list)
10298
self.channel_state = {chan.channel_id: "DISCONNECTED" for chan in self.channels.values()}
103-
self.lnwatcher = LNWatcher(network, self.channel_state)
10499
for chan_id, chan in self.channels.items():
105-
self.lnwatcher.watch_channel(chan, self.on_channel_utxos)
100+
self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos)
106101
for host, port, pubkey in peer_list:
107102
self.add_peer(host, int(port), pubkey)
108103
# wait until we see confirmations
@@ -199,7 +194,7 @@ def pay(self, invoice):
199194
payment_hash = addr.paymenthash
200195
invoice_pubkey = addr.pubkey.serialize()
201196
amount_msat = int(addr.amount * COIN * 1000)
202-
path = self.path_finder.find_path_for_payment(self.pubkey, invoice_pubkey, amount_msat)
197+
path = self.network.path_finder.find_path_for_payment(self.pubkey, invoice_pubkey, amount_msat)
203198
if path is None:
204199
raise Exception("No path found")
205200
node_id, short_channel_id = path[0]

0 commit comments

Comments
 (0)