Skip to content

Commit

Permalink
feat: filter out a contract with empty storage maps (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs authored May 19, 2024
1 parent 549b1b1 commit 111bdf8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/block_hash/state_diff_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,23 @@ fn chain_deprecated_declared_classes(
// ]
fn chain_storage_diffs(
storage_diffs: &IndexMap<ContractAddress, IndexMap<StorageKey, StarkFelt>>,
mut hash_chain: HashChain,
hash_chain: HashChain,
) -> HashChain {
hash_chain = hash_chain.chain(&storage_diffs.len().into());
let mut n_updated_contracts = 0_u64;
let mut storage_diffs_chain = HashChain::new();
for (contract_address, key_value_map) in sorted_index_map(storage_diffs) {
hash_chain = hash_chain.chain(&contract_address);
hash_chain = hash_chain.chain(&key_value_map.len().into());
if key_value_map.is_empty() {
// Filter out a contract with empty storage maps.
continue;
}
n_updated_contracts += 1;
storage_diffs_chain = storage_diffs_chain.chain(&contract_address);
storage_diffs_chain = storage_diffs_chain.chain(&key_value_map.len().into());
for (key, value) in sorted_index_map(&key_value_map) {
hash_chain = hash_chain.chain(&key).chain(&value);
storage_diffs_chain = storage_diffs_chain.chain(&key).chain(&value);
}
}
hash_chain
hash_chain.chain(&n_updated_contracts.into()).extend(storage_diffs_chain)
}

// Chains: [number_of_updated_contracts nonces,
Expand Down
20 changes: 20 additions & 0 deletions src/block_hash/state_diff_hash_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ fn test_sorting_storage_diffs() {
);
}

#[test]
fn test_empty_storage_diffs() {
let storage_diffs_0 = indexmap! {
0u64.into() => indexmap! {
1u64.into() => 2u64.into(),
},
3u64.into() => indexmap! {
},
};
let storage_diffs_1 = indexmap! {
0u64.into() => indexmap! {
1u64.into() => 2u64.into(),
},
};
assert_eq!(
chain_storage_diffs(&storage_diffs_0, HashChain::new()).get_poseidon_hash(),
chain_storage_diffs(&storage_diffs_1, HashChain::new()).get_poseidon_hash(),
);
}

#[test]
fn test_sorting_nonces() {
let nonces_0 = indexmap! {
Expand Down
6 changes: 6 additions & 0 deletions src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl HashChain {
self.chain(&felts.len().into()).chain_iter(felts.iter())
}

// Chains a chain of felts to the hash chain.
pub fn extend(mut self, chain: HashChain) -> Self {
self.elements.extend(chain.elements);
self
}

// Returns the pedersen hash of the chained felts, hashed with the length of the chain.
pub fn get_pedersen_hash(&self) -> StarkHash {
let current_hash = self
Expand Down

0 comments on commit 111bdf8

Please sign in to comment.