chainsyncer

Blockchain syncer driver
Log | Files | Refs | LICENSE

commit a414f0ed5b79f0947ec5db2eb8d9b45a9fb0397f
parent c0e4ab5c2cd361d24da5a080ac79021b6a251d14
Author: nolash <dev@holbrook.no>
Date:   Wed,  3 Feb 2021 21:57:26 +0100

Add get block eth websocket

Diffstat:
Mcic_syncer/client/evm/__pycache__/response.cpython-38.pyc | 0
Mcic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc | 0
Mcic_syncer/client/evm/response.py | 8++++++--
Mcic_syncer/client/evm/websocket.py | 22++++++++++++++++++++++
Mcic_syncer/driver.py | 21++++-----------------
Mcic_syncer/runnable/tracker.py | 5++---
6 files changed, 34 insertions(+), 22 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 @@ -1,7 +1,11 @@ +import json + from cic_syncer.client import translate + translations = { - 'block_number': 'hex_to_int', + 'block_number': translate.hex_to_int, + 'get_block': json.dumps, } @@ -10,7 +14,7 @@ class EVMResponse: def __init__(self, item, response_object): self.response_object = response_object self.item = item - self.fn = getattr(translate, translations[self.item]) + self.fn = translations[self.item] def get_error(self): diff --git a/cic_syncer/client/evm/websocket.py b/cic_syncer/client/evm/websocket.py @@ -1,3 +1,4 @@ +import logging import uuid import json @@ -6,6 +7,7 @@ import websocket from .response import EVMResponse from cic_syncer.error import RequestError +logg = logging.getLogger() class EVMWebsocketClient: @@ -35,3 +37,23 @@ class EVMWebsocketClient: raise RequestError(err) return res.get_result() + + + def get_block_by_integer(self, n): + req_id = str(uuid.uuid4()) + nhx = '0x' + n.to_bytes(8, 'big').hex() + req = { + 'jsonrpc': '2.0', + 'method': 'eth_getBlockByNumber', + 'id': str(req_id), + 'params': [nhx, False], + } + self.conn.send(json.dumps(req)) + r = self.conn.recv() + res = EVMResponse('get_block', json.loads(r)) + err = res.get_error() + if err != None: + raise RequestError(err) + + return res.get_result() + diff --git a/cic_syncer/driver.py b/cic_syncer/driver.py @@ -3,9 +3,6 @@ import uuid import logging import time -# third-party imports -import websockets - logg = logging.getLogger() @@ -36,10 +33,8 @@ class MinedSyncer(Syncer): super(MinedSyncer, self).__init__(backend) - def loop(self, interval): + def loop(self, interval, getter): while self.running and Syncer.running_global: - getter = self.backend.connect() - logg.debug('loop execute') e = self.get(getter) time.sleep(interval) @@ -53,17 +48,9 @@ class HeadSyncer(MinedSyncer): def get(self, getter): (block_number, tx_number) = self.backend.get() block_hash = [] - try: - uu = uuid.uuid4() - req = { - 'jsonrpc': '2.0', - 'method': 'eth_getBlock', - 'id': str(uu), - 'param': [block_number], - } - logg.debug(req) - except Exception as e: - logg.error(e) + uu = uuid.uuid4() + res = getter.get_block_by_integer(block_number) + logg.debug(res) return block_hash diff --git a/cic_syncer/runnable/tracker.py b/cic_syncer/runnable/tracker.py @@ -97,9 +97,7 @@ chain = args.i def main(): - block_offset = c.block_number() - logg.debug('block offset {}'.format(block_offset)) syncer_backend = SyncerBackend.live(chain, block_offset+1) syncer = HeadSyncer(syncer_backend) @@ -113,7 +111,8 @@ def main(): syncer.filter.append(task_pair) try: - syncer.loop(int(config.get('SYNCER_LOOP_INTERVAL'))) + logg.debug('block offset {} {}'.format(block_offset, c)) + syncer.loop(int(config.get('SYNCER_LOOP_INTERVAL')), c) except LoopDone as e: sys.stderr.write("sync '{}' done at block {}\n".format(args.mode, e))