From 8c57193e66886cba50cab68ae6f24dcfc7032bd1 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 9 Sep 2024 10:37:55 +0300 Subject: [PATCH] test: streamline the v0 version tx for invoke tx towards code dedup in test utils --- crates/blockifier/src/test_utils/invoke.rs | 17 +-------- crates/starknet_api/src/test_utils/invoke.rs | 37 +++++++++++++++----- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/crates/blockifier/src/test_utils/invoke.rs b/crates/blockifier/src/test_utils/invoke.rs index 65f032d8a20..e9b11650bb6 100644 --- a/crates/blockifier/src/test_utils/invoke.rs +++ b/crates/blockifier/src/test_utils/invoke.rs @@ -1,26 +1,11 @@ use starknet_api::test_utils::invoke::InvokeTxArgs; -use starknet_api::transaction::{InvokeTransactionV0, TransactionVersion}; -use crate::abi::abi_utils::selector_from_name; -use crate::transaction::constants::EXECUTE_ENTRY_POINT_NAME; use crate::transaction::transactions::InvokeTransaction; pub fn invoke_tx(invoke_args: InvokeTxArgs) -> InvokeTransaction { let tx_hash = invoke_args.tx_hash; let only_query = invoke_args.only_query; - // TODO: Make TransactionVersion an enum and use match here. - let invoke_tx = if invoke_args.version == TransactionVersion::ZERO { - starknet_api::transaction::InvokeTransaction::V0(InvokeTransactionV0 { - max_fee: invoke_args.max_fee, - calldata: invoke_args.calldata, - contract_address: invoke_args.sender_address, - signature: invoke_args.signature, - // V0 transactions should always select the `__execute__` entry point. - entry_point_selector: selector_from_name(EXECUTE_ENTRY_POINT_NAME), - }) - } else { - starknet_api::test_utils::invoke::invoke_tx(invoke_args) - }; + let invoke_tx = starknet_api::test_utils::invoke::invoke_tx(invoke_args); match only_query { true => InvokeTransaction::new_for_query(invoke_tx, tx_hash), diff --git a/crates/starknet_api/src/test_utils/invoke.rs b/crates/starknet_api/src/test_utils/invoke.rs index d32612e868a..fed7a6d1f8c 100644 --- a/crates/starknet_api/src/test_utils/invoke.rs +++ b/crates/starknet_api/src/test_utils/invoke.rs @@ -1,5 +1,6 @@ -use crate::calldata; -use crate::core::{ContractAddress, Nonce}; +use std::sync::OnceLock; + +use crate::core::{ContractAddress, EntryPointSelector, Nonce}; use crate::data_availability::DataAvailabilityMode; use crate::executable_transaction::InvokeTransaction as ExecutableInvokeTransaction; use crate::transaction::{ @@ -7,6 +8,7 @@ use crate::transaction::{ Calldata, Fee, InvokeTransaction, + InvokeTransactionV0, InvokeTransactionV1, InvokeTransactionV3, PaymasterData, @@ -16,6 +18,7 @@ use crate::transaction::{ TransactionVersion, ValidResourceBounds, }; +use crate::{calldata, felt}; #[derive(Clone)] pub struct InvokeTxArgs { @@ -73,15 +76,33 @@ macro_rules! invoke_tx_args { }; } +static EXECUTE_ENTRY_POINT_SELECTOR: OnceLock = OnceLock::new(); + +/// V0 transactions should always select the `__execute__` entry point. +/// The original code from the blockifier crate was: +/// +/// crate::abi::abi_utils::selector_from_name( +/// crate::transaction::constants::EXECUTE_ENTRY_POINT_NAME +/// ) +fn execute_entry_point_selector() -> EntryPointSelector { + // This is the result of hashing the string "__execute__" with the Starknet keccak function. + const EXECUTE_ENTRY_POINT_NAME_THROUGH_KECCAK: &str = + "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad"; + + *EXECUTE_ENTRY_POINT_SELECTOR + .get_or_init(|| EntryPointSelector(felt!(EXECUTE_ENTRY_POINT_NAME_THROUGH_KECCAK))) +} + pub fn invoke_tx(invoke_args: InvokeTxArgs) -> InvokeTransaction { // TODO: Make TransactionVersion an enum and use match here. if invoke_args.version == TransactionVersion::ZERO { - // TODO(Arni): Implement V0. See blockifier test utils for reference. There is an issue with - // the computation of the entry_point_selector. - panic!( - "This test util does not supported creation of transaction version: {:?}.", - invoke_args.version - ); + InvokeTransaction::V0(InvokeTransactionV0 { + max_fee: invoke_args.max_fee, + calldata: invoke_args.calldata, + contract_address: invoke_args.sender_address, + signature: invoke_args.signature, + entry_point_selector: execute_entry_point_selector(), + }) } else if invoke_args.version == TransactionVersion::ONE { InvokeTransaction::V1(InvokeTransactionV1 { max_fee: invoke_args.max_fee,