Use hexathon, block string representation

This commit is contained in:
nolash 2021-02-09 23:26:48 +01:00
parent 8c67758b10
commit 88df1418b0
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
8 changed files with 24 additions and 31 deletions

View File

@ -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))

View File

@ -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',

View File

@ -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)

View File

@ -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

View File

@ -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(','):

View File

@ -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