Skip to content

Commit

Permalink
test: add tests to verify block hash changes if the input change.
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Jun 10, 2024
1 parent 1521999 commit 69f2e8c
Showing 1 changed file with 93 additions and 2 deletions.
95 changes: 93 additions & 2 deletions src/block_hash/block_hash_calculator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,54 @@ use crate::block::{
StarknetVersion,
};
use crate::block_hash::block_hash_calculator::{
calculate_block_commitments, calculate_block_hash, TransactionHashingData,
calculate_block_commitments, calculate_block_hash, BlockHeaderCommitments,
TransactionHashingData,
};
use crate::block_hash::test_utils::{get_state_diff, get_transaction_output};
use crate::core::{ContractAddress, GlobalRoot, PatriciaKey, SequencerContractAddress};
use crate::core::{
ContractAddress, EventCommitment, GlobalRoot, PatriciaKey, ReceiptCommitment,
SequencerContractAddress, StateDiffCommitment, TransactionCommitment,
};
use crate::data_availability::L1DataAvailabilityMode;
use crate::felt;
use crate::hash::PoseidonHash;
use crate::transaction::{TransactionHash, TransactionSignature};

/// Macro to test if changing any field in the header or commitments
/// results a change in the block hash.
/// The macro clones the original header and commitments, modifies each specified field,
/// and asserts that the block hash changes as a result.
macro_rules! test_hash_changes {
($header:expr, $commitments:expr, header_fields => { $($header_field:ident),* }, commitments_fields => { $($commitments_field:ident),* }) => {
{
let original_hash = calculate_block_hash($header.clone(), $commitments.clone());

$(
// Test changing the field in the header
let mut modified_header = $header.clone();
change_field_value(&mut modified_header.$header_field);
let new_hash = calculate_block_hash(modified_header, $commitments.clone());
assert_ne!(original_hash, new_hash, concat!("Hash should change when ", stringify!($header_field), " is modified"));
)*

$(
// Test changing the field in the commitments
let mut modified_commitments = $commitments.clone();
change_field_value(&mut modified_commitments.$commitments_field);
let new_hash = calculate_block_hash($header.clone(), modified_commitments);
assert_ne!(original_hash, new_hash, concat!("Hash should change when ", stringify!($commitments_field), " is modified"));
)*
}
};
}

fn change_field_value<T>(field: &mut T)
where
T: std::fmt::Debug + Default,
{
*field = Default::default();
}

#[test]
fn test_block_hash_regression() {
let block_header = BlockHeaderWithoutHash {
Expand Down Expand Up @@ -51,3 +91,54 @@ fn concat_counts_test() {
let expected_felt = felt!("0x0000000000000004000000000000000300000000000000028000000000000000");
assert_eq!(concated, expected_felt)
}

/// Test that if one of the input to block hash changes, the hash changes.
#[test]
fn change_field_of_hash_input() {
let header = BlockHeaderWithoutHash {
parent_hash: BlockHash(Felt::ONE),
block_number: BlockNumber(1),
l1_gas_price: GasPricePerToken { price_in_fri: GasPrice(1), price_in_wei: GasPrice(1) },
l1_data_gas_price: GasPricePerToken {
price_in_fri: GasPrice(1),
price_in_wei: GasPrice(1),
},
state_root: GlobalRoot(Felt::ONE),
sequencer: SequencerContractAddress(ContractAddress::from(1_u128)),
timestamp: BlockTimestamp(1),
l1_da_mode: L1DataAvailabilityMode::Blob,
starknet_version: StarknetVersion("0.1.0".to_string()),
};

let block_commitments = BlockHeaderCommitments {
transactions_commitment: TransactionCommitment(Felt::ONE),
events_commitment: EventCommitment(Felt::ONE),
receipts_commitment: ReceiptCommitment(Felt::ONE),
state_diff_commitment: StateDiffCommitment(PoseidonHash(Felt::ONE)),
concatenated_counts: Felt::ONE,
};

// Test that changing any of the fields in the header or the commitments changes the hash.
test_hash_changes!(
header,
block_commitments,
header_fields => {
parent_hash,
block_number,
l1_gas_price,
l1_data_gas_price,
state_root,
sequencer,
timestamp,
starknet_version
},
commitments_fields => {
transactions_commitment,
events_commitment,
receipts_commitment,
state_diff_commitment,
concatenated_counts
}
);
// TODO(Aviv, 10/06/2024): add tests that changes the first hash input, and the const zero.
}

0 comments on commit 69f2e8c

Please sign in to comment.