Add Merkle proof test

This commit is contained in:
Hsiao-Wei Wang 2023-10-31 00:53:53 +08:00
parent 51343f54fe
commit 1bac25a623
No known key found for this signature in database
GPG Key ID: AE3D6B174F971DE4
7 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,46 @@
from eth2spec.test.context import (
spec_state_test,
with_deneb_and_later,
with_test_suite_name,
)
from eth2spec.test.helpers.block import (
build_empty_block_for_next_slot,
sign_block
)
from eth2spec.test.helpers.execution_payload import (
compute_el_block_hash,
)
from eth2spec.test.helpers.sharding import (
get_sample_opaque_tx,
)
@with_test_suite_name("BeaconBlockBody")
@with_deneb_and_later
@spec_state_test
def test_blob_kzg_commitment_merkle_proof(spec, state):
opaque_tx, blobs, blob_kzg_commitments, proofs = get_sample_opaque_tx(spec, blob_count=1)
block = build_empty_block_for_next_slot(spec, state)
block.body.blob_kzg_commitments = blob_kzg_commitments
block.body.execution_payload.transactions = [opaque_tx]
block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload)
signed_block = sign_block(spec, state, block, proposer_index=0)
blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs)
blob_index = 0
blob_sidecar = blob_sidecars[blob_index]
yield "object", block.body
commitment_inclusion_proof = blob_sidecar.commitment_inclusion_proof
gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', blob_index)
yield "proof", {
"leaf": "0x" + blob_sidecar.kzg_commitment.hash_tree_root().hex(),
"leaf_index": gindex,
"branch": ['0x' + root.hex() for root in commitment_inclusion_proof]
}
assert spec.is_valid_merkle_branch(
leaf=blob_sidecar.kzg_commitment.hash_tree_root(),
branch=blob_sidecar.commitment_inclusion_proof,
depth=spec.floorlog2(gindex),
index=spec.get_subtree_index(gindex),
root=blob_sidecar.signed_block_header.message.body_root,
)

View File

@ -0,0 +1,4 @@
# Merkle proof tests
Handlers:
- `single_merkle_proof`: see [Single leaf merkle proof test format](../light_client/single_merkle_proof.md)

View File

@ -0,0 +1,5 @@
# Merkle proof tests
The purpose of this test-generator is to provide test-vectors for validating the correct implementation of the Merkle proof verification.
Test-format documentation can be found [here](../../formats/merkle_proof/README.md).

View File

@ -0,0 +1,14 @@
from eth2spec.test.helpers.constants import DENEB
from eth2spec.gen_helpers.gen_from_tests.gen import run_state_test_generators
if __name__ == "__main__":
deneb_mods = {key: 'eth2spec.test.deneb.merkle_proof.test_' + key for key in [
'single_merkle_proof',
]}
all_mods = {
DENEB: deneb_mods,
}
run_state_test_generators(runner_name="merkle_proof", all_mods=all_mods)

View File

@ -0,0 +1,2 @@
pytest>=4.4
../../../[generator]