Skip to content

Commit

Permalink
add tests for misbehaviour case
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 4, 2024
1 parent edc0991 commit 68f1b6c
Show file tree
Hide file tree
Showing 17 changed files with 862 additions and 226 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub enum Error {

#[derive(Debug, Display)]
pub enum MerkleError {
/// invalid merkle branch error: leaf={0:?} branch={1:?} subtree_index={2:?} root={3:?}
InvalidMerkleBranch(H256, Vec<H256>, u32, Root),
/// invalid merkle branch error: leaf={0:?} branch={1:?} subtree_index={2:?} expected={3:?} actual={4:?}
InvalidMerkleBranch(H256, Vec<H256>, u32, Root, Root),
/// too long merkle branch error: depth={0:?} leaf={1:?} branch={2:?} subtree_index={3:?} root={4:?}
TooLongMerkleBranchLength(u32, H256, Vec<H256>, u32, Root),
/// invalid merkle branch length error: depth={0:?} leaf={1:?} branch={2:?} subtree_index={3:?} root={4:?}
Expand Down
138 changes: 69 additions & 69 deletions crates/consensus/src/fork/deneb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
compute::hash_tree_root,
errors::Error,
internal_prelude::*,
merkle::MerkleTree,
merkle::{get_subtree_index, MerkleTree},
sync_protocol::{SyncAggregate, SyncCommittee},
types::{Address, ByteList, ByteVector, Bytes32, H256, U256, U64},
};
Expand Down Expand Up @@ -365,6 +365,74 @@ pub fn gen_execution_payload_field_proof<
))
}

pub fn gen_execution_payload_proof<
const MAX_PROPOSER_SLASHINGS: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const MAX_ATTESTER_SLASHINGS: usize,
const MAX_ATTESTATIONS: usize,
const DEPOSIT_CONTRACT_TREE_DEPTH: usize,
const MAX_DEPOSITS: usize,
const MAX_VOLUNTARY_EXITS: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const MAX_BYTES_PER_TRANSACTION: usize,
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
const MAX_WITHDRAWALS_PER_PAYLOAD: usize,
const MAX_BLS_TO_EXECUTION_CHANGES: usize,
const SYNC_COMMITTEE_SIZE: usize,
const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize,
>(
body: &BeaconBlockBody<
MAX_PROPOSER_SLASHINGS,
MAX_VALIDATORS_PER_COMMITTEE,
MAX_ATTESTER_SLASHINGS,
MAX_ATTESTATIONS,
DEPOSIT_CONTRACT_TREE_DEPTH,
MAX_DEPOSITS,
MAX_VOLUNTARY_EXITS,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
MAX_BYTES_PER_TRANSACTION,
MAX_TRANSACTIONS_PER_PAYLOAD,
MAX_WITHDRAWALS_PER_PAYLOAD,
MAX_BLS_TO_EXECUTION_CHANGES,
SYNC_COMMITTEE_SIZE,
MAX_BLOB_COMMITMENTS_PER_BLOCK,
>,
) -> Result<(Root, Vec<H256>), Error> {
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(&[get_subtree_index(DENEB_FORK_SPEC.execution_payload_gindex) as usize])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect(),
))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -442,72 +510,4 @@ mod tests {
.is_ok());
}
}

fn gen_execution_payload_proof<
const MAX_PROPOSER_SLASHINGS: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const MAX_ATTESTER_SLASHINGS: usize,
const MAX_ATTESTATIONS: usize,
const DEPOSIT_CONTRACT_TREE_DEPTH: usize,
const MAX_DEPOSITS: usize,
const MAX_VOLUNTARY_EXITS: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const MAX_BYTES_PER_TRANSACTION: usize,
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
const MAX_WITHDRAWALS_PER_PAYLOAD: usize,
const MAX_BLS_TO_EXECUTION_CHANGES: usize,
const SYNC_COMMITTEE_SIZE: usize,
const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize,
>(
body: &BeaconBlockBody<
MAX_PROPOSER_SLASHINGS,
MAX_VALIDATORS_PER_COMMITTEE,
MAX_ATTESTER_SLASHINGS,
MAX_ATTESTATIONS,
DEPOSIT_CONTRACT_TREE_DEPTH,
MAX_DEPOSITS,
MAX_VOLUNTARY_EXITS,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
MAX_BYTES_PER_TRANSACTION,
MAX_TRANSACTIONS_PER_PAYLOAD,
MAX_WITHDRAWALS_PER_PAYLOAD,
MAX_BLS_TO_EXECUTION_CHANGES,
SYNC_COMMITTEE_SIZE,
MAX_BLOB_COMMITMENTS_PER_BLOCK,
>,
) -> Result<(Root, Vec<H256>), Error> {
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(&[get_subtree_index(DENEB_FORK_SPEC.execution_payload_gindex) as usize])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect(),
))
}
}
1 change: 1 addition & 0 deletions crates/consensus/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn is_valid_merkle_branch(
branch.to_vec(),
subtree_index,
root,
value,
))
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/light-client-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ rlp = { version = "0.5.2", default-features = false }
[dev-dependencies]
serde_json = "1.0.91"
hex-literal = "0.3.4"
rand = { version = "0.8.5", features = ["std", "std_rng"] }
ssz-rs = { git = "https://github.com/bluele/ssz_rs", branch = "serde-no-std", default-features = false, features = ["serde"] }
milagro_bls = { git = "https://github.com/datachainlab/milagro_bls", rev = "bc2b5b5e8d48b7e2e1bfaa56dc2d93e13cb32095", default-features = false }

[features]
default = ["std"]
Expand Down
Loading

0 comments on commit 68f1b6c

Please sign in to comment.