Skip to content

Commit

Permalink
refactor(starknet_api): use tx_hash constructor macro
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-starkware committed Nov 28, 2024
1 parent 8533938 commit 5ce2158
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use starknet_api::transaction::{
EventContent,
EventData,
EventKey,
TransactionHash,
TransactionVersion,
QUERY_VERSION_BASE,
};
use starknet_api::{calldata, felt, nonce, storage_key};
use starknet_api::{calldata, felt, nonce, storage_key, tx_hash};
use starknet_types_core::felt::Felt;
use test_case::test_case;

Expand Down Expand Up @@ -467,7 +466,7 @@ fn test_tx_info(#[values(false, true)] only_query: bool) {
let simulate_version_base = *QUERY_VERSION_BASE;
version += simulate_version_base;
}
let tx_hash = TransactionHash(felt!(1991_u16));
let tx_hash = tx_hash!(1991);
let max_fee = Fee(0);
let nonce = nonce!(3_u16);
let sender_address = test_contract.get_instance_address(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use starknet_api::transaction::fields::{
Tip,
ValidResourceBounds,
};
use starknet_api::transaction::{TransactionHash, TransactionVersion, QUERY_VERSION_BASE};
use starknet_api::{felt, nonce};
use starknet_api::transaction::{TransactionVersion, QUERY_VERSION_BASE};
use starknet_api::{felt, nonce, tx_hash};
use starknet_types_core::felt::Felt;
use test_case::test_case;

Expand Down Expand Up @@ -200,7 +200,7 @@ fn test_get_execution_info(
version = TransactionVersion(query_version);
}

let tx_hash = TransactionHash(felt!(1991_u16));
let tx_hash = tx_hash!(1991);
let max_fee = Fee(42);
let nonce = nonce!(3_u16);
let sender_address = test_contract_address;
Expand Down
5 changes: 2 additions & 3 deletions crates/papyrus_protobuf/src/converters/transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ use starknet_api::transaction::{
InvokeTransactionOutput,
L1HandlerTransactionOutput,
Transaction as StarknetApiTransaction,
TransactionHash,
TransactionOutput,
};
use starknet_types_core::felt::Felt;
use starknet_api::tx_hash;

use crate::sync::DataOrFin;

Expand Down Expand Up @@ -167,7 +166,7 @@ fn assert_transaction_to_vec_u8_and_back(
transaction: StarknetApiTransaction,
transaction_output: TransactionOutput,
) {
let random_transaction_hash = TransactionHash(Felt::from(random::<u64>()));
let random_transaction_hash = tx_hash!(random::<u64>());
let data = DataOrFin(Some(FullTransaction {
transaction,
transaction_output,
Expand Down
22 changes: 10 additions & 12 deletions crates/papyrus_rpc/src/v0_8/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ use starknet_api::deprecated_contract_class::{
FunctionAbiEntry,
FunctionStateMutability,
};
use starknet_api::hash::StarkHash;
use starknet_api::state::{SierraContractClass as StarknetApiContractClass, StateDiff};
use starknet_api::transaction::{
Event as StarknetApiEvent,
Expand All @@ -81,7 +80,7 @@ use starknet_api::transaction::{
TransactionOffsetInBlock,
TransactionOutput as StarknetApiTransactionOutput,
};
use starknet_api::{class_hash, contract_address, felt, storage_key};
use starknet_api::{class_hash, contract_address, felt, storage_key, tx_hash};
use starknet_client::reader::objects::pending_data::{
DeprecatedPendingBlock,
PendingBlockOrDeprecated,
Expand Down Expand Up @@ -1350,7 +1349,7 @@ async fn get_transaction_status() {
call_api_then_assert_and_validate_schema_for_err::<_, TransactionStatus>(
&module,
method_name,
vec![Box::new(TransactionHash(StarkHash::from(1_u8)))],
vec![Box::new(tx_hash!(1))],
&VERSION,
SpecFile::StarknetApiOpenrpc,
&TRANSACTION_HASH_NOT_FOUND.into(),
Expand Down Expand Up @@ -1474,7 +1473,7 @@ async fn get_transaction_receipt() {
call_api_then_assert_and_validate_schema_for_err::<_, TransactionReceipt>(
&module,
method_name,
vec![Box::new(TransactionHash(StarkHash::from(1_u8)))],
vec![Box::new(tx_hash!(1))],
&VERSION,
SpecFile::StarknetApiOpenrpc,
&TRANSACTION_HASH_NOT_FOUND.into(),
Expand Down Expand Up @@ -2202,7 +2201,7 @@ async fn get_storage_at() {
fn generate_client_transaction_client_receipt_rpc_transaction_and_rpc_receipt(
rng: &mut ChaCha8Rng,
) -> (ClientTransaction, ClientTransactionReceipt, Transaction, PendingTransactionReceipt) {
let pending_transaction_hash = TransactionHash(StarkHash::from(rng.next_u64()));
let pending_transaction_hash = tx_hash!(rng.next_u64());
let mut client_transaction_receipt = ClientTransactionReceipt::get_test_instance(rng);
client_transaction_receipt.transaction_hash = pending_transaction_hash;
client_transaction_receipt.execution_resources.n_memory_holes = 1;
Expand Down Expand Up @@ -2279,7 +2278,7 @@ async fn get_transaction_by_hash() {
let mut block = get_test_block(1, None, None, None);
// Change the transaction hash from 0 to a random value, so that later on we can add a
// transaction with 0 hash to the pending block.
block.body.transaction_hashes[0] = TransactionHash(StarkHash::from(random::<u64>()));
block.body.transaction_hashes[0] = tx_hash!(random::<u64>());
storage_writer
.begin_rw_txn()
.unwrap()
Expand Down Expand Up @@ -2333,7 +2332,7 @@ async fn get_transaction_by_hash() {
call_api_then_assert_and_validate_schema_for_err::<_, TransactionWithHash>(
&module,
method_name,
vec![Box::new(TransactionHash(StarkHash::from(1_u8)))],
vec![Box::new(tx_hash!(1))],
&VERSION,
SpecFile::StarknetApiOpenrpc,
&TRANSACTION_HASH_NOT_FOUND.into(),
Expand All @@ -2344,7 +2343,7 @@ async fn get_transaction_by_hash() {
#[tokio::test]
async fn get_transaction_by_hash_state_only() {
let method_name = "starknet_V0_8_getTransactionByHash";
let params = [TransactionHash(StarkHash::from(1_u8))];
let params = [tx_hash!(1)];
let (module, _) = get_test_rpc_server_and_storage_writer_from_params::<JsonRpcServerImpl>(
None,
None,
Expand Down Expand Up @@ -2736,7 +2735,7 @@ impl BlockMetadata {
block.header.block_hash = BlockHash(rng.next_u64().into());
// Randomize the transaction hashes because get_test_block returns constant hashes
for transaction_hash in &mut block.body.transaction_hashes {
*transaction_hash = TransactionHash(rng.next_u64().into());
*transaction_hash = tx_hash!(rng.next_u64());
}

for (output, event_metadatas_of_tx) in
Expand All @@ -2761,9 +2760,8 @@ impl BlockMetadata {
rng: &mut ChaCha8Rng,
parent_hash: BlockHash,
) -> PendingBlockOrDeprecated {
let transaction_hashes = iter::repeat_with(|| TransactionHash(rng.next_u64().into()))
.take(self.0.len())
.collect::<Vec<_>>();
let transaction_hashes =
iter::repeat_with(|| tx_hash!(rng.next_u64())).take(self.0.len()).collect::<Vec<_>>();
PendingBlockOrDeprecated::Deprecated(DeprecatedPendingBlock {
parent_block_hash: parent_hash,
transactions: transaction_hashes
Expand Down
20 changes: 10 additions & 10 deletions crates/papyrus_rpc/src/v0_8/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use starknet_api::transaction::{
TransactionOffsetInBlock,
TransactionVersion,
};
use starknet_api::{calldata, class_hash, contract_address, felt, nonce};
use starknet_api::{calldata, class_hash, contract_address, felt, nonce, tx_hash};
use starknet_client::reader::objects::pending_data::{
PendingBlock,
PendingBlockOrDeprecated,
Expand Down Expand Up @@ -302,7 +302,7 @@ async fn execution_call() {
*BLOCK_TIMESTAMP,
*SEQUENCER_ADDRESS,
&InvokeTransactionV1::default(),
TransactionHash(StarkHash::ZERO),
tx_hash!(0),
Some(Felt::ZERO),
);
// Calling the contract directly and not through the account contract.
Expand Down Expand Up @@ -363,7 +363,7 @@ async fn pending_execution_call() {
*BLOCK_TIMESTAMP,
*SEQUENCER_ADDRESS,
&InvokeTransactionV1::default(),
TransactionHash(StarkHash::ZERO),
tx_hash!(0),
Some(Felt::ZERO),
);
// Calling the contract directly and not through the account contract.
Expand Down Expand Up @@ -630,7 +630,7 @@ async fn test_call_simulate(
// Because the transaction hash depends on the calldata and the calldata needs to contain
// the transaction hash, there's no way to put the correct hash here. Instead, we'll check
// that the function `test_get_execution_info` fails on the transaction hash validation.
TransactionHash(StarkHash::ZERO),
tx_hash!(0),
None,
);
invoke_v1.calldata = calldata;
Expand Down Expand Up @@ -763,8 +763,8 @@ async fn trace_block_transactions_regular_and_pending() {

let mut writer = prepare_storage_for_execution(storage_writer);

let tx_hash1 = TransactionHash(felt!("0x1234"));
let tx_hash2 = TransactionHash(felt!("0x5678"));
let tx_hash1 = tx_hash!(0x1234);
let tx_hash2 = tx_hash!(0x5678);

let client_tx1 = ClientTransaction::Invoke(ClientInvokeTransaction {
max_fee: Some(*MAX_FEE),
Expand Down Expand Up @@ -945,8 +945,8 @@ async fn trace_block_transactions_regular_and_pending() {

#[tokio::test]
async fn trace_block_transactions_and_trace_transaction_execution_context() {
let tx_hash1 = TransactionHash(felt!("0x1234"));
let tx_hash2 = TransactionHash(felt!("0x5678"));
let tx_hash1 = tx_hash!(0x1234);
let tx_hash2 = tx_hash!(0x5678);

let mut invoke_tx1 = starknet_api::transaction::InvokeTransactionV1 {
max_fee: *MAX_FEE,
Expand Down Expand Up @@ -1084,8 +1084,8 @@ async fn trace_block_transactions_and_trace_transaction_execution_context() {

#[tokio::test]
async fn pending_trace_block_transactions_and_trace_transaction_execution_context() {
let tx_hash1 = TransactionHash(felt!("0x1234"));
let tx_hash2 = TransactionHash(felt!("0x5678"));
let tx_hash1 = tx_hash!(0x1234);
let tx_hash2 = tx_hash!(0x5678);

let mut client_invoke_tx1 = ClientInvokeTransaction {
max_fee: Some(*MAX_FEE),
Expand Down
8 changes: 4 additions & 4 deletions crates/papyrus_rpc/src/v0_8/write_api_result_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use papyrus_test_utils::{auto_impl_get_test_instance, get_rng, GetTestInstance};
use serde::Serialize;
use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::transaction::TransactionHash;
use starknet_api::{class_hash, felt};
use starknet_api::{class_hash, felt, tx_hash};
use starknet_client::writer::objects::response::{
DeclareResponse,
DeployAccountResponse,
Expand Down Expand Up @@ -54,7 +54,7 @@ fn add_deploy_account_ok_result_fits_rpc() {

#[test]
fn add_invoke_ok_result_from_response() {
let transaction_hash = TransactionHash(felt!("0x12345"));
let transaction_hash = tx_hash!(0x12345);
let ok_result = AddInvokeOkResult::from(InvokeResponse {
code: SuccessfulStarknetErrorCode::default(),
transaction_hash,
Expand All @@ -65,7 +65,7 @@ fn add_invoke_ok_result_from_response() {

#[test]
fn add_declare_ok_result_from_response() {
let transaction_hash = TransactionHash(felt!("0x12345"));
let transaction_hash = tx_hash!(0x12345);
let class_hash = class_hash!("0xabcde");
let ok_result = AddDeclareOkResult::from(DeclareResponse {
code: SuccessfulStarknetErrorCode::default(),
Expand All @@ -78,7 +78,7 @@ fn add_declare_ok_result_from_response() {

#[test]
fn add_deploy_account_ok_result_from_response() {
let transaction_hash = TransactionHash(felt!("0x12345"));
let transaction_hash = tx_hash!(0x12345);
let contract_address = ContractAddress(PatriciaKey::try_from(felt!("0xabcde")).unwrap());
let ok_result = AddDeployAccountOkResult::from(DeployAccountResponse {
code: SuccessfulStarknetErrorCode::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use starknet_api::block::{
GasPricePerToken,
StarknetVersion,
};
use starknet_api::class_hash;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
ClassHash,
Expand Down Expand Up @@ -156,6 +155,7 @@ use starknet_api::transaction::{
TransactionOutput,
TransactionVersion,
};
use starknet_api::{class_hash, tx_hash};
use starknet_types_core::felt::Felt;

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -280,7 +280,7 @@ fn get_rand_test_body_with_events(
while is_v3_transaction(&transaction) {
transaction = Transaction::get_test_instance(rng);
}
transaction_hashes.push(TransactionHash(StarkHash::from(u128::try_from(i).unwrap())));
transaction_hashes.push(tx_hash!(i));
let transaction_output = get_test_transaction_output(&transaction);
transactions.push(transaction);
transaction_outputs.push(transaction_output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ use crate::core::{
TransactionCommitment,
};
use crate::data_availability::L1DataAvailabilityMode;
use crate::felt;
use crate::hash::PoseidonHash;
use crate::transaction::fields::TransactionSignature;
use crate::transaction::TransactionHash;
use crate::{felt, tx_hash};

/// Macro to test if changing any field in the header or commitments
/// results a change in the block hash.
Expand Down Expand Up @@ -95,7 +94,7 @@ fn test_block_hash_regression(
let transactions_data = vec![TransactionHashingData {
transaction_signature: TransactionSignature(vec![Felt::TWO, Felt::THREE]),
transaction_output: get_transaction_output(),
transaction_hash: TransactionHash(Felt::ONE),
transaction_hash: tx_hash!(1),
}];

let state_diff = get_state_diff();
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_api/src/block_hash/event_commitment_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use starknet_types_core::hash::Poseidon;

use super::{calculate_event_commitment, calculate_event_hash, EventLeafElement};
use crate::core::EventCommitment;
use crate::transaction::{Event, EventContent, EventData, EventKey, TransactionHash};
use crate::{contract_address, felt};
use crate::transaction::{Event, EventContent, EventData, EventKey};
use crate::{contract_address, felt, tx_hash};

#[test]
fn test_event_commitment_regression() {
Expand Down Expand Up @@ -39,6 +39,6 @@ fn get_event_leaf_element(seed: u8) -> EventLeafElement {
),
},
},
transaction_hash: TransactionHash(felt!("0x1234")),
transaction_hash: tx_hash!(0x1234),
}
}
10 changes: 3 additions & 7 deletions crates/starknet_api/src/block_hash/receipt_commitment_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ use crate::block_hash::receipt_commitment::{
};
use crate::block_hash::test_utils::{generate_message_to_l1, get_transaction_output};
use crate::core::ReceiptCommitment;
use crate::felt;
use crate::transaction::{
RevertedTransactionExecutionStatus,
TransactionExecutionStatus,
TransactionHash,
};
use crate::transaction::{RevertedTransactionExecutionStatus, TransactionExecutionStatus};
use crate::{felt, tx_hash};

#[test]
fn test_receipt_hash_regression() {
let transaction_receipt = ReceiptElement {
transaction_hash: TransactionHash(Felt::from(1234_u16)),
transaction_hash: tx_hash!(1234),
transaction_output: get_transaction_output(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use crate::block_hash::transaction_commitment::{
calculate_transaction_leaf,
};
use crate::core::TransactionCommitment;
use crate::felt;
use crate::transaction::fields::TransactionSignature;
use crate::transaction::TransactionHash;
use crate::{felt, tx_hash};

#[test]
fn test_transaction_leaf_regression() {
Expand All @@ -22,7 +21,7 @@ fn test_transaction_leaf_regression() {
#[test]
fn test_transaction_leaf_without_signature_regression() {
let transaction_leaf_elements = TransactionLeafElement {
transaction_hash: TransactionHash(Felt::ONE),
transaction_hash: tx_hash!(1),
transaction_signature: TransactionSignature(vec![]),
};
let expected_leaf = felt!("0x579e8877c7755365d5ec1ec7d3a94a457eff5d1f40482bbe9729c064cdead2");
Expand All @@ -45,7 +44,7 @@ fn test_transaction_commitment_regression() {
}

fn get_transaction_leaf_element() -> TransactionLeafElement {
let transaction_hash = TransactionHash(Felt::ONE);
let transaction_hash = tx_hash!(1);
let transaction_signature = TransactionSignature(vec![Felt::TWO, Felt::THREE]);
TransactionLeafElement { transaction_hash, transaction_signature }
}
9 changes: 9 additions & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,15 @@ impl From<Vec<u8>> for TransactionHash {
}
}

/// A utility macro to create a [`TransactionHash`] from an unsigned integer representation.
#[cfg(any(feature = "testing", test))]
#[macro_export]
macro_rules! tx_hash {
($tx_hash:expr) => {
$crate::transaction::TransactionHash($crate::hash::StarkHash::from($tx_hash))
};
}

/// A transaction version.
#[derive(
Debug,
Expand Down
Loading

0 comments on commit 5ce2158

Please sign in to comment.