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

chore(blockifier,starknet_api): move l1handler to executable tx #1723

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
4 changes: 2 additions & 2 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::l1_handler::l1handler_tx;
use crate::test_utils::{
create_calldata,
maybe_dummy_block_hash_and_number,
Expand All @@ -38,7 +39,6 @@ use crate::transaction::test_utils::{
TestInitData,
};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::L1HandlerTransaction;
fn tx_executor_test_body<S: StateReader>(
state: CachedState<S>,
block_context: BlockContext,
Expand Down Expand Up @@ -230,7 +230,7 @@ fn test_l1_handler(block_context: BlockContext) {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let state = test_state(&block_context.chain_info, BALANCE, &[(test_contract, 1)]);

let tx = Transaction::L1Handler(L1HandlerTransaction::create_for_testing(
let tx = Transaction::L1Handler(l1handler_tx(
Fee(1908000000000000),
test_contract.get_instance_address(0),
));
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod deploy_account;
pub mod dict_state_reader;
pub mod initial_test_state;
pub mod invoke;
pub mod l1_handler;
pub mod prices;
pub mod struct_impls;
pub mod syscall;
Expand Down
24 changes: 24 additions & 0 deletions crates/blockifier/src/test_utils/l1_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use starknet_api::calldata;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::{Fee, TransactionHash, TransactionVersion};
use starknet_types_core::felt::Felt;

use crate::abi::abi_utils::selector_from_name;

pub fn l1handler_tx(l1_fee: Fee, contract_address: ContractAddress) -> L1HandlerTransaction {
let calldata = calldata![
Felt::from(0x123), // from_address.
Felt::from(0x876), // key.
Felt::from(0x44) // value.
];
let tx = starknet_api::transaction::L1HandlerTransaction {
version: TransactionVersion::ZERO,
nonce: Nonce::default(),
contract_address,
entry_point_selector: selector_from_name("l1_handler_set_value"),
calldata,
};
let tx_hash = TransactionHash::default();
L1HandlerTransaction { tx, tx_hash, paid_fee_on_l1: l1_fee }
}
27 changes: 2 additions & 25 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
use starknet_api::block::{BlockNumber, BlockTimestamp, NonzeroGasPrice};
use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce};
use starknet_api::contract_address;
use starknet_api::core::{ChainId, ClassHash};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::transaction::{Fee, TransactionHash, TransactionVersion};
use starknet_api::{calldata, contract_address};
use starknet_types_core::felt::Felt;

use super::update_json_value;
use crate::abi::abi_utils::selector_from_name;
use crate::blockifier::block::{BlockInfo, GasPrices};
use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
Expand All @@ -37,7 +34,6 @@ use crate::test_utils::{
TEST_SEQUENCER_ADDRESS,
};
use crate::transaction::objects::{DeprecatedTransactionInfo, TransactionInfo};
use crate::transaction::transactions::L1HandlerTransaction;
use crate::versioned_constants::{
GasCosts,
OsConstants,
Expand Down Expand Up @@ -237,25 +233,6 @@ impl ContractClassV1 {
}
}

impl L1HandlerTransaction {
pub fn create_for_testing(l1_fee: Fee, contract_address: ContractAddress) -> Self {
let calldata = calldata![
Felt::from(0x123), // from_address.
Felt::from(0x876), // key.
Felt::from(0x44) // value.
];
let tx = starknet_api::transaction::L1HandlerTransaction {
version: TransactionVersion::ZERO,
nonce: Nonce::default(),
contract_address,
entry_point_selector: selector_from_name("l1_handler_set_value"),
calldata,
};
let tx_hash = TransactionHash::default();
Self { tx, tx_hash, paid_fee_on_l1: l1_fee }
}
}

impl BouncerWeights {
pub fn create_for_testing(builtin_count: BuiltinCount) -> Self {
Self { builtin_count, ..Self::empty() }
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{calculate_contract_address, ContractAddress, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::{Fee, Transaction as StarknetApiTransaction, TransactionHash};

use crate::bouncer::verify_tx_weights_within_max_capacity;
Expand All @@ -27,7 +28,6 @@ use crate::transaction::transactions::{
ExecutableTransaction,
ExecutionFlags,
InvokeTransaction,
L1HandlerTransaction,
};

// TODO: Move into transaction.rs, makes more sense to be defined there.
Expand Down
15 changes: 1 addition & 14 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
Expand Down Expand Up @@ -557,20 +558,6 @@ impl TransactionInfoCreator for InvokeTransaction {
}
}

#[derive(Clone, Debug)]
pub struct L1HandlerTransaction {
pub tx: starknet_api::transaction::L1HandlerTransaction,
pub tx_hash: TransactionHash,
pub paid_fee_on_l1: Fee,
}

impl L1HandlerTransaction {
pub fn payload_size(&self) -> usize {
// The calldata includes the "from" field, which is not a part of the payload.
self.tx.calldata.0.len() - 1
}
}

impl HasRelatedFeeType for L1HandlerTransaction {
fn version(&self) -> TransactionVersion {
self.tx.version
Expand Down
7 changes: 4 additions & 3 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::invoke::invoke_tx;
use crate::test_utils::l1_handler::l1handler_tx;
use crate::test_utils::prices::Prices;
use crate::test_utils::{
create_calldata,
Expand Down Expand Up @@ -145,7 +146,7 @@ use crate::transaction::test_utils::{
VALID,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{ExecutableTransaction, L1HandlerTransaction};
use crate::transaction::transactions::ExecutableTransaction;
use crate::versioned_constants::VersionedConstants;
use crate::{
check_tx_execution_error_for_custom_hint,
Expand Down Expand Up @@ -2204,7 +2205,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
let block_context = &BlockContext::create_for_account_testing_with_kzg(use_kzg_da);
let contract_address = test_contract.get_instance_address(0);
let versioned_constants = &block_context.versioned_constants;
let tx = L1HandlerTransaction::create_for_testing(Fee(1), contract_address);
let tx = l1handler_tx(Fee(1), contract_address);
let calldata = tx.tx.calldata.clone();
let key = calldata.0[1];
let value = calldata.0[2];
Expand Down Expand Up @@ -2338,7 +2339,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
// TODO(Meshi, 15/6/2024): change the l1_handler_set_value cairo function to
// always uptade the storage instad.
state.set_storage_at(contract_address, StorageKey::try_from(key).unwrap(), Felt::ZERO).unwrap();
let tx_no_fee = L1HandlerTransaction::create_for_testing(Fee(0), contract_address);
let tx_no_fee = l1handler_tx(Fee(0), contract_address);
let error = tx_no_fee.execute(state, block_context, false, true).unwrap_err(); // Do not charge fee as L1Handler's resource bounds (/max fee) is 0.
// Today, we check that the paid_fee is positive, no matter what was the actual fee.
let expected_actual_fee =
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/py_l1_handler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::Arc;

use blockifier::abi::constants;
use blockifier::transaction::transactions::L1HandlerTransaction;
use pyo3::prelude::*;
use starknet_api::core::{ContractAddress, EntryPointSelector, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::{Calldata, Fee, TransactionHash};

use crate::errors::{NativeBlockifierInputError, NativeBlockifierResult};
Expand Down
15 changes: 15 additions & 0 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::transaction::{
AllResourceBounds,
Calldata,
ContractAddressSalt,
Fee,
PaymasterData,
Tip,
TransactionHash,
Expand Down Expand Up @@ -292,3 +293,17 @@ impl InvokeTransaction {
Self::create(invoke_tx, chain_id)
}
}

#[derive(Clone, Debug)]
pub struct L1HandlerTransaction {
pub tx: crate::transaction::L1HandlerTransaction,
pub tx_hash: TransactionHash,
pub paid_fee_on_l1: Fee,
}

impl L1HandlerTransaction {
pub fn payload_size(&self) -> usize {
// The calldata includes the "from" field, which is not a part of the payload.
self.tx.calldata.0.len() - 1
}
}
Loading