Skip to content

Commit

Permalink
Cleaned state root commitments crate (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro authored May 27, 2024
1 parent d534817 commit 94be89e
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 760 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- fix(root): Cleaned state root commitments crate
- fix(hash): declare tx v0 hash computation
- perf(db): db contract history parallel fetching and batching
- remove RuntimeApi on RPC
Expand Down
58 changes: 58 additions & 0 deletions crates/client/sync/src/commitments/classes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use blockifier::state::cached_state::CommitmentStateDiff;
use mc_db::storage_handler::{self, DeoxysStorageError};
use mp_felt::Felt252Wrapper;
use mp_hashers::poseidon::PoseidonHasher;
use mp_hashers::HasherT;
use rayon::prelude::*;
use starknet_ff::FieldElement;

// "CONTRACT_CLASS_LEAF_V0"
const CONTRACT_CLASS_HASH_VERSION: FieldElement =
FieldElement::from_mont([9331882290187415277, 12057587991035439952, 18444375821049509847, 115292049744600508]);

/// Calculates the class trie root
///
/// # Arguments
///
/// * `csd` - Commitment state diff for the current block.
/// * `bonsai_class` - Bonsai db used to store class hashes.
/// * `block_number` - The current block number.
///
/// # Returns
///
/// The class root.
pub fn class_trie_root(csd: &CommitmentStateDiff, block_number: u64) -> Result<Felt252Wrapper, DeoxysStorageError> {
let mut handler_class = storage_handler::class_trie_mut();

let updates = csd
.class_hash_to_compiled_class_hash
.iter()
.par_bridge()
.map(|(class_hash, compiled_class_hash)| {
let compiled_class_hash = FieldElement::from_bytes_be(&compiled_class_hash.0.0).unwrap();

let hash = PoseidonHasher::hash_elements(CONTRACT_CLASS_HASH_VERSION, compiled_class_hash);

(class_hash, hash)
})
.collect::<Vec<_>>();

handler_class.init()?;
handler_class.update(updates)?;
handler_class.commit(block_number)?;

Ok(handler_class.root()?.into())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_contract_class_hash_version() {
assert_eq!(
CONTRACT_CLASS_HASH_VERSION,
FieldElement::from_byte_slice_be("CONTRACT_CLASS_LEAF_V0".as_bytes()).unwrap(),
);
}
}
Loading

0 comments on commit 94be89e

Please sign in to comment.