eth-cache

Ethereum chain data caching tools
Log | Files | Refs | LICENSE

commit 7ffd3e25c717cfc8d822cfd4d40bdca713b602be
parent 3a9797cafa81375085ea88d7519033a6bb0aaa92
Author: nolash <dev@holbrook.no>
Date:   Mon, 14 Jun 2021 09:27:10 +0200

Add cli tx dumper

Diffstat:
Aeth_cache/runnable/get.py | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mexamples/dump.py | 38+++++++++++++++++++++++++++++++++-----
2 files changed, 112 insertions(+), 5 deletions(-)

diff --git a/eth_cache/runnable/get.py b/eth_cache/runnable/get.py @@ -0,0 +1,79 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +# standard imports +import sys +import os +import argparse +import logging +import select + +# external imports +from chainlib.chain import ChainSpec +from chainlib.eth.connection import EthHTTPConnection +from chainlib.eth.tx import ( + pack, + receipt, + transaction, + Tx, + ) +from hexathon import strip_0x + +# local imports +from eth_cache.store import PointerHexDir + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger() + +default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545') +default_data_dir = os.path.realpath(os.path.join(os.environ.get('HOME', ''), '.eth_cache')) + +def stdin_arg(t=0): + h = select.select([sys.stdin], [], [], t) + if len(h[0]) > 0: + v = h[0][0].read() + return v.rstrip() + return None + + +argparser = argparse.ArgumentParser('eth-get', description='display information about an Ethereum address or transaction', epilog='address/transaction can be provided as an argument or from standard input') +argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)') +argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') +argparser.add_argument('-v', action='store_true', help='Be verbose') +argparser.add_argument('-vv', action='store_true', help='Be more verbose') +argparser.add_argument('--data-dir', dest='data_dir', type=str, help='Be more verbose') +argparser.add_argument('tx_hash', nargs='?', default=stdin_arg(), type=str, help='Item to get information for (address og transaction)') +args = argparser.parse_args() + + +if args.vv: + logg.setLevel(logging.DEBUG) +elif args.v: + logg.setLevel(logging.INFO) + +tx_hash = args.tx_hash +if tx_hash == None: + tx_hash = stdin_arg(t=None) + if tx_hash == None: + argparser.error('need first positional argument or value from stdin') + +chain_spec = ChainSpec.from_chain_str(args.i) +data_dir = args.data_dir +if data_dir == None: + data_dir = os.path.join(default_data_dir, str(chain_spec)) +rpc_provider = args.p + +if __name__ == '__main__': + rpc = EthHTTPConnection(rpc_provider) + o = transaction(tx_hash) + r = rpc.do(o) + tx = Tx(r) + tx_raw = pack(tx.src, chain_spec) + + o = receipt(tx_hash) + r = rpc.do(o) + tx.apply_receipt(r) + + store_backend = PointerHexDir(data_dir, 32) + store_backend.register_pointer('address') + #store = TxFileStore(chain_spec, backend) + store_backend.add(bytes.fromhex(strip_0x(tx_hash)), tx_raw) diff --git a/examples/dump.py b/examples/dump.py @@ -21,6 +21,7 @@ from chainlib.eth.tx import ( from chainlib.interface import ChainInterface from chainsyncer.backend.memory import MemBackend from chainsyncer.driver.threadpool import ThreadPoolHistorySyncer +from chainsyncer.filter import SyncFilter # local imports from eth_cache.account import AccountRegistry @@ -30,8 +31,9 @@ from eth_cache.store import PointerHexDir logging.basicConfig(level=logging.INFO) logg = logging.getLogger() logging.getLogger('eth_cache.store').setLevel(logging.DEBUG) +#logging.getLogger('chainsyncer.filter').setLevel(logging.DEBUG) logging.getLogger('chainsyncer.driver.threadpool').setLevel(logging.DEBUG) -logging.getLogger('chainsyncer.driver.head').setLevel(logging.DEBUG) +#logging.getLogger('chainsyncer.driver.head').setLevel(logging.DEBUG) #logging.getLogger('chainsyncer.backend.memory').setLevel(logging.DEBUG) root_dir = tempfile.mkdtemp(dir=os.path.join('/tmp/ethsync')) @@ -62,10 +64,10 @@ rpc = conn_factory() start = 12423900 #start = 0 -o = block_latest() -r = rpc.do(o) -stop = int(r, 16) -stop = start + 3 +#o = block_latest() +#r = rpc.do(o) +#stop = int(r, 16) +stop = start + 10 syncer_backend = MemBackend(chain_spec, None, target_block=stop) syncer_backend.set(start, 0) @@ -87,6 +89,29 @@ syncer_backend.set(start, 0) account_registry = AccountRegistry() account_registry.add('0x6bd8cb96bbc58a73d5360301b7791457bc93da24', 'money') + +def apply_one_async(fltr, idx, conn, block, tx, session): + logg.error('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> apply async') + fltr.filter(conn, block, tx, session) + return (block.number, tx.index,) + + + + +class ThreadedSyncFilter(SyncFilter): + + def __init__(self, pool, backend): + super(ThreadedSyncFilter, self).__init__(backend) + self.pool = pool + + + def apply_one(self, fltr, idx, conn, block, tx, session): + logg.error('applyone  {} {}'.format(fltr, self.pool)) + #self.pool.apply_async(apply_one_async, (fltr, idx, conn, block, tx, session, self.backend.complete_filter,)) + self.pool.apply(fltr, (idx, conn, block, tx, session,), {}, self.backend.complete_filter) + logg.debug('applyone end') + + class StoreFilter: def __init__(self, store, registry): @@ -110,6 +135,8 @@ class MonitorFilter: def filter(self, rpc, block, tx, session=None): + logg.error('foozzzzzzzzzz') + logg.debug('foo') if tx == None: s = '{} sync block error in tx lookup ({})'.format(self.name, block.number, len(block.txs)) else: @@ -124,4 +151,5 @@ if __name__ == '__main__': syncer = ThreadPoolHistorySyncer(conn_factory, 2, syncer_backend, chain_interface) syncer.add_filter(MonitorFilter()) syncer.add_filter(fltr) + syncer.filter = ThreadedSyncFilter(syncer.worker_pool, syncer_backend) syncer.loop(0, rpc)