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

Commit

Permalink
Merge branch 'main' into move-cli-to-crate
Browse files Browse the repository at this point in the history
  • Loading branch information
mfachal committed Jun 1, 2023
2 parents 7a37905 + b40f075 commit 4669702
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 227 deletions.
52 changes: 9 additions & 43 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ awc = "3.1.1"
mimalloc = { version = "0.1.29", default-features = false, optional = true }
hex = "0.4.3"
cargo-llvm-cov = "0.5.14"
starknet-rs = {git = "https://github.com/lambdaclass/starknet_in_rust", rev = "04f854e9495ee08fa74eedfdc19b3fe79d03caa9"}
starknet-rs = {git = "https://github.com/lambdaclass/starknet_in_rust", branch = "main"}
starknet-contract-class = { path = "../crates/starknet-contract-class" }

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet-contract-class/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ serde_json = "1.0.94"
git = "https://github.com/lambdaclass/cairo-rs.git"

[dependencies.starknet_api]
git = "https://github.com/lambdaclass/starknet-api"
rev = "b29d881"
git = "https://github.com/starkware-libs/starknet-api"
rev = "a891109ecc9c269e91b1afd2d44bc597728e1172"
features = ["testing"]
2 changes: 1 addition & 1 deletion fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
honggfuzz = "0.5.55"
starknet-rs = { path = "../" }
num-traits = "0.2.15"
starknet_api = { git = "https://github.com/lambdaclass/starknet-api", branch = "main", features = [
starknet_api = { git = "https://github.com/starkware-libs/starknet-api", rev="a891109ecc9c269e91b1afd2d44bc597728e1172", features = [
"testing",
] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
Expand Down
8 changes: 4 additions & 4 deletions src/business_logic/fact_state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct StateDiff {
pub(crate) address_to_class_hash: HashMap<Address, ClassHash>,
pub(crate) address_to_nonce: HashMap<Address, Felt252>,
pub(crate) class_hash_to_compiled_class: HashMap<ClassHash, CompiledClass>,
pub(crate) storage_updates: HashMap<Felt252, HashMap<ClassHash, Address>>,
pub(crate) storage_updates: HashMap<Address, HashMap<Felt252, Felt252>>,
}

impl StateDiff {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl StateDiff {
T: StateReader + Clone,
{
let mut cache_state = CachedState::new(state_reader, None, None);
let cache_storage_mapping = to_cache_state_storage_mapping(self.storage_updates.clone());
let cache_storage_mapping = to_cache_state_storage_mapping(&self.storage_updates);

cache_state.cache_mut().set_initial_values(
&self.address_to_class_hash,
Expand All @@ -118,11 +118,11 @@ impl StateDiff {

let mut storage_updates = HashMap::new();

let addresses: Vec<Felt252> =
let addresses: Vec<Address> =
get_keys(self.storage_updates.clone(), other.storage_updates.clone());

for address in addresses {
let default: HashMap<ClassHash, Address> = HashMap::new();
let default: HashMap<Felt252, Felt252> = HashMap::new();
let mut map_a = self
.storage_updates
.get(&address)
Expand Down
60 changes: 59 additions & 1 deletion src/business_logic/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use super::{
state_cache::{StateCache, StorageEntry},
};
use crate::{
business_logic::fact_state::state::StateDiff,
core::errors::state_errors::StateError,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
starknet_storage::errors::storage_errors::StorageError,
utils::{subtract_mappings, Address, ClassHash},
utils::{subtract_mappings, to_cache_state_storage_mapping, Address, ClassHash},
};
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_vm::felt::Felt252;
Expand Down Expand Up @@ -314,6 +315,18 @@ impl<T: StateReader> State for CachedState<T> {
.insert(class_hash, compiled_class_hash);
Ok(())
}

fn apply_state_update(&mut self, state_updates: &StateDiff) -> Result<(), StateError> {
let storage_updates = to_cache_state_storage_mapping(&state_updates.storage_updates);

self.cache.update_writes(
&state_updates.address_to_class_hash,
&state_updates.class_hash_to_compiled_class,
&state_updates.address_to_nonce,
&storage_updates,
);
Ok(())
}
}

#[cfg(test)]
Expand All @@ -326,6 +339,7 @@ mod tests {
},
};
use cairo_vm::types::program::Program;
use num_traits::One;

#[test]
fn get_class_hash_and_nonce_from_state_reader() {
Expand Down Expand Up @@ -589,4 +603,48 @@ mod tests {
Ok(class_hash) if class_hash == [12u8; 32]
);
}

#[test]
fn cached_state_apply_state_update() {
let state_reader = InMemoryStateReader::new(
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
);

let address_one = Address(Felt252::one());

let mut cached_state = CachedState::new(state_reader, None, None);

let state_diff = StateDiff {
address_to_class_hash: HashMap::from([(
address_one.clone(),
Felt252::one().to_be_bytes(),
)]),
address_to_nonce: HashMap::from([(address_one.clone(), Felt252::one())]),
class_hash_to_compiled_class: HashMap::new(),
storage_updates: HashMap::new(),
};
assert!(cached_state.apply_state_update(&state_diff).is_ok());
assert!(cached_state
.cache
.nonce_writes
.get(&address_one)
.unwrap()
.is_one());
assert!(Felt252::from_bytes_be(
cached_state
.cache
.class_hash_writes
.get(&address_one)
.unwrap()
)
.is_one());
assert!(cached_state.cache.storage_writes.is_empty());
assert!(cached_state.cache.nonce_initial_values.is_empty());
assert!(cached_state.cache.class_hash_initial_values.is_empty());
}
}
2 changes: 2 additions & 0 deletions src/business_logic/state/state_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{cached_state::UNINITIALIZED_CLASS_HASH, state_cache::StorageEntry};
use crate::{
business_logic::fact_state::state::StateDiff,
core::errors::state_errors::StateError,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
Expand Down Expand Up @@ -73,4 +74,5 @@ pub trait State {
class_hash: &Felt252,
compiled_class_hash: &Felt252,
) -> Result<(), StateError>;
fn apply_state_update(&mut self, sate_updates: &StateDiff) -> Result<(), StateError>;
}
4 changes: 3 additions & 1 deletion src/business_logic/transaction/objects/internal_declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use cairo_vm::felt::Felt252;
use num_traits::Zero;
use std::collections::HashMap;

const VERSION_0: u64 = 0;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// Represents an internal transaction in the StarkNet network that is a declaration of a Cairo
/// contract class.
Expand Down Expand Up @@ -117,7 +119,7 @@ impl InternalDeclare {
}
}

if !self.signature.len().is_zero() {
if self.version == VERSION_0 && !self.signature.len().is_zero() {
return Err(TransactionError::StarknetError(
"The signature field in Declare transactions must be an empty list.".to_string(),
));
Expand Down
Loading

0 comments on commit 4669702

Please sign in to comment.