def _get_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 [b'\x01' + path] + _get_branch(db, R, keypath[len(L):]) else: return [b'\x01' + path, db.get(R)] elif nodetype == BRANCH_TYPE: if keypath[:1] == b0: return [b'\x02' + R] + _get_branch(db, L, keypath[1:]) else: return [b'\x03' + L] + _get_branch(db, R, keypath[1:])
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_long_format_branch( db, R, keypath[len(L):]) else: return [db.get(node)] elif nodetype == BRANCH_TYPE: if keypath[:1] == b0: return [db.get(node)] + _get_long_format_branch(db, L, keypath[1:]) else: return [db.get(node)] + _get_long_format_branch(db, R, keypath[1:])
def _get_prefix_witness(db, node, keypath): dbnode = db.get(node) if not keypath: return _get_subtrie(db, node) L, R, nodetype = parse_node(dbnode) if nodetype == KV_TYPE: path = encode_bin_path(L) if len(keypath) < len(L) and L[:len(keypath)] == keypath: return [dbnode] + _get_subtrie(db, R) if keypath[:len(L)] == L: return [dbnode] + _get_prefix_witness(db, R, keypath[len(L):]) else: return [dbnode] elif nodetype == BRANCH_TYPE: if keypath[:1] == b0: return [dbnode] + _get_prefix_witness(db, L, keypath[1:]) else: return [dbnode] + _get_prefix_witness(db, R, keypath[1:])
def encode_kv_node(keypath, node): assert keypath assert len(node) == 32 o = bytes([KV_TYPE]) + encode_bin_path(keypath) + node return o