1
0

restructure synchronizer

fix CLI notify cmd. fix merchant websockets.
This commit is contained in:
SomberNight
2018-10-03 17:13:46 +02:00
parent 788b5b04fe
commit 02f108d927
9 changed files with 249 additions and 178 deletions

View File

@@ -25,14 +25,14 @@ import asyncio
from typing import Sequence, Optional
import aiorpcx
from aiorpcx import TaskGroup
from .util import PrintError, bh2u, VerifiedTxInfo
from .util import bh2u, VerifiedTxInfo
from .bitcoin import Hash, hash_decode, hash_encode
from .transaction import Transaction
from .blockchain import hash_header
from .interface import GracefulDisconnect
from . import constants
from .network import NetworkJobOnDefaultServer
class MerkleVerificationFailure(Exception): pass
@@ -41,26 +41,33 @@ class MerkleRootMismatch(MerkleVerificationFailure): pass
class InnerNodeOfSpvProofIsValidTx(MerkleVerificationFailure): pass
class SPV(PrintError):
class SPV(NetworkJobOnDefaultServer):
""" Simple Payment Verification """
def __init__(self, network, wallet):
NetworkJobOnDefaultServer.__init__(self, network)
self.wallet = wallet
self.network = network
def _reset(self):
super()._reset()
self.merkle_roots = {} # txid -> merkle root (once it has been verified)
self.requested_merkle = set() # txid set of pending requests
async def _start_tasks(self):
async with self.group as group:
await group.spawn(self.main)
def diagnostic_name(self):
return '{}:{}'.format(self.__class__.__name__, self.wallet.diagnostic_name())
async def main(self, group: TaskGroup):
async def main(self):
self.blockchain = self.network.blockchain()
while True:
await self._maybe_undo_verifications()
await self._request_proofs(group)
await self._request_proofs()
await asyncio.sleep(0.1)
async def _request_proofs(self, group: TaskGroup):
async def _request_proofs(self):
local_height = self.blockchain.height()
unverified = self.wallet.get_unverified_txs()
@@ -75,12 +82,12 @@ class SPV(PrintError):
header = self.blockchain.read_header(tx_height)
if header is None:
if tx_height < constants.net.max_checkpoint():
await group.spawn(self.network.request_chunk(tx_height, None, can_return_early=True))
await self.group.spawn(self.network.request_chunk(tx_height, None, can_return_early=True))
continue
# request now
self.print_error('requested merkle', tx_hash)
self.requested_merkle.add(tx_hash)
await group.spawn(self._request_and_verify_single_proof, tx_hash, tx_height)
await self.group.spawn(self._request_and_verify_single_proof, tx_hash, tx_height)
async def _request_and_verify_single_proof(self, tx_hash, tx_height):
try: