Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,7 @@ mod test {
#[test]
fn test_deploy_and_invoke_simulation() {
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);

state
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
.unwrap();
let state = CachedState::new(state_reader, Some(Default::default()), None);

let block_context = &Default::default();

Expand Down
39 changes: 26 additions & 13 deletions src/transaction/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::execution::execution_entry_point::ExecutionResult;
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use crate::services::api::contract_classes::deprecated_contract_class::{
ContractClass, EntryPointType,
};
use crate::state::cached_state::CachedState;
use crate::syscalls::syscall_handler_errors::SyscallHandlerError;
use crate::{
Expand All @@ -17,10 +19,7 @@ use crate::{
},
hash_utils::calculate_contract_address,
services::api::{
contract_class_errors::ContractClassError,
contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass,
},
state::state_api::{State, StateReader},
state::ExecutionResourcesManager,
Expand All @@ -40,6 +39,7 @@ pub struct Deploy {
pub contract_address: Address,
pub contract_address_salt: Felt252,
pub contract_hash: ClassHash,
pub contract_class: CompiledClass,
pub constructor_calldata: Vec<Felt252>,
pub tx_type: TransactionType,
pub skip_validate: bool,
Expand Down Expand Up @@ -80,6 +80,7 @@ impl Deploy {
contract_address,
contract_address_salt,
contract_hash,
contract_class: CompiledClass::Deprecated(Box::new(contract_class)),
constructor_calldata,
tx_type: TransactionType::Deploy,
skip_validate: false,
Expand Down Expand Up @@ -113,6 +114,7 @@ impl Deploy {
contract_address_salt,
contract_hash,
constructor_calldata,
contract_class: CompiledClass::Deprecated(Box::new(contract_class)),
tx_type: TransactionType::Deploy,
skip_validate: false,
skip_execute: false,
Expand Down Expand Up @@ -147,11 +149,21 @@ impl Deploy {
state: &mut CachedState<S>,
block_context: &BlockContext,
) -> Result<TransactionExecutionInfo, TransactionError> {
match self.contract_class.clone() {
CompiledClass::Casm(contract_class) => {
state.set_compiled_class(
&Felt252::from_bytes_be(&self.contract_hash),
*contract_class,
)?;
}
CompiledClass::Deprecated(contract_class) => {
state.set_contract_class(&self.contract_hash, &contract_class)?;
}
}

state.deploy_contract(self.contract_address.clone(), self.contract_hash)?;
let class_hash: ClassHash = self.contract_hash;
let contract_class = state.get_contract_class(&class_hash)?;

if self.constructor_entry_points_empty(contract_class)? {
if self.constructor_entry_points_empty(self.contract_class.clone())? {
// Contract has no constructors
Ok(self.handle_empty_constructor(state)?)
} else {
Expand Down Expand Up @@ -323,13 +335,9 @@ mod tests {
//transform class_hash to [u8; 32]
let class_hash_bytes = class_hash.to_be_bytes();

state
.set_contract_class(&class_hash_bytes, &contract_class)
.unwrap();

let internal_deploy = Deploy::new(
0.into(),
contract_class,
contract_class.clone(),
vec![10.into()],
0.into(),
0.into(),
Expand All @@ -340,6 +348,11 @@ mod tests {

let _result = internal_deploy.apply(&mut state, &block_context).unwrap();

assert_eq!(
state.get_contract_class(&class_hash_bytes).unwrap(),
CompiledClass::Deprecated(Box::new(contract_class))
);

assert_eq!(
state
.get_class_hash_at(&internal_deploy.contract_address)
Expand Down
2 changes: 2 additions & 0 deletions src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum TransactionError {
InvalidMaxFee,
#[error("The nonce field in Declare transactions of version 0 must be 0.")]
InvalidNonce,
#[error("Couldn't convert from {0} to {1}")]
Conversion(String, String),
#[error("The signature field in Declare transactions of version 0 must be an empty list.")]
InvalidSignature,
#[error("An InvokeFunction transaction (version != 0) must have a nonce.")]
Expand Down
10 changes: 10 additions & 0 deletions tests/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use starknet_in_rust::definitions::constants::{
};
use starknet_in_rust::execution::execution_entry_point::ExecutionEntryPoint;
use starknet_in_rust::execution::TransactionExecutionContext;
use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass;
use starknet_in_rust::services::api::contract_classes::deprecated_contract_class::ContractClass;
use starknet_in_rust::state::ExecutionResourcesManager;
use starknet_in_rust::transaction::fee::calculate_tx_fee;
Expand Down Expand Up @@ -727,6 +728,14 @@ fn declarev2_tx() -> DeclareV2 {
}

fn deploy_fib_syscall() -> Deploy {
#[cfg(not(feature = "cairo_1_tests"))]
let program_data = include_bytes!("../starknet_programs/cairo2/fibonacci.sierra");
#[cfg(feature = "cairo_1_tests")]
let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.sierra");
let sierra_contract_class: SierraContractClass = serde_json::from_slice(program_data).unwrap();
let casm_class = CasmContractClass::from_contract_class(sierra_contract_class, true).unwrap();
let contract_class = CompiledClass::Casm(Box::new(casm_class));

let contract_hash;
#[cfg(not(feature = "cairo_1_tests"))]
{
Expand All @@ -742,6 +751,7 @@ fn deploy_fib_syscall() -> Deploy {
contract_address: TEST_FIB_CONTRACT_ADDRESS.clone(),
contract_address_salt: 0.into(),
contract_hash,
contract_class,
constructor_calldata: Vec::new(),
tx_type: TransactionType::Deploy,
skip_execute: false,
Expand Down