Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
fix(common): fix state diff commitment calculation (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware authored Jun 25, 2024
1 parent 89854f5 commit bf2d8f6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/papyrus_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Common utils and objects for a Starknet node."
bitvec.workspace = true
cairo-lang-starknet-classes.workspace = true
hex.workspace = true
itertools.workspace = true
lazy_static.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions crates/papyrus_common/src/state_diff_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[path = "state_diff_commitment_test.rs"]
mod state_diff_commitment_test;

use itertools::Itertools;
use starknet_api::core::StateDiffCommitment;
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::hash::PoseidonHash;
Expand Down Expand Up @@ -49,8 +50,11 @@ pub fn calculate_state_diff_commitment(
// diff.
let num_deployed_contracts =
state_diff.deployed_contracts.len() + state_diff.replaced_classes.len();
let deployed_contracts_iter =
state_diff.deployed_contracts.iter().chain(state_diff.replaced_classes.iter());
let deployed_contracts_iter = state_diff
.deployed_contracts
.iter()
.chain(state_diff.replaced_classes.iter())
.sorted_by_key(|(contract_address, _)| *contract_address);
let mut flattened_deployed_contracts = vec![Felt::from(num_deployed_contracts as u64)];
for (contract_address, class_hash) in deployed_contracts_iter {
flattened_deployed_contracts.push(*contract_address.0.key());
Expand Down
24 changes: 24 additions & 0 deletions crates/papyrus_common/src/state_diff_commitment_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,27 @@ fn empty_storage_diff() {
calculate_state_diff_commitment(&state_diff_with_empty_storage_diff, StateDiffVersion::V0)
);
}

#[test]
fn deployed_and_replaced_contracts_are_sorted_for_hashing() {
// Tests a bug fix where the squashing of deployed and replaced contracts was not sorted by
// contract address.
let state_diff = ThinStateDiff {
deployed_contracts: [(contract_address!("0x2"), class_hash!("0x2"))].into(),
storage_diffs: [].into(),
declared_classes: [].into(),
deprecated_declared_classes: [].into(),
nonces: [].into(),
replaced_classes: [
(contract_address!("0x1"), class_hash!("0x1")),
(contract_address!("0x3"), class_hash!("0x3")),
]
.into(),
};

let calculated_commitment = calculate_state_diff_commitment(&state_diff, StateDiffVersion::V0);
assert_eq!(
calculated_commitment.0.0.to_hex_string(),
"0x5264ab018246d1ab06704c6016285e90962e843561ebb82c4325d7254b1724b"
);
}

0 comments on commit bf2d8f6

Please sign in to comment.