diff --git a/src/block_hash/block_hash_calculator_test.rs b/src/block_hash/block_hash_calculator_test.rs index 7685812..ff3baae 100644 --- a/src/block_hash/block_hash_calculator_test.rs +++ b/src/block_hash/block_hash_calculator_test.rs @@ -45,7 +45,7 @@ fn test_block_hash_regression() { ); let expected_hash = - StarkFelt::try_from("0x069c273a5f40b62efb03e0a8f46f6eb68533f578adbfcc57a604e9a63b066f28") + StarkFelt::try_from("0x061e4998d51a248f1d0288d7e17f6287757b0e5e6c5e1e58ddf740616e312134") .unwrap(); assert_eq!(BlockHash(expected_hash), calculate_block_hash(block_header, block_commitments),); diff --git a/src/block_hash/state_diff_hash.rs b/src/block_hash/state_diff_hash.rs index 8fab0c2..b881879 100644 --- a/src/block_hash/state_diff_hash.rs +++ b/src/block_hash/state_diff_hash.rs @@ -22,7 +22,11 @@ static STARKNET_STATE_DIFF0: Lazy = Lazy::new(|| { pub fn calculate_state_diff_hash(state_diff: &ThinStateDiff) -> StateDiffCommitment { let mut hash_chain = HashChain::new(); hash_chain = hash_chain.chain(&STARKNET_STATE_DIFF0); - hash_chain = chain_deployed_contracts(&state_diff.deployed_contracts, hash_chain); + hash_chain = chain_updated_contracts( + &state_diff.deployed_contracts, + &state_diff.replaced_classes, + hash_chain, + ); hash_chain = chain_declared_classes(&state_diff.declared_classes, hash_chain); hash_chain = chain_deprecated_declared_classes(&state_diff.deprecated_declared_classes, hash_chain); @@ -33,14 +37,17 @@ pub fn calculate_state_diff_hash(state_diff: &ThinStateDiff) -> StateDiffCommitm StateDiffCommitment(PoseidonHash(hash_chain.get_poseidon_hash())) } -// Chains: [number_of_deployed_contracts, address_0, class_hash_0, address_1, class_hash_1, ...]. -fn chain_deployed_contracts( +// Chains: [number_of_updated_contracts, address_0, class_hash_0, address_1, class_hash_1, ...]. +// The updated contracts includes deployed contracts and replaced classes. +fn chain_updated_contracts( deployed_contracts: &IndexMap, + replaced_classes: &IndexMap, mut hash_chain: HashChain, ) -> HashChain { - hash_chain = hash_chain.chain(&deployed_contracts.len().into()); - for (address, class_hash) in sorted_index_map(deployed_contracts) { - hash_chain = hash_chain.chain(&address.0).chain(&class_hash); + let updated_contracts = deployed_contracts.iter().chain(replaced_classes.iter()); + hash_chain = hash_chain.chain(&(deployed_contracts.len() + replaced_classes.len()).into()); + for (address, class_hash) in sorted_index_map(&updated_contracts.collect()) { + hash_chain = hash_chain.chain(&address.0).chain(class_hash); } hash_chain } diff --git a/src/block_hash/state_diff_hash_test.rs b/src/block_hash/state_diff_hash_test.rs index 9215c59..2c2cacc 100644 --- a/src/block_hash/state_diff_hash_test.rs +++ b/src/block_hash/state_diff_hash_test.rs @@ -1,8 +1,8 @@ use indexmap::indexmap; use crate::block_hash::state_diff_hash::{ - calculate_state_diff_hash, chain_declared_classes, chain_deployed_contracts, - chain_deprecated_declared_classes, chain_nonces, chain_storage_diffs, + calculate_state_diff_hash, chain_declared_classes, chain_deprecated_declared_classes, + chain_nonces, chain_storage_diffs, chain_updated_contracts, }; use crate::block_hash::test_utils::get_state_diff; use crate::core::{ClassHash, CompiledClassHash, Nonce, StateDiffCommitment}; @@ -14,7 +14,7 @@ fn test_state_diff_hash_regression() { let state_diff = get_state_diff(); let expected_hash = StateDiffCommitment(PoseidonHash( - StarkFelt::try_from("0x05b8241020c186585f4273cf991d35ad703e808bd9b40242cec584e7f2d86495") + StarkFelt::try_from("0x0281f5966e49ad7dad9323826d53d1d27c0c4e6ebe5525e2e2fbca549bfa0a67") .unwrap(), )); @@ -27,13 +27,21 @@ fn test_sorting_deployed_contracts() { 0u64.into() => ClassHash(3u64.into()), 1u64.into() => ClassHash(2u64.into()), }; + let replaced_classes_0 = indexmap! { + 2u64.into() => ClassHash(1u64.into()), + }; let deployed_contracts_1 = indexmap! { - 1u64.into() => ClassHash(2u64.into()), + 2u64.into() => ClassHash(1u64.into()), 0u64.into() => ClassHash(3u64.into()), }; + let replaced_classes_1 = indexmap! { + 1u64.into() => ClassHash(2u64.into()), + }; assert_eq!( - chain_deployed_contracts(&deployed_contracts_0, HashChain::new()).get_poseidon_hash(), - chain_deployed_contracts(&deployed_contracts_1, HashChain::new()).get_poseidon_hash(), + chain_updated_contracts(&deployed_contracts_0, &replaced_classes_0, HashChain::new()) + .get_poseidon_hash(), + chain_updated_contracts(&deployed_contracts_1, &replaced_classes_1, HashChain::new()) + .get_poseidon_hash(), ); }