Skip to content

Commit

Permalink
feat(blockifier): add get_execution_info_v2 syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 committed Nov 13, 2024
1 parent 4ba7720 commit 2c26d76
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/cairo_native
Submodule cairo_native updated 48 files
+5 −5 .github/workflows/bench-hyperfine.yml
+12 −12 .github/workflows/ci.yml
+2 −2 .github/workflows/publish.yml
+2 −2 .github/workflows/release.yml
+2 −2 .github/workflows/rustdoc.yml
+132 −0 .github/workflows/starknet-blocks.yml
+51 −51 Cargo.lock
+6 −80 benches/benches.rs
+17 −0 benches/compile_time.rs
+54 −3 benches/libfuncs.rs
+33 −45 docs/implementing_libfuncs.md
+0 −5 programs/benches/factorial_2M.c
+0 −5 programs/benches/fib_2M.c
+0 −5 programs/benches/logistic_map.c
+42 −48 programs/compile_benches/dijkstra.cairo
+8 −17 programs/compile_benches/extended_euclidean_algorithm.cairo
+78 −27 programs/compile_benches/fast_power.cairo
+0 −285 programs/compile_benches/sha256.cairo
+539 −0 programs/compile_benches/sha512.cairo
+5 −2 runtime/Cargo.toml
+236 −6 runtime/src/lib.rs
+1 −1 rust-toolchain.toml
+32 −0 scripts/diff-check.sh
+6 −6 src/arch.rs
+2 −26 src/compiler.rs
+15 −1 src/context.rs
+0 −3 src/error.rs
+30 −58 src/executor.rs
+0 −12 src/executor/aot.rs
+13 −66 src/executor/contract.rs
+0 −13 src/executor/jit.rs
+25 −0 src/ffi.rs
+5 −6 src/libfuncs/array.rs
+5 −3 src/libfuncs/circuit.rs
+18 −121 src/libfuncs/gas.rs
+290 −595 src/libfuncs/starknet.rs
+676 −1,507 src/libfuncs/starknet/secp256.rs
+6 −9 src/metadata/gas.rs
+4 −6 src/types.rs
+19 −24 src/types/array.rs
+2 −6 src/types/builtin_costs.rs
+3 −45 src/utils.rs
+87 −1 src/utils/block_ext.rs
+2 −2 src/values.rs
+6 −6 tests/alexandria/Scarb.lock
+4 −4 tests/alexandria/Scarb.toml
+6 −12 tests/common.rs
+0 −2 tests/tests/boolean.rs
53 changes: 50 additions & 3 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ use cairo_native::starknet::{
StarknetSyscallHandler,
SyscallResult,
TxInfo,
TxV2Info,
U256,
};
use cairo_native::starknet_stub::encode_str_as_felts;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::native::utils::{
calculate_resource_bounds,
default_tx_v2_info,
encode_str_as_felts,
};
use crate::execution::syscalls::hint_processor::{SyscallExecutionError, 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 @@ -167,6 +173,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().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 @@ -195,9 +231,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::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,
},
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,
},
]
}
})
}
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

0 comments on commit 2c26d76

Please sign in to comment.