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

Add set_ap_to_actual_fee hint #2

Merged
merged 11 commits into from
Feb 26, 2024
41 changes: 37 additions & 4 deletions src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod unimplemented;
mod vars;

use std::collections::{HashMap, HashSet};
use std::ops::Add;

use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
Expand All @@ -28,6 +27,7 @@ use indoc::indoc;
use num_bigint::BigInt;

use crate::config::DEFAULT_INPUT_PATH;
use crate::execution::helper::ExecutionHelperWrapper;
use crate::io::input::StarknetOsInput;

type HintImpl = fn(
Expand All @@ -38,7 +38,7 @@ type HintImpl = fn(
&HashMap<String, Felt252>,
) -> Result<(), HintError>;

static HINTS: [(&str, HintImpl); 51] = [
static HINTS: [(&str, HintImpl); 52] = [
// (BREAKPOINT, breakpoint),
(STARKNET_OS_INPUT, starknet_os_input),
(INITIALIZE_STATE_CHANGES, initialize_state_changes),
Expand Down Expand Up @@ -90,6 +90,7 @@ static HINTS: [(&str, HintImpl); 51] = [
(syscalls::SEND_MESSAGE_TO_L1, syscalls::send_message_to_l1),
(syscalls::STORAGE_READ, syscalls::storage_read),
(syscalls::STORAGE_WRITE, syscalls::storage_write),
(SET_AP_TO_ACTUAL_FEE, set_ap_to_actual_fee),
(IS_ON_CURVE, is_on_curve),
(IS_N_GE_TWO, is_n_ge_two),
];
Expand Down Expand Up @@ -204,6 +205,7 @@ pub const STARKNET_OS_INPUT: &str = indoc! {r#"
ids.initial_carried_outputs.messages_to_l1 = segments.add_temp_segment()
ids.initial_carried_outputs.messages_to_l2 = segments.add_temp_segment()"#
};

pub fn starknet_os_input(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand Down Expand Up @@ -239,6 +241,7 @@ pub const INITIALIZE_STATE_CHANGES: &str = indoc! {r#"
for address, contract in os_input.contracts.items()
}"#
};

pub fn initialize_state_changes(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -263,6 +266,7 @@ pub fn initialize_state_changes(
}

pub const INITIALIZE_CLASS_HASHES: &str = "initial_dict = os_input.class_hash_to_compiled_class_hash";

pub fn initialize_class_hashes(
_vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -281,6 +285,7 @@ pub fn initialize_class_hashes(
}

pub const SEGMENTS_ADD: &str = "memory[ap] = to_felt_or_relocatable(segments.add())";

pub fn segments_add(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
Expand All @@ -293,6 +298,7 @@ pub fn segments_add(
}

pub const SEGMENTS_ADD_TEMP: &str = "memory[ap] = to_felt_or_relocatable(segments.add_temp_segment())";

pub fn segments_add_temp(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
Expand All @@ -304,7 +310,8 @@ pub fn segments_add_temp(
insert_value_into_ap(vm, temp_segment)
}

pub const TRANSACTIONS_LEN: &str = "memory[fp + 8] = to_felt_or_relocatable(len(os_input.transactions))";
pub const TRANSACTIONS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(os_input.transactions))";

pub fn transactions_len(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -313,10 +320,12 @@ pub fn transactions_len(
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("os_input")?;
vm.insert_value(vm.get_fp().add(8)?, os_input.transactions.len()).map_err(HintError::Memory)

insert_value_into_ap(vm, os_input.transactions.len())
}

pub const BREAKPOINT: &str = "breakpoint()";

pub fn breakpoint(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
Expand Down Expand Up @@ -346,6 +355,7 @@ pub fn breakpoint(
}

pub const IS_N_GE_TWO: &str = "memory[ap] = to_felt_or_relocatable(ids.n >= 2)";

pub fn is_n_ge_two(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
Expand All @@ -359,7 +369,30 @@ pub fn is_n_ge_two(
Ok(())
}

pub const SET_AP_TO_ACTUAL_FEE: &str =
"memory[ap] = to_felt_or_relocatable(execution_helper.tx_execution_info.actual_fee)";

pub fn set_ap_to_actual_fee(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let execution_helper = exec_scopes.get::<ExecutionHelperWrapper>(vars::scopes::EXECUTION_HELPER)?;
let actual_fee = execution_helper
.execution_helper
.borrow()
.tx_execution_info

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a fn tx_execution_info(&self) -> Result<&TransactionExecutionInfo, ExecutionHelperError> method to ExecutionHelper?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, that doesn't really make sense to me because it's an Option<TransactionExecutionInfo> which expresses that the TEI may or may not be present (depending on context, specifically whether we are between start_tx() and end_tx(), I believe).

Also, there currently isn't any impl ExecutionHelper, all the "helpers" exist in impl ExecutionHelperWrapper. Something like this might fit in there, but there are a lot of fields you could potentially do this for, which would be a lot of code bloat. I still don't think a Result makes sense, either.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the model used for the DeprecatedOsSyscallHandlerWrapper. I agree that it's a lot of bloat but all that RC + RefCell unwrapping should remain an implementation detail IMO.

.as_ref()
.ok_or(HintError::CustomHint("ExecutionHelper should have tx_execution_info".to_owned().into_boxed_str()))?
.actual_fee;

insert_value_into_ap(vm, Felt252::from(actual_fee.0))
}

pub const IS_ON_CURVE: &str = "ids.is_on_curve = (y * y) % SECP_P == y_square_int";

pub fn is_on_curve(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand Down
47 changes: 20 additions & 27 deletions src/hints/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::execution::deprecated_syscall_handler::DeprecatedOsSyscallHandlerWrap
use crate::hints::vars;

pub const CALL_CONTRACT: &str = "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn call_contract(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -29,6 +30,7 @@ pub fn call_contract(
}

pub const DELEGATE_CALL: &str = "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn delegate_call(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -46,6 +48,7 @@ pub fn delegate_call(

pub const DELEGATE_L1_HANDLER: &str =
"syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn delegate_l1_handler(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -62,6 +65,7 @@ pub fn delegate_l1_handler(
}

pub const DEPLOY: &str = "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn deploy(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -78,6 +82,7 @@ pub fn deploy(
}

pub const EMIT_EVENT: &str = "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn emit_event(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -94,6 +99,7 @@ pub fn emit_event(
}

pub const GET_BLOCK_NUMBER: &str = "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_block_number(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -111,6 +117,7 @@ pub fn get_block_number(

pub const GET_BLOCK_TIMESTAMP: &str =
"syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_block_timestamp(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -128,6 +135,7 @@ pub fn get_block_timestamp(

pub const GET_CALLER_ADDRESS: &str =
"syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_caller_address(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -145,6 +153,7 @@ pub fn get_caller_address(

pub const GET_CONTRACT_ADDRESS: &str =
"syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_contract_address(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -162,6 +171,7 @@ pub fn get_contract_address(

pub const GET_SEQUENCER_ADDRESS: &str =
"syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_sequencer_address(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -178,6 +188,7 @@ pub fn get_sequencer_address(
}

pub const GET_TX_INFO: &str = "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_tx_info(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -194,6 +205,7 @@ pub fn get_tx_info(
}

pub const GET_TX_SIGNATURE: &str = "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn get_tx_signature(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -210,6 +222,7 @@ pub fn get_tx_signature(
}

pub const LIBRARY: &str = "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn library_call(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -227,6 +240,7 @@ pub fn library_call(

pub const LIBRARY_CALL_L1_HANDLER: &str =
"syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn library_call_l1_handler(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -243,6 +257,7 @@ pub fn library_call_l1_handler(
}

pub const REPLACE_CLASS: &str = "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn replace_class(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -260,6 +275,7 @@ pub fn replace_class(

pub const SEND_MESSAGE_TO_L1: &str =
"syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn send_message_to_l1(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -276,6 +292,7 @@ pub fn send_message_to_l1(
}

pub const STORAGE_READ: &str = "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn storage_read(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand All @@ -292,6 +309,7 @@ pub fn storage_read(
}

pub const STORAGE_WRITE: &str = "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)";

pub fn storage_write(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand Down Expand Up @@ -336,39 +354,14 @@ pub fn set_syscall_ptr(

#[cfg(test)]
mod tests {
use std::sync::Arc;

use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use cairo_vm::types::exec_scope::ExecutionScopes;
use blockifier::block_context::BlockContext;
use cairo_vm::types::relocatable::Relocatable;
use rstest::{fixture, rstest};
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::{contract_address, patricia_key};

use super::*;
use crate::hints::tests::tests::block_context;
use crate::ExecutionHelperWrapper;

#[fixture]
fn block_context() -> BlockContext {
BlockContext {
chain_id: ChainId("SN_GOERLI".to_string()),
block_number: BlockNumber(1_000_000),
block_timestamp: BlockTimestamp(1_704_067_200),
sequencer_address: contract_address!("0x0"),
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: contract_address!("0x1"),
strk_fee_token_address: contract_address!("0x2"),
},
vm_resource_fee_cost: Arc::new(HashMap::new()),
gas_prices: GasPrices { eth_l1_gas_price: 1, strk_l1_gas_price: 1 },
invoke_tx_max_n_steps: 1,
validate_max_n_steps: 1,
max_recursion_depth: 50,
}
}

#[fixture]
fn exec_scopes(block_context: BlockContext) -> ExecutionScopes {
let syscall_ptr = Relocatable::from((0, 0));
Expand Down
Loading
Loading