Skip to content

Commit

Permalink
refactor(starknet_api): test that changing any fields changes the blo…
Browse files Browse the repository at this point in the history
…ck hash (#844)
  • Loading branch information
yoavGrs authored Sep 18, 2024
1 parent ecea76c commit 208b583
Showing 1 changed file with 37 additions and 49 deletions.
86 changes: 37 additions & 49 deletions crates/starknet_api/src/block_hash/block_hash_calculator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,33 @@ use crate::transaction::{TransactionHash, TransactionSignature};
/// 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),* }) => {
(
BlockHeaderWithoutHash { $($header_field:ident: $header_value:expr),* },
BlockHeaderCommitments { $($commitments_field:ident: $commitments_value:expr),* }
) => {
{
let original_hash = calculate_block_hash($header.clone(), $commitments.clone());
let header = BlockHeaderWithoutHash {
l1_da_mode: L1DataAvailabilityMode::Blob,
$($header_field: $header_value),*
};
let commitments = BlockHeaderCommitments {
$($commitments_field: $commitments_value),*
};
let original_hash = calculate_block_hash(header.clone(), commitments.clone());

$(
// Test changing the field in the header.
let mut modified_header = $header.clone();
let mut modified_header = header.clone();
modified_header.$header_field = Default::default();
let new_hash = calculate_block_hash(modified_header, $commitments.clone());
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();
let mut modified_commitments = commitments.clone();
modified_commitments.$commitments_field = Default::default();
let new_hash = calculate_block_hash($header.clone(), modified_commitments);
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"));
)*
}
Expand Down Expand Up @@ -132,51 +142,29 @@ fn concat_counts_test() {
/// 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),
},
l2_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: BlockHashVersion::VO_13_3.into(),
};

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.
// Set non-default values for the header and the commitments fields. Test that changing any of
// these fields changes the hash.
test_hash_changes!(
header,
block_commitments,
header_fields => {
parent_hash,
block_number,
l1_gas_price,
l1_data_gas_price,
l2_gas_price,
state_root,
sequencer,
timestamp,
starknet_version
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),
},
l2_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),
starknet_version: BlockHashVersion::VO_13_3.into()
},
commitments_fields => {
transaction_commitment,
event_commitment,
receipt_commitment,
state_diff_commitment,
concatenated_counts
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
}
);
// TODO(Aviv, 10/06/2024): add tests that changes the first hash input, and the const zero.
Expand Down

0 comments on commit 208b583

Please sign in to comment.