base.py (2003B)
1 # external imports 2 from chainlib.eth.tx import pack 3 import json 4 from hexathon import strip_0x 5 6 # local imports 7 from . import StoreAction 8 9 class Store: 10 11 def __init__(self, chain_spec, cache_root, address_rules=None): 12 self.chain_spec = chain_spec 13 self.address_rules = address_rules 14 self.adder = {} 15 16 17 def register_adder(self, action, adder): 18 if self.adder.get(action) != None: 19 raise ValueError('Already added action ' + action.value) 20 self.adder[action] = adder 21 22 23 def add(self, action, k, v): 24 return self.adder[action].add(k, v) 25 26 27 def to_filepath(self, action, v): 28 return self.adder[action].to_filepath(v) 29 30 31 def put_tx(self, tx, include_data=False): 32 raw = pack(tx.src, self.chain_spec) 33 #tx_hash_dirnormal = strip_0x(tx.hash).upper() 34 tx_hash_dirnormal = strip_0x(tx.hash) 35 tx_hash_bytes = bytes.fromhex(tx_hash_dirnormal) 36 #self.tx_raw_dir.add(tx_hash_bytes, raw) 37 self.add(StoreAction.TX_RAW, tx_hash_bytes, raw) 38 addresses = [] 39 if self.address_rules != None: 40 for a in tx.outputs + tx.inputs: 41 if a not in addresses: 42 addresses.append(a) 43 else: 44 for a in tx.outputs + tx.inputs: 45 addresses.append(a) 46 47 if include_data: 48 src = json.dumps(tx.src).encode('utf-8') 49 #self.tx_dir.add(bytes.fromhex(strip_0x(tx.hash)), src) 50 self.add(StoreAction.TX, bytes.fromhex(strip_0x(tx.hash)), src) 51 52 if tx.result != None: 53 rcpt_src = tx.result.src 54 rcpt_src = json.dumps(rcpt_src).encode('utf-8') 55 #self.rcpt_dir.add(bytes.fromhex(strip_0x(tx.hash)), rcpt_src) 56 self.add(StoreAction.RCPT, bytes.fromhex(strip_0x(tx.hash)), rcpt_src) 57 58 for a in addresses: 59 self.put_address(tx, a) 60 61 62 def put_address(self, tx, address): 63 raise NotImplementedError()