def make_hash_root(list_obj): trie = prepare_single_trie() for seek_index, obj in enumerate(list_obj): trie_key = get_trie_key(int_to_bytes32(seek_index)) trie.put(trie_key, obj.to_dict()) return trie
def _get_account(self, address) -> BaseAccount: if address in self._cache: return self._cache[address] try: trie_key = get_trie_key(address) account = self._deserialize(self._trie.get(trie_key)) except KeyError: account = prepare_account(address_account=address) self._cache[address] = account return account
def get_transaction_from_lookup(self, tx_hash) -> BaseTransaction: lookup = Lookup.transaction(tx_hash) if lookup in self.db: height, seek_index = loads(self.db.get(lookup)) header = self.get_header_from_height(height) tx_root = header.hash_transaction_root trie = prepare_trie(tx_root, self.db) trie_key = get_trie_key(int_to_bytes32(seek_index)) tx = trie.get(trie_key) return tx
def get_receipt(self, tx_hash): lookup = Lookup.transaction(tx_hash) if lookup in self.db: height, seek_index = loads(self.db.get(lookup)) header = self.get_header_from_height(height) receipt_root = header.hash_receipt_root trie = prepare_trie(receipt_root, self.db) trie_key = get_trie_key(int_to_bytes32(seek_index)) receipt = trie.get(trie_key) return receipt
def get_vote_from_lookup(self, vote_hash) -> BaseVote: lookup = Lookup.vote(vote_hash) if lookup in self.db: height, seek_index = loads(self.db.get(lookup)) header = self.get_header_from_height(height) vote_root = header.hash_vote_root trie = prepare_trie(vote_root, self.db) trie_key = get_trie_key(int_to_bytes32(seek_index)) vote = trie.get(trie_key) return vote
def _get_const_validator(self): # update coming soon TODO trie_key = get_trie_key(Lookup.constant_rep()) validators = self._trie.get(trie_key) list_validators = [] for rep in validators: ex = extract_values(rep, REP_DICT) rep_obj = prepare_rep(node_id=ex.node_id.encode(), account=ex.account.encode(), delegate=ex.delegated) list_validators.append(rep_obj) return list_validators
def _set_const_validator(self, address, rep_id): # update coming soon TODO trie_key = get_trie_key(Lookup.constant_rep()) rep = prepare_rep(node_id=rep_id, account=address, delegate=self.get_delegated_balance(address)) try: qualify = self.get_const_validator_list() qualify.append(rep.to_dict()) self._trie.put(trie_key, qualify) except KeyError: self._trie.put(trie_key, [rep.to_dict()])
def commit(self): # state cache. committed db before compare cache?? for address, account in self._cache.copy().items(): trie_key = get_trie_key(address) try: acc = self._trie.get(trie_key) except KeyError: pass else: if account.to_dict() != acc: self.logger.debug("statedb cache: {}, " "trie cache: {}".format( account.to_dict(), acc)) raise CacheError("latest account state error, " "{}".format(address.decode())) self._root = self._trie.commit()
def _set_account(self, address, account): self._cache[address] = account trie_key = get_trie_key(address) self._trie.put(trie_key, account.to_dict())
def _get_const_validator_list(self): trie_key = get_trie_key(Lookup.constant_rep()) reps = self._trie.get(trie_key) return reps