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

feat(blockifier): add get_execution_info_v2 syscall #1812

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cairo_native::starknet::{
StarknetSyscallHandler,
SyscallResult,
TxInfo,
TxV2Info,
U256,
};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
Expand All @@ -35,12 +36,14 @@ use crate::execution::entry_point::{
};
use crate::execution::errors::EntryPointExecutionError;
use crate::execution::execution_utils::execute_deployment;
use crate::execution::native::utils::{calculate_resource_bounds, default_tx_v2_info};
use crate::execution::syscalls::hint_processor::{
SyscallExecutionError,
INVALID_INPUT_LENGTH_ERROR,
OUT_OF_GAS_ERROR,
};
use crate::state::state_api::State;
use crate::transaction::objects::TransactionInfo;

pub struct NativeSyscallHandler<'state> {
// Input for execution.
Expand Down Expand Up @@ -216,6 +219,36 @@ impl<'state> NativeSyscallHandler<'state> {
}
}
}

fn get_tx_info_v2(&self) -> SyscallResult<TxV2Info> {
let tx_info = &self.context.tx_context.tx_info;
let native_tx_info = TxV2Info {
version: tx_info.version().0,
account_contract_address: Felt::from(tx_info.sender_address()),
max_fee: tx_info.max_fee_for_execution_info_syscall().0,
signature: tx_info.signature().0,
transaction_hash: tx_info.transaction_hash().0,
chain_id: Felt::from_hex(
&self.context.tx_context.block_context.chain_info.chain_id.as_hex(),
)
.expect("Failed to convert the chain_id to hex."),
nonce: tx_info.nonce().0,
..default_tx_v2_info()
};

match tx_info {
TransactionInfo::Deprecated(_) => Ok(native_tx_info),
TransactionInfo::Current(context) => Ok(TxV2Info {
resource_bounds: calculate_resource_bounds(context)?,
tip: context.tip.0.into(),
paymaster_data: context.paymaster_data.0.clone(),
nonce_data_availability_mode: context.nonce_data_availability_mode.into(),
fee_data_availability_mode: context.fee_data_availability_mode.into(),
account_deployment_data: context.account_deployment_data.0.clone(),
..native_tx_info
}),
}
}
}

impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
Expand Down Expand Up @@ -244,9 +277,20 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

fn get_execution_info_v2(
&mut self,
_remaining_gas: &mut u128,
remaining_gas: &mut u128,
) -> SyscallResult<ExecutionInfoV2> {
todo!("Implement get_execution_info_v2 syscall.");
self.pre_execute_syscall(
remaining_gas,
self.context.gas_costs().get_execution_info_gas_cost,
)?;

Ok(ExecutionInfoV2 {
block_info: self.get_block_info(),
tx_info: self.get_tx_info_v2()?,
caller_address: Felt::from(self.call.caller_address),
contract_address: Felt::from(self.call.storage_address),
entry_point_selector: self.call.entry_point_selector.0,
})
}

fn deploy(
Expand Down
62 changes: 62 additions & 0 deletions crates/blockifier/src/execution/native/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use cairo_lang_starknet_classes::contract_class::ContractEntryPoint;
use cairo_native::starknet::{ResourceBounds, SyscallResult, TxV2Info};
use starknet_api::core::EntryPointSelector;
use starknet_api::transaction::fields::{Resource, ValidResourceBounds};
use starknet_types_core::felt::Felt;

use crate::transaction::objects::CurrentTransactionInfo;

pub fn contract_entrypoint_to_entrypoint_selector(
entrypoint: &ContractEntryPoint,
) -> EntryPointSelector {
Expand All @@ -20,3 +24,61 @@ pub fn encode_str_as_felts(msg: &str) -> Vec<Felt> {
}
encoding
}

pub fn default_tx_v2_info() -> TxV2Info {
TxV2Info {
version: Default::default(),
account_contract_address: Default::default(),
max_fee: 0,
signature: vec![],
transaction_hash: Default::default(),
chain_id: Default::default(),
nonce: Default::default(),
resource_bounds: vec![],
tip: 0,
paymaster_data: vec![],
nonce_data_availability_mode: 0,
fee_data_availability_mode: 0,
account_deployment_data: vec![],
}
}

pub fn calculate_resource_bounds(
tx_info: &CurrentTransactionInfo,
) -> SyscallResult<Vec<ResourceBounds>> {
Ok(match tx_info.resource_bounds {
ValidResourceBounds::L1Gas(l1_bounds) => {
vec![
ResourceBounds {
resource: Felt::from_hex(Resource::L1Gas.to_hex()).unwrap(),
max_amount: l1_bounds.max_amount.0,
max_price_per_unit: l1_bounds.max_price_per_unit.0,
},
ResourceBounds {
resource: Felt::from_hex(Resource::L2Gas.to_hex()).unwrap(),
max_amount: 0,
max_price_per_unit: 0,
},
]
}
ValidResourceBounds::AllResources(all_bounds) => {
vec![
ResourceBounds {
resource: Felt::from_hex(Resource::L1Gas.to_hex()).unwrap(),
max_amount: all_bounds.l1_gas.max_amount.0,
max_price_per_unit: all_bounds.l1_gas.max_price_per_unit.0,
},
ResourceBounds {
resource: Felt::from_hex(Resource::L2Gas.to_hex()).unwrap(),
max_amount: all_bounds.l2_gas.max_amount.0,
max_price_per_unit: all_bounds.l2_gas.max_price_per_unit.0,
},
ResourceBounds {
resource: Felt::from_hex(Resource::L1DataGas.to_hex()).unwrap(),
max_amount: all_bounds.l1_data_gas.max_amount.0,
max_price_per_unit: all_bounds.l1_data_gas.max_price_per_unit.0,
},
]
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ use crate::transaction::objects::{
"Native [V1]: Execute execution mode: block info should be as usual. Transaction V1."
)
)]
#[cfg_attr(
feature = "cairo_native",
test_case(
FeatureContract::TestContract(CairoVersion::Native),
ExecutionMode::Validate,
TransactionVersion::ONE,
false;
"Native: Validate execution mode: block info fields should be zeroed. Transaction V1."
)
)]
#[cfg_attr(
feature = "cairo_native",
test_case(
FeatureContract::TestContract(CairoVersion::Native),
ExecutionMode::Execute,
TransactionVersion::ONE,
false;
"Native: Execute execution mode: block info should be as usual. Transaction V1."
)
)]
#[cfg_attr(
feature = "cairo_native",
test_case(
FeatureContract::TestContract(CairoVersion::Native),
ExecutionMode::Validate,
TransactionVersion::THREE,
false;
"Native: Validate execution mode: block info fields should be zeroed. Transaction V3."
)
)]
#[cfg_attr(
feature = "cairo_native",
test_case(
FeatureContract::TestContract(CairoVersion::Native),
ExecutionMode::Execute,
TransactionVersion::THREE,
false;
"Native: Execute execution mode: block info should be as usual. Transaction V3."
)
)]
#[test_case(
FeatureContract::TestContract(CairoVersion::Cairo1),
ExecutionMode::Validate,
Expand Down
9 changes: 9 additions & 0 deletions crates/starknet_api/src/data_availability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ impl From<DataAvailabilityMode> for Felt {
}
}

impl From<DataAvailabilityMode> for u32 {
fn from(data_availability_mode: DataAvailabilityMode) -> u32 {
match data_availability_mode {
DataAvailabilityMode::L1 => 0,
DataAvailabilityMode::L2 => 1,
}
}
}

#[derive(
Clone, Default, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
Expand Down
Loading