starknet_in_rust 0.4.0

A Rust implementation of Starknet execution logic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use std::sync::Arc;

use crate::execution::execution_entry_point::ExecutionResult;
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::{
    core::{
        contract_address::compute_deprecated_class_hash, errors::hash_errors::HashError,
        errors::state_errors::StateError, transaction_hash::calculate_deploy_transaction_hash,
    },
    definitions::{
        block_context::BlockContext, constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR,
        transaction_type::TransactionType,
    },
    execution::{
        execution_entry_point::ExecutionEntryPoint, CallInfo, TransactionExecutionContext,
        TransactionExecutionInfo,
    },
    hash_utils::calculate_contract_address,
    services::api::{
        contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass,
    },
    state::state_api::{State, StateReader},
    state::ExecutionResourcesManager,
    transaction::error::TransactionError,
    utils::{calculate_tx_resources, felt_to_hash, Address, ClassHash},
};
use cairo_vm::felt::Felt252;
use num_traits::Zero;

use super::Transaction;

/// Represents a Deploy Transaction in the starknet network
#[derive(Debug, Clone)]
pub struct Deploy {
    pub hash_value: Felt252,
    pub version: Felt252,
    pub contract_address: Address,
    pub contract_address_salt: Felt252,
    pub contract_hash: ClassHash,
    pub contract_class: CompiledClass,
    pub constructor_calldata: Vec<Felt252>,
    pub skip_validate: bool,
    pub skip_execute: bool,
    pub skip_fee_transfer: bool,
}

impl Deploy {
    pub fn new(
        contract_address_salt: Felt252,
        contract_class: ContractClass,
        constructor_calldata: Vec<Felt252>,
        chain_id: Felt252,
        version: Felt252,
    ) -> Result<Self, SyscallHandlerError> {
        let class_hash = compute_deprecated_class_hash(&contract_class).map_err(|e| {
            SyscallHandlerError::HashError(HashError::FailedToComputeHash(e.to_string()))
        })?;

        let contract_hash: ClassHash = felt_to_hash(&class_hash);
        let contract_address = Address(calculate_contract_address(
            &contract_address_salt,
            &class_hash,
            &constructor_calldata,
            Address(Felt252::zero()),
        )?);

        let hash_value = calculate_deploy_transaction_hash(
            version.clone(),
            &contract_address,
            &constructor_calldata,
            chain_id,
        )?;

        Ok(Deploy {
            hash_value,
            version,
            contract_address,
            contract_address_salt,
            contract_hash,
            contract_class: CompiledClass::Deprecated(Arc::new(contract_class)),
            constructor_calldata,
            skip_validate: false,
            skip_execute: false,
            skip_fee_transfer: false,
        })
    }

    pub fn new_with_tx_hash(
        contract_address_salt: Felt252,
        contract_class: ContractClass,
        constructor_calldata: Vec<Felt252>,
        version: Felt252,
        hash_value: Felt252,
    ) -> Result<Self, SyscallHandlerError> {
        let class_hash = compute_deprecated_class_hash(&contract_class).map_err(|e| {
            SyscallHandlerError::HashError(HashError::FailedToComputeHash(e.to_string()))
        })?;
        let contract_hash: ClassHash = felt_to_hash(&class_hash);
        let contract_address = Address(calculate_contract_address(
            &contract_address_salt,
            &class_hash,
            &constructor_calldata,
            Address(Felt252::zero()),
        )?);

        Ok(Deploy {
            hash_value,
            version,
            contract_address,
            contract_address_salt,
            contract_hash,
            constructor_calldata,
            contract_class: CompiledClass::Deprecated(Arc::new(contract_class)),
            skip_validate: false,
            skip_execute: false,
            skip_fee_transfer: false,
        })
    }

    /// Returns the class hash of the deployed contract
    pub const fn class_hash(&self) -> ClassHash {
        self.contract_hash
    }

    fn constructor_entry_points_empty(
        &self,
        contract_class: CompiledClass,
    ) -> Result<bool, StateError> {
        match contract_class {
            CompiledClass::Deprecated(class) => Ok(class
                .entry_points_by_type
                .get(&EntryPointType::Constructor)
                .ok_or(ContractClassError::NoneEntryPointType)?
                .is_empty()),
            CompiledClass::Casm(class) => Ok(class.entry_points_by_type.constructor.is_empty()),
        }
    }
    /// Deploys the contract in the starknet network and calls its constructor if it has one.
    /// ## Parameters
    /// - state: A state that implements the [`State`] and [`StateReader`] traits.
    /// - block_context: The block's execution context.
    pub fn apply<S: StateReader>(
        &self,
        state: &mut CachedState<S>,
        block_context: &BlockContext,
    ) -> Result<TransactionExecutionInfo, TransactionError> {
        state.set_contract_class(&self.contract_hash, &self.contract_class)?;
        state.deploy_contract(self.contract_address.clone(), self.contract_hash)?;

        if self.constructor_entry_points_empty(self.contract_class.clone())? {
            // Contract has no constructors
            Ok(self.handle_empty_constructor(state)?)
        } else {
            self.invoke_constructor(state, block_context)
        }
    }

    /// Executes the contract without constructor
    /// ## Parameters
    /// - state: A state that implements the [`State`] and [`StateReader`] traits.
    pub fn handle_empty_constructor<S: State + StateReader>(
        &self,
        state: &mut S,
    ) -> Result<TransactionExecutionInfo, TransactionError> {
        if !self.constructor_calldata.is_empty() {
            return Err(TransactionError::EmptyConstructorCalldata);
        }

        let class_hash: ClassHash = self.contract_hash;
        let call_info = CallInfo::empty_constructor_call(
            self.contract_address.clone(),
            Address(Felt252::zero()),
            Some(class_hash),
        );

        let resources_manager = ExecutionResourcesManager::default();

        let changes = state.count_actual_storage_changes(None)?;
        let actual_resources = calculate_tx_resources(
            resources_manager,
            &[Some(call_info.clone())],
            TransactionType::Deploy,
            changes,
            None,
            0,
        )?;

        Ok(TransactionExecutionInfo::new_without_fee_info(
            None,
            Some(call_info),
            None,
            actual_resources,
            Some(TransactionType::Deploy),
        ))
    }

    /// Execute the contract using its constructor
    /// ## Parameters
    /// - state: A state that implements the [`State`] and [`StateReader`] traits.
    /// - block_context: The block's execution context.
    pub fn invoke_constructor<S: StateReader>(
        &self,
        state: &mut CachedState<S>,
        block_context: &BlockContext,
    ) -> Result<TransactionExecutionInfo, TransactionError> {
        let call = ExecutionEntryPoint::new(
            self.contract_address.clone(),
            self.constructor_calldata.clone(),
            CONSTRUCTOR_ENTRY_POINT_SELECTOR.clone(),
            Address(Felt252::zero()),
            EntryPointType::Constructor,
            None,
            None,
            0,
        );

        let mut tx_execution_context = TransactionExecutionContext::new(
            Address(Felt252::zero()),
            self.hash_value.clone(),
            Vec::new(),
            0,
            Felt252::zero(),
            block_context.invoke_tx_max_n_steps,
            self.version.clone(),
        );

        let mut resources_manager = ExecutionResourcesManager::default();
        let ExecutionResult {
            call_info,
            revert_error,
            n_reverted_steps,
        } = call.execute(
            state,
            block_context,
            &mut resources_manager,
            &mut tx_execution_context,
            true,
            block_context.validate_max_n_steps,
        )?;

        let changes = state.count_actual_storage_changes(None)?;
        let actual_resources = calculate_tx_resources(
            resources_manager,
            &[call_info.clone()],
            TransactionType::Deploy,
            changes,
            None,
            n_reverted_steps,
        )?;

        Ok(TransactionExecutionInfo::new_without_fee_info(
            None,
            call_info,
            revert_error,
            actual_resources,
            Some(TransactionType::Deploy),
        ))
    }

    /// Calculates actual fee used by the transaction using the execution
    /// info returned by apply(), then updates the transaction execution info with the data of the fee.
    /// ## Parameters
    /// - state: A state that implements the [`State`] and [`StateReader`] traits.
    /// - block_context: The block's execution context.
    pub fn execute<S: StateReader>(
        &self,
        state: &mut CachedState<S>,
        block_context: &BlockContext,
    ) -> Result<TransactionExecutionInfo, TransactionError> {
        let mut tx_exec_info = self.apply(state, block_context)?;
        let (fee_transfer_info, actual_fee) = (None, 0);
        tx_exec_info.set_fee_info(actual_fee, fee_transfer_info);

        Ok(tx_exec_info)
    }

    // ---------------
    //   Simulation
    // ---------------

    /// Creates a Deploy transaction for simulate a deploy
    pub(crate) fn create_for_simulation(
        &self,
        skip_validate: bool,
        skip_execute: bool,
        skip_fee_transfer: bool,
    ) -> Transaction {
        let tx = Deploy {
            skip_validate,
            skip_execute,
            skip_fee_transfer,
            ..self.clone()
        };

        Transaction::Deploy(tx)
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, sync::Arc};

    use super::*;
    use crate::{
        state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader,
        utils::calculate_sn_keccak,
    };

    #[test]
    fn invoke_constructor_test() {
        // Instantiate CachedState
        let state_reader = Arc::new(InMemoryStateReader::default());
        let mut state = CachedState::new(state_reader, HashMap::new());

        // Set contract_class
        let contract_class =
            ContractClass::from_path("starknet_programs/constructor.json").unwrap();
        let class_hash: Felt252 = compute_deprecated_class_hash(&contract_class).unwrap();
        //transform class_hash to [u8; 32]
        let class_hash_bytes = class_hash.to_be_bytes();

        let internal_deploy = Deploy::new(
            0.into(),
            contract_class.clone(),
            vec![10.into()],
            0.into(),
            0.into(),
        )
        .unwrap();

        let block_context = Default::default();

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

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

        assert_eq!(
            state
                .get_class_hash_at(&internal_deploy.contract_address)
                .unwrap(),
            class_hash_bytes
        );

        let storage_key = calculate_sn_keccak(b"owner");

        assert_eq!(
            state
                .get_storage_at(&(internal_deploy.contract_address, storage_key))
                .unwrap(),
            Felt252::from(10)
        );
    }

    #[test]
    fn invoke_constructor_no_calldata_should_fail() {
        // Instantiate CachedState
        let state_reader = Arc::new(InMemoryStateReader::default());
        let mut state = CachedState::new(state_reader, HashMap::new());

        let contract_class =
            ContractClass::from_path("starknet_programs/constructor.json").unwrap();

        let class_hash: Felt252 = compute_deprecated_class_hash(&contract_class).unwrap();
        //transform class_hash to [u8; 32]
        let class_hash_bytes = class_hash.to_be_bytes();

        state
            .set_contract_class(
                &class_hash_bytes,
                &CompiledClass::Deprecated(Arc::new(contract_class.clone())),
            )
            .unwrap();

        let internal_deploy =
            Deploy::new(0.into(), contract_class, Vec::new(), 0.into(), 0.into()).unwrap();

        let block_context = Default::default();

        let result = internal_deploy.execute(&mut state, &block_context);
        assert_matches!(result.unwrap_err(), TransactionError::CairoRunner(..))
    }

    #[test]
    fn deploy_contract_without_constructor_should_fail() {
        // Instantiate CachedState
        let state_reader = Arc::new(InMemoryStateReader::default());
        let mut state = CachedState::new(state_reader, HashMap::new());

        let contract_path = "starknet_programs/amm.json";
        let contract_class = ContractClass::from_path(contract_path).unwrap();

        let class_hash: Felt252 = compute_deprecated_class_hash(&contract_class).unwrap();
        //transform class_hash to [u8; 32]
        let mut class_hash_bytes = [0u8; 32];
        class_hash_bytes.copy_from_slice(&class_hash.to_bytes_be());

        state
            .set_contract_class(
                &class_hash_bytes,
                &CompiledClass::Deprecated(Arc::new(contract_class.clone())),
            )
            .unwrap();

        let internal_deploy = Deploy::new(
            0.into(),
            contract_class,
            vec![10.into()],
            0.into(),
            0.into(),
        )
        .unwrap();

        let block_context = Default::default();

        let result = internal_deploy.execute(&mut state, &block_context);
        assert_matches!(
            result.unwrap_err(),
            TransactionError::EmptyConstructorCalldata
        )
    }

    #[test]
    fn internal_deploy_computing_classhash_should_fail() {
        let contract_path = "starknet_programs/amm.json";
        // Take a contrat class to copy the program
        let contract_class = ContractClass::from_path(contract_path).unwrap();

        // Make a new contract class with the same program but with errors
        let error_contract_class = ContractClass {
            hinted_class_hash: contract_class.hinted_class_hash,
            program: contract_class.program,
            entry_points_by_type: HashMap::new(),
            abi: None,
        };

        // Should fail when compouting the hash due to a failed contract class
        let internal_deploy_error = Deploy::new(
            0.into(),
            error_contract_class,
            Vec::new(),
            0.into(),
            1.into(),
        );
        assert_matches!(
            internal_deploy_error.unwrap_err(),
            SyscallHandlerError::HashError(HashError::FailedToComputeHash(_))
        )
    }
}