Add abstracted block and tx objects
This commit is contained in:
parent
a414f0ed5b
commit
991e909a2c
16
cic_syncer/client/block.py
Normal file
16
cic_syncer/client/block.py
Normal file
@ -0,0 +1,16 @@
|
||||
class Block:
|
||||
|
||||
def __init__(self, hsh, obj):
|
||||
self.hash = hsh
|
||||
self.obj = obj
|
||||
|
||||
|
||||
def tx(self, idx):
|
||||
return NotImplementedError
|
||||
|
||||
|
||||
def number(self):
|
||||
return NotImplementedError
|
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,11 +1,14 @@
|
||||
import json
|
||||
|
||||
from cic_syncer.client import translate
|
||||
from cic_syncer.client.block import Block
|
||||
from cic_syncer.client.tx import Tx
|
||||
|
||||
|
||||
translations = {
|
||||
'block_number': translate.hex_to_int,
|
||||
'get_block': json.dumps,
|
||||
'number': translate.hex_to_int,
|
||||
}
|
||||
|
||||
|
||||
@ -22,4 +25,24 @@ class EVMResponse:
|
||||
|
||||
|
||||
def get_result(self):
|
||||
return self.fn(self.response_object.get('result'))
|
||||
r = self.fn(self.response_object.get('result'))
|
||||
if r == 'null':
|
||||
return None
|
||||
return r
|
||||
|
||||
|
||||
class EVMTx(Tx):
|
||||
|
||||
def __init__(self, block, tx_number, obj):
|
||||
super(EVMTx, self).__init__(block, tx_number, obj)
|
||||
|
||||
|
||||
class EVMBlock(Block):
|
||||
|
||||
def tx(self, idx):
|
||||
o = self.obj['transactions'][idx]
|
||||
return Tx(self, idx, o)
|
||||
|
||||
|
||||
def number(self):
|
||||
return translate.hex_to_int(self.obj['number'])
|
||||
|
@ -6,6 +6,8 @@ import websocket
|
||||
|
||||
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()
|
||||
|
||||
@ -55,5 +57,31 @@ class EVMWebsocketClient:
|
||||
if err != None:
|
||||
raise RequestError(err)
|
||||
|
||||
return res.get_result()
|
||||
j = res.get_result()
|
||||
if j == None:
|
||||
return None
|
||||
o = json.loads(j)
|
||||
return EVMBlock(o['hash'], o)
|
||||
|
||||
|
||||
def get_block_by_hash(self, hx_in):
|
||||
req_id = str(uuid.uuid4())
|
||||
hx = with_0x(hx_in)
|
||||
req ={
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'eth_getBlockByHash',
|
||||
'id': str(req_id),
|
||||
'params': [hx, 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)
|
||||
|
||||
j = res.get_result()
|
||||
if j == None:
|
||||
return None
|
||||
o = json.loads(j)
|
||||
return EVMBlock(o['hash'], o)
|
||||
|
@ -15,6 +15,12 @@ def strip_0x(hx):
|
||||
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'):
|
||||
hx = strip_0x(hx)
|
||||
b = bytes.fromhex(hx)
|
||||
|
6
cic_syncer/client/tx.py
Normal file
6
cic_syncer/client/tx.py
Normal file
@ -0,0 +1,6 @@
|
||||
class Tx:
|
||||
|
||||
def __init__(self, block, tx_number, obj):
|
||||
self.block = block
|
||||
self.tx_number = tx_number
|
||||
self.obj = obj
|
@ -35,7 +35,9 @@ class MinedSyncer(Syncer):
|
||||
|
||||
def loop(self, interval, getter):
|
||||
while self.running and Syncer.running_global:
|
||||
e = self.get(getter)
|
||||
block_hash = self.get(getter)
|
||||
if block_hash != None:
|
||||
self.process(getter, block_hash)
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
@ -45,12 +47,30 @@ class HeadSyncer(MinedSyncer):
|
||||
super(HeadSyncer, self).__init__(backend)
|
||||
|
||||
|
||||
def process(self, getter, block):
|
||||
logg.debug('process block {}'.format(block))
|
||||
block = getter.get_block_by_hash(block.hash)
|
||||
i = 0
|
||||
tx = None
|
||||
while True:
|
||||
try:
|
||||
tx = block.tx(i)
|
||||
logg.debug('tx {}'.format(tx))
|
||||
self.backend.set(block.number(), i)
|
||||
for f in self.filter:
|
||||
f(getter, block, tx)
|
||||
except IndexError as e:
|
||||
self.backend.set(block.number() + 1, 0)
|
||||
break
|
||||
i += 1
|
||||
|
||||
|
||||
def get(self, getter):
|
||||
(block_number, tx_number) = self.backend.get()
|
||||
block_hash = []
|
||||
uu = uuid.uuid4()
|
||||
res = getter.get_block_by_integer(block_number)
|
||||
logg.debug(res)
|
||||
logg.debug('get {}'.format(res))
|
||||
|
||||
return block_hash
|
||||
return res
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user