more explicit about merkleization limit/pad
This commit is contained in:
parent
b2c8570606
commit
65b0311582
|
@ -37,12 +37,17 @@ def get_merkle_proof(tree, item_index):
|
||||||
return proof
|
return proof
|
||||||
|
|
||||||
|
|
||||||
def merkleize_chunks(chunks, pad_to: int=1):
|
def merkleize_chunks(chunks, limit=None):
|
||||||
if pad_to == 0:
|
# If no limit is defined, we are just merkleizing chunks (e.g. SSZ container).
|
||||||
|
if limit is None:
|
||||||
|
limit = len(chunks)
|
||||||
|
if limit == 0:
|
||||||
return zerohashes[0]
|
return zerohashes[0]
|
||||||
count = min(len(chunks), pad_to)
|
# Limit strictly. Makes no sense to merkleize objects above the intended padding.
|
||||||
|
# And illegal to exceed list limits, just as with serialization.
|
||||||
|
count = min(len(chunks), limit)
|
||||||
depth = max(count - 1, 0).bit_length()
|
depth = max(count - 1, 0).bit_length()
|
||||||
max_depth = (pad_to - 1).bit_length()
|
max_depth = (limit - 1).bit_length()
|
||||||
tmp = [None for _ in range(max_depth + 1)]
|
tmp = [None for _ in range(max_depth + 1)]
|
||||||
|
|
||||||
def merge(h, i):
|
def merge(h, i):
|
||||||
|
|
|
@ -126,6 +126,7 @@ def item_length(typ: SSZType) -> int:
|
||||||
|
|
||||||
|
|
||||||
def chunk_count(typ: SSZType) -> int:
|
def chunk_count(typ: SSZType) -> int:
|
||||||
|
# note that for lists, .length *on the type* describes the list limit.
|
||||||
if isinstance(typ, BasicType):
|
if isinstance(typ, BasicType):
|
||||||
return 1
|
return 1
|
||||||
elif issubclass(typ, Bits):
|
elif issubclass(typ, Bits):
|
||||||
|
@ -150,7 +151,7 @@ def hash_tree_root(obj: SSZValue):
|
||||||
raise Exception(f"Type not supported: {type(obj)}")
|
raise Exception(f"Type not supported: {type(obj)}")
|
||||||
|
|
||||||
if isinstance(obj, (List, Bytes, Bitlist)):
|
if isinstance(obj, (List, Bytes, Bitlist)):
|
||||||
return mix_in_length(merkleize_chunks(leaves, pad_to=chunk_count(obj.type())), len(obj))
|
return mix_in_length(merkleize_chunks(leaves, limit=chunk_count(obj.type())), len(obj))
|
||||||
else:
|
else:
|
||||||
return merkleize_chunks(leaves)
|
return merkleize_chunks(leaves)
|
||||||
|
|
||||||
|
|
|
@ -61,10 +61,10 @@ cases = [
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'count,pad_to,value',
|
'count,limit,value',
|
||||||
cases,
|
cases,
|
||||||
)
|
)
|
||||||
def test_merkleize_chunks_and_get_merkle_root(count, pad_to, value):
|
def test_merkleize_chunks_and_get_merkle_root(count, limit, value):
|
||||||
chunks = [e(i) for i in range(count)]
|
chunks = [e(i) for i in range(count)]
|
||||||
assert merkleize_chunks(chunks, pad_to=pad_to) == value
|
assert merkleize_chunks(chunks, limit=limit) == value
|
||||||
assert get_merkle_root(chunks, pad_to=pad_to) == value
|
assert get_merkle_root(chunks, pad_to=limit) == value
|
||||||
|
|
Loading…
Reference in New Issue