def set(self, key: bytes, value: bytes): self._trie.update(key, rlp_encode([value]))
def verify_state_proof(root, key, value, proof_nodes, serialized=False): return Trie.verify_spv_proof(root, key, rlp_encode([value]), proof_nodes, serialized)
def encode_kv_for_verification(key, value): encoded_key = key.encode() if isinstance(key, str) else key encoded_value = rlp_encode([value]) if value is not None else b'' return encoded_key, encoded_value
def verify_state_proof(root, key, value, proof_nodes, serialized=False): encoded_value = rlp_encode([value]) if value is not None else b'' return Trie.verify_spv_proof(root, key, encoded_value, proof_nodes, serialized)
def test_get_values_at_roots_in_memory(): # Update key with different values but preserve root after each update # Check values of keys with different previous roots and check that they # are correct trie = Trie(PersistentDB(KeyValueStorageInMemory())) trie.update('k1'.encode(), rlp_encode(['v1'])) # print state.root_hash.encode('hex') # print state.root_node val = trie.get('k1') print(rlp_decode(val)) oldroot1 = trie.root_node old_root1_hash = trie.root_hash assert trie._decode_to_node(old_root1_hash) == oldroot1 trie.update('k1'.encode(), rlp_encode(['v1a'])) val = trie.get('k1') assert rlp_decode(val) == [b'v1a', ] # Already saved roots help in getting previous values oldval = trie.get_at(oldroot1, 'k1') assert rlp_decode(oldval) == [b'v1', ] oldroot1a = trie.root_node trie.update('k1'.encode(), rlp_encode([b'v1b'])) val = trie.get('k1') assert rlp_decode(val) == [b'v1b'] oldval = trie.get_at(oldroot1a, 'k1') assert rlp_decode(oldval) == [b'v1a', ] oldval = trie.get_at(oldroot1, 'k1') assert rlp_decode(oldval) == [b'v1', ] oldroot1b = trie.root_node trie.update('k1'.encode(), rlp_encode([b'v1c'])) val = trie.get('k1') assert rlp_decode(val) == [b'v1c', ] oldval = trie.get_at(oldroot1b, 'k1') assert rlp_decode(oldval) == [b'v1b', ] oldval = trie.get_at(oldroot1a, 'k1') assert rlp_decode(oldval) == [b'v1a', ] oldval = trie.get_at(oldroot1, 'k1') assert rlp_decode(oldval) == [b'v1', ] oldroot1c = trie.root_node trie.delete('k1'.encode()) assert trie.get('k1') == BLANK_NODE oldval = trie.get_at(oldroot1c, 'k1') assert rlp_decode(oldval) == [b'v1c', ] oldval = trie.get_at(oldroot1b, 'k1') assert rlp_decode(oldval) == [b'v1b', ] oldval = trie.get_at(oldroot1a, 'k1') assert rlp_decode(oldval) == [b'v1a', ] oldval = trie.get_at(oldroot1, 'k1') assert rlp_decode(oldval) == [b'v1', ] trie.root_node = oldroot1c val = trie.get('k1') assert rlp_decode(val) == [b'v1c', ] trie.root_node = oldroot1 val = trie.get('k1') assert rlp_decode(val) == [b'v1', ]
def verify_state_proof_multi(root, key_values, proof_nodes, serialized=False): encoded_key_values = {k: rlp_encode([v]) if v is not None else b'' for k, v in key_values.items()} return Trie.verify_spv_proof_multi(root, encoded_key_values, proof_nodes, serialized)