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
|
from cic_syncer.client import translate
|
||||||
|
|
||||||
|
|
||||||
translations = {
|
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):
|
def __init__(self, item, response_object):
|
||||||
self.response_object = response_object
|
self.response_object = response_object
|
||||||
self.item = item
|
self.item = item
|
||||||
self.fn = getattr(translate, translations[self.item])
|
self.fn = translations[self.item]
|
||||||
|
|
||||||
|
|
||||||
def get_error(self):
|
def get_error(self):
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -6,6 +7,7 @@ import websocket
|
|||||||
from .response import EVMResponse
|
from .response import EVMResponse
|
||||||
from cic_syncer.error import RequestError
|
from cic_syncer.error import RequestError
|
||||||
|
|
||||||
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
class EVMWebsocketClient:
|
class EVMWebsocketClient:
|
||||||
@ -35,3 +37,23 @@ class EVMWebsocketClient:
|
|||||||
raise RequestError(err)
|
raise RequestError(err)
|
||||||
|
|
||||||
return res.get_result()
|
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 logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# third-party imports
|
|
||||||
import websockets
|
|
||||||
|
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
@ -36,10 +33,8 @@ class MinedSyncer(Syncer):
|
|||||||
super(MinedSyncer, self).__init__(backend)
|
super(MinedSyncer, self).__init__(backend)
|
||||||
|
|
||||||
|
|
||||||
def loop(self, interval):
|
def loop(self, interval, getter):
|
||||||
while self.running and Syncer.running_global:
|
while self.running and Syncer.running_global:
|
||||||
getter = self.backend.connect()
|
|
||||||
logg.debug('loop execute')
|
|
||||||
e = self.get(getter)
|
e = self.get(getter)
|
||||||
time.sleep(interval)
|
time.sleep(interval)
|
||||||
|
|
||||||
@ -53,17 +48,9 @@ class HeadSyncer(MinedSyncer):
|
|||||||
def get(self, getter):
|
def get(self, getter):
|
||||||
(block_number, tx_number) = self.backend.get()
|
(block_number, tx_number) = self.backend.get()
|
||||||
block_hash = []
|
block_hash = []
|
||||||
try:
|
|
||||||
uu = uuid.uuid4()
|
uu = uuid.uuid4()
|
||||||
req = {
|
res = getter.get_block_by_integer(block_number)
|
||||||
'jsonrpc': '2.0',
|
logg.debug(res)
|
||||||
'method': 'eth_getBlock',
|
|
||||||
'id': str(uu),
|
|
||||||
'param': [block_number],
|
|
||||||
}
|
|
||||||
logg.debug(req)
|
|
||||||
except Exception as e:
|
|
||||||
logg.error(e)
|
|
||||||
|
|
||||||
return block_hash
|
return block_hash
|
||||||
|
|
||||||
|
@ -97,9 +97,7 @@ chain = args.i
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
block_offset = c.block_number()
|
block_offset = c.block_number()
|
||||||
logg.debug('block offset {}'.format(block_offset))
|
|
||||||
|
|
||||||
syncer_backend = SyncerBackend.live(chain, block_offset+1)
|
syncer_backend = SyncerBackend.live(chain, block_offset+1)
|
||||||
syncer = HeadSyncer(syncer_backend)
|
syncer = HeadSyncer(syncer_backend)
|
||||||
@ -113,7 +111,8 @@ def main():
|
|||||||
syncer.filter.append(task_pair)
|
syncer.filter.append(task_pair)
|
||||||
|
|
||||||
try:
|
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:
|
except LoopDone as e:
|
||||||
sys.stderr.write("sync '{}' done at block {}\n".format(args.mode, e))
|
sys.stderr.write("sync '{}' done at block {}\n".format(args.mode, e))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user