Add get block eth websocket
This commit is contained in:
parent
c0e4ab5c2c
commit
a414f0ed5b
Binary file not shown.
Binary file not shown.
@ -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):
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
res = getter.get_block_by_integer(block_number)
|
||||
logg.debug(res)
|
||||
|
||||
return block_hash
|
||||
|
||||
|
@ -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))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user