Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: calculate timestamp and block number tacking account execution mode (execution vs validate) #401

Merged
merged 12 commits into from
Oct 16, 2024
2 changes: 2 additions & 0 deletions crates/bin/prove_block/tests/prove_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ use rstest::rstest;
#[case::key_not_in_proof_0(155087)]
#[case::key_not_in_proof_1(162388)]
#[case::key_not_in_proof_2(155172)]
#[case::timestamp_rounding_1(162389)]
#[case::timestamp_rounding_2(167815)]
#[ignore = "Requires a running Pathfinder node"]
#[tokio::test(flavor = "multi_thread")]
async fn test_prove_selected_blocks(#[case] block_number: u64) {
Expand Down
23 changes: 23 additions & 0 deletions crates/starknet-os/src/cairo_types/new_syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,26 @@ pub struct StorageWriteRequest {
pub struct ReplaceClassRequest {
pub class_hash: Felt252,
}

#[allow(unused)]
#[derive(FieldOffsetGetters)]
pub struct ExecutionInfo {
block_info: Relocatable,
tx_info: Relocatable,
// Entry-point-specific info.
caller_address: Felt252,
// The execution is done in the context of the contract at this address.
// It controls the storage being used, messages sent to L1, calling contracts, etc.
contract_address: Felt252,
// The entry point selector.
selector: Felt252,
}

#[allow(unused)]
#[derive(FieldOffsetGetters)]
pub struct BlockInfo {
block_number: Felt252,
block_timestamp: Felt252,
// The address of the sequencer that is creating this block.
sequencer_address: Felt252,
}
20 changes: 16 additions & 4 deletions crates/starknet-os/src/execution/deprecated_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cairo_vm::Felt252;
use tokio::sync::RwLock;

use super::helper::ExecutionHelperWrapper;
use crate::cairo_types::new_syscalls::{BlockInfo as BlockInfoStruct, ExecutionInfo};
use crate::cairo_types::syscalls::{
CallContract, CallContractResponse, Deploy, DeployResponse, GetBlockNumber, GetBlockNumberResponse,
GetBlockTimestamp, GetBlockTimestampResponse, GetContractAddress, GetContractAddressResponse, GetSequencerAddress,
Expand Down Expand Up @@ -134,11 +135,16 @@ where

pub async fn get_block_number(&self, syscall_ptr: Relocatable, vm: &mut VirtualMachine) -> Result<(), HintError> {
let syscall_handler = self.deprecated_syscall_handler.read().await;
let execution_helper = syscall_handler.exec_wrapper.execution_helper.read().await;

let block_number = syscall_handler.block_info.block_number;
let execution_info_ptr = execution_helper
.call_execution_info_ptr
.ok_or(HintError::SyscallError("Execution info pointer not set".to_string().into_boxed_str()))?;
let block_info_ptr = vm.get_relocatable((execution_info_ptr + ExecutionInfo::block_info_offset())?)?;
let block_number = vm.get_integer((block_info_ptr + BlockInfoStruct::block_number_offset())?)?.into_owned();

let response_offset = GetBlockNumber::response_offset() + GetBlockNumberResponse::block_number_offset();
vm.insert_value((syscall_ptr + response_offset)?, Felt252::from(block_number.0))?;
vm.insert_value((syscall_ptr + response_offset)?, block_number)?;

Ok(())
}
Expand All @@ -149,12 +155,18 @@ where
vm: &mut VirtualMachine,
) -> Result<(), HintError> {
let syscall_handler = self.deprecated_syscall_handler.read().await;
let execution_helper = syscall_handler.exec_wrapper.execution_helper.read().await;

let block_timestamp = syscall_handler.block_info.block_timestamp;
let execution_info_ptr = execution_helper
.call_execution_info_ptr
.ok_or(HintError::SyscallError("Execution info pointer not set".to_string().into_boxed_str()))?;
let block_info_ptr = vm.get_relocatable((execution_info_ptr + ExecutionInfo::block_info_offset())?)?;
let block_timestamp =
vm.get_integer((block_info_ptr + BlockInfoStruct::block_timestamp_offset())?)?.into_owned();

let response_offset =
GetBlockTimestamp::response_offset() + GetBlockTimestampResponse::block_timestamp_offset();
vm.insert_value((syscall_ptr + response_offset)?, Felt252::from(block_timestamp.0))?;
vm.insert_value((syscall_ptr + response_offset)?, block_timestamp)?;

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/deprecated_syscalls_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ async fn test_syscall_get_block_number_cairo0(
let sender_address = initial_state.deployed_cairo0_contracts.get("account_with_dummy_validate").unwrap().address;
let contract_address = initial_state.deployed_cairo0_contracts.get("test_contract").unwrap().address;

let expected_block_number = felt!(block_context.block_info().block_number.0);
let block_number = block_context.block_info().block_number.0;

let tx_version = TransactionVersion::ZERO;
let mut nonce_manager = NonceManager::default();
let tx = test_utils::account_invoke_tx(invoke_tx_args! {
max_fee,
sender_address: sender_address,
calldata: create_calldata(contract_address, "test_get_block_number", &[expected_block_number]),
calldata: create_calldata(contract_address, "test_get_block_number", &[felt!(block_number)]),
version: tx_version,
nonce: nonce_manager.next(sender_address),
});
Expand Down Expand Up @@ -136,14 +136,14 @@ async fn test_syscall_get_block_timestamp_cairo0(
let sender_address = initial_state.deployed_cairo0_contracts.get("account_with_dummy_validate").unwrap().address;
let contract_address = initial_state.deployed_cairo0_contracts.get("test_contract").unwrap().address;

let expected_block_timestamp = felt!(block_context.block_info().block_timestamp.0);
let block_timestamp = block_context.block_info().block_timestamp.0;

let tx_version = TransactionVersion::ZERO;
let mut nonce_manager = NonceManager::default();
let tx = test_utils::account_invoke_tx(invoke_tx_args! {
max_fee,
sender_address: sender_address,
calldata: create_calldata(contract_address, "test_get_block_timestamp", &[expected_block_timestamp]),
calldata: create_calldata(contract_address, "test_get_block_timestamp", &[felt!(block_timestamp)]),
version: tx_version,
nonce: nonce_manager.next(sender_address),
});
Expand Down
Loading