From e4a1ae67df254a9f319faf77605a08ce62623b4d Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Thu, 11 Jan 2024 18:34:44 +0100 Subject: [PATCH] ssz: bump (#5717) height-based merkleizer --- beacon_chain/el/merkle_minimal.nim | 2 +- beacon_chain/spec/beaconstate.nim | 42 ++++++++++-------------- beacon_chain/spec/datatypes/base.nim | 2 -- beacon_chain/spec/eth2_merkleization.nim | 2 +- vendor/nim-ssz-serialization | 2 +- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/beacon_chain/el/merkle_minimal.nim b/beacon_chain/el/merkle_minimal.nim index 6020189e9..801b9eefb 100644 --- a/beacon_chain/el/merkle_minimal.nim +++ b/beacon_chain/el/merkle_minimal.nim @@ -32,7 +32,7 @@ template getProof*( func attachMerkleProofs*(deposits: var openArray[Deposit]): Eth2Digest = let depositsRoots = mapIt(deposits, hash_tree_root(it.data)) - var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT) + var merkleizer = createMerkleizer2(DEPOSIT_CONTRACT_TREE_DEPTH + 1) let proofs = merkleizer.addChunksAndGenMerkleProofs(depositsRoots) for i in 0 ..< depositsRoots.len: deposits[i].proof[0 ..< DEPOSIT_CONTRACT_TREE_DEPTH] = getProof(proofs, i) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index c558b8a5d..c32bbce2d 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -878,6 +878,14 @@ func get_next_sync_committee*( res.aggregate_pubkey = finish(attestersAgg).toPubKey() res +func compute_deposit_root(deposits: openArray[DepositData]): Eth2Digest = + var merkleizer = createMerkleizer2(DEPOSIT_CONTRACT_TREE_DEPTH + 1) + for i, deposit in deposits: + let htr = hash_tree_root(deposit) + merkleizer.addChunk(htr.data) + + mixInLength(merkleizer.getFinalHash(), deposits.len) + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/phase0/beacon-chain.md#genesis proc initialize_beacon_state_from_eth1( cfg: RuntimeConfig, @@ -906,8 +914,11 @@ proc initialize_beacon_state_from_eth1( state = phase0.BeaconState( fork: genesisFork(cfg), genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp), - eth1_data: - Eth1Data(block_hash: eth1_block_hash, deposit_count: uint64(len(deposits))), + eth1_data:Eth1Data( + deposit_count: deposits.lenu64, + deposit_root: compute_deposit_root(deposits), + block_hash: eth1_block_hash), + eth1_deposit_index: deposits.lenu64, latest_block_header: BeaconBlockHeader( body_root: hash_tree_root(default(phase0.BeaconBlockBody)))) @@ -915,17 +926,6 @@ proc initialize_beacon_state_from_eth1( # Seed RANDAO with Eth1 entropy state.randao_mixes.fill(eth1_block_hash) - var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT) - for i, deposit in deposits: - let htr = hash_tree_root(deposit) - merkleizer.addChunk(htr.data) - - # This is already known in the Eth1 monitor, but it would be too - # much work to refactor all the existing call sites in the test suite - state.eth1_data.deposit_root = mixInLength(merkleizer.getFinalHash(), - deposits.len) - state.eth1_deposit_index = deposits.lenu64 - var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]() for idx, deposit in deposits: let @@ -1020,24 +1020,16 @@ proc initialize_beacon_state_from_eth1*( fork: fork, genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp), eth1_data: Eth1Data( - block_hash: eth1_block_hash, deposit_count: uint64(len(deposits))), + deposit_count: deposits.lenu64, + deposit_root: compute_deposit_root(deposits), + block_hash: eth1_block_hash), + eth1_deposit_index: deposits.lenu64, latest_block_header: BeaconBlockHeader( body_root: hash_tree_root(default consensusFork.BeaconBlockBody))) # Seed RANDAO with Eth1 entropy state.randao_mixes.data.fill(eth1_block_hash) - var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT) - for i, deposit in deposits: - let htr = hash_tree_root(deposit) - merkleizer.addChunk(htr.data) - - # This is already known in the Eth1 monitor, but it would be too - # much work to refactor all the existing call sites in the test suite - state.eth1_data.deposit_root = mixInLength(merkleizer.getFinalHash(), - deposits.len) - state.eth1_deposit_index = deposits.lenu64 - var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]() for idx, deposit in deposits: let diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 8dcd6d37c..f8667b64d 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -87,8 +87,6 @@ const DEPOSIT_CONTRACT_TREE_DEPTH* = 32 BASE_REWARDS_PER_EPOCH* = 4 - DEPOSIT_CONTRACT_LIMIT* = Limit(1'u64 shl DEPOSIT_CONTRACT_TREE_DEPTH) - template maxSize*(n: int) {.pragma.} # Block validation flow diff --git a/beacon_chain/spec/eth2_merkleization.nim b/beacon_chain/spec/eth2_merkleization.nim index d7ed80da0..ceff91acf 100644 --- a/beacon_chain/spec/eth2_merkleization.nim +++ b/beacon_chain/spec/eth2_merkleization.nim @@ -22,7 +22,7 @@ from ./datatypes/capella import HashedBeaconState, SignedBeaconBlock export ssz_codec, merkleization, proofs type - DepositsMerkleizer* = SszMerkleizer[DEPOSIT_CONTRACT_LIMIT] + DepositsMerkleizer* = SszMerkleizer2[DEPOSIT_CONTRACT_TREE_DEPTH + 1] # Can't use `ForkyHashedBeaconState`/`ForkyHashedSignedBeaconBlock` without # creating recursive module dependency through `forks`. diff --git a/vendor/nim-ssz-serialization b/vendor/nim-ssz-serialization index f87c99be0..66de36a9e 160000 --- a/vendor/nim-ssz-serialization +++ b/vendor/nim-ssz-serialization @@ -1 +1 @@ -Subproject commit f87c99be04f3051fe2d9b4facf5e6e71251e8555 +Subproject commit 66de36a9ecc67c98ee858faf555b8a8dd2ea2b5f