mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-03 12:30:07 +00:00
lnchannelverifier: some clean-up
This commit is contained in:
parent
c91fe27e7d
commit
200b012e57
1 changed files with 16 additions and 15 deletions
|
@ -25,11 +25,13 @@
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from aiorpcx import TaskGroup
|
from aiorpcx import TaskGroup
|
||||||
|
|
||||||
from . import lnbase
|
from . import lnbase
|
||||||
from . import bitcoin
|
from . import bitcoin
|
||||||
from . import ecc
|
from . import ecc
|
||||||
|
from . import constants
|
||||||
from .util import ThreadJob, bh2u, bfh
|
from .util import ThreadJob, bh2u, bfh
|
||||||
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
|
||||||
|
@ -50,6 +52,7 @@ class LNChannelVerifier(ThreadJob):
|
||||||
|
|
||||||
self.unverified_channel_info = {} # short_channel_id -> channel_info
|
self.unverified_channel_info = {} # short_channel_id -> channel_info
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
@ -64,18 +67,13 @@ class LNChannelVerifier(ThreadJob):
|
||||||
|
|
||||||
async def main(self):
|
async def main(self):
|
||||||
while True:
|
while True:
|
||||||
async with TaskGroup() as tg:
|
async with TaskGroup() as group:
|
||||||
await self.iteration(tg)
|
await self.iteration(group)
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
async def iteration(self, tg):
|
async def iteration(self, group: TaskGroup):
|
||||||
interface = self.network.interface
|
blockchain = self.network.blockchain()
|
||||||
if not interface:
|
local_height = blockchain.height()
|
||||||
return
|
|
||||||
|
|
||||||
blockchain = interface.blockchain
|
|
||||||
if not blockchain:
|
|
||||||
return
|
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
unverified_channel_info = list(self.unverified_channel_info)
|
unverified_channel_info = list(self.unverified_channel_info)
|
||||||
|
@ -85,13 +83,14 @@ class LNChannelVerifier(ThreadJob):
|
||||||
continue
|
continue
|
||||||
block_height, tx_pos, output_idx = invert_short_channel_id(short_channel_id)
|
block_height, tx_pos, output_idx = invert_short_channel_id(short_channel_id)
|
||||||
# only resolve short_channel_id if headers are available.
|
# only resolve short_channel_id if headers are available.
|
||||||
|
if block_height <= 0 or block_height > local_height:
|
||||||
|
continue
|
||||||
header = blockchain.read_header(block_height)
|
header = blockchain.read_header(block_height)
|
||||||
if header is None:
|
if header is None:
|
||||||
index = block_height // 2016
|
if block_height < constants.net.max_checkpoint():
|
||||||
if index < len(blockchain.checkpoints):
|
await group.spawn(self.network.request_chunk(block_height, None, can_return_early=True))
|
||||||
await tg.spawn(self.network.request_chunk(block_height, None, can_return_early=True))
|
|
||||||
continue
|
continue
|
||||||
await tg.spawn(self.verify_channel(block_height, tx_pos, short_channel_id))
|
await 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):
|
||||||
|
@ -100,7 +99,9 @@ class LNChannelVerifier(ThreadJob):
|
||||||
result = await self.network.get_txid_from_txpos(block_height, tx_pos, True)
|
result = await self.network.get_txid_from_txpos(block_height, tx_pos, True)
|
||||||
tx_hash = result['tx_hash']
|
tx_hash = result['tx_hash']
|
||||||
merkle_branch = result['merkle']
|
merkle_branch = result['merkle']
|
||||||
header = self.network.blockchain().read_header(block_height)
|
# we need to wait if header sync/reorg is still ongoing, hence lock:
|
||||||
|
async with self.network.bhi_lock:
|
||||||
|
header = self.network.blockchain().read_header(block_height)
|
||||||
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:
|
||||||
|
|
Loading…
Add table
Reference in a new issue