From 88df1418b0c3814f49185cebd7e7021c27ff8125 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 9 Feb 2021 23:26:48 +0100 Subject: [PATCH] Use hexathon, block string representation --- .../evm/__pycache__/response.cpython-38.pyc | Bin 1886 -> 0 bytes .../evm/__pycache__/websocket.cpython-38.pyc | Bin 2454 -> 0 bytes cic_syncer/client/evm/response.py | 4 +++ cic_syncer/client/evm/websocket.py | 7 +++-- cic_syncer/client/translate.py | 25 +++--------------- cic_syncer/driver.py | 13 ++++----- cic_syncer/runnable/tracker.py | 5 ++-- requirements.txt | 1 + 8 files changed, 24 insertions(+), 31 deletions(-) delete mode 100644 cic_syncer/client/evm/__pycache__/response.cpython-38.pyc delete mode 100644 cic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc diff --git a/cic_syncer/client/evm/__pycache__/response.cpython-38.pyc b/cic_syncer/client/evm/__pycache__/response.cpython-38.pyc deleted file mode 100644 index 59f7df3747ddea05b00b849d5fadb909ee956904..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1886 zcmZux-EJH;6t+D-JNuKQr70zpRw@FCK_dGC2t`zgEB1mEkmkm0#!j=_`3d$c+D6-} zRO(A`kzD4Le9INDfI#9qo=m#Q;*mY)9NWjAzjKa%+S(d0w5LxV|N2kB*q@}Vw-Cw! zx;@39nBoP?=r0OhicD~lgCZ!yEc9|%L}i@CoM|!dX*N&jucAMKtp7bzvFg2Ls&_83 z0klN*q4jlq9%Mu412u#`)FJed;)g69{e@H6B;~H(m}zB;X``V;-xhWL3W9jCOanJM zB2`w4@<%aqf0mH?l{9CP8?UbTWEE0Y=&@aC{S0-?BMtJFXNNC)Z=1mPsY~dW4&y~`79s5 zG*fF$T{i$(S9LkdE!n@aud7@e5@jbAGhH=f{d+m?(CnYSc8Qd;YSu`3AII1j1LHBi zv)#4(?{|xGP7AIZ$wY=HVgvzKXBS-YOA4i!xJVmQn^gFlxBzjw;$|BQ{hiw39@I93 ze(@uyme^(d?BO~B3bJ2u3j(c-&^?4tA*|R1!QdAN?G#NUM>^@A1Na7^)wj8!wjiN%$r)QV% z@7OV7gPc#Y;2jH*@kEA;0Q^*1K(&9!Odpfv z!!Aw{oF~xyJ<_mcFlx)P-z>Xg4Nw4AsBS59`3(+)E?#F&#k6 z8>&0M!EIf;%5_n6?mT&v4#x9FxJdJk6X9`ga7L+gK#RzA$Jyo^0nQLlAoEXBPP@8B zbP;vW(JeuxdQkKjpuZ7BA<=gp`0B{o>YwHfRX=j>8Fm=jkA(F37tGYs@%l~{IMFP> zq!VZ<oPLkF$-@-gz^`5Y6p!rusK9T~EuTP~d2y2|dqf5i54 z|A=|8*`p2nzE3wpamaQzXPc#`*QA(tky@0e*3h%%Yp6|LI{5!Ey5#G&Y`-B+Ezy#Q S5yuyXsQvKYxVQb_Ui=^GB8H;? diff --git a/cic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc b/cic_syncer/client/evm/__pycache__/websocket.cpython-38.pyc deleted file mode 100644 index 58a6efe5cda7f8255fafbdcf624532e309d5667b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2454 zcmbuBOK%)S5P-XT-s@ezu*k!Z@CZaJz-yu$5sDH8IKUnf@z7j`-RWj`oq5>Zy@_p& zFWH>=0f@uw1`~m~W_}*Tf$e))m`8DB`+nBS zm*OQ#1gYC|5_i5PJmBFo!owMhmpOe%y3uoZAL&1*AiLBo-T&dc2SV+Zg%U7qKM;FU zq4a$zO9|uP$wZHn?S1Hmu=8$KrjJrouy`>T+<{x&1|bNV52jPjo)IwJ;V!f;_qY$O z*BBK0JmM`F2Yd;PYMXV?{-YQw&@c48Y$6JsqTZ7W`{=TJaI3dLRJ0-q(X?h2onznO zEYE2t+3n97~#V+9^BRlj9qOZ%*l)%QG~7m&5Vu+ zcYb$|7?|kPuJ&rbVsijR(P*a*=H$rHA;*=da^`f#j@-(vTQKic9w(0+In+y)H=|D} z%tJIon0Wmc&aJKOctt=^4lx${(|jo8u^R_Fsx0JgYWy4)OK#YN8-I5o2e~qByEiH9 z=5EJ0N=xHTr?ACCdAso$#6rQ1i<*qbr}?fjt`zBGiGgRltQ>Gr%>sxOdUy8j1bUV-*DzR0@foINfke5NhRk#%jLfec{1`)yj@%?q$qN_B-A3MB$ z0h`57I+H0_c1{8P0fPlVkd+m!Z8@2<+MSb2fPq790yH+GmBalJ;{kwk0TV#eK^<1^ zDL8(=@`BOUd^`qZ00KJ-OGd~-FLcB8@RFVHu9zSzN25tGYVyfu3~r1= zC};NnLQ@x4U!8nguj*pFxiFpD193~sL7_4@h%K}D+Uq;u$lN;Bj=5)Z{9CX?K*2_P etZ^58jS0VKeDWn8lL~#%RL_vLA<5v!Lg!zau|w?u diff --git a/cic_syncer/client/evm/response.py b/cic_syncer/client/evm/response.py index c53933c..9d4f17a 100644 --- 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 index 8feadde..3a3ba4a 100644 --- 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 index 4331afd..0b29e47 100644 --- 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 index 87cfa60..2068ea3 100644 --- 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 index ae9dc21..7b9c5e3 100644 --- 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 index 6f24aa8..b2a8af7 100644 --- 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