Skip to content

Commit

Permalink
improve compile check for merkle leaf number
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jan 14, 2024
1 parent c02536d commit 5d534ae
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 130 deletions.
79 changes: 43 additions & 36 deletions crates/consensus/src/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<rs_merkle::algorithms::Sha256>::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])
Expand All @@ -328,24 +332,27 @@ pub fn gen_execution_payload_field_proof<
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::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])
Expand Down
83 changes: 45 additions & 38 deletions crates/consensus/src/capella.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<rs_merkle::algorithms::Sha256>::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])
Expand All @@ -371,24 +375,27 @@ pub fn gen_execution_payload_field_proof<
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::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])
Expand Down
117 changes: 61 additions & 56 deletions crates/consensus/src/deneb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -346,27 +347,29 @@ pub fn gen_execution_payload_proof<
MAX_BLOB_COMMITMENTS_PER_BLOCK,
>,
) -> Result<(Root, Vec<H256>), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::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])
Expand All @@ -384,41 +387,43 @@ pub fn gen_execution_payload_fields_proof<
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_indices: &[usize],
) -> Result<(Root, Vec<H256>), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::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)
Expand Down
3 changes: 3 additions & 0 deletions crates/consensus/src/merkle.rs
Original file line number Diff line number Diff line change
@@ -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<rs_merkle::algorithms::Sha256>;

/// 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(
Expand Down

0 comments on commit 5d534ae

Please sign in to comment.