create class for ShortChannelID and use it
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Dict, Set
|
||||
|
||||
import aiorpcx
|
||||
|
||||
@@ -33,7 +33,7 @@ from . import bitcoin
|
||||
from . import ecc
|
||||
from . import constants
|
||||
from .util import bh2u, bfh, NetworkJobOnDefaultServer
|
||||
from .lnutil import invert_short_channel_id, funding_output_script_from_keys
|
||||
from .lnutil import funding_output_script_from_keys, ShortChannelID
|
||||
from .verifier import verify_tx_is_in_block, MerkleVerificationFailure
|
||||
from .transaction import Transaction
|
||||
from .interface import GracefulDisconnect
|
||||
@@ -56,17 +56,16 @@ class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||
NetworkJobOnDefaultServer.__init__(self, network)
|
||||
self.channel_db = channel_db
|
||||
self.lock = threading.Lock()
|
||||
self.unverified_channel_info = {} # short_channel_id -> msg_payload
|
||||
self.unverified_channel_info = {} # type: Dict[ShortChannelID, dict] # scid -> msg_payload
|
||||
# channel announcements that seem to be invalid:
|
||||
self.blacklist = set() # short_channel_id
|
||||
self.blacklist = set() # type: Set[ShortChannelID]
|
||||
|
||||
def _reset(self):
|
||||
super()._reset()
|
||||
self.started_verifying_channel = set() # short_channel_id
|
||||
self.started_verifying_channel = set() # type: Set[ShortChannelID]
|
||||
|
||||
# TODO make async; and rm self.lock completely
|
||||
def add_new_channel_info(self, short_channel_id_hex, msg_payload):
|
||||
short_channel_id = bfh(short_channel_id_hex)
|
||||
def add_new_channel_info(self, short_channel_id: ShortChannelID, msg_payload):
|
||||
if short_channel_id in self.unverified_channel_info:
|
||||
return
|
||||
if short_channel_id in self.blacklist:
|
||||
@@ -93,7 +92,7 @@ class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||
for short_channel_id in unverified_channel_info:
|
||||
if short_channel_id in self.started_verifying_channel:
|
||||
continue
|
||||
block_height, tx_pos, output_idx = invert_short_channel_id(short_channel_id)
|
||||
block_height = short_channel_id.block_height
|
||||
# only resolve short_channel_id if headers are available.
|
||||
if block_height <= 0 or block_height > local_height:
|
||||
continue
|
||||
@@ -103,16 +102,17 @@ class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||
await self.group.spawn(self.network.request_chunk(block_height, None, can_return_early=True))
|
||||
continue
|
||||
self.started_verifying_channel.add(short_channel_id)
|
||||
await self.group.spawn(self.verify_channel(block_height, tx_pos, short_channel_id))
|
||||
await self.group.spawn(self.verify_channel(block_height, short_channel_id))
|
||||
#self.logger.info(f'requested short_channel_id {bh2u(short_channel_id)}')
|
||||
|
||||
async def verify_channel(self, block_height: int, tx_pos: int, short_channel_id: bytes):
|
||||
async def verify_channel(self, block_height: int, short_channel_id: ShortChannelID):
|
||||
# we are verifying channel announcements as they are from untrusted ln peers.
|
||||
# we use electrum servers to do this. however we don't trust electrum servers either...
|
||||
try:
|
||||
result = await self.network.get_txid_from_txpos(block_height, tx_pos, True)
|
||||
result = await self.network.get_txid_from_txpos(
|
||||
block_height, short_channel_id.txpos, True)
|
||||
except aiorpcx.jsonrpc.RPCError:
|
||||
# the electrum server is complaining about the tx_pos for given block.
|
||||
# the electrum server is complaining about the txpos 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
|
||||
@@ -122,7 +122,7 @@ class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||
async with self.network.bhi_lock:
|
||||
header = self.network.blockchain().read_header(block_height)
|
||||
try:
|
||||
verify_tx_is_in_block(tx_hash, merkle_branch, tx_pos, header, block_height)
|
||||
verify_tx_is_in_block(tx_hash, merkle_branch, short_channel_id.txpos, header, block_height)
|
||||
except MerkleVerificationFailure as e:
|
||||
# the electrum server sent an incorrect proof. blame is on server, not the ln peer
|
||||
raise GracefulDisconnect(e) from e
|
||||
@@ -151,28 +151,27 @@ class LNChannelVerifier(NetworkJobOnDefaultServer):
|
||||
assert msg_type == 'channel_announcement'
|
||||
redeem_script = funding_output_script_from_keys(chan_ann['bitcoin_key_1'], chan_ann['bitcoin_key_2'])
|
||||
expected_address = bitcoin.redeem_script_to_address('p2wsh', redeem_script)
|
||||
output_idx = invert_short_channel_id(short_channel_id)[2]
|
||||
try:
|
||||
actual_output = tx.outputs()[output_idx]
|
||||
actual_output = tx.outputs()[short_channel_id.output_index]
|
||||
except IndexError:
|
||||
self._blacklist_short_channel_id(short_channel_id)
|
||||
return
|
||||
if expected_address != actual_output.address:
|
||||
# FIXME what now? best would be to ban the originating ln peer.
|
||||
self.logger.info(f"funding output script mismatch for {bh2u(short_channel_id)}")
|
||||
self.logger.info(f"funding output script mismatch for {short_channel_id}")
|
||||
self._remove_channel_from_unverified_db(short_channel_id)
|
||||
return
|
||||
# put channel into channel DB
|
||||
self.channel_db.add_verified_channel_info(short_channel_id, actual_output.value)
|
||||
self._remove_channel_from_unverified_db(short_channel_id)
|
||||
|
||||
def _remove_channel_from_unverified_db(self, short_channel_id: bytes):
|
||||
def _remove_channel_from_unverified_db(self, short_channel_id: ShortChannelID):
|
||||
with self.lock:
|
||||
self.unverified_channel_info.pop(short_channel_id, None)
|
||||
try: self.started_verifying_channel.remove(short_channel_id)
|
||||
except KeyError: pass
|
||||
|
||||
def _blacklist_short_channel_id(self, short_channel_id: bytes) -> None:
|
||||
def _blacklist_short_channel_id(self, short_channel_id: ShortChannelID) -> None:
|
||||
self.blacklist.add(short_channel_id)
|
||||
with self.lock:
|
||||
self.unverified_channel_info.pop(short_channel_id, None)
|
||||
|
||||
Reference in New Issue
Block a user