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 Sep 15, 2024
1 parent 7c12924 commit 7a04c94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
17 changes: 2 additions & 15 deletions crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
use starknet_api::test_utils::invoke::InvokeTxArgs;
use starknet_api::transaction::{InvokeTransactionV0, TransactionHash, TransactionVersion};
use starknet_api::transaction::TransactionHash;

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 default_tx_hash = TransactionHash::default();
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, default_tx_hash),
Expand Down
37 changes: 29 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::OnceLock;

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 @@ -71,15 +74,33 @@ macro_rules! invoke_tx_args {
};
}

static EXECUTE_ENTRY_POINT_SELECTOR: OnceLock<EntryPointSelector> = 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,
Expand Down

0 comments on commit 7a04c94

Please sign in to comment.