eth-cache

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

rpc.py (2646B)


      1 # standard imports
      2 import json
      3 import logging
      4 
      5 # external imports
      6 from jsonrpc_std.parse import jsonrpc_from_dict
      7 from hexathon import strip_0x
      8 
      9 logg = logging.getLogger(__name__)
     10 
     11 class CacheRPC:
     12 
     13     def __init__(self, rpc, store):
     14         self.rpc = rpc
     15         self.store = store
     16 
     17 
     18     def do(self, o):
     19         req = jsonrpc_from_dict(o)
     20         r = None
     21         if req['method'] == 'eth_getBlockByNumber':
     22             block_number = req['params'][0]
     23             v = int(strip_0x(block_number), 16)
     24             try:
     25                 j = self.store.get_block_number(v)
     26                 r = json.loads(j)
     27                 logg.debug('using cached block {} -> {}'.format(v, r['hash']))
     28             except FileNotFoundError as e:
     29                 pass
     30         elif req['method'] == 'eth_getBlockByHash':
     31             block_hash = req['params'][0]
     32             v = strip_0x(block_hash)
     33             try:
     34                 j = self.store.get_block(v)
     35                 r = json.loads(j)
     36                 logg.debug('using cached block {}'.format(r['hash']))
     37             except FileNotFoundError as e:
     38                 logg.debug('not found {}'.format(e))
     39                 pass
     40         elif req['method'] == 'eth_getTransactionReceipt':
     41             tx_hash = req['params'][0]
     42             j = None
     43             try:
     44                 tx_hash = strip_0x(tx_hash)
     45                 j = self.store.get_rcpt(tx_hash)
     46                 r = json.loads(j)
     47                 logg.debug('using cached rcpt {}'.format(tx_hash))
     48             except FileNotFoundError as e:
     49                 logg.debug('no file {}'.format(e))
     50                 pass
     51                 
     52 #        elif req['method'] == 'eth_getTransactionByHash':
     53 #            raise ValueError(o)
     54 #        elif req['method'] == 'eth_getTransactionByBlockHashAndIndex':
     55 #            logg.debug('trying tx index {}'.format(o))
     56 #            v = req['params'][0]
     57 #            j = None
     58 #            try:
     59 #                j = self.store.get_block(v)
     60 #            except FileNotFoundError:
     61 #                pass
     62 #               
     63 #            if j != None:
     64 #                o = json.loads(j)
     65 #                idx = int(req['params'][1], 16)
     66 #                v = r['transactions'][idx] 
     67 #                j = None
     68 #                try:
     69 #                    j = self.store.get_tx(v)
     70 #                except FileNotFoundError:
     71 #                    pass
     72 #
     73 #                if j != None:
     74 #                    r = json.loads(j)
     75 #                    logg.debug('using cached tx {} -> {}'.format(req['params'], r['hash']))
     76 
     77         if r == None:
     78             logg.debug('passthru {}'.format(o))
     79             r = self.rpc.do(o)
     80 
     81         return r