Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests to verify block hash changes if the input change. #275

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 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,47 @@ 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();
modified_header.$header_field = Default::default();
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();
modified_commitments.$commitments_field = Default::default();
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"));
)*
}
};
}

#[test]
fn test_block_hash_regression() {
let block_header = BlockHeaderWithoutHash {
Expand Down Expand Up @@ -51,3 +84,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 {
transaction_commitment: TransactionCommitment(Felt::ONE),
event_commitment: EventCommitment(Felt::ONE),
receipt_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 => {
transaction_commitment,
event_commitment,
receipt_commitment,
state_diff_commitment,
concatenated_counts
}
);
// TODO(Aviv, 10/06/2024): add tests that changes the first hash input, and the const zero.
}
Loading