eth-cache

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

file.py (4394B)


      1 # standard imports
      2 import os
      3 import json
      4 import logging
      5 
      6 # external imports
      7 from hexathon import strip_0x
      8 from chainlib.eth.tx import (
      9         Tx,
     10         )
     11 from leveldir.numeric import NumDir
     12 from leveldir.hex import HexDir
     13 
     14 # local imports
     15 from eth_cache.store.fs import FsStore
     16 from eth_cache.store.base import StoreAction
     17 
     18 logg = logging.getLogger(__name__)
     19 
     20 
     21 class FileStore(FsStore):
     22 
     23 
     24     def add_address_dir(self, dirhsh, address):
     25         address_dir_adder = self.adder[StoreAction.ADDRESS]
     26         address_dir_adder.add_dir(dirhsh, address, b'')
     27 
     28 
     29     def put_address(self, tx, address):
     30         a_hex = strip_0x(address).upper()
     31         a = bytes.fromhex(a_hex)
     32         #self.address_dir.add_dir(tx_hash_dirnormal, a, b'')
     33         #address_dir_adder = self.adder[StoreAction.ADDRESS]
     34         #address_dir_adder.add_dir(tx_hash_dirnormal, a, b'')
     35         #self.add_address_dir(tx_hash_dirnormal, a)
     36         tx_hash = strip_0x(tx.hash).upper()
     37         self.add_address_dir(tx_hash, a)
     38         #dirpath = self.address_dir.to_filepath(a_hex)
     39         #dirpath = address_dir_adder.to_filepath(a_hex)
     40         dirpath = self.to_filepath(StoreAction.ADDRESS, a_hex)
     41         fp = os.path.join(dirpath, '.start')
     42         num = tx.block.number
     43         num_compare = 0
     44         try:
     45             f = open(fp, 'rb')
     46             r = f.read(8)
     47             f.close()
     48             num_compare = int.from_bytes(r, 'big')
     49         except FileNotFoundError:
     50             pass
     51 
     52         if num_compare == 0 or num < num_compare:
     53             logg.debug('recoding new start block {} for {}'.format(num, a))
     54             num_bytes = num.to_bytes(8, 'big')
     55             f = open(fp, 'wb')
     56             f.write(num_bytes)
     57             f.close()
     58 
     59 
     60     def put_tx(self, tx, include_data=False):
     61         super(FileStore, self).put_tx(tx, include_data=include_data) 
     62         
     63 
     64     def get_block_number(self, block_number):
     65         #fp = self.block_num_dir.to_filepath(block_number)
     66         fp = self.to_filepath(StoreAction.BLOCK_NUM, block_number)
     67         f = open(fp, 'rb')
     68         r = f.read()
     69         f.close()
     70         return self.get_block(r.hex())
     71 
     72 
     73     def get_block(self, block_hash):
     74         #fp = self.block_src_dir.to_filepath(block_hash)
     75         fp = self.to_filepath(StoreAction.BLOCK, block_hash)
     76         f = open(fp, 'rb')
     77         r = f.read()
     78         f.close()
     79         return r
     80 
     81 
     82     def get_tx(self, tx_hash):
     83         #fp = self.tx_dir.to_filepath(tx_hash)
     84         fp = self.to_filepath(StoreAction.TX, tx_hash)
     85         f = open(fp, 'rb')
     86         r = f.read()
     87         f.close()
     88         return r
     89 
     90 
     91     def get_rcpt(self, tx_hash):
     92         #fp = self.rcpt_dir.to_filepath(tx_hash)
     93         fp = self.to_filepath(StoreAction.RCPT, tx_hash)
     94         f = open(fp, 'rb')
     95         r = f.read()
     96         f.close()
     97         return r
     98 
     99 
    100     def get_address_tx(self, address):
    101         #fp = self.address_dir.to_filepath(address)
    102         fp = self.to_filepath(StoreAction.ADDRESS, address)
    103         tx_hashes = []
    104         for tx_hash in os.listdir(fp):
    105             if tx_hash[0] == '.':
    106                 continue
    107             tx_hashes.append(tx_hash)
    108         return tx_hashes
    109 
    110 
    111     def __init__(self, chain_spec, cache_root=None, address_rules=None):
    112         super(FileStore, self).__init__(chain_spec, cache_root=cache_root, address_rules=address_rules)
    113         block_src_dir = HexDir(self.block_src_path, 32, levels=2)
    114         self.register_adder(StoreAction.BLOCK, block_src_dir)
    115         block_num_dir = NumDir(self.block_num_path, [100000, 1000])
    116         self.register_adder(StoreAction.BLOCK_NUM, block_num_dir)
    117         block_hash_dir = HexDir(self.block_hash_path, 32, levels=2)
    118         self.register_adder(StoreAction.BLOCK_HASH, block_hash_dir)
    119         tx_dir = HexDir(self.tx_path, 32, levels=2)
    120         self.register_adder(StoreAction.TX, tx_dir)
    121         tx_raw_dir = HexDir(self.tx_raw_path, 32, levels=2)
    122         self.register_adder(StoreAction.TX_RAW, tx_raw_dir)
    123         rcpt_dir = HexDir(self.rcpt_path, 32, levels=2)
    124         self.register_adder(StoreAction.RCPT, rcpt_dir)
    125         rcpt_raw_dir = HexDir(self.rcpt_raw_path, 32, levels=2)
    126         self.register_adder(StoreAction.RCPT_RAW, rcpt_raw_dir)
    127         address_dir = HexDir(self.address_path, 20, levels=2)
    128         self.register_adder(StoreAction.ADDRESS, address_dir)
    129 
    130 
    131     def __str__(self):
    132         return 'FileStore: root {}'.format(self.cache_dir)