Skip to content

Commit

Permalink
feat(blockifier): version bound accounts: add max tip
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Mar 3, 2025
1 parent a40b53d commit 2da31fc
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@
}
},
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@
},
"validated": "VALID",
"v1_bound_accounts_cairo0": [],
"v1_bound_accounts_cairo1": []
"v1_bound_accounts_cairo1": [],
"v1_bound_accounts_max_tip": "0x0"
},
"os_resources": {
"execute_syscalls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
],
"v1_bound_accounts_cairo1": [
"0x04c6d6cf894f8bc96bb9c525e6853e5483177841f7388f74a46cfda6f028c755"
]
],
"v1_bound_accounts_max_tip": "0x1000"
},
"os_resources": {
"execute_syscalls": {
Expand Down
6 changes: 5 additions & 1 deletion crates/blockifier/src/blockifier_versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::define_versioned_constants;
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::transaction::fields::GasVectorComputationMode;
use starknet_api::transaction::fields::{GasVectorComputationMode, Tip};
use strum::IntoEnumIterator;
use thiserror::Error;

Expand Down Expand Up @@ -790,6 +790,7 @@ pub struct OsConstants {
pub execute_max_sierra_gas: GasAmount,
pub v1_bound_accounts_cairo0: Vec<ClassHash>,
pub v1_bound_accounts_cairo1: Vec<ClassHash>,
pub v1_bound_accounts_max_tip: Tip,
}

impl OsConstants {
Expand Down Expand Up @@ -876,6 +877,7 @@ impl TryFrom<OsConstantsRawJson> for OsConstants {
)?);
let v1_bound_accounts_cairo0 = raw_json_data.v1_bound_accounts_cairo0;
let v1_bound_accounts_cairo1 = raw_json_data.v1_bound_accounts_cairo1;
let v1_bound_accounts_max_tip = raw_json_data.v1_bound_accounts_max_tip;
let os_constants = OsConstants {
gas_costs,
validate_rounding_consts,
Expand All @@ -884,6 +886,7 @@ impl TryFrom<OsConstantsRawJson> for OsConstants {
execute_max_sierra_gas,
v1_bound_accounts_cairo0,
v1_bound_accounts_cairo1,
v1_bound_accounts_max_tip,
};
Ok(os_constants)
}
Expand Down Expand Up @@ -926,6 +929,7 @@ struct OsConstantsRawJson {
os_contract_addresses: OsContractAddresses,
v1_bound_accounts_cairo0: Vec<ClassHash>,
v1_bound_accounts_cairo1: Vec<ClassHash>,
v1_bound_accounts_max_tip: Tip,
}

impl OsConstantsRawJson {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_api::test_utils::{
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE,
TEST_SEQUENCER_ADDRESS,
};
use starknet_api::transaction::fields::{Calldata, ContractAddressSalt, Fee};
use starknet_api::transaction::fields::{Calldata, ContractAddressSalt, Fee, Tip};
use starknet_api::transaction::{
EventContent,
EventData,
Expand Down Expand Up @@ -45,11 +45,7 @@ use crate::test_utils::{
get_syscall_resources,
trivial_external_entry_point_new,
};
use crate::transaction::objects::{
CommonAccountFields,
DeprecatedTransactionInfo,
TransactionInfo,
};
use crate::transaction::objects::{CommonAccountFields, CurrentTransactionInfo, TransactionInfo};
use crate::{check_entry_point_execution_error_for_custom_hint, retdata};

#[test]
Expand Down Expand Up @@ -466,6 +462,8 @@ fn test_block_info_syscalls(
fn test_tx_info(
#[values(false, true)] only_query: bool,
#[values(false, true)] v1_bound_account: bool,
// Whether the tip is larger than `v1_bound_accounts_max_tip`.
#[values(false, true)] high_tip: bool,
) {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0);
let mut test_contract_data: FeatureContractData = test_contract.into();
Expand All @@ -479,7 +477,7 @@ fn test_tx_info(
let mut state =
test_state_ex(&ChainInfo::create_for_testing(), Fee(0), &[(test_contract_data, 1)]);
let mut version = felt!(3_u8);
let mut expected_version = if v1_bound_account { felt!(1_u8) } else { version };
let mut expected_version = if v1_bound_account && !high_tip { felt!(1_u8) } else { version };
if only_query {
let simulate_version_base = *QUERY_VERSION_BASE;
version += simulate_version_base;
Expand All @@ -503,7 +501,12 @@ fn test_tx_info(
calldata: expected_tx_info,
..trivial_external_entry_point_new(test_contract)
};
let tx_info = TransactionInfo::Deprecated(DeprecatedTransactionInfo {

// Transaction tip.
let tip = Tip(VersionedConstants::latest_constants().os_constants.v1_bound_accounts_max_tip.0
+ if high_tip { 1 } else { 0 });

let tx_info = TransactionInfo::Current(CurrentTransactionInfo {
common_fields: CommonAccountFields {
transaction_hash: tx_hash,
version: TransactionVersion::THREE,
Expand All @@ -512,7 +515,8 @@ fn test_tx_info(
only_query,
..Default::default()
},
max_fee,
tip,
..CurrentTransactionInfo::create_for_testing()
});
let limit_steps_by_resources = false; // Do not limit steps by resources as we use default reasources.
let result = entry_point_call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use crate::execution::hint_code;
use crate::execution::syscalls::hint_processor::{EmitEventError, SyscallUsageMap};
use crate::state::errors::StateError;
use crate::state::state_api::State;
use crate::transaction::objects::TransactionInfo;

#[derive(Debug, Error)]
pub enum DeprecatedSyscallExecutionError {
Expand Down Expand Up @@ -332,11 +333,19 @@ impl<'a> DeprecatedSyscallHintProcessor<'a> {
// the syscall should return transaction version 1 instead.
// In such a case, `self.tx_info_start_ptr` is not used.
if version == TransactionVersion::THREE && v1_bound_accounts.contains(&self.class_hash) {
let modified_version = signed_tx_version(
&TransactionVersion::ONE,
&TransactionOptions { only_query: tx_context.tx_info.only_query() },
);
return self.allocate_tx_info_segment(vm, Some(modified_version));
let tip = match &tx_context.tx_info {
TransactionInfo::Current(transaction_info) => transaction_info.tip,
TransactionInfo::Deprecated(_) => {
panic!("Transaction info variant doesn't match transaction version")
}
};
if tip <= versioned_constants.os_constants.v1_bound_accounts_max_tip {
let modified_version = signed_tx_version(
&TransactionVersion::ONE,
&TransactionOptions { only_query: tx_context.tx_info.only_query() },
);
return self.allocate_tx_info_segment(vm, Some(modified_version));
}
}

match self.tx_info_start_ptr {
Expand Down
21 changes: 15 additions & 6 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::execution::syscalls::hint_processor::{
};
use crate::state::state_api::State;
use crate::transaction::account_transaction::is_cairo1;
use crate::transaction::objects::TransactionInfo;

pub type SyscallResult<T> = Result<T, SyscallExecutionError>;
pub const KECCAK_FULL_RATE_IN_WORDS: usize = 17;
Expand Down Expand Up @@ -195,13 +196,21 @@ impl<'state> SyscallHandlerBase<'state> {
// If the transaction version is 3 and the account is in the v1-bound-accounts set,
// the syscall should return transaction version 1 instead.
if version == TransactionVersion::THREE && v1_bound_accounts.contains(class_hash) {
signed_tx_version(
&TransactionVersion::ONE,
&TransactionOptions { only_query: tx_context.tx_info.only_query() },
)
} else {
tx_context.tx_info.signed_version()
let tip = match &tx_context.tx_info {
TransactionInfo::Current(transaction_info) => transaction_info.tip,
TransactionInfo::Deprecated(_) => {
panic!("Transaction info variant doesn't match transaction version")
}
};
if tip <= versioned_constants.os_constants.v1_bound_accounts_max_tip {
return signed_tx_version(
&TransactionVersion::ONE,
&TransactionOptions { only_query: tx_context.tx_info.only_query() },
);
}
}

tx_context.tx_info.signed_version()
}

pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> {
Expand Down
Loading

0 comments on commit 2da31fc

Please sign in to comment.