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

Commit

Permalink
Add reverted transactions (#813)
Browse files Browse the repository at this point in the history
* Remove mutable ref from StateReader

* Fix tests

* Fix tests

* Fix cairo2 tests

* Fix clippy

* Add tmp_state

* Fix tmp_state

* Remove unused max steps

* Revert "Remove unused max steps"

This reverts commit a8c6374.

* Add reverted Transactions

* Add revert_error to TransactionExecutionInfo

* Test test_reverted_transaction_wrong_entry_point

* Disable reverted transaction for declare verify

* Replace empty for default

* Update CachedState

* Fix tests

* Fix test

* Remove unused imports
  • Loading branch information
matias-gonz authored Jul 17, 2023
1 parent 4b90ba8 commit 8809ba0
Show file tree
Hide file tree
Showing 46 changed files with 941 additions and 631 deletions.
10 changes: 5 additions & 5 deletions bench/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_in_rust::{
transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction},
utils::Address,
};
use std::hint::black_box;
use std::{hint::black_box, sync::Arc};

lazy_static! {
// include_str! doesn't seem to work in CI
Expand Down Expand Up @@ -60,7 +60,7 @@ fn main() {
fn deploy_account() {
const RUNS: usize = 500;

let state_reader = InMemoryStateReader::default();
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);

state
Expand Down Expand Up @@ -96,7 +96,7 @@ fn deploy_account() {
fn declare() {
const RUNS: usize = 5;

let state_reader = InMemoryStateReader::default();
let state_reader = Arc::new(InMemoryStateReader::default());
let state = CachedState::new(state_reader, Some(Default::default()), None);

let block_context = &Default::default();
Expand Down Expand Up @@ -128,7 +128,7 @@ fn declare() {
fn deploy() {
const RUNS: usize = 8;

let state_reader = InMemoryStateReader::default();
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);

state
Expand Down Expand Up @@ -163,7 +163,7 @@ fn deploy() {
fn invoke() {
const RUNS: usize = 100;

let state_reader = InMemoryStateReader::default();
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);

state
Expand Down
29 changes: 19 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ use starknet_in_rust::{
block_context::BlockContext,
constants::{DECLARE_VERSION, TRANSACTION_VERSION},
},
execution::{execution_entry_point::ExecutionEntryPoint, TransactionExecutionContext},
execution::{
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
TransactionExecutionContext,
},
hash_utils::calculate_contract_address,
parser_errors::ParserError,
serde_structs::read_abi,
services::api::contract_classes::deprecated_contract_class::ContractClass,
state::{
cached_state::CachedState,
state_api::{State, StateReader},
},
state::{cached_state::CachedState, state_api::State},
state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager},
transaction::InvokeFunction,
transaction::{error::TransactionError, InvokeFunction},
utils::{felt_to_hash, string_to_hash, Address},
};
use std::{collections::HashMap, path::PathBuf, sync::Mutex};
use std::{
collections::HashMap,
path::PathBuf,
sync::{Arc, Mutex},
};

#[derive(Parser)]
struct Cli {
Expand Down Expand Up @@ -248,13 +252,18 @@ fn call_parser(
None,
0,
);
let call_info = execution_entry_point.execute(
let block_context = BlockContext::default();
let ExecutionResult { call_info, .. } = execution_entry_point.execute(
cached_state,
&BlockContext::default(),
&block_context,
&mut ExecutionResourcesManager::default(),
&mut TransactionExecutionContext::default(),
false,
block_context.invoke_tx_max_n_steps(),
)?;

let call_info = call_info.ok_or(TransactionError::CallInfoIsNone)?;

Ok(call_info.retdata)
}

Expand Down Expand Up @@ -303,7 +312,7 @@ async fn call_req(data: web::Data<AppState>, args: web::Json<CallArgs>) -> HttpR
pub async fn start_devnet(port: u16) -> Result<(), std::io::Error> {
let cached_state = web::Data::new(AppState {
cached_state: Mutex::new(CachedState::<InMemoryStateReader>::new(
InMemoryStateReader::default(),
Arc::new(InMemoryStateReader::default()),
Some(HashMap::new()),
None,
)),
Expand Down
28 changes: 15 additions & 13 deletions fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate honggfuzz;
use cairo_vm::felt::Felt252;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_traits::Zero;
use starknet_in_rust::execution::execution_entry_point::ExecutionResult;
use starknet_in_rust::EntryPointType;
use starknet_in_rust::{
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
Expand All @@ -18,6 +19,7 @@ use starknet_in_rust::{
utils::{calculate_sn_keccak, Address},
};

use std::sync::Arc;
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
Expand Down Expand Up @@ -124,7 +126,8 @@ fn main() {
//* Create state with previous data
//* ---------------------------------------

let mut state = CachedState::new(state_reader, Some(contract_class_cache), None);
let mut state =
CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None);

//* ------------------------------------
//* Create execution entry point
Expand Down Expand Up @@ -180,18 +183,17 @@ fn main() {
..Default::default()
};

assert_eq!(
exec_entry_point
.execute(
&mut state,
&block_context,
&mut resources_manager,
&mut tx_execution_context,
false,
)
.unwrap(),
expected_call_info
);
let ExecutionResult { call_info, .. } = exec_entry_point
.execute(
&mut state,
&block_context,
&mut resources_manager,
&mut tx_execution_context,
false,
block_context.invoke_tx_max_n_steps(),
)
.unwrap();
assert_eq!(call_info.unwrap(), expected_call_info);

assert!(!state.cache().storage_writes().is_empty());
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/bin/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use cairo_vm::felt::{felt_str, Felt252};
use num_traits::Zero;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
state_reader
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use cairo_vm::felt::{felt_str, Felt252};
use num_traits::Zero;
Expand Down Expand Up @@ -99,7 +99,7 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
state_reader
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/invoke_with_cachedstate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use cairo_vm::felt::{felt_str, Felt252};
use num_traits::Zero;
Expand Down Expand Up @@ -106,7 +106,7 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
state_reader
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
Expand Down
Loading

0 comments on commit 8809ba0

Please sign in to comment.