Skip to content

Commit

Permalink
fix(execution): use the right values to check if the node is synced (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware authored Nov 19, 2023
1 parent bd0ecd6 commit 92f19bb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
58 changes: 58 additions & 0 deletions crates/papyrus_execution/src/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use blockifier::abi::abi_utils::get_storage_var_address;
use blockifier::abi::constants::STEP_GAS_COST;
use blockifier::execution::call_info::Retdata;
use indexmap::indexmap;
use papyrus_storage::compiled_class::CasmStorageReader;
use papyrus_storage::test_utils::get_test_storage;
use pretty_assertions::assert_eq;
use starknet_api::block::{BlockNumber, GasPrice};
Expand Down Expand Up @@ -52,6 +53,7 @@ use crate::{
BlockExecutionConfig,
ExecutableTransactionInput,
ExecutionConfigByBlock,
ExecutionError,
};

// Test calling entry points of a deprecated class.
Expand Down Expand Up @@ -774,3 +776,59 @@ fn induced_state_diff() {
};
assert_eq!(simulation_results[3].1, expected_deploy_account);
}

#[test]
fn execute_call_checks_if_node_is_synced() {
let ((storage_reader, storage_writer), _temp_dir) = get_test_storage();
prepare_storage(storage_writer);

let casm_marker = storage_reader.begin_ro_txn().unwrap().get_compiled_class_marker().unwrap();
let latest_block = casm_marker.prev().unwrap();
let chain_id = ChainId(CHAIN_ID.to_string());

// At the beginning of the latest block.
let state_number = StateNumber::right_before_block(latest_block);
let block_context = latest_block;
execute_call(
storage_reader.clone(),
&chain_id,
state_number,
block_context,
&DEPRECATED_CONTRACT_ADDRESS,
selector_from_name("without_arg"),
Calldata::default(),
&test_block_execution_config(),
)
.unwrap();

// At the end of the latest block.
let state_number = StateNumber::right_after_block(latest_block);
let block_context = latest_block;
execute_call(
storage_reader.clone(),
&chain_id,
state_number,
block_context,
&DEPRECATED_CONTRACT_ADDRESS,
selector_from_name("without_arg"),
Calldata::default(),
&test_block_execution_config(),
)
.unwrap();

// At the beginning of the next block.
let state_number = StateNumber::right_before_block(latest_block.next());
let block_context = latest_block.next();
let err = execute_call(
storage_reader.clone(),
&chain_id,
state_number,
block_context,
&DEPRECATED_CONTRACT_ADDRESS,
selector_from_name("without_arg"),
Calldata::default(),
&test_block_execution_config(),
)
.unwrap_err();
assert_matches!(err, ExecutionError::NotSynced { state_number: _, compiled_class_marker: _ });
}
10 changes: 9 additions & 1 deletion crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#![warn(missing_docs)]
//! Functionality for executing Starknet transactions and contract entry points.
//!
//! In this module, we use the term "state_number" to refer to the state of the storage at the
//! execution, and "block_context_block_number" to refer to the block in which the transactions
//! should run. For example, if you want to simulate transactions at the beginning of block 10, you
//! should use state_number = 10 and block_context_block_number = 10. If you want to simulate
//! transactions at the end of block 10, you should use state_number = 11 and
//! block_context_block_number = 10.
//! See documentation of [StateNumber] for more details.
#[cfg(test)]
mod execution_test;
pub mod execution_utils;
Expand Down Expand Up @@ -182,7 +190,7 @@ pub fn execute_call(
// transactions.
{
let txn = storage_reader.begin_ro_txn()?;
verify_node_synced(&txn, state_number.0, state_number)?;
verify_node_synced(&txn, block_context_number, state_number)?;
verify_contract_exists(contract_address, &txn, state_number)?;
}

Expand Down

0 comments on commit 92f19bb

Please sign in to comment.