Skip to content

Commit

Permalink
test: streamline the v0 version tx for invoke tx towards code dedup i…
Browse files Browse the repository at this point in the history
…n test utils
  • Loading branch information
ArniStarkware committed Nov 4, 2024
1 parent 9818bb1 commit 0e25403
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
17 changes: 1 addition & 16 deletions crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
33 changes: 25 additions & 8 deletions crates/starknet_api/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::calldata;
use crate::core::{ContractAddress, Nonce};
use std::sync::LazyLock;

use crate::core::{ContractAddress, EntryPointSelector, Nonce};
use crate::data_availability::DataAvailabilityMode;
use crate::executable_transaction::InvokeTransaction as ExecutableInvokeTransaction;
use crate::transaction::{
AccountDeploymentData,
Calldata,
Fee,
InvokeTransaction,
InvokeTransactionV0,
InvokeTransactionV1,
InvokeTransactionV3,
PaymasterData,
Expand All @@ -16,6 +18,7 @@ use crate::transaction::{
TransactionVersion,
ValidResourceBounds,
};
use crate::{calldata, felt};

#[derive(Clone)]
pub struct InvokeTxArgs {
Expand Down Expand Up @@ -73,15 +76,29 @@ macro_rules! invoke_tx_args {
};
}

/// 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
/// )
static EXECUTE_ENTRY_POINT_SELECTOR: LazyLock<EntryPointSelector> = LazyLock::new(|| {
const EXECUTE_ENTRY_POINT_NAME_THROUGH_KECCAK: &str =
"0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad";

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,
Expand Down

0 comments on commit 0e25403

Please sign in to comment.