lightning: single shared instance of Watcher, ChannelDB and PathFinder

This commit is contained in:
ThomasV 2018-06-26 12:10:03 +02:00 committed by SomberNight
parent 7711bc1074
commit 586dc0a891
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
4 changed files with 15 additions and 11 deletions

View file

@ -50,6 +50,10 @@ from .interface import Interface, serialize_server, deserialize_server, RequestT
from .version import PROTOCOL_VERSION
from .simple_config import SimpleConfig
# lightning network
from . import lnwatcher
from . import lnrouter
NODES_RETRY_INTERVAL = 60
SERVER_RETRY_INTERVAL = 10
@ -229,6 +233,11 @@ class Network(PrintError):
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)

View file

@ -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

View file

@ -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 = {}

View file

@ -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]