From 0e7fab5e60392178ea39b5eee02ceff68ffe62c0 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Fri, 22 Jun 2018 12:17:11 +0200 Subject: [PATCH] reestablish channels in network callback --- lib/lnbase.py | 2 -- lib/lnwatcher.py | 15 ++++++--------- lib/lnworker.py | 10 +++++++++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/lnbase.py b/lib/lnbase.py index 68a87544..fd1b8b1c 100644 --- a/lib/lnbase.py +++ b/lib/lnbase.py @@ -790,8 +790,6 @@ class Peer(PrintError): msg = await self.read_message() self.process_message(msg) self.initialized.set_result(True) - # reestablish channels - [self.reestablish_channel(c) for c in self.channels.values() if self.lnworker.channel_state[c.channel_id] != "CLOSED"] # loop while True: self.ping_if_required() diff --git a/lib/lnwatcher.py b/lib/lnwatcher.py index 4cea46c5..4f64470d 100644 --- a/lib/lnwatcher.py +++ b/lib/lnwatcher.py @@ -1,13 +1,12 @@ from .util import PrintError -from .lnbase import Outpoint, funding_output_script +from .lnbase import funding_output_script from .bitcoin import redeem_script_to_address class LNWatcher(PrintError): def __init__(self, network, channel_state): self.network = network - self.channel_state = channel_state - self.channels ={} + self.watched_channels = {} def parse_response(self, response): if response.get('error'): @@ -15,10 +14,10 @@ class LNWatcher(PrintError): return None, None return response['params'], response['result'] - def watch_channel(self, chan): + def watch_channel(self, chan, callback): script = funding_output_script(chan.local_config, chan.remote_config) funding_address = redeem_script_to_address('p2wsh', script) - self.channels[funding_address] = chan + self.watched_channels[funding_address] = chan, callback self.network.subscribe_to_addresses([funding_address], self.on_address_status) def on_address_status(self, response): @@ -33,7 +32,5 @@ class LNWatcher(PrintError): if not params: return addr = params[0] - chan = self.channels[addr] - outpoints = [Outpoint(x["tx_hash"], x["tx_pos"]) for x in result] - if chan.funding_outpoint not in outpoints: - self.channel_state[chan.channel_id] = "CLOSED" + chan, callback = self.watched_channels[addr] + callback(chan, result) diff --git a/lib/lnworker.py b/lib/lnworker.py index 84e0880c..acf8d5db 100644 --- a/lib/lnworker.py +++ b/lib/lnworker.py @@ -102,7 +102,7 @@ class LNWorker(PrintError): 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.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 @@ -153,6 +153,14 @@ class LNWorker(PrintError): return chan return None + def on_channel_utxos(self, chan, utxos): + outpoints = [Outpoint(x["tx_hash"], x["tx_pos"]) for x in utxos] + if chan.funding_outpoint not in outpoints: + self.channel_state[chan.channel_id] = "CLOSED" + elif chan.channel_id not in self.channel_state: + peer = self.peers[chan.node_id] + peer.reestablish_channel(c) + def on_network_update(self, event, *args): for chan in self.channels.values(): peer = self.peers[chan.node_id]