mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-27 23:41:35 +00:00
channel watcher class
This commit is contained in:
parent
3dbd1e4af6
commit
1be29dbad6
2 changed files with 45 additions and 11 deletions
39
lib/lnwatcher.py
Normal file
39
lib/lnwatcher.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
from .util import PrintError
|
||||||
|
from .lnbase import Outpoint, 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 ={}
|
||||||
|
|
||||||
|
def parse_response(self, response):
|
||||||
|
if response.get('error'):
|
||||||
|
self.print_error("response error:", response)
|
||||||
|
return None, None
|
||||||
|
return response['params'], response['result']
|
||||||
|
|
||||||
|
def watch_channel(self, chan):
|
||||||
|
script = funding_output_script(chan.local_config, chan.remote_config)
|
||||||
|
funding_address = redeem_script_to_address('p2wsh', script)
|
||||||
|
self.channels[funding_address] = chan
|
||||||
|
self.network.subscribe_to_addresses([funding_address], self.on_address_status)
|
||||||
|
|
||||||
|
def on_address_status(self, response):
|
||||||
|
params, result = self.parse_response(response)
|
||||||
|
if not params:
|
||||||
|
return
|
||||||
|
addr = params[0]
|
||||||
|
self.network.request_address_utxos(addr, self.on_utxos)
|
||||||
|
|
||||||
|
def on_utxos(self, response):
|
||||||
|
params, result = self.parse_response(response)
|
||||||
|
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"
|
|
@ -7,14 +7,15 @@ import threading
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from . import constants
|
from . import constants
|
||||||
from .bitcoin import sha256, COIN, address_to_scripthash, redeem_script_to_address
|
from .bitcoin import sha256, COIN
|
||||||
from .util import bh2u, bfh, PrintError
|
from .util import bh2u, bfh, PrintError
|
||||||
from .constants import set_testnet, set_simnet
|
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, funding_output_script
|
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 .lightning_payencode.lnaddr import lnencode, LnAddr, lndecode
|
||||||
from . import lnrouter
|
from . import lnrouter
|
||||||
from .ecc import ECPrivkey, CURVE_ORDER, der_sig_from_sig_string
|
from .ecc import ECPrivkey, CURVE_ORDER, der_sig_from_sig_string
|
||||||
from .transaction import Transaction
|
from .transaction import Transaction
|
||||||
|
from .lnwatcher import LNWatcher
|
||||||
|
|
||||||
is_key = lambda k: k.endswith("_basepoint") or k.endswith("_key")
|
is_key = lambda k: k.endswith("_basepoint") or k.endswith("_key")
|
||||||
|
|
||||||
|
@ -99,6 +100,9 @@ class LNWorker(PrintError):
|
||||||
self.invoices = wallet.storage.get('lightning_invoices', {})
|
self.invoices = wallet.storage.get('lightning_invoices', {})
|
||||||
peer_list = network.config.get('lightning_peers', node_list)
|
peer_list = network.config.get('lightning_peers', node_list)
|
||||||
self.channel_state = {chan.channel_id: "DISCONNECTED" for chan in self.channels.values()}
|
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)
|
||||||
for host, port, pubkey in peer_list:
|
for host, port, pubkey in peer_list:
|
||||||
self.add_peer(host, int(port), pubkey)
|
self.add_peer(host, int(port), pubkey)
|
||||||
# wait until we see confirmations
|
# wait until we see confirmations
|
||||||
|
@ -111,15 +115,6 @@ class LNWorker(PrintError):
|
||||||
|
|
||||||
def add_peer(self, host, port, pubkey):
|
def add_peer(self, host, port, pubkey):
|
||||||
node_id = bfh(pubkey)
|
node_id = bfh(pubkey)
|
||||||
for chan_id in self.channels_for_peer(node_id):
|
|
||||||
chan = self.channels[chan_id]
|
|
||||||
script = funding_output_script(chan.local_config, chan.remote_config)
|
|
||||||
funding_address = redeem_script_to_address('p2wsh', script)
|
|
||||||
scripthash = address_to_scripthash(funding_address)
|
|
||||||
utxos = self.network.listunspent_for_scripthash(scripthash)
|
|
||||||
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"
|
|
||||||
peer = Peer(self, host, int(port), node_id, request_initial_sync=self.config.get("request_initial_sync", True))
|
peer = Peer(self, host, int(port), node_id, request_initial_sync=self.config.get("request_initial_sync", True))
|
||||||
self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop()))
|
self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop()))
|
||||||
self.peers[node_id] = peer
|
self.peers[node_id] = peer
|
||||||
|
|
Loading…
Add table
Reference in a new issue