eth-cache

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

commit 50804f4d70dd00831ae909448a54b237abbeed7e
parent 4a8b162cde88d6dc14d32cfdf559a6fa3c6936a9
Author: lash <dev@holbrook.no>
Date:   Tue,  1 Mar 2022 16:05:00 +0000

Add test for addresses index, rcpt

Diffstat:
Meth_cache/store/file.py | 60+++++++++++++++++++++++++++++-------------------------------
Mtests/test_basic.py | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 87 insertions(+), 35 deletions(-)

diff --git a/eth_cache/store/file.py b/eth_cache/store/file.py @@ -29,30 +29,37 @@ class FileStore: tx_hash_dirnormal = strip_0x(tx.hash).upper() tx_hash_bytes = bytes.fromhex(tx_hash_dirnormal) self.tx_raw_dir.add(tx_hash_bytes, raw) + addresses = [] if self.address_rules != None: for a in tx.outputs + tx.inputs: - if self.address_rules.apply_rules_addresses(a, a, tx.hash): - a_hex = strip_0x(a).upper() - a = bytes.fromhex(a_hex) - self.address_dir.add_dir(tx_hash_dirnormal, a, b'') - dirpath = self.address_dir.to_filepath(a_hex) - fp = os.path.join(dirpath, '.start') - num = tx.block.number - num_compare = 0 - try: - f = open(fp, 'rb') - r = f.read(8) - f.close() - num_compare = int.from_bytes(r, 'big') - except FileNotFoundError: - pass - - if num_compare == 0 or num < num_compare: - logg.debug('recoding new start block {} for {}'.format(num, a)) - num_bytes = num.to_bytes(8, 'big') - f = open(fp, 'wb') - f.write(num_bytes) - f.close() + if a not in addresses: + addresses.append(a) + else: + for a in tx.outputs + tx.inputs: + addresses.append(a) + + for a in addresses: + a_hex = strip_0x(a).upper() + a = bytes.fromhex(a_hex) + self.address_dir.add_dir(tx_hash_dirnormal, a, b'') + dirpath = self.address_dir.to_filepath(a_hex) + fp = os.path.join(dirpath, '.start') + num = tx.block.number + num_compare = 0 + try: + f = open(fp, 'rb') + r = f.read(8) + f.close() + num_compare = int.from_bytes(r, 'big') + except FileNotFoundError: + pass + + if num_compare == 0 or num < num_compare: + logg.debug('recoding new start block {} for {}'.format(num, a)) + num_bytes = num.to_bytes(8, 'big') + f = open(fp, 'wb') + f.write(num_bytes) + f.close() if include_data: src = json.dumps(tx.src()).encode('utf-8') @@ -119,15 +126,6 @@ class FileStore: def __init__(self, chain_spec, cache_root=default_base_dir, address_rules=None): -# self.cache_root = os.path.join( -# cache_root, -# 'eth_cache', -# chain_spec.engine(), -# chain_spec.fork(), -# str(chain_spec.chain_id()), -# ) - #self.cache_root = os.path.realpath(self.cache_root) - #self.chain_dir = chain_dir_for(chain_spec, self.cache_root) self.chain_dir = chain_dir_for(chain_spec, cache_root) self.cache_dir = self.chain_dir self.block_src_path = os.path.join(self.cache_dir, 'block', 'src') diff --git a/tests/test_basic.py b/tests/test_basic.py @@ -39,7 +39,12 @@ class TestCache(EthTesterCase): super(TestCache, self).setUp() fp = tempfile.mkdtemp() self.cache_dir = fp - self.store = FileStore(self.chain_spec, cache_root=self.cache_dir) + + class Applier: + def apply_rules_addresses(self, sender, recipient, address): + return True + + self.store = FileStore(self.chain_spec, cache_root=self.cache_dir, address_rules=Applier()) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) gas_oracle = OverrideGasOracle(price=100000000000, limit=30000) c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) @@ -64,7 +69,6 @@ class TestCache(EthTesterCase): def test_tx(self): - logg.debug('tx {}'.format(self.tx)) self.store.put_tx(self.tx, include_data=True) j = self.store.get_tx(self.tx.hash) tx = json.loads(j) @@ -72,7 +76,6 @@ class TestCache(EthTesterCase): def test_block(self): - logg.debug('tx {}'.format(self.tx)) self.store.put_block(self.block, include_data=True) block_hash = strip_0x(self.block.hash) j = self.store.get_block(block_hash) @@ -81,6 +84,57 @@ class TestCache(EthTesterCase): self.assertEqual(retrieved_block_hash, block_hash) + def test_block_number(self): + self.store.put_block(self.block, include_data=True) + block_hash = strip_0x(self.block.hash) + block_number = int(self.block.number) + j = self.store.get_block_number(block_number) + block = json.loads(j) + retrieved_block_hash = strip_0x(block['hash']) + self.assertEqual(retrieved_block_hash, block_hash) + + + def test_rcpt(self): + self.store.put_tx(self.tx, include_data=True) + j = self.store.get_rcpt(self.tx.hash) + rcpt = json.loads(j) + self.assertTrue(is_same_address(rcpt['transaction_hash'], self.tx.hash)) + + + def test_address(self): + nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) + gas_oracle = OverrideGasOracle(price=100000000000, limit=30000) + c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash, o) = c.create(self.accounts[2], self.accounts[1], 1024) + r = self.rpc.do(o) + o = transaction(tx_hash) + tx_src = self.rpc.do(o) + + o = block_by_hash(tx_src['block_hash']) + block_src = self.rpc.do(o) + block = Block(block_src) + + tx = Tx(tx_src, block=block) + self.store.put_tx(tx, include_data=True) + + nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) + c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash, o) = c.create(self.accounts[1], self.accounts[0], 1024) + r = self.rpc.do(o) + o = transaction(tx_hash) + tx_src = self.rpc.do(o) + + o = block_by_hash(tx_src['block_hash']) + block_src = self.rpc.do(o) + block = Block(block_src) + + tx = Tx(tx_src, block=block) + self.store.put_tx(tx, include_data=True) + + address = strip_0x(self.accounts[1]) + txs = self.store.get_address_tx(address) + self.assertEqual(len(txs), 2) + + if __name__ == '__main__': unittest.main() -