mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-14 14:39:50 +00:00
channel verifier: NetworkJobOnDefaultServer, and some error handling
This commit is contained in:
parent
5a081b2131
commit
17ccb79ca4
3 changed files with 60 additions and 26 deletions
|
@ -169,7 +169,6 @@ class Daemon(DaemonThread):
|
||||||
self.network.start([
|
self.network.start([
|
||||||
self.fx.run,
|
self.fx.run,
|
||||||
self.network.lnwatcher.watchtower_task,
|
self.network.lnwatcher.watchtower_task,
|
||||||
self.network.channel_db.ca_verifier.main
|
|
||||||
])
|
])
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
|
|
|
@ -26,37 +26,45 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from aiorpcx import TaskGroup
|
import aiorpcx
|
||||||
|
|
||||||
from . import lnbase
|
from . import lnbase
|
||||||
from . import bitcoin
|
from . import bitcoin
|
||||||
from . import ecc
|
from . import ecc
|
||||||
from . import constants
|
from . import constants
|
||||||
from .util import ThreadJob, bh2u, bfh
|
from .util import bh2u, bfh, NetworkJobOnDefaultServer
|
||||||
from .lnutil import invert_short_channel_id, funding_output_script_from_keys
|
from .lnutil import invert_short_channel_id, funding_output_script_from_keys
|
||||||
from .verifier import verify_tx_is_in_block, MerkleVerificationFailure
|
from .verifier import verify_tx_is_in_block, MerkleVerificationFailure
|
||||||
from .transaction import Transaction
|
from .transaction import Transaction
|
||||||
|
from .interface import GracefulDisconnect
|
||||||
|
|
||||||
|
|
||||||
class LNChannelVerifier(ThreadJob):
|
class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||||
""" Verify channel announcements for the Channel DB """
|
""" Verify channel announcements for the Channel DB """
|
||||||
|
|
||||||
|
# FIXME the initial routing sync is bandwidth-heavy, and the electrum server
|
||||||
|
# will start throttling us, making it even slower. one option would be to
|
||||||
|
# spread it over multiple servers.
|
||||||
|
|
||||||
def __init__(self, network, channel_db):
|
def __init__(self, network, channel_db):
|
||||||
self.network = network
|
NetworkJobOnDefaultServer.__init__(self, network)
|
||||||
self.channel_db = channel_db
|
self.channel_db = channel_db
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
# items only removed when whole verification succeeds for them.
|
|
||||||
# fixme: if it fails, it will never succeed
|
|
||||||
self.started_verifying_channel = set() # short_channel_id
|
|
||||||
|
|
||||||
self.unverified_channel_info = {} # short_channel_id -> channel_info
|
self.unverified_channel_info = {} # short_channel_id -> channel_info
|
||||||
|
# channel announcements that seem to be invalid:
|
||||||
|
self.blacklist = set() # short_channel_id
|
||||||
|
|
||||||
|
def _reset(self):
|
||||||
|
super()._reset()
|
||||||
|
self.started_verifying_channel = set() # short_channel_id
|
||||||
|
|
||||||
# TODO make async; and rm self.lock completely
|
# TODO make async; and rm self.lock completely
|
||||||
def add_new_channel_info(self, channel_info):
|
def add_new_channel_info(self, channel_info):
|
||||||
short_channel_id = channel_info.channel_id
|
short_channel_id = channel_info.channel_id
|
||||||
if short_channel_id in self.unverified_channel_info:
|
if short_channel_id in self.unverified_channel_info:
|
||||||
return
|
return
|
||||||
|
if short_channel_id in self.blacklist:
|
||||||
|
return
|
||||||
if not verify_sigs_for_channel_announcement(channel_info.msg_payload):
|
if not verify_sigs_for_channel_announcement(channel_info.msg_payload):
|
||||||
return
|
return
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
@ -65,13 +73,16 @@ class LNChannelVerifier(ThreadJob):
|
||||||
def get_pending_channel_info(self, short_channel_id):
|
def get_pending_channel_info(self, short_channel_id):
|
||||||
return self.unverified_channel_info.get(short_channel_id, None)
|
return self.unverified_channel_info.get(short_channel_id, None)
|
||||||
|
|
||||||
|
async def _start_tasks(self):
|
||||||
|
async with self.group as group:
|
||||||
|
await group.spawn(self.main)
|
||||||
|
|
||||||
async def main(self):
|
async def main(self):
|
||||||
while True:
|
while True:
|
||||||
async with TaskGroup() as group:
|
await self._verify_some_channels()
|
||||||
await self.iteration(group)
|
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
async def iteration(self, group: TaskGroup):
|
async def _verify_some_channels(self):
|
||||||
blockchain = self.network.blockchain()
|
blockchain = self.network.blockchain()
|
||||||
local_height = blockchain.height()
|
local_height = blockchain.height()
|
||||||
|
|
||||||
|
@ -88,15 +99,22 @@ class LNChannelVerifier(ThreadJob):
|
||||||
header = blockchain.read_header(block_height)
|
header = blockchain.read_header(block_height)
|
||||||
if header is None:
|
if header is None:
|
||||||
if block_height < constants.net.max_checkpoint():
|
if block_height < constants.net.max_checkpoint():
|
||||||
await group.spawn(self.network.request_chunk(block_height, None, can_return_early=True))
|
await self.group.spawn(self.network.request_chunk(block_height, None, can_return_early=True))
|
||||||
continue
|
continue
|
||||||
await group.spawn(self.verify_channel(block_height, tx_pos, short_channel_id))
|
self.started_verifying_channel.add(short_channel_id)
|
||||||
|
await self.group.spawn(self.verify_channel(block_height, tx_pos, short_channel_id))
|
||||||
#self.print_error('requested short_channel_id', bh2u(short_channel_id))
|
#self.print_error('requested short_channel_id', bh2u(short_channel_id))
|
||||||
|
|
||||||
async def verify_channel(self, block_height, tx_pos, short_channel_id):
|
async def verify_channel(self, block_height, tx_pos, short_channel_id):
|
||||||
with self.lock:
|
# we are verifying channel announcements as they are from untrusted ln peers.
|
||||||
self.started_verifying_channel.add(short_channel_id)
|
# we use electrum servers to do this. however we don't trust electrum servers either...
|
||||||
result = await self.network.get_txid_from_txpos(block_height, tx_pos, True)
|
try:
|
||||||
|
result = await self.network.get_txid_from_txpos(block_height, tx_pos, True)
|
||||||
|
except aiorpcx.jsonrpc.RPCError:
|
||||||
|
# the electrum server is complaining about the tx_pos for given block.
|
||||||
|
# it is not clear what to do now, but let's believe the server.
|
||||||
|
self._blacklist_short_channel_id(short_channel_id)
|
||||||
|
return
|
||||||
tx_hash = result['tx_hash']
|
tx_hash = result['tx_hash']
|
||||||
merkle_branch = result['merkle']
|
merkle_branch = result['merkle']
|
||||||
# we need to wait if header sync/reorg is still ongoing, hence lock:
|
# we need to wait if header sync/reorg is still ongoing, hence lock:
|
||||||
|
@ -105,17 +123,26 @@ class LNChannelVerifier(ThreadJob):
|
||||||
try:
|
try:
|
||||||
verify_tx_is_in_block(tx_hash, merkle_branch, tx_pos, header, block_height)
|
verify_tx_is_in_block(tx_hash, merkle_branch, tx_pos, header, block_height)
|
||||||
except MerkleVerificationFailure as e:
|
except MerkleVerificationFailure as e:
|
||||||
self.print_error(str(e))
|
# the electrum server sent an incorrect proof. blame is on server, not the ln peer
|
||||||
return
|
raise GracefulDisconnect(e) from e
|
||||||
tx = Transaction(await self.network.get_transaction(tx_hash))
|
try:
|
||||||
|
raw_tx = await self.network.get_transaction(tx_hash)
|
||||||
|
except aiorpcx.jsonrpc.RPCError as e:
|
||||||
|
# the electrum server can't find the tx; but it was the
|
||||||
|
# one who told us about the txid!! blame is on server
|
||||||
|
raise GracefulDisconnect(e) from e
|
||||||
|
tx = Transaction(raw_tx)
|
||||||
try:
|
try:
|
||||||
tx.deserialize()
|
tx.deserialize()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
# either bug in client, or electrum server is evil.
|
||||||
|
# if we connect to a diff server at some point, let's try again.
|
||||||
self.print_msg("cannot deserialize transaction, skipping", tx_hash)
|
self.print_msg("cannot deserialize transaction, skipping", tx_hash)
|
||||||
return
|
return
|
||||||
if tx_hash != tx.txid():
|
if tx_hash != tx.txid():
|
||||||
self.print_error("received tx does not match expected txid ({} != {})"
|
# either bug in client, or electrum server is evil.
|
||||||
.format(tx_hash, tx.txid()))
|
# if we connect to a diff server at some point, let's try again.
|
||||||
|
self.print_error(f"received tx does not match expected txid ({tx_hash} != {tx.txid()})")
|
||||||
return
|
return
|
||||||
# check funding output
|
# check funding output
|
||||||
channel_info = self.unverified_channel_info[short_channel_id]
|
channel_info = self.unverified_channel_info[short_channel_id]
|
||||||
|
@ -126,8 +153,12 @@ class LNChannelVerifier(ThreadJob):
|
||||||
try:
|
try:
|
||||||
actual_output = tx.outputs()[output_idx]
|
actual_output = tx.outputs()[output_idx]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
self._blacklist_short_channel_id(short_channel_id)
|
||||||
return
|
return
|
||||||
if expected_address != actual_output.address:
|
if expected_address != actual_output.address:
|
||||||
|
# FIXME what now? best would be to ban the originating ln peer.
|
||||||
|
self.print_error(f"funding output script mismatch for {bh2u(short_channel_id)}")
|
||||||
|
self.started_verifying_channel.remove(short_channel_id)
|
||||||
return
|
return
|
||||||
# put channel into channel DB
|
# put channel into channel DB
|
||||||
channel_info.set_capacity(actual_output.value)
|
channel_info.set_capacity(actual_output.value)
|
||||||
|
@ -135,8 +166,12 @@ class LNChannelVerifier(ThreadJob):
|
||||||
# remove channel from unverified
|
# remove channel from unverified
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.unverified_channel_info.pop(short_channel_id, None)
|
self.unverified_channel_info.pop(short_channel_id, None)
|
||||||
try: self.started_verifying_channel.remove(short_channel_id)
|
self.started_verifying_channel.remove(short_channel_id)
|
||||||
except KeyError: pass
|
|
||||||
|
def _blacklist_short_channel_id(self, short_channel_id: bytes) -> None:
|
||||||
|
self.blacklist.add(short_channel_id)
|
||||||
|
with self.lock:
|
||||||
|
self.unverified_channel_info.pop(short_channel_id, None)
|
||||||
|
|
||||||
|
|
||||||
def verify_sigs_for_channel_announcement(chan_ann: dict) -> bool:
|
def verify_sigs_for_channel_announcement(chan_ann: dict) -> bool:
|
||||||
|
|
|
@ -122,7 +122,7 @@ class SPV(NetworkJobOnDefaultServer):
|
||||||
self.logger.info(f"skipping merkle proof check {tx_hash}")
|
self.logger.info(f"skipping merkle proof check {tx_hash}")
|
||||||
else:
|
else:
|
||||||
self.logger.info(repr(e))
|
self.logger.info(repr(e))
|
||||||
raise GracefulDisconnect(e)
|
raise GracefulDisconnect(e) from e
|
||||||
# we passed all the tests
|
# we passed all the tests
|
||||||
self.merkle_roots[tx_hash] = header.get('merkle_root')
|
self.merkle_roots[tx_hash] = header.get('merkle_root')
|
||||||
self.requested_merkle.discard(tx_hash)
|
self.requested_merkle.discard(tx_hash)
|
||||||
|
|
Loading…
Add table
Reference in a new issue