ethereum/consensus-specs

`get_merkle_proof` returning too many values

Opened this issue · 0 comments

The get_merkle_proof function in utils/merkle_minimal.py returns too many values when tree_len is not explicitly set.

One might think the last value is the root but that's not the case. It's the root being mixed together with the zero_hash value of the respective layer.
Specifying the tree_len at len(tree) -1 circumvents it.

Not sure if there are edge cases to consider but having len(tree) -1 fixes it.

Sample Code:

from eth2spec.utils.merkle_minimal import (
    get_merkle_proof,
    calc_merkle_tree_from_leaves,
)


def run():
    nodes = [i.to_bytes(32, byteorder="little") for i in range(16)]
    tree = calc_merkle_tree_from_leaves(nodes, layer_count=32)
    print(len(tree))
    proof = get_merkle_proof(tree, 3)
    print(proof)
    print(len(proof)) # logs "33"


if __name__ == "__main__":
    run()

The expected result would be len(proof) = 32 (not including the root).