def delete(self, key): """ Equals to setting the value to None """ validate_is_bytes(key) self.root_hash = self._set(self.root_hash, encode_to_bin(key), b'')
def get_branch(db, root_hash, key): """ Get a long-format Merkle branch """ validate_is_bytes(key) return tuple(_get_branch(db, root_hash, encode_to_bin(key)))
def check_if_branch_exist(db, root_hash, key_prefix): """ Given a key prefix, return whether this prefix is the prefix of an existing key in the trie. """ validate_is_bytes(key_prefix) return _check_if_branch_exist(db, root_hash, encode_to_bin(key_prefix))
def get(self, key): """ Fetches the value with a given keypath from the given node. Key will be encoded into binary array format first. """ validate_is_bytes(key) return self._get(self.root_hash, encode_to_bin(key))
def set(self, key, value): """ Sets the value at the given keypath from the given node Key will be encoded into binary array format first. """ validate_is_bytes(key) validate_is_bytes(value) self.root_hash = self._set(self.root_hash, encode_to_bin(key), value)
def get_witness_for_key_prefix(db, node_hash, key): """ Get all witness given a keypath prefix. Include 1. witness along the keypath and 2. witness in the subtrie of the last node in keypath """ validate_is_bytes(key) return tuple(_get_witness_for_key_prefix(db, node_hash, encode_to_bin(key)))
def delete_subtrie(self, key): """ Given a key prefix, delete the whole subtrie that starts with the key prefix. Key will be encoded into binary array format first. It will call `_set` with `if_delete_subtrie` set to True. """ validate_is_bytes(key) self.root_hash = self._set( self.root_hash, encode_to_bin(key), value=b'', if_delete_subtrie=True, )
def test_round_trip_bin_encoding(value): value_as_binaries = encode_to_bin(value) result = decode_from_bin(value_as_binaries) assert result == value