From 48b77f867408e5b40ec72eb9059afe7b7c39aeaf Mon Sep 17 00:00:00 2001 From: Bastin Date: Sun, 16 Feb 2025 17:21:38 +0100 Subject: [PATCH 1/3] add validator merkle proof test | update gitignore --- .gitignore | 1 + .../light_client/test_single_merkle_proof.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/.gitignore b/.gitignore index 56f84dafd8..e27c0ac766 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ venv /.pytest_cache *.swp .eth2spec +.idea build/ output/ diff --git a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py index dd2f4a7164..16a9f004f4 100644 --- a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py @@ -72,3 +72,36 @@ def test_finality_root_merkle_proof(spec, state): index=spec.get_subtree_index(gindex), root=state.hash_tree_root(), ) + +@with_test_suite_name("BeaconState") +@with_light_client +@spec_state_test +def test_validator_merkle_proof(spec, state): + """ + Demonstrates the validity of a Merkle proof for state.validators[0] in the BeaconState. + Ensures that a client can prove inclusion of a single validator record. + (for example, to verify the validator's public key or effective balance). + """ + yield "object", state + + validator_0_gindex = spec.get_generalized_index( + state, + 'validators', + 0 + ) + + branch = spec.compute_merkle_proof(state, validator_0_gindex) + + yield "proof", { + "leaf": "0x" + state.validators[0].hash_tree_root().hex(), + "leaf_index": validator_0_gindex, + "branch": ['0x' + root.hex() for root in branch] + } + + assert spec.is_valid_merkle_branch( + leaf=state.validators[0].hash_tree_root(), + branch=branch, + depth=spec.floorlog2(validator_0_gindex), + index=spec.get_subtree_index(validator_0_gindex), + root=state.hash_tree_root(), + ) \ No newline at end of file From 5ac73bfab7d032a5322bc71fdf5a783176059b4d Mon Sep 17 00:00:00 2001 From: Bastin Date: Sun, 16 Feb 2025 17:24:17 +0100 Subject: [PATCH 2/3] lint --- .../test/altair/light_client/test_single_merkle_proof.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py index 16a9f004f4..9a6f2c5c31 100644 --- a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py @@ -73,6 +73,7 @@ def test_finality_root_merkle_proof(spec, state): root=state.hash_tree_root(), ) + @with_test_suite_name("BeaconState") @with_light_client @spec_state_test @@ -104,4 +105,4 @@ def test_validator_merkle_proof(spec, state): depth=spec.floorlog2(validator_0_gindex), index=spec.get_subtree_index(validator_0_gindex), root=state.hash_tree_root(), - ) \ No newline at end of file + ) From d051cb7e20b4505d69e96743d2794dafb226cf04 Mon Sep 17 00:00:00 2001 From: Bastin <43618253+Inspector-Butters@users.noreply.github.com> Date: Wed, 19 Feb 2025 00:24:28 +0100 Subject: [PATCH 3/3] refactor code Co-authored-by: Justin Traglia <95511699+jtraglia@users.noreply.github.com> --- .../test/altair/light_client/test_single_merkle_proof.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py index 9a6f2c5c31..7d828f926e 100644 --- a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py @@ -84,13 +84,7 @@ def test_validator_merkle_proof(spec, state): (for example, to verify the validator's public key or effective balance). """ yield "object", state - - validator_0_gindex = spec.get_generalized_index( - state, - 'validators', - 0 - ) - + validator_0_gindex = spec.get_generalized_index(state, 'validators', 0) branch = spec.compute_merkle_proof(state, validator_0_gindex) yield "proof", {