Add block and tx class

This commit is contained in:
nolash
2021-02-11 10:16:05 +01:00
parent a70744ea7b
commit 7c19b3616f
4 changed files with 731 additions and 7 deletions

View File

@@ -1,21 +1,56 @@
# third-party imports
from chainlib.eth.rpc import jsonrpc_template
from chainlib.eth.tx import Tx
from hexathon import (
add_0x,
strip_0x,
even,
)
def block(self):
def block():
o = jsonrpc_template()
o['method'] = 'eth_blockNumber'
return o
def block_by_hash(self, hsh):
def block_by_hash(hsh):
o = jsonrpc_template()
o['method'] = 'eth_getBlock'
o['method'] = 'eth_getBlockByHash'
o['params'].append(hsh)
return o
def block_by_number(self, n):
def block_by_number(n):
nhx = add_0x(even(hex(n)[2:]))
o = jsonrpc_template()
o['method'] = 'eth_getBlock'
o['params'].append(n)
o['method'] = 'eth_getBlockByNumber'
o['params'].append(nhx)
o['params'].append(True)
return o
class Block:
def __init__(self, src):
self.hash = src['hash']
self.number = int(strip_0x(src['number']), 16)
self.txs = src['transactions']
self.block_src = src
def src(self):
return self.block_src
def tx(self, i):
return Tx(self.txs[i], self)
def tx_src(self, i):
return self.txs[i]
def __str__(self):
return 'block {} {}'.format(self.number, self.hash)

View File

@@ -3,6 +3,7 @@ import logging
# third-party imports
import sha3
from hexathon import strip_0x
from eth_keys import KeyAPI
from eth_keys.backends import NativeECCBackend
from rlp import decode as rlp_decode
@@ -135,3 +136,16 @@ class TxFactory:
logg.debug('using hardcoded gas limit of 8000000 until we have reliable vm executor')
tx['gas'] = 8000000
return tx
class Tx:
def __init__(self, src, block):
self.index = int(strip_0x(src['transactionIndex']), 16)
self.nonce = src['nonce']
self.hash = src['hash']
self.block = block
def __str__(self):
return 'block {} tx {} {}'.format(self.block.number, self.index, self.hash)