Skip to content

Commit

Permalink
feat(blockifier): version bound accounts: support v1-bound Cairo1 acc…
Browse files Browse the repository at this point in the history
…ounts

commit-id:1c02f843
  • Loading branch information
liorgold2 committed Feb 5, 2025
1 parent 7b93eb6 commit a7ee214
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<'state> NativeSyscallHandler<'state> {
fn get_tx_info_v1(&self) -> TxInfo {
let tx_info = &self.base.context.tx_context.tx_info;
TxInfo {
version: tx_info.signed_version().0,
version: self.base.tx_version_for_get_execution_info().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,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'state> NativeSyscallHandler<'state> {
fn get_tx_info_v2(&self) -> SyscallResult<TxV2Info> {
let tx_info = &self.base.context.tx_context.tx_info;
let native_tx_info = TxV2Info {
version: tx_info.signed_version().0,
version: self.base.tx_version_for_get_execution_info().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,
Expand Down
23 changes: 19 additions & 4 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use starknet_api::transaction::fields::{
Resource,
ValidResourceBounds,
};
use starknet_api::transaction::TransactionVersion;
use starknet_api::StarknetApiError;
use starknet_types_core::felt::{Felt, FromStrError};
use thiserror::Error;
Expand Down Expand Up @@ -404,10 +405,19 @@ impl<'a> SyscallHintProcessor<'a> {
&mut self,
vm: &mut VirtualMachine,
) -> SyscallResult<Relocatable> {
let returned_version = self.base.tx_version_for_get_execution_info();
let original_version = self.base.context.tx_context.tx_info.signed_version();

// If the transaction version was overridden, `self.execution_info_ptr` cannot be used.
if returned_version != original_version {
return self.allocate_execution_info_segment(vm, returned_version);
}

match self.execution_info_ptr {
Some(execution_info_ptr) => Ok(execution_info_ptr),
None => {
let execution_info_ptr = self.allocate_execution_info_segment(vm)?;
let execution_info_ptr =
self.allocate_execution_info_segment(vm, original_version)?;
self.execution_info_ptr = Some(execution_info_ptr);
Ok(execution_info_ptr)
}
Expand Down Expand Up @@ -532,9 +542,10 @@ impl<'a> SyscallHintProcessor<'a> {
fn allocate_execution_info_segment(
&mut self,
vm: &mut VirtualMachine,
tx_version_override: TransactionVersion,
) -> SyscallResult<Relocatable> {
let block_info_ptr = self.allocate_block_info_segment(vm)?;
let tx_info_ptr = self.allocate_tx_info_segment(vm)?;
let tx_info_ptr = self.allocate_tx_info_segment(vm, tx_version_override)?;

let additional_info: Vec<MaybeRelocatable> = vec![
block_info_ptr.into(),
Expand Down Expand Up @@ -580,13 +591,17 @@ impl<'a> SyscallHintProcessor<'a> {
Ok((data_segment_start_ptr, data_segment_end_ptr))
}

fn allocate_tx_info_segment(&mut self, vm: &mut VirtualMachine) -> SyscallResult<Relocatable> {
fn allocate_tx_info_segment(
&mut self,
vm: &mut VirtualMachine,
tx_version_override: TransactionVersion,
) -> SyscallResult<Relocatable> {
let tx_info = &self.base.context.tx_context.clone().tx_info;
let (tx_signature_start_ptr, tx_signature_end_ptr) =
&self.allocate_data_segment(vm, &tx_info.signature().0)?;

let mut tx_data: Vec<MaybeRelocatable> = vec![
tx_info.signed_version().0.into(),
tx_version_override.0.into(),
tx_info.sender_address().0.key().into(),
Felt::from(tx_info.max_fee_for_execution_info_syscall().0).into(),
tx_signature_start_ptr.into(),
Expand Down
29 changes: 28 additions & 1 deletion crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
use starknet_api::transaction::EventContent;
use starknet_api::transaction::{
signed_tx_version,
EventContent,
TransactionOptions,
TransactionVersion,
};
use starknet_types_core::felt::Felt;

use super::exceeds_event_size_limit;
Expand Down Expand Up @@ -177,6 +182,28 @@ impl<'state> SyscallHandlerBase<'state> {
Ok(class_hash)
}

/// Returns the transaction version for the `get_execution_info` syscall.
pub fn tx_version_for_get_execution_info(&self) -> TransactionVersion {
let tx_context = &self.context.tx_context;
// The transaction version, ignoring the only_query bit.
let version = tx_context.tx_info.version();
let versioned_constants = &tx_context.block_context.versioned_constants;
// The set of v1-bound-accounts.
let v1_bound_accounts = &versioned_constants.os_constants.v1_bound_accounts_cairo1;
let class_hash = &self.call.class_hash;

// 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()
}
}

pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> {
exceeds_event_size_limit(
self.context.versioned_constants(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ fn test_get_execution_info(
}
};

let mut expected_version = version.0;
// TODO(lior): Uncomment the line below once version-bound accounts are supported.
// let mut expected_version = if v1_bound_account { 1.into() } else { version.0 };
let mut expected_version = if v1_bound_account { 1.into() } else { version.0 };
if only_query {
let simulate_version_base = *QUERY_VERSION_BASE;
let query_version = simulate_version_base + version.0;
Expand Down

0 comments on commit a7ee214

Please sign in to comment.