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

Commit e37b679

Browse files
authored
Fix Deploy not declaring class (#865)
* Update Deploy transaction * Remove unused clone * Remove unused get * Enter
1 parent f659e05 commit e37b679

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,7 @@ mod test {
933933
#[test]
934934
fn test_deploy_and_invoke_simulation() {
935935
let state_reader = Arc::new(InMemoryStateReader::default());
936-
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
937-
938-
state
939-
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
940-
.unwrap();
936+
let state = CachedState::new(state_reader, Some(Default::default()), None);
941937

942938
let block_context = &Default::default();
943939

src/transaction/deploy.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::execution::execution_entry_point::ExecutionResult;
2-
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
2+
use crate::services::api::contract_classes::deprecated_contract_class::{
3+
ContractClass, EntryPointType,
4+
};
35
use crate::state::cached_state::CachedState;
46
use crate::syscalls::syscall_handler_errors::SyscallHandlerError;
57
use crate::{
@@ -17,10 +19,7 @@ use crate::{
1719
},
1820
hash_utils::calculate_contract_address,
1921
services::api::{
20-
contract_class_errors::ContractClassError,
21-
contract_classes::{
22-
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
23-
},
22+
contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass,
2423
},
2524
state::state_api::{State, StateReader},
2625
state::ExecutionResourcesManager,
@@ -40,6 +39,7 @@ pub struct Deploy {
4039
pub contract_address: Address,
4140
pub contract_address_salt: Felt252,
4241
pub contract_hash: ClassHash,
42+
pub contract_class: CompiledClass,
4343
pub constructor_calldata: Vec<Felt252>,
4444
pub tx_type: TransactionType,
4545
pub skip_validate: bool,
@@ -80,6 +80,7 @@ impl Deploy {
8080
contract_address,
8181
contract_address_salt,
8282
contract_hash,
83+
contract_class: CompiledClass::Deprecated(Box::new(contract_class)),
8384
constructor_calldata,
8485
tx_type: TransactionType::Deploy,
8586
skip_validate: false,
@@ -113,6 +114,7 @@ impl Deploy {
113114
contract_address_salt,
114115
contract_hash,
115116
constructor_calldata,
117+
contract_class: CompiledClass::Deprecated(Box::new(contract_class)),
116118
tx_type: TransactionType::Deploy,
117119
skip_validate: false,
118120
skip_execute: false,
@@ -147,11 +149,21 @@ impl Deploy {
147149
state: &mut CachedState<S>,
148150
block_context: &BlockContext,
149151
) -> Result<TransactionExecutionInfo, TransactionError> {
152+
match self.contract_class.clone() {
153+
CompiledClass::Casm(contract_class) => {
154+
state.set_compiled_class(
155+
&Felt252::from_bytes_be(&self.contract_hash),
156+
*contract_class,
157+
)?;
158+
}
159+
CompiledClass::Deprecated(contract_class) => {
160+
state.set_contract_class(&self.contract_hash, &contract_class)?;
161+
}
162+
}
163+
150164
state.deploy_contract(self.contract_address.clone(), self.contract_hash)?;
151-
let class_hash: ClassHash = self.contract_hash;
152-
let contract_class = state.get_contract_class(&class_hash)?;
153165

154-
if self.constructor_entry_points_empty(contract_class)? {
166+
if self.constructor_entry_points_empty(self.contract_class.clone())? {
155167
// Contract has no constructors
156168
Ok(self.handle_empty_constructor(state)?)
157169
} else {
@@ -323,13 +335,9 @@ mod tests {
323335
//transform class_hash to [u8; 32]
324336
let class_hash_bytes = class_hash.to_be_bytes();
325337

326-
state
327-
.set_contract_class(&class_hash_bytes, &contract_class)
328-
.unwrap();
329-
330338
let internal_deploy = Deploy::new(
331339
0.into(),
332-
contract_class,
340+
contract_class.clone(),
333341
vec![10.into()],
334342
0.into(),
335343
0.into(),
@@ -340,6 +348,11 @@ mod tests {
340348

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

351+
assert_eq!(
352+
state.get_contract_class(&class_hash_bytes).unwrap(),
353+
CompiledClass::Deprecated(Box::new(contract_class))
354+
);
355+
343356
assert_eq!(
344357
state
345358
.get_class_hash_at(&internal_deploy.contract_address)

src/transaction/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub enum TransactionError {
2828
InvalidMaxFee,
2929
#[error("The nonce field in Declare transactions of version 0 must be 0.")]
3030
InvalidNonce,
31+
#[error("Couldn't convert from {0} to {1}")]
32+
Conversion(String, String),
3133
#[error("The signature field in Declare transactions of version 0 must be an empty list.")]
3234
InvalidSignature,
3335
#[error("An InvokeFunction transaction (version != 0) must have a nonce.")]

tests/internals.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use starknet_in_rust::definitions::constants::{
2121
};
2222
use starknet_in_rust::execution::execution_entry_point::ExecutionEntryPoint;
2323
use starknet_in_rust::execution::TransactionExecutionContext;
24+
use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass;
2425
use starknet_in_rust::services::api::contract_classes::deprecated_contract_class::ContractClass;
2526
use starknet_in_rust::state::ExecutionResourcesManager;
2627
use starknet_in_rust::transaction::fee::calculate_tx_fee;
@@ -727,6 +728,14 @@ fn declarev2_tx() -> DeclareV2 {
727728
}
728729

729730
fn deploy_fib_syscall() -> Deploy {
731+
#[cfg(not(feature = "cairo_1_tests"))]
732+
let program_data = include_bytes!("../starknet_programs/cairo2/fibonacci.sierra");
733+
#[cfg(feature = "cairo_1_tests")]
734+
let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.sierra");
735+
let sierra_contract_class: SierraContractClass = serde_json::from_slice(program_data).unwrap();
736+
let casm_class = CasmContractClass::from_contract_class(sierra_contract_class, true).unwrap();
737+
let contract_class = CompiledClass::Casm(Box::new(casm_class));
738+
730739
let contract_hash;
731740
#[cfg(not(feature = "cairo_1_tests"))]
732741
{
@@ -742,6 +751,7 @@ fn deploy_fib_syscall() -> Deploy {
742751
contract_address: TEST_FIB_CONTRACT_ADDRESS.clone(),
743752
contract_address_salt: 0.into(),
744753
contract_hash,
754+
contract_class,
745755
constructor_calldata: Vec::new(),
746756
tx_type: TransactionType::Deploy,
747757
skip_execute: false,

0 commit comments

Comments
 (0)