Use hexathon, block string representation
This commit is contained in:
parent
8c67758b10
commit
88df1418b0
Binary file not shown.
Binary file not shown.
@ -46,3 +46,7 @@ class EVMBlock(Block):
|
|||||||
|
|
||||||
def number(self):
|
def number(self):
|
||||||
return translate.hex_to_int(self.obj['number'])
|
return translate.hex_to_int(self.obj['number'])
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str('block {} {}'.format(self.number(), self.hash))
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
|
# standard imports
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
# third-party imports
|
||||||
import websocket
|
import websocket
|
||||||
|
from hexathon import add_0x
|
||||||
|
|
||||||
|
# local imports
|
||||||
from .response import EVMResponse
|
from .response import EVMResponse
|
||||||
from cic_syncer.error import RequestError
|
from cic_syncer.error import RequestError
|
||||||
from cic_syncer.client.translate import with_0x
|
|
||||||
from cic_syncer.client.evm.response import EVMBlock
|
from cic_syncer.client.evm.response import EVMBlock
|
||||||
|
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
@ -66,7 +69,7 @@ class EVMWebsocketClient:
|
|||||||
|
|
||||||
def get_block_by_hash(self, hx_in):
|
def get_block_by_hash(self, hx_in):
|
||||||
req_id = str(uuid.uuid4())
|
req_id = str(uuid.uuid4())
|
||||||
hx = with_0x(hx_in)
|
hx = add_0x(hx_in)
|
||||||
req ={
|
req ={
|
||||||
'jsonrpc': '2.0',
|
'jsonrpc': '2.0',
|
||||||
'method': 'eth_getBlockByHash',
|
'method': 'eth_getBlockByHash',
|
||||||
|
@ -1,27 +1,10 @@
|
|||||||
import re
|
# third-party imports
|
||||||
|
from hexathon import strip_0x
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def hex_to_int(hx, endianness='big'):
|
def hex_to_int(hx, endianness='big'):
|
||||||
hx = strip_0x(hx)
|
hx = strip_0x(hx)
|
||||||
|
if len(hx) % 2 == 1:
|
||||||
|
hx = '0' + hx
|
||||||
b = bytes.fromhex(hx)
|
b = bytes.fromhex(hx)
|
||||||
return int.from_bytes(b, endianness)
|
return int.from_bytes(b, endianness)
|
||||||
|
@ -36,8 +36,10 @@ class MinedSyncer(Syncer):
|
|||||||
|
|
||||||
def loop(self, interval, getter):
|
def loop(self, interval, getter):
|
||||||
while self.running and Syncer.running_global:
|
while self.running and Syncer.running_global:
|
||||||
|
while True:
|
||||||
block_hash = self.get(getter)
|
block_hash = self.get(getter)
|
||||||
if block_hash != None:
|
if block_hash == None:
|
||||||
|
break
|
||||||
self.process(getter, block_hash)
|
self.process(getter, block_hash)
|
||||||
time.sleep(interval)
|
time.sleep(interval)
|
||||||
|
|
||||||
@ -49,18 +51,18 @@ class HeadSyncer(MinedSyncer):
|
|||||||
|
|
||||||
|
|
||||||
def process(self, getter, block):
|
def process(self, getter, block):
|
||||||
logg.debug('process block {}'.format(block))
|
logg.debug('process {}'.format(block))
|
||||||
block = getter.get_block_by_hash(block.hash)
|
block = getter.get_block_by_hash(block.hash)
|
||||||
i = 0
|
i = 0
|
||||||
tx = None
|
tx = None
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self.filter[0].handle(getter, block, None)
|
#self.filter[0].handle(getter, block, None)
|
||||||
tx = block.tx(i)
|
tx = block.tx(i)
|
||||||
logg.debug('tx {}'.format(tx))
|
logg.debug('tx {}'.format(tx))
|
||||||
self.backend.set(block.number(), i)
|
self.backend.set(block.number(), i)
|
||||||
for f in self.filter:
|
for f in self.filter:
|
||||||
f(getter, block, tx)
|
f.handle(getter, block, tx)
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
self.backend.set(block.number() + 1, 0)
|
self.backend.set(block.number() + 1, 0)
|
||||||
break
|
break
|
||||||
@ -75,4 +77,3 @@ class HeadSyncer(MinedSyncer):
|
|||||||
logg.debug('get {}'.format(res))
|
logg.debug('get {}'.format(res))
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -104,13 +104,14 @@ def tx_filter(w3, tx, rcpt, chain_spec):
|
|||||||
re_websocket = re.compile('^wss?://')
|
re_websocket = re.compile('^wss?://')
|
||||||
re_http = re.compile('^https?://')
|
re_http = re.compile('^https?://')
|
||||||
c = EVMWebsocketClient(config.get('ETH_PROVIDER'))
|
c = EVMWebsocketClient(config.get('ETH_PROVIDER'))
|
||||||
chain = args.i
|
chain = config.get('CIC_CHAIN_SPEC')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
block_offset = c.block_number()
|
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)
|
syncer = HeadSyncer(syncer_backend, handler)
|
||||||
|
|
||||||
for cb in config.get('TASKS_SYNCER_CALLBACKS', '').split(','):
|
for cb in config.get('TASKS_SYNCER_CALLBACKS', '').split(','):
|
||||||
|
@ -6,3 +6,4 @@ eth-tester==0.5.0b3
|
|||||||
web3==5.12.2
|
web3==5.12.2
|
||||||
confini==0.3.6b2
|
confini==0.3.6b2
|
||||||
semver==2.13.0
|
semver==2.13.0
|
||||||
|
hexathon==0.0.1a2
|
||||||
|
Loading…
Reference in New Issue
Block a user