mirror of
https://github.com/status-im/research.git
synced 2025-02-04 11:13:43 +00:00
Added long-format branches
This commit is contained in:
parent
5c84d06a59
commit
03ef04dad0
@ -229,6 +229,29 @@ def _get_branch(db, node, keypath):
|
||||
else:
|
||||
return [b'\x03'+L] + _get_branch(db, R, keypath[1:])
|
||||
|
||||
# Get a long-format Merkle branch
|
||||
def _get_long_format_branch(db, node, keypath):
|
||||
if not keypath:
|
||||
return [db.get(node)]
|
||||
L, R, nodetype = parse_node(db.get(node))
|
||||
if nodetype == KV_TYPE:
|
||||
path = encode_bin_path(L)
|
||||
if keypath[:len(L)] == L:
|
||||
return [db.get(node)] + _get_branch(db, R, keypath[len(L):])
|
||||
else:
|
||||
return [db.get(node), db.get(R)]
|
||||
elif nodetype == BRANCH_TYPE:
|
||||
if keypath[:1] == b0:
|
||||
return [db.get(node)] + _get_branch(db, L, keypath[1:])
|
||||
else:
|
||||
return [db.get(node)] + _get_branch(db, R, keypath[1:])
|
||||
|
||||
def _verify_long_format_branch(branch, root, keypath, value):
|
||||
db = EphemDB()
|
||||
db.kv = {sha3(node): node for node in branch}
|
||||
assert _get(db, root, keypath) == value
|
||||
return True
|
||||
|
||||
# Verify a Merkle proof
|
||||
def _verify_branch(branch, root, keypath, value):
|
||||
nodes = [branch[-1]]
|
||||
@ -275,6 +298,11 @@ class Trie():
|
||||
assert _verify_branch(o, self.root, encode_bin(key), self.get(key))
|
||||
return o
|
||||
|
||||
def get_long_format_branch(self, key):
|
||||
o = _get_long_format_branch(self.db, self.root, encode_bin(key))
|
||||
assert _verify_long_format_branch(o, self.root, encode_bin(key), self.get(key))
|
||||
return o
|
||||
|
||||
def update(self, key, value):
|
||||
assert len(key) == 20
|
||||
self.root = _update(self.db, self.root, encode_bin(key), value)
|
||||
|
Loading…
x
Reference in New Issue
Block a user