def commit(self, request): self.db[keccak(request.data)] = request.data self.requests.pop(request.node_key) for ancestor in request.parents: ancestor.dependencies -= 1 if ancestor.dependencies == 0: self.commit(ancestor)
def _hash_and_save(self, node): """ Saves a node into the database and returns its hash """ validate_is_bin_node(node) node_hash = keccak(node) self.db[node_hash] = node return node_hash
def _persist_node(self, node): validate_is_node(node) if is_blank_node(node): return BLANK_NODE encoded_node = rlp.encode(node) if len(encoded_node) < 32: return node encoded_node_hash = keccak(encoded_node) self.db[encoded_node_hash] = encoded_node return encoded_node_hash
def if_branch_valid(branch, root_hash, key, value): # value being None means the key is not in the trie if value is not None: validate_is_bytes(key) # branch must not be empty assert branch for node in branch: validate_is_bin_node(node) db = {keccak(node): node for node in branch} assert BinaryTrie(db=db, root_hash=root_hash).get(key) == value return True
def _set_root_node(self, root_node): validate_is_node(root_node) encoded_root_node = rlp.encode(root_node) self.root_hash = keccak(encoded_root_node) self.db[self.root_hash] = encoded_root_node
) from trie.utils.nodes import ( get_node_type, extract_key, compute_leaf_key, compute_extension_key, is_extension_node, is_leaf_node, is_blank_node, consume_common_prefix, key_starts_with, ) # sanity check assert BLANK_NODE_HASH == keccak(rlp.encode(b'')) class Trie(object): db = None root_hash = None # Shortcuts BLANK_NODE_HASH = BLANK_NODE_HASH BLANK_NODE = BLANK_NODE def __init__(self, db, root_hash=BLANK_NODE_HASH): self.db = db validate_is_bytes(root_hash) self.root_hash = root_hash
def test_get_from_proof_empty(): state_root = keccak(b'state root') key = keccak(b'some key') proof = [] with pytest.raises(BadTrieProof): HexaryTrie.get_from_proof(state_root, key, proof)