eth-cache

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

commit 3671d458a857eb552c91b2cef7db06608eccdc02
parent efe160dc233864277485194c23784318cfd0cebe
Author: lash <dev@holbrook.no>
Date:   Tue, 18 Jun 2024 02:51:49 +0100

Add lmdb get block by hash, number

Diffstat:
Meth_cache/store/file.py | 14--------------
Meth_cache/store/fs.py | 13++++++++++++-
Meth_cache/store/lmdb.py | 30+++++++++++++++++++++++++++---
Mtests/test_lmdb.py | 19+++++++++++++++++++
4 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/eth_cache/store/file.py b/eth_cache/store/file.py @@ -61,20 +61,6 @@ class FileStore(FsStore): super(FileStore, self).put_tx(tx, include_data=include_data) - - def put_block(self, block, include_data=False): - hash_bytes = bytes.fromhex(strip_0x(block.hash)) - #self.block_num_dir.add(block.number, hash_bytes) - self.add(StoreAction.BLOCK_NUM, block.number, hash_bytes) - num_bytes = block.number.to_bytes(8, 'big') - #self.block_hash_dir.add(hash_bytes, num_bytes) - self.add(StoreAction.BLOCK_HASH, hash_bytes, num_bytes) - if include_data: - src = json.dumps(block.src).encode('utf-8') - #self.block_src_dir.add(hash_bytes, src) - self.add(StoreAction.BLOCK, hash_bytes, src) - - def get_block_number(self, block_number): #fp = self.block_num_dir.to_filepath(block_number) fp = self.to_filepath(StoreAction.BLOCK_NUM, block_number) diff --git a/eth_cache/store/fs.py b/eth_cache/store/fs.py @@ -1,9 +1,11 @@ # standard imports import os +import json # local imports from eth_cache.store.base import Store from eth_cache.store.base import StoreAction +from hexathon import strip_0x default_base_dir = '/var/lib' @@ -13,9 +15,18 @@ def chain_dir_for(chain_spec, base_dir=default_base_dir): return os.path.join(chain_dir, 'eth_cache') - class FsStore(Store): + def put_block(self, block, include_data=False): + hash_bytes = bytes.fromhex(strip_0x(block.hash)) + self.add(StoreAction.BLOCK_NUM, block.number, hash_bytes) + num_bytes = block.number.to_bytes(8, 'big') + self.add(StoreAction.BLOCK_HASH, hash_bytes, num_bytes) + if include_data: + src = json.dumps(block.src).encode('utf-8') + self.add(StoreAction.BLOCK, hash_bytes, src) + + def __init__(self, chain_spec, cache_root=None, address_rules=None): if cache_root == None: cache_root = default_base_dir diff --git a/eth_cache/store/lmdb.py b/eth_cache/store/lmdb.py @@ -19,8 +19,10 @@ logg = logging.getLogger(__name__) def to_path_key(path, k): - if type(k) != bytes: + if type(k) == str: k = k.encode('utf-8') + elif type(k) == int: + k = k.to_bytes(8, byteorder='big') if path[len(path)-1] != '/': path += '/' return path.encode('utf-8') + k @@ -60,13 +62,35 @@ class LmdbStore(FsStore): def get_tx(self, tx_hash): - print("getting {}\n".format(tx_hash)) k = bytes.fromhex(tx_hash) - k = to_path_key(StoreAction.TX.value, k) #self.adder[Store.ActionTX].get() + k = to_path_key(StoreAction.TX.value, k) with self.db.begin() as tx: return tx.get(k) + def get_rcpt(self, tx_hash): + k = bytes.fromhex(tx_hash) + k = to_path_key(StoreAction.RCPT.value, k) + with self.db.begin() as tx: + return tx.get(k) + + + def get_block(self, block_hash): + k = bytes.fromhex(block_hash) + k = to_path_key(StoreAction.BLOCK.value, k) + with self.db.begin() as tx: + return tx.get(k) + + + def get_block_number(self, block_number): + r = None + k = block_number.to_bytes(8, byteorder='big') + k = to_path_key(StoreAction.BLOCK_NUM.value, k) + with self.db.begin() as tx: + r = tx.get(k) + return self.get_block(r.hex()) + + def put_address(self, tx, address): pass diff --git a/tests/test_lmdb.py b/tests/test_lmdb.py @@ -4,6 +4,7 @@ import json # external imports from chainlib.eth.address import is_same_address +from hexathon import strip_0x # local imports from eth_cache.store.lmdb import LmdbStore @@ -24,5 +25,23 @@ class TestCacheBasic(TestCache): self.assertTrue(is_same_address(tx['hash'], self.tx.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_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) + print("foo {}".format(j)) + block = json.loads(j) + retrieved_block_hash = strip_0x(block['hash']) + self.assertEqual(retrieved_block_hash, block_hash) + + if __name__ == '__main__': unittest.main()