fs.py (1909B)
1 # standard imports 2 import os 3 import json 4 5 # local imports 6 from eth_cache.store.base import Store 7 from eth_cache.store.base import StoreAction 8 from hexathon import strip_0x 9 10 11 default_base_dir = '/var/lib' 12 13 def chain_dir_for(chain_spec, base_dir=default_base_dir): 14 chain_dir = os.path.join(base_dir, str(chain_spec).replace(':', '/')) 15 return os.path.join(chain_dir, 'eth_cache') 16 17 18 class FsStore(Store): 19 20 def put_block(self, block, include_data=False): 21 hash_bytes = bytes.fromhex(strip_0x(block.hash)) 22 self.add(StoreAction.BLOCK_NUM, block.number, hash_bytes) 23 num_bytes = block.number.to_bytes(8, 'big') 24 self.add(StoreAction.BLOCK_HASH, hash_bytes, num_bytes) 25 if include_data: 26 src = json.dumps(block.src).encode('utf-8') 27 self.add(StoreAction.BLOCK, hash_bytes, src) 28 29 30 def __init__(self, chain_spec, cache_root=None, address_rules=None): 31 if cache_root == None: 32 cache_root = default_base_dir 33 super(FsStore, self).__init__(chain_spec, cache_root=cache_root, address_rules=address_rules) 34 self.chain_dir = chain_dir_for(chain_spec, cache_root) 35 self.cache_dir = self.chain_dir 36 os.makedirs(self.cache_dir, exist_ok=True) 37 self.block_src_path = os.path.join(self.cache_dir, StoreAction.BLOCK.value) 38 self.block_num_path = os.path.join(self.cache_dir, StoreAction.BLOCK_NUM.value) 39 self.block_hash_path = os.path.join(self.cache_dir, StoreAction.BLOCK_HASH.value) 40 self.tx_path = os.path.join(self.cache_dir, StoreAction.TX.value) 41 self.tx_raw_path = os.path.join(self.cache_dir, StoreAction.TX_RAW.value) 42 self.rcpt_path = os.path.join(self.cache_dir, StoreAction.RCPT.value) 43 self.rcpt_raw_path = os.path.join(self.cache_dir, StoreAction.RCPT_RAW.value) 44 self.address_path = os.path.join(self.cache_dir, StoreAction.ADDRESS.value)