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

fix: consider replace classes as deployed contracts #262

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/block_hash/block_hash_calculator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),);
Expand Down
19 changes: 13 additions & 6 deletions src/block_hash/state_diff_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ static STARKNET_STATE_DIFF0: Lazy<StarkFelt> = 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);
Expand All @@ -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<ContractAddress, ClassHash>,
replaced_classes: &IndexMap<ContractAddress, ClassHash>,
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
}
Expand Down
20 changes: 14 additions & 6 deletions src/block_hash/state_diff_hash_test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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(),
));

Expand All @@ -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(),
);
}

Expand Down
Loading