chainsyncer

Blockchain syncer driver
Log | Files | Refs | LICENSE

commit 88df1418b0c3814f49185cebd7e7021c27ff8125
parent 8c67758b10e4669c4debb699235c06178a4a87eb
Author: nolash <dev@holbrook.no>
Date:   Tue,  9 Feb 2021 23:26:48 +0100

Use hexathon, block string representation

Diffstat:
Dcic_syncer/client/evm/__pycache__/response.cpython-38.pyc | 0
Dcic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc | 0
Mcic_syncer/client/evm/response.py | 4++++
Mcic_syncer/client/evm/websocket.py | 7+++++--
Mcic_syncer/client/translate.py | 25++++---------------------
Mcic_syncer/driver.py | 13+++++++------
Mcic_syncer/runnable/tracker.py | 5+++--
Mrequirements.txt | 1+
8 files changed, 24 insertions(+), 31 deletions(-)

diff --git a/cic_syncer/client/evm/__pycache__/response.cpython-38.pyc b/cic_syncer/client/evm/__pycache__/response.cpython-38.pyc Binary files differ. diff --git a/cic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc b/cic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc Binary files differ. diff --git a/cic_syncer/client/evm/response.py b/cic_syncer/client/evm/response.py @@ -46,3 +46,7 @@ class EVMBlock(Block): def number(self): return translate.hex_to_int(self.obj['number']) + + + def __str__(self): + return str('block {} {}'.format(self.number(), self.hash)) diff --git a/cic_syncer/client/evm/websocket.py b/cic_syncer/client/evm/websocket.py @@ -1,12 +1,15 @@ +# standard imports import logging import uuid import json +# third-party imports import websocket +from hexathon import add_0x +# local imports from .response import EVMResponse from cic_syncer.error import RequestError -from cic_syncer.client.translate import with_0x from cic_syncer.client.evm.response import EVMBlock logg = logging.getLogger() @@ -66,7 +69,7 @@ class EVMWebsocketClient: def get_block_by_hash(self, hx_in): req_id = str(uuid.uuid4()) - hx = with_0x(hx_in) + hx = add_0x(hx_in) req ={ 'jsonrpc': '2.0', 'method': 'eth_getBlockByHash', diff --git a/cic_syncer/client/translate.py b/cic_syncer/client/translate.py @@ -1,27 +1,10 @@ -import re - -re_hex = r'^[0-9a-fA-Z]+$' -def is_hex(hx): - m = re.match(re_hex, hx) - if m == None: - raise ValueError('not valid hex {}'.format(hx)) - - return hx - - -def strip_0x(hx): - if len(hx) >= 2 and hx[:2] == '0x': - hx = hx[2:] - return is_hex(hx) - - -def with_0x(hx): - if len(hx) >= 2 and hx[:2] == '0x': - hx = hx[2:] - return '0x' + is_hex(hx) +# third-party imports +from hexathon import strip_0x def hex_to_int(hx, endianness='big'): hx = strip_0x(hx) + if len(hx) % 2 == 1: + hx = '0' + hx b = bytes.fromhex(hx) return int.from_bytes(b, endianness) diff --git a/cic_syncer/driver.py b/cic_syncer/driver.py @@ -36,8 +36,10 @@ class MinedSyncer(Syncer): def loop(self, interval, getter): while self.running and Syncer.running_global: - block_hash = self.get(getter) - if block_hash != None: + while True: + block_hash = self.get(getter) + if block_hash == None: + break self.process(getter, block_hash) time.sleep(interval) @@ -49,18 +51,18 @@ class HeadSyncer(MinedSyncer): def process(self, getter, block): - logg.debug('process block {}'.format(block)) + logg.debug('process {}'.format(block)) block = getter.get_block_by_hash(block.hash) i = 0 tx = None while True: try: - self.filter[0].handle(getter, block, None) + #self.filter[0].handle(getter, block, None) tx = block.tx(i) logg.debug('tx {}'.format(tx)) self.backend.set(block.number(), i) for f in self.filter: - f(getter, block, tx) + f.handle(getter, block, tx) except IndexError as e: self.backend.set(block.number() + 1, 0) break @@ -75,4 +77,3 @@ class HeadSyncer(MinedSyncer): logg.debug('get {}'.format(res)) return res - diff --git a/cic_syncer/runnable/tracker.py b/cic_syncer/runnable/tracker.py @@ -104,13 +104,14 @@ def tx_filter(w3, tx, rcpt, chain_spec): re_websocket = re.compile('^wss?://') re_http = re.compile('^https?://') c = EVMWebsocketClient(config.get('ETH_PROVIDER')) -chain = args.i +chain = config.get('CIC_CHAIN_SPEC') def main(): block_offset = c.block_number() - syncer_backend = SyncerBackend.live(chain, block_offset+1) + #syncer_backend = SyncerBackend.live(chain, block_offset+1) + syncer_backend = SyncerBackend.live(chain, 0) syncer = HeadSyncer(syncer_backend, handler) for cb in config.get('TASKS_SYNCER_CALLBACKS', '').split(','): diff --git a/requirements.txt b/requirements.txt @@ -6,3 +6,4 @@ eth-tester==0.5.0b3 web3==5.12.2 confini==0.3.6b2 semver==2.13.0 +hexathon==0.0.1a2