class StateDataset: def __init__(self, db, state_root): self.db = db if isinstance(state_root, str): self.state_root = decode_hex(state_root) else: self.state_root = state_root try: self.trie = Trie(db, self.state_root) self.is_in_db = True except KeyError: self.state_root = None self.is_in_db = False logging.warning('State root %s not in database', self.state_root) logging.info('State created') def to_dict(self): state_dict = dict() for k in self.trie: try: acc = Account.from_trie(self.db, k, self.trie[k]) except KeyError: logging.error('value in trie for %s not found', k) state_dict[acc.address] = (acc.nonce, acc.balance, acc.storage_root, acc.contract_code, acc.is_address_in_db) return state_dict def to_panda_dataframe(self): trie_dict = self.trie.to_dict() size = len(trie_dict) dtype = [('sha3_account', np.str, ACCOUNT_LENGTH), ('account', np.str, ACCOUNT_LENGTH), ('nonce', np.float), ('balance', np.float), ('is_contract', np.bool), ('code_size', np.float), ('storage_size', np.float), ('key_in_db', np.bool)] arr = np.zeros(size, dtype=dtype) i = 0 for k in self.trie: try: account = Account.from_trie(self.db, k, self.trie[k]) except KeyError: logging.error('value in trie for %s not found', k) continue arr[i] = (encode_hex(k), account.address, account.nonce, account.balance, account.is_contract, account.code_size(self.db), account.storage_size(self.db), account.is_address_in_db) i += 1 df = pd.DataFrame.from_records(arr, index='sha3_account') return df def get_account(self, address): key = utils.sha3(to_canonical_address(address)) try: rlp_data = self.trie.get(key) acc = Account.from_trie(self.db, key, rlp_data) except KeyError: acc = Account.notFound(address) return acc
def storage_size(self, db): size = 0 if self.is_contract: try: storage_trie = Trie(db, decode_hex(self.storage_root)) except KeyError: logging.warning('storage root %s not in database', self.storage_root) return size try: trie_dict = storage_trie.to_dict() except KeyError: logging.warning('storage root %s integrity error in database', self.storage_root) return size size = len(trie_dict) return size