From 5d534ae5e8ad4be16ec884c67aff6bc0b811e053 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Sat, 13 Jan 2024 11:38:10 +0900 Subject: [PATCH] improve compile check for merkle leaf number Signed-off-by: Jun Kimura --- crates/consensus/src/bellatrix.rs | 79 +++++++++++--------- crates/consensus/src/capella.rs | 83 +++++++++++---------- crates/consensus/src/deneb.rs | 117 ++++++++++++++++-------------- crates/consensus/src/merkle.rs | 3 + 4 files changed, 152 insertions(+), 130 deletions(-) diff --git a/crates/consensus/src/bellatrix.rs b/crates/consensus/src/bellatrix.rs index 5a7947b..2b8908b 100644 --- a/crates/consensus/src/bellatrix.rs +++ b/crates/consensus/src/bellatrix.rs @@ -8,6 +8,7 @@ use crate::{ errors::Error, execution::BlockNumber, internal_prelude::*, + merkle::MerkleTree, sync_protocol::{ SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, EXECUTION_PAYLOAD_DEPTH, FINALIZED_ROOT_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH, @@ -291,24 +292,27 @@ pub fn gen_execution_payload_proof< SYNC_COMMITTEE_SIZE, >, ) -> Result<(Root, [H256; EXECUTION_PAYLOAD_DEPTH]), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - hash_tree_root(body.randao_reveal.clone()).unwrap().0, - hash_tree_root(body.eth1_data.clone()).unwrap().0, - body.graffiti.0, - hash_tree_root(body.proposer_slashings.clone()).unwrap().0, - hash_tree_root(body.attester_slashings.clone()).unwrap().0, - hash_tree_root(body.attestations.clone()).unwrap().0, - hash_tree_root(body.deposits.clone()).unwrap().0, - hash_tree_root(body.voluntary_exits.clone()).unwrap().0, - hash_tree_root(body.sync_aggregate.clone()).unwrap().0, - hash_tree_root(body.execution_payload.clone()).unwrap().0, - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - ]); + let tree = MerkleTree::from_leaves( + ([ + hash_tree_root(body.randao_reveal.clone()).unwrap().0, + hash_tree_root(body.eth1_data.clone()).unwrap().0, + body.graffiti.0, + hash_tree_root(body.proposer_slashings.clone()).unwrap().0, + hash_tree_root(body.attester_slashings.clone()).unwrap().0, + hash_tree_root(body.attestations.clone()).unwrap().0, + hash_tree_root(body.deposits.clone()).unwrap().0, + hash_tree_root(body.voluntary_exits.clone()).unwrap().0, + hash_tree_root(body.sync_aggregate.clone()).unwrap().0, + hash_tree_root(body.execution_payload.clone()).unwrap().0, + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ] as [_; 16]) + .as_ref(), + ); let mut branch = [Default::default(); EXECUTION_PAYLOAD_DEPTH]; branch.copy_from_slice( tree.proof(&[BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX]) @@ -328,24 +332,27 @@ pub fn gen_execution_payload_field_proof< payload: &ExecutionPayloadHeader, leaf_index: usize, ) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - payload.parent_hash.0, - hash_tree_root(payload.fee_recipient.clone()).unwrap().0, - payload.state_root.0, - payload.receipts_root.0, - hash_tree_root(payload.logs_bloom.clone()).unwrap().0, - payload.prev_randao.0, - hash_tree_root(payload.block_number).unwrap().0, - hash_tree_root(payload.gas_limit).unwrap().0, - hash_tree_root(payload.gas_used).unwrap().0, - hash_tree_root(payload.timestamp).unwrap().0, - hash_tree_root(payload.extra_data.clone()).unwrap().0, - hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, - payload.block_hash.0, - payload.transactions_root.0, - Default::default(), - Default::default(), - ]); + let tree = MerkleTree::from_leaves( + ([ + payload.parent_hash.0, + hash_tree_root(payload.fee_recipient.clone()).unwrap().0, + payload.state_root.0, + payload.receipts_root.0, + hash_tree_root(payload.logs_bloom.clone()).unwrap().0, + payload.prev_randao.0, + hash_tree_root(payload.block_number).unwrap().0, + hash_tree_root(payload.gas_limit).unwrap().0, + hash_tree_root(payload.gas_used).unwrap().0, + hash_tree_root(payload.timestamp).unwrap().0, + hash_tree_root(payload.extra_data.clone()).unwrap().0, + hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, + payload.block_hash.0, + payload.transactions_root.0, + Default::default(), + Default::default(), + ] as [_; 16]) + .as_ref(), + ); let mut branch = [Default::default(); EXECUTION_PAYLOAD_TREE_DEPTH]; branch.copy_from_slice( tree.proof(&[leaf_index]) diff --git a/crates/consensus/src/capella.rs b/crates/consensus/src/capella.rs index df16507..04e547b 100644 --- a/crates/consensus/src/capella.rs +++ b/crates/consensus/src/capella.rs @@ -10,6 +10,7 @@ use crate::{ errors::Error, execution::BlockNumber, internal_prelude::*, + merkle::MerkleTree, sync_protocol::{ SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, EXECUTION_PAYLOAD_DEPTH, FINALIZED_ROOT_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH, @@ -332,26 +333,29 @@ pub fn gen_execution_payload_proof< SYNC_COMMITTEE_SIZE, >, ) -> Result<(Root, [H256; EXECUTION_PAYLOAD_DEPTH]), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - hash_tree_root(body.randao_reveal.clone()).unwrap().0, - hash_tree_root(body.eth1_data.clone()).unwrap().0, - body.graffiti.0, - hash_tree_root(body.proposer_slashings.clone()).unwrap().0, - hash_tree_root(body.attester_slashings.clone()).unwrap().0, - hash_tree_root(body.attestations.clone()).unwrap().0, - hash_tree_root(body.deposits.clone()).unwrap().0, - hash_tree_root(body.voluntary_exits.clone()).unwrap().0, - hash_tree_root(body.sync_aggregate.clone()).unwrap().0, - hash_tree_root(body.execution_payload.clone()).unwrap().0, - hash_tree_root(body.bls_to_execution_changes.clone()) - .unwrap() - .0, - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - ]); + let tree = MerkleTree::from_leaves( + ([ + hash_tree_root(body.randao_reveal.clone()).unwrap().0, + hash_tree_root(body.eth1_data.clone()).unwrap().0, + body.graffiti.0, + hash_tree_root(body.proposer_slashings.clone()).unwrap().0, + hash_tree_root(body.attester_slashings.clone()).unwrap().0, + hash_tree_root(body.attestations.clone()).unwrap().0, + hash_tree_root(body.deposits.clone()).unwrap().0, + hash_tree_root(body.voluntary_exits.clone()).unwrap().0, + hash_tree_root(body.sync_aggregate.clone()).unwrap().0, + hash_tree_root(body.execution_payload.clone()).unwrap().0, + hash_tree_root(body.bls_to_execution_changes.clone()) + .unwrap() + .0, + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ] as [_; 16]) + .as_ref(), + ); let mut branch = [Default::default(); EXECUTION_PAYLOAD_DEPTH]; branch.copy_from_slice( tree.proof(&[BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX]) @@ -371,24 +375,27 @@ pub fn gen_execution_payload_field_proof< payload: &ExecutionPayloadHeader, leaf_index: usize, ) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - payload.parent_hash.0, - hash_tree_root(payload.fee_recipient.clone()).unwrap().0, - payload.state_root.0, - payload.receipts_root.0, - hash_tree_root(payload.logs_bloom.clone()).unwrap().0, - payload.prev_randao.0, - hash_tree_root(payload.block_number).unwrap().0, - hash_tree_root(payload.gas_limit).unwrap().0, - hash_tree_root(payload.gas_used).unwrap().0, - hash_tree_root(payload.timestamp).unwrap().0, - hash_tree_root(payload.extra_data.clone()).unwrap().0, - hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, - payload.block_hash.0, - payload.transactions_root.0, - payload.withdrawals_root.0, - Default::default(), - ]); + let tree = MerkleTree::from_leaves( + ([ + payload.parent_hash.0, + hash_tree_root(payload.fee_recipient.clone()).unwrap().0, + payload.state_root.0, + payload.receipts_root.0, + hash_tree_root(payload.logs_bloom.clone()).unwrap().0, + payload.prev_randao.0, + hash_tree_root(payload.block_number).unwrap().0, + hash_tree_root(payload.gas_limit).unwrap().0, + hash_tree_root(payload.gas_used).unwrap().0, + hash_tree_root(payload.timestamp).unwrap().0, + hash_tree_root(payload.extra_data.clone()).unwrap().0, + hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, + payload.block_hash.0, + payload.transactions_root.0, + payload.withdrawals_root.0, + Default::default(), + ] as [_; 16]) + .as_ref(), + ); let mut branch = [Default::default(); EXECUTION_PAYLOAD_TREE_DEPTH]; branch.copy_from_slice( tree.proof(&[leaf_index]) diff --git a/crates/consensus/src/deneb.rs b/crates/consensus/src/deneb.rs index 83dc294..776536f 100644 --- a/crates/consensus/src/deneb.rs +++ b/crates/consensus/src/deneb.rs @@ -8,6 +8,7 @@ use crate::{ errors::Error, execution::BlockNumber, internal_prelude::*, + merkle::MerkleTree, sync_protocol::{ SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, EXECUTION_PAYLOAD_DEPTH, FINALIZED_ROOT_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH, @@ -346,27 +347,29 @@ pub fn gen_execution_payload_proof< MAX_BLOB_COMMITMENTS_PER_BLOCK, >, ) -> Result<(Root, Vec), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - hash_tree_root(body.randao_reveal.clone()).unwrap().0, - hash_tree_root(body.eth1_data.clone()).unwrap().0, - body.graffiti.0, - hash_tree_root(body.proposer_slashings.clone()).unwrap().0, - hash_tree_root(body.attester_slashings.clone()).unwrap().0, - hash_tree_root(body.attestations.clone()).unwrap().0, - hash_tree_root(body.deposits.clone()).unwrap().0, - hash_tree_root(body.voluntary_exits.clone()).unwrap().0, - hash_tree_root(body.sync_aggregate.clone()).unwrap().0, - hash_tree_root(body.execution_payload.clone()).unwrap().0, - hash_tree_root(body.bls_to_execution_changes.clone()) - .unwrap() - .0, - hash_tree_root(body.blob_kzg_commitments.clone()).unwrap().0, - Default::default(), - Default::default(), - Default::default(), - Default::default(), - ]); - assert_eq!(tree.leaves_len(), 16); + let tree = MerkleTree::from_leaves( + ([ + hash_tree_root(body.randao_reveal.clone()).unwrap().0, + hash_tree_root(body.eth1_data.clone()).unwrap().0, + body.graffiti.0, + hash_tree_root(body.proposer_slashings.clone()).unwrap().0, + hash_tree_root(body.attester_slashings.clone()).unwrap().0, + hash_tree_root(body.attestations.clone()).unwrap().0, + hash_tree_root(body.deposits.clone()).unwrap().0, + hash_tree_root(body.voluntary_exits.clone()).unwrap().0, + hash_tree_root(body.sync_aggregate.clone()).unwrap().0, + hash_tree_root(body.execution_payload.clone()).unwrap().0, + hash_tree_root(body.bls_to_execution_changes.clone()) + .unwrap() + .0, + hash_tree_root(body.blob_kzg_commitments.clone()).unwrap().0, + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ] as [_; 16]) + .as_ref(), + ); Ok(( H256(tree.root().unwrap()), tree.proof(&[9]) @@ -384,41 +387,43 @@ pub fn gen_execution_payload_fields_proof< payload: &ExecutionPayloadHeader, leaf_indices: &[usize], ) -> Result<(Root, Vec), Error> { - let tree = rs_merkle::MerkleTree::::from_leaves(&[ - payload.parent_hash.0, - hash_tree_root(payload.fee_recipient.clone()).unwrap().0, - payload.state_root.0, - payload.receipts_root.0, - hash_tree_root(payload.logs_bloom.clone()).unwrap().0, - payload.prev_randao.0, - hash_tree_root(payload.block_number).unwrap().0, - hash_tree_root(payload.gas_limit).unwrap().0, - hash_tree_root(payload.gas_used).unwrap().0, - hash_tree_root(payload.timestamp).unwrap().0, - hash_tree_root(payload.extra_data.clone()).unwrap().0, - hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, - payload.block_hash.0, - payload.transactions_root.0, - payload.withdrawals_root.0, - hash_tree_root(payload.blob_gas_used).unwrap().0, - hash_tree_root(payload.excess_blob_gas).unwrap().0, - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - Default::default(), - ]); - assert_eq!(tree.leaves_len(), 32); + let tree = MerkleTree::from_leaves( + ([ + payload.parent_hash.0, + hash_tree_root(payload.fee_recipient.clone()).unwrap().0, + payload.state_root.0, + payload.receipts_root.0, + hash_tree_root(payload.logs_bloom.clone()).unwrap().0, + payload.prev_randao.0, + hash_tree_root(payload.block_number).unwrap().0, + hash_tree_root(payload.gas_limit).unwrap().0, + hash_tree_root(payload.gas_used).unwrap().0, + hash_tree_root(payload.timestamp).unwrap().0, + hash_tree_root(payload.extra_data.clone()).unwrap().0, + hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0, + payload.block_hash.0, + payload.transactions_root.0, + payload.withdrawals_root.0, + hash_tree_root(payload.blob_gas_used).unwrap().0, + hash_tree_root(payload.excess_blob_gas).unwrap().0, + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ] as [_; 32]) + .as_ref(), + ); Ok(( H256(tree.root().unwrap()), tree.proof(leaf_indices) diff --git a/crates/consensus/src/merkle.rs b/crates/consensus/src/merkle.rs index e88d309..189f110 100644 --- a/crates/consensus/src/merkle.rs +++ b/crates/consensus/src/merkle.rs @@ -1,6 +1,9 @@ use crate::{beacon::Root, errors::MerkleError, internal_prelude::*, types::H256}; use sha2::{Digest, Sha256}; +/// MerkleTree is a merkle tree implementation using sha256 as a hashing algorithm. +pub type MerkleTree = rs_merkle::MerkleTree; + /// Check if ``leaf`` at ``index`` verifies against the Merkle ``root`` and ``branch``. /// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#is_valid_merkle_branch pub fn is_valid_merkle_branch(