Skip to content

Commit

Permalink
feat: add SCALE Codec to CommitmentStateDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Mar 12, 2024
1 parent 545d71e commit 128d097
Showing 1 changed file with 53 additions and 51 deletions.
104 changes: 53 additions & 51 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use cached::{Cached, SizedCache};
use derive_more::IntoIterator;
use indexmap::IndexMap;
use parity_scale_codec::{Decode, Encode};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
Expand All @@ -13,8 +14,6 @@ use crate::execution::contract_class::ContractClass;
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateReader, StateResult};
use crate::utils::subtract_mappings;

// use parity_scale_codec::{Encode, Decode};
#[cfg(test)]
#[path = "cached_state_test.rs"]
mod test;
Expand Down Expand Up @@ -645,55 +644,58 @@ pub struct CommitmentStateDiff {
pub class_hash_to_compiled_class_hash: IndexMap<ClassHash, CompiledClassHash>,
}

// TODO @bidzyyys
// impl Encode for CommitmentStateDiff {
// fn size_hint(&self) -> usize {
// (4 + self.storage_updates.len()) // Lengths of vectors.
// + self.address_to_class_hash.len()
// * (core::mem::size_of::<ContractAddress>() + core::mem::size_of::<ClassHash>())
// + self.address_to_nonce.len()
// * (core::mem::size_of::<ContractAddress>() + core::mem::size_of::<Nonce>())
// + self.class_hash_to_compiled_class_hash.len()
// * (core::mem::size_of::<ClassHash>() + core::mem::size_of::<CompiledClassHash>())
// + self.storage_updates.len() * core::mem::size_of::<ContractAddress>()
// }

// fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, dest: &mut T) {
// parity_scale_codec::Compact(self.address_to_class_hash.len() as u64).encode_to(dest);
// self.address_to_class_hash.iter().for_each(|v| v.encode_to(dest));
// parity_scale_codec::Compact(self.address_to_nonce.len() as u64).encode_to(dest);
// self.address_to_nonce.iter().for_each(|v| v.encode_to(dest));
// parity_scale_codec::Compact(self.storage_updates.len() as u64).encode_to(dest);
// self.storage_updates.iter().for_each(|(address, idx_map)| {
// address.encode_to(dest);
// parity_scale_codec::Compact(idx_map.len() as u64).encode_to(dest);
// idx_map.iter().for_each(|v| v.encode_to(dest));
// });
// parity_scale_codec::Compact(self.class_hash_to_compiled_class_hash.len() as u64)
// .encode_to(dest);
// self.class_hash_to_compiled_class_hash.iter().for_each(|v| v.encode_to(dest));
// }
// }

// impl Decode for CommitmentStateDiff {
// fn decode<I: parity_scale_codec::Input>(
// input: &mut I,
// ) -> Result<Self, parity_scale_codec::Error> { let res = <( Vec<(ContractAddress,
// ClassHash)>, Vec<(ContractAddress, Nonce)>, Vec<(ContractAddress, Vec<(StorageKey,
// StarkFelt)>)>, Vec<(ClassHash, CompiledClassHash)>, )>::decode(input)?;

// Ok(CommitmentStateDiff {
// address_to_class_hash: res.0.into_iter().collect(),
// address_to_nonce: res.1.into_iter().collect(),
// storage_updates: res
// .2
// .into_iter()
// .map(|(address, v)| (address, v.into_iter().collect()))
// .collect(),
// class_hash_to_compiled_class_hash: res.3.into_iter().collect(),
// })
// }
// }
impl Encode for CommitmentStateDiff {
fn size_hint(&self) -> usize {
(4 + self.storage_updates.len()) // Lengths of vectors.
+ self.address_to_class_hash.len()
* (core::mem::size_of::<ContractAddress>() + core::mem::size_of::<ClassHash>())
+ self.address_to_nonce.len()
* (core::mem::size_of::<ContractAddress>() + core::mem::size_of::<Nonce>())
+ self.class_hash_to_compiled_class_hash.len()
* (core::mem::size_of::<ClassHash>() + core::mem::size_of::<CompiledClassHash>())
+ self.storage_updates.len() * core::mem::size_of::<ContractAddress>()
}

fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, dest: &mut T) {
parity_scale_codec::Compact(self.address_to_class_hash.len() as u64).encode_to(dest);
self.address_to_class_hash.iter().for_each(|v| v.encode_to(dest));
parity_scale_codec::Compact(self.address_to_nonce.len() as u64).encode_to(dest);
self.address_to_nonce.iter().for_each(|v| v.encode_to(dest));
parity_scale_codec::Compact(self.storage_updates.len() as u64).encode_to(dest);
self.storage_updates.iter().for_each(|(address, idx_map)| {
address.encode_to(dest);
parity_scale_codec::Compact(idx_map.len() as u64).encode_to(dest);
idx_map.iter().for_each(|v| v.encode_to(dest));
});
parity_scale_codec::Compact(self.class_hash_to_compiled_class_hash.len() as u64)
.encode_to(dest);
self.class_hash_to_compiled_class_hash.iter().for_each(|v| v.encode_to(dest));
}
}

impl Decode for CommitmentStateDiff {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let res = <(
Vec<(ContractAddress, ClassHash)>,
Vec<(ContractAddress, Nonce)>,
Vec<(ContractAddress, Vec<(StorageKey, StarkFelt)>)>,
Vec<(ClassHash, CompiledClassHash)>,
)>::decode(input)?;

Ok(CommitmentStateDiff {
address_to_class_hash: res.0.into_iter().collect(),
address_to_nonce: res.1.into_iter().collect(),
storage_updates: res
.2
.into_iter()
.map(|(address, v)| (address, v.into_iter().collect()))
.collect(),
class_hash_to_compiled_class_hash: res.3.into_iter().collect(),
})
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 128d097

Please sign in to comment.