From 0a0d6ecef84790eb6400e44eb3886beeaaa6b7d6 Mon Sep 17 00:00:00 2001 From: marioiordanov Date: Thu, 4 Apr 2024 18:24:12 +0300 Subject: [PATCH] Refactor Transaction enum (#411) * refactor starknet response variants to be created based on return type not on method * added todo statement to not forget * rename Transaction enum to TransactionWithHash * refactor transaction with hash to be a struct and contain two types: transaction_hash and transaction * remove transaction hash from transaction variants except for l1 handler transaction * clippy + test fixes * refactor l1 transactions * clippy * added deny_unknown_fields * test fixes * chain id returns felt type instead of string * clippy fix * rename enum variants from Version to V --- .../starknet-devnet-core/src/messaging/mod.rs | 10 +- .../src/starknet/add_declare_transaction.rs | 33 ++++-- .../add_deploy_account_transaction.rs | 30 ++--- .../src/starknet/add_invoke_transaction.rs | 19 ++-- .../starknet/add_l1_handler_transaction.rs | 23 ++-- .../starknet-devnet-core/src/starknet/dump.rs | 3 +- .../src/starknet/events.rs | 6 +- .../starknet-devnet-core/src/starknet/mod.rs | 37 +++--- .../starknet-devnet-core/src/transactions.rs | 31 +++-- crates/starknet-devnet-core/src/utils.rs | 13 ++- .../src/api/http/endpoints/postman.rs | 16 +-- .../src/api/http/models.rs | 1 - .../src/api/json_rpc/endpoints.rs | 29 ++--- .../src/api/json_rpc/mod.rs | 21 ++-- .../src/api/json_rpc/spec_reader/mod.rs | 55 +++++---- .../src/rpc/transactions.rs | 106 ++++++------------ .../broadcasted_declare_transaction_v1.rs | 17 --- .../broadcasted_declare_transaction_v2.rs | 23 +--- ...oadcasted_deploy_account_transaction_v1.rs | 22 +--- .../broadcasted_invoke_transaction_v1.rs | 20 +--- .../transactions/declare_transaction_v0v1.rs | 27 ++--- .../transactions/declare_transaction_v2.rs | 18 ++- .../transactions/declare_transaction_v3.rs | 23 +--- .../deploy_account_transaction_v1.rs | 31 +++-- .../deploy_account_transaction_v3.rs | 19 +--- .../rpc/transactions/deploy_transaction.rs | 10 +- .../rpc/transactions/invoke_transaction_v1.rs | 27 ++--- .../rpc/transactions/invoke_transaction_v3.rs | 24 ++-- .../transactions/l1_handler_transaction.rs | 38 ++++--- 29 files changed, 308 insertions(+), 424 deletions(-) diff --git a/crates/starknet-devnet-core/src/messaging/mod.rs b/crates/starknet-devnet-core/src/messaging/mod.rs index 31b92e380..98220c77a 100644 --- a/crates/starknet-devnet-core/src/messaging/mod.rs +++ b/crates/starknet-devnet-core/src/messaging/mod.rs @@ -35,7 +35,7 @@ use std::collections::HashMap; use starknet_rs_core::types::{BlockId, ExecutionResult, Hash256}; use starknet_types::felt::TransactionHash; use starknet_types::rpc::messaging::{MessageToL1, MessageToL2}; -use starknet_types::rpc::transactions::L1HandlerTransaction; +use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; use crate::error::{DevnetResult, Error, MessagingError}; use crate::starknet::Starknet; @@ -225,15 +225,13 @@ impl Starknet { &mut self, messages: &[MessageToL2], ) -> DevnetResult> { - let chain_id = self.chain_id().to_felt(); let mut transactions_hashes = vec![]; for message in messages { - let transaction = - L1HandlerTransaction::try_from_message_to_l2(message.clone())?.with_hash(chain_id); - transactions_hashes.push(transaction.transaction_hash); + let transaction = L1HandlerTransaction::try_from_message_to_l2(message.clone())?; - self.add_l1_handler_transaction(transaction)?; + let transaction_hash = self.add_l1_handler_transaction(transaction)?; + transactions_hashes.push(transaction_hash); } Ok(transactions_hashes) diff --git a/crates/starknet-devnet-core/src/starknet/add_declare_transaction.rs b/crates/starknet-devnet-core/src/starknet/add_declare_transaction.rs index 11717d667..5331f5695 100644 --- a/crates/starknet-devnet-core/src/starknet/add_declare_transaction.rs +++ b/crates/starknet-devnet-core/src/starknet/add_declare_transaction.rs @@ -3,9 +3,11 @@ use starknet_types::felt::{ClassHash, TransactionHash}; use starknet_types::rpc::transactions::broadcasted_declare_transaction_v1::BroadcastedDeclareTransactionV1; use starknet_types::rpc::transactions::broadcasted_declare_transaction_v2::BroadcastedDeclareTransactionV2; use starknet_types::rpc::transactions::broadcasted_declare_transaction_v3::BroadcastedDeclareTransactionV3; +use starknet_types::rpc::transactions::declare_transaction_v0v1::DeclareTransactionV0V1; +use starknet_types::rpc::transactions::declare_transaction_v2::DeclareTransactionV2; use starknet_types::rpc::transactions::declare_transaction_v3::DeclareTransactionV3; use starknet_types::rpc::transactions::{ - BroadcastedDeclareTransaction, DeclareTransaction, Transaction, + BroadcastedDeclareTransaction, DeclareTransaction, Transaction, TransactionWithHash, }; use super::dump::DumpEvent; @@ -26,12 +28,13 @@ pub fn add_declare_transaction_v3( let transaction_hash = blockifier_declare_transaction.tx_hash().0.into(); let class_hash = blockifier_declare_transaction.class_hash().0.into(); - let transaction = - Transaction::Declare(DeclareTransaction::Version3(DeclareTransactionV3::new( - broadcasted_declare_transaction.clone(), + let transaction = TransactionWithHash::new( + transaction_hash, + Transaction::Declare(DeclareTransaction::V3(DeclareTransactionV3::new( + &broadcasted_declare_transaction, class_hash, - transaction_hash, - ))); + ))), + ); let blockifier_execution_result = blockifier::transaction::account_transaction::AccountTransaction::Declare( @@ -65,9 +68,13 @@ pub fn add_declare_transaction_v2( let transaction_hash = blockifier_declare_transaction.tx_hash().0.into(); let class_hash = blockifier_declare_transaction.class_hash().0.into(); - let transaction = Transaction::Declare(DeclareTransaction::Version2( - broadcasted_declare_transaction.create_declare(class_hash, transaction_hash), - )); + let transaction = TransactionWithHash::new( + transaction_hash, + Transaction::Declare(DeclareTransaction::V2(DeclareTransactionV2::new( + &broadcasted_declare_transaction, + class_hash, + ))), + ); let blockifier_execution_result = blockifier::transaction::account_transaction::AccountTransaction::Declare( @@ -100,8 +107,12 @@ pub fn add_declare_transaction_v1( .calculate_transaction_hash(&starknet.config.chain_id.to_felt(), &class_hash)?; let declare_transaction = - broadcasted_declare_transaction.create_declare(class_hash, transaction_hash); - let transaction = Transaction::Declare(DeclareTransaction::Version1(declare_transaction)); + DeclareTransactionV0V1::new(&broadcasted_declare_transaction, class_hash); + + let transaction = TransactionWithHash::new( + transaction_hash, + Transaction::Declare(DeclareTransaction::V1(declare_transaction)), + ); let blockifier_declare_transaction = broadcasted_declare_transaction.create_blockifier_declare(class_hash, transaction_hash)?; diff --git a/crates/starknet-devnet-core/src/starknet/add_deploy_account_transaction.rs b/crates/starknet-devnet-core/src/starknet/add_deploy_account_transaction.rs index c1ca66ba2..d47d92758 100644 --- a/crates/starknet-devnet-core/src/starknet/add_deploy_account_transaction.rs +++ b/crates/starknet-devnet-core/src/starknet/add_deploy_account_transaction.rs @@ -3,9 +3,10 @@ use starknet_types::contract_address::ContractAddress; use starknet_types::felt::TransactionHash; use starknet_types::rpc::transactions::broadcasted_deploy_account_transaction_v1::BroadcastedDeployAccountTransactionV1; use starknet_types::rpc::transactions::broadcasted_deploy_account_transaction_v3::BroadcastedDeployAccountTransactionV3; +use starknet_types::rpc::transactions::deploy_account_transaction_v1::DeployAccountTransactionV1; use starknet_types::rpc::transactions::deploy_account_transaction_v3::DeployAccountTransactionV3; use starknet_types::rpc::transactions::{ - BroadcastedDeployAccountTransaction, DeployAccountTransaction, Transaction, + BroadcastedDeployAccountTransaction, DeployAccountTransaction, Transaction, TransactionWithHash, }; use super::dump::DumpEvent; @@ -32,16 +33,16 @@ pub fn add_deploy_account_transaction_v3( let transaction_hash = blockifier_deploy_account_transaction.tx_hash.0.into(); let address: ContractAddress = blockifier_deploy_account_transaction.contract_address.into(); - let deploy_account_transaction_v3 = DeployAccountTransactionV3::new( - broadcasted_deploy_account_transaction.clone(), - address, + let deploy_account_transaction_v3 = + DeployAccountTransactionV3::new(&broadcasted_deploy_account_transaction, address); + + let transaction = TransactionWithHash::new( transaction_hash, + Transaction::DeployAccount(DeployAccountTransaction::V3(Box::new( + deploy_account_transaction_v3, + ))), ); - let transaction = Transaction::DeployAccount(DeployAccountTransaction::Version3(Box::new( - deploy_account_transaction_v3, - ))); - let blockifier_execution_result = blockifier::transaction::account_transaction::AccountTransaction::DeployAccount( blockifier_deploy_account_transaction, @@ -75,12 +76,15 @@ pub fn add_deploy_account_transaction_v1( let transaction_hash = blockifier_deploy_account_transaction.tx_hash.0.into(); let address: ContractAddress = blockifier_deploy_account_transaction.contract_address.into(); - let deploy_account_transaction_v1 = broadcasted_deploy_account_transaction - .compile_deploy_account_transaction_v1(&transaction_hash, address); + let deploy_account_transaction_v1 = + DeployAccountTransactionV1::new(&broadcasted_deploy_account_transaction, address); - let transaction = Transaction::DeployAccount(DeployAccountTransaction::Version1(Box::new( - deploy_account_transaction_v1, - ))); + let transaction = TransactionWithHash::new( + transaction_hash, + Transaction::DeployAccount(DeployAccountTransaction::V1(Box::new( + deploy_account_transaction_v1, + ))), + ); let blockifier_execution_result = blockifier::transaction::account_transaction::AccountTransaction::DeployAccount( diff --git a/crates/starknet-devnet-core/src/starknet/add_invoke_transaction.rs b/crates/starknet-devnet-core/src/starknet/add_invoke_transaction.rs index 9e61a74ff..56a44d489 100644 --- a/crates/starknet-devnet-core/src/starknet/add_invoke_transaction.rs +++ b/crates/starknet-devnet-core/src/starknet/add_invoke_transaction.rs @@ -2,9 +2,10 @@ use blockifier::transaction::transactions::ExecutableTransaction; use starknet_types::felt::TransactionHash; use starknet_types::rpc::transactions::broadcasted_invoke_transaction_v1::BroadcastedInvokeTransactionV1; use starknet_types::rpc::transactions::broadcasted_invoke_transaction_v3::BroadcastedInvokeTransactionV3; +use starknet_types::rpc::transactions::invoke_transaction_v1::InvokeTransactionV1; use starknet_types::rpc::transactions::invoke_transaction_v3::InvokeTransactionV3; use starknet_types::rpc::transactions::{ - BroadcastedInvokeTransaction, InvokeTransaction, Transaction, + BroadcastedInvokeTransaction, InvokeTransaction, Transaction, TransactionWithHash, }; use super::dump::DumpEvent; @@ -23,9 +24,11 @@ pub fn add_invoke_transaction_v1( .create_blockifier_invoke_transaction(starknet.chain_id().to_felt(), false)?; let transaction_hash = blockifier_invoke_transaction.tx_hash.0.into(); - let invoke_transaction = - broadcasted_invoke_transaction.create_invoke_transaction(transaction_hash); - let transaction = Transaction::Invoke(InvokeTransaction::Version1(invoke_transaction)); + let invoke_transaction = InvokeTransactionV1::new(&broadcasted_invoke_transaction); + let transaction = TransactionWithHash::new( + transaction_hash, + Transaction::Invoke(InvokeTransaction::V1(invoke_transaction)), + ); let blockifier_execution_result = blockifier::transaction::account_transaction::AccountTransaction::Invoke( @@ -60,10 +63,12 @@ pub fn add_invoke_transaction_v3( ) .execute(&mut starknet.state.state, &starknet.block_context, true, true); - let transaction = Transaction::Invoke(InvokeTransaction::Version3(InvokeTransactionV3::new( - broadcasted_invoke_transaction.clone(), + let transaction = TransactionWithHash::new( transaction_hash, - ))); + Transaction::Invoke(InvokeTransaction::V3(InvokeTransactionV3::new( + &broadcasted_invoke_transaction, + ))), + ); starknet.handle_transaction_result(transaction, None, blockifier_execution_result)?; starknet.handle_dump_event(DumpEvent::AddInvokeTransaction( diff --git a/crates/starknet-devnet-core/src/starknet/add_l1_handler_transaction.rs b/crates/starknet-devnet-core/src/starknet/add_l1_handler_transaction.rs index 1d5742d74..66ae9dc69 100644 --- a/crates/starknet-devnet-core/src/starknet/add_l1_handler_transaction.rs +++ b/crates/starknet-devnet-core/src/starknet/add_l1_handler_transaction.rs @@ -1,6 +1,7 @@ use blockifier::transaction::transactions::ExecutableTransaction; use starknet_types::felt::TransactionHash; -use starknet_types::rpc::transactions::{L1HandlerTransaction, Transaction}; +use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; +use starknet_types::rpc::transactions::{Transaction, TransactionWithHash}; use tracing::trace; use super::Starknet; @@ -11,10 +12,10 @@ pub fn add_l1_handler_transaction( starknet: &mut Starknet, transaction: L1HandlerTransaction, ) -> DevnetResult { - let transaction_hash = transaction.transaction_hash; - trace!("Executing L1 handler transaction [{:#064x}]", transaction.transaction_hash); - - let blockifier_transaction = transaction.create_blockifier_transaction()?; + let blockifier_transaction = + transaction.create_blockifier_transaction(starknet.chain_id().to_felt())?; + let transaction_hash = blockifier_transaction.tx_hash.0.into(); + trace!("Executing L1 handler transaction [{:#064x}]", transaction_hash); // Fees are charges on L1 as `L1HandlerTransaction` is not executed by an // account, but directly by the sequencer. @@ -30,7 +31,7 @@ pub fn add_l1_handler_transaction( ); starknet.handle_transaction_result( - Transaction::L1Handler(transaction.clone()), + TransactionWithHash::new(transaction_hash, Transaction::L1Handler(transaction.clone())), None, blockifier_execution_result, )?; @@ -55,7 +56,7 @@ mod tests { use starknet_types::contract_class::{Cairo0ContractClass, ContractClass}; use starknet_types::felt::Felt; use starknet_types::rpc::state::Balance; - use starknet_types::rpc::transactions::L1HandlerTransaction; + use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; use starknet_types::traits::HashProducer; use crate::account::Account; @@ -91,8 +92,9 @@ mod tests { nonce: nonce.into(), paid_fee_on_l1: fee, ..Default::default() - } - .with_hash(ChainId::Testnet.to_felt()); + }; + + let l1_handler_transaction_hash = transaction.compute_hash(ChainId::Testnet.to_felt()); let transaction_hash = Felt::from_prefixed_hex_str( "0x1b24ea8dd9e0cb603043958b27a8569635ea13568883cc155130591b7ffe37a", @@ -100,7 +102,7 @@ mod tests { .unwrap(); assert_eq!(transaction.version, Felt::from(0)); - assert_eq!(transaction.transaction_hash, transaction_hash); + assert_eq!(l1_handler_transaction_hash, transaction_hash); } #[test] @@ -174,7 +176,6 @@ mod tests { paid_fee_on_l1: fee, ..Default::default() } - .with_hash(ChainId::Testnet.to_felt()) } /// Initializes a starknet object with: l1l2 dummy contract that has two functions for diff --git a/crates/starknet-devnet-core/src/starknet/dump.rs b/crates/starknet-devnet-core/src/starknet/dump.rs index 25086592c..78907786a 100644 --- a/crates/starknet-devnet-core/src/starknet/dump.rs +++ b/crates/starknet-devnet-core/src/starknet/dump.rs @@ -3,9 +3,10 @@ use std::io::{Read, Seek, SeekFrom, Write}; use std::path::Path; use serde::{Deserialize, Serialize}; +use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; use starknet_types::rpc::transactions::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, - BroadcastedInvokeTransaction, L1HandlerTransaction, + BroadcastedInvokeTransaction, }; use super::{DumpOn, Starknet}; diff --git a/crates/starknet-devnet-core/src/starknet/events.rs b/crates/starknet-devnet-core/src/starknet/events.rs index 59bdec43f..4ecaeb954 100644 --- a/crates/starknet-devnet-core/src/starknet/events.rs +++ b/crates/starknet-devnet-core/src/starknet/events.rs @@ -130,7 +130,6 @@ mod tests { use starknet_types::contract_address::ContractAddress; use starknet_types::emitted_event::Event; use starknet_types::felt::Felt; - use starknet_types::rpc::transactions::{DeclareTransaction, Transaction}; use super::{check_if_filter_applies_for_event, get_events}; use crate::starknet::events::check_if_filter_applies_for_event_keys; @@ -394,10 +393,9 @@ mod tests { // each transaction should have events count equal to the order of the transaction let mut starknet = Starknet::new(&StarknetConfig::default()).unwrap(); - for idx in 0..5 { - let transaction = - Transaction::Declare(DeclareTransaction::Version1(dummy_declare_transaction_v1())); + let transaction = dummy_declare_transaction_v1(); + for idx in 0..5 { let txn_info = blockifier::transaction::objects::TransactionExecutionInfo { execute_call_info: Some(dummy_call_info(idx + 1)), ..Default::default() diff --git a/crates/starknet-devnet-core/src/starknet/mod.rs b/crates/starknet-devnet-core/src/starknet/mod.rs index 1b43f61a7..4c89ec247 100644 --- a/crates/starknet-devnet-core/src/starknet/mod.rs +++ b/crates/starknet-devnet-core/src/starknet/mod.rs @@ -40,10 +40,11 @@ use starknet_types::rpc::transactions::broadcasted_deploy_account_transaction_v1 use starknet_types::rpc::transactions::broadcasted_deploy_account_transaction_v3::BroadcastedDeployAccountTransactionV3; use starknet_types::rpc::transactions::broadcasted_invoke_transaction_v1::BroadcastedInvokeTransactionV1; use starknet_types::rpc::transactions::broadcasted_invoke_transaction_v3::BroadcastedInvokeTransactionV3; +use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; use starknet_types::rpc::transactions::{ BlockTransactionTrace, BroadcastedTransaction, BroadcastedTransactionCommon, - DeclareTransaction, L1HandlerTransaction, SimulatedTransaction, SimulationFlag, Transaction, - TransactionTrace, TransactionWithReceipt, Transactions, + DeclareTransaction, SimulatedTransaction, SimulationFlag, Transaction, TransactionTrace, + TransactionWithHash, TransactionWithReceipt, Transactions, }; use starknet_types::traits::HashProducer; use tracing::{error, info}; @@ -327,7 +328,7 @@ impl Starknet { /// * `transaction_result` - Result with transaction_execution_info pub(crate) fn handle_transaction_result( &mut self, - transaction: Transaction, + transaction: TransactionWithHash, contract_class: Option, transaction_result: Result< TransactionExecutionInfo, @@ -354,22 +355,22 @@ impl Starknet { // If transaction is not reverted // then save the contract class in the state cache for Declare transactions if !tx_info.is_reverted() { - match &transaction { - Transaction::Declare(DeclareTransaction::Version1(declare_v1)) => { + match &transaction.transaction { + Transaction::Declare(DeclareTransaction::V1(declare_v1)) => { declare_contract_class( &declare_v1.class_hash, contract_class, &mut self.state, )? } - Transaction::Declare(DeclareTransaction::Version2(declare_v2)) => { + Transaction::Declare(DeclareTransaction::V2(declare_v2)) => { declare_contract_class( &declare_v2.class_hash, contract_class, &mut self.state, )? } - Transaction::Declare(DeclareTransaction::Version3(declare_v3)) => { + Transaction::Declare(DeclareTransaction::V3(declare_v3)) => { declare_contract_class( declare_v3.get_class_hash(), contract_class, @@ -426,7 +427,7 @@ impl Starknet { pub(crate) fn handle_accepted_transaction( &mut self, transaction_hash: &TransactionHash, - transaction: &Transaction, + transaction: &TransactionWithHash, tx_info: TransactionExecutionInfo, ) -> DevnetResult<()> { let state_diff = self.state.commit_with_diff()?; @@ -839,7 +840,7 @@ impl Starknet { // starting block is reached in while loop. if last_reached_block_hash == Felt::from(0) && reached_starting_block { self.blocks.last_block_hash = None; - self.state = self.init_state.clone_historic(); // This will be refactored during the genesis block PR + self.state = self.init_state.clone_historic(); // TODO: This will be refactored during the genesis block PR } else if reached_starting_block { let current_block = self.blocks.hash_to_block.get(&last_reached_block_hash).ok_or(Error::NoBlock)?; @@ -899,7 +900,7 @@ impl Starknet { .ok_or(Error::NoTransaction) .map(|transaction| transaction.inner.clone()) }) - .collect::>>()?; + .collect::>>()?; Ok(Block { status: *block.status(), @@ -934,7 +935,8 @@ impl Starknet { common_field.maybe_pending_properties.block_hash = None; common_field.maybe_pending_properties.block_number = None; - transaction_receipts.push(TransactionWithReceipt { receipt, transaction }); + transaction_receipts + .push(TransactionWithReceipt { receipt, transaction: transaction.transaction }); } Ok(Block { @@ -948,7 +950,7 @@ impl Starknet { &self, block_id: &BlockId, index: u64, - ) -> DevnetResult<&Transaction> { + ) -> DevnetResult<&TransactionWithHash> { let block = self.get_block(block_id)?; let transaction_hash = block .get_transactions() @@ -967,7 +969,10 @@ impl Starknet { Ok(block.clone()) } - pub fn get_transaction_by_hash(&self, transaction_hash: Felt) -> DevnetResult<&Transaction> { + pub fn get_transaction_by_hash( + &self, + transaction_hash: Felt, + ) -> DevnetResult<&TransactionWithHash> { self.transactions .get_by_hash(transaction_hash) .map(|starknet_transaction| &starknet_transaction.inner) @@ -1267,7 +1272,7 @@ mod tests { let tx = dummy_declare_transaction_v1(); // add transaction hash to pending block - starknet.blocks.pending_block.add_transaction(tx.transaction_hash); + starknet.blocks.pending_block.add_transaction(*tx.get_transaction_hash()); // pending block has some transactions assert!(!starknet.pending_block().get_transactions().is_empty()); @@ -1283,7 +1288,7 @@ mod tests { starknet.blocks.get_by_hash(starknet.blocks.last_block_hash.unwrap()).unwrap(); assert!(added_block.get_transactions().len() == 1); - assert_eq!(*added_block.get_transactions().first().unwrap(), tx.transaction_hash); + assert_eq!(*added_block.get_transactions().first().unwrap(), *tx.get_transaction_hash()); } #[test] @@ -1536,7 +1541,7 @@ mod tests { let tx = dummy_declare_transaction_v1(); // add transaction hash to pending block - starknet.blocks.pending_block.add_transaction(tx.transaction_hash); + starknet.blocks.pending_block.add_transaction(*tx.get_transaction_hash()); starknet.generate_new_block(StateDiff::default()).unwrap(); diff --git a/crates/starknet-devnet-core/src/transactions.rs b/crates/starknet-devnet-core/src/transactions.rs index af52a3e47..3cb40b550 100644 --- a/crates/starknet-devnet-core/src/transactions.rs +++ b/crates/starknet-devnet-core/src/transactions.rs @@ -15,7 +15,7 @@ use starknet_types::rpc::transaction_receipt::{ }; use starknet_types::rpc::transactions::{ DeclareTransaction, DeployAccountTransaction, InvokeTransaction, Transaction, TransactionTrace, - TransactionType, + TransactionType, TransactionWithHash, }; use crate::constants::UDC_CONTRACT_ADDRESS; @@ -58,7 +58,7 @@ impl HashIdentified for StarknetTransactions { #[allow(unused)] #[derive(Debug, Serialize, Deserialize)] pub struct StarknetTransaction { - pub inner: Transaction, + pub inner: TransactionWithHash, pub(crate) finality_status: TransactionFinalityStatus, pub(crate) execution_result: ExecutionResult, pub(crate) block_hash: Option, @@ -71,7 +71,7 @@ pub struct StarknetTransaction { impl StarknetTransaction { pub fn create_accepted( - transaction: &Transaction, + transaction: &TransactionWithHash, execution_info: TransactionExecutionInfo, trace: TransactionTrace, ) -> Self { @@ -173,11 +173,11 @@ impl StarknetTransaction { // V3 transactions are in STRK(FRI) // Other transactions versions are in ETH(WEI) let fee_amount = FeeAmount { amount: self.execution_info.actual_fee }; - let actual_fee_in_units = match self.inner { + let actual_fee_in_units = match self.inner.transaction { Transaction::L1Handler(_) => FeeInUnits::WEI(fee_amount), - Transaction::Declare(DeclareTransaction::Version3(_)) - | Transaction::DeployAccount(DeployAccountTransaction::Version3(_)) - | Transaction::Invoke(InvokeTransaction::Version3(_)) => FeeInUnits::FRI(fee_amount), + Transaction::Declare(DeclareTransaction::V3(_)) + | Transaction::DeployAccount(DeployAccountTransaction::V3(_)) + | Transaction::Invoke(InvokeTransaction::V3(_)) => FeeInUnits::FRI(fee_amount), _ => FeeInUnits::WEI(fee_amount), }; @@ -192,7 +192,7 @@ impl StarknetTransaction { &self.execution_info, ); - match &self.inner { + match &self.inner.transaction { Transaction::DeployAccount(deploy_account_txn) => { Ok(TransactionReceipt::Deploy(DeployTransactionReceipt { common: common_receipt, @@ -277,15 +277,14 @@ impl StarknetTransaction { mod tests { use blockifier::transaction::objects::TransactionExecutionInfo; use starknet_rs_core::types::{TransactionExecutionStatus, TransactionFinalityStatus}; - use starknet_types::rpc::transactions::{DeclareTransaction, Transaction, TransactionTrace}; - use starknet_types::traits::HashProducer; + use starknet_types::rpc::transactions::{TransactionTrace, TransactionWithHash}; use super::{StarknetTransaction, StarknetTransactions}; use crate::starknet::transaction_trace::create_trace; use crate::traits::HashIdentifiedMut; use crate::utils::test_utils::dummy_declare_transaction_v1; - fn dummy_trace(tx: &Transaction) -> TransactionTrace { + fn dummy_trace(tx: &TransactionWithHash) -> TransactionTrace { create_trace( &mut Default::default(), tx.get_type(), @@ -297,9 +296,7 @@ mod tests { #[test] fn get_transaction_by_hash() { - let declare_transaction = dummy_declare_transaction_v1(); - let hash = declare_transaction.generate_hash().unwrap(); - let tx = Transaction::Declare(DeclareTransaction::Version1(declare_transaction)); + let tx = dummy_declare_transaction_v1(); let trace = dummy_trace(&tx); let sn_tx = StarknetTransaction::create_accepted( @@ -309,11 +306,11 @@ mod tests { ); let mut sn_txs = StarknetTransactions::default(); sn_txs.insert( - &hash, + tx.get_transaction_hash(), StarknetTransaction::create_accepted(&tx, TransactionExecutionInfo::default(), trace), ); - let extracted_tran = sn_txs.get_by_hash_mut(&hash).unwrap(); + let extracted_tran = sn_txs.get_by_hash_mut(tx.get_transaction_hash()).unwrap(); assert_eq!(sn_tx.block_hash, extracted_tran.block_hash); assert_eq!(sn_tx.block_number, extracted_tran.block_number); @@ -324,7 +321,7 @@ mod tests { #[test] fn check_correct_successful_transaction_creation() { - let tx = Transaction::Declare(DeclareTransaction::Version1(dummy_declare_transaction_v1())); + let tx = dummy_declare_transaction_v1(); let trace = dummy_trace(&tx); let sn_tran = StarknetTransaction::create_accepted(&tx, TransactionExecutionInfo::default(), trace); diff --git a/crates/starknet-devnet-core/src/utils.rs b/crates/starknet-devnet-core/src/utils.rs index 66032f569..56a2119f3 100644 --- a/crates/starknet-devnet-core/src/utils.rs +++ b/crates/starknet-devnet-core/src/utils.rs @@ -67,7 +67,8 @@ pub(crate) mod test_utils { use starknet_types::rpc::transactions::broadcasted_declare_transaction_v3::BroadcastedDeclareTransactionV3; use starknet_types::rpc::transactions::declare_transaction_v0v1::DeclareTransactionV0V1; use starknet_types::rpc::transactions::{ - BroadcastedTransactionCommonV3, ResourceBoundsWrapper, + BroadcastedTransactionCommonV3, DeclareTransaction, ResourceBoundsWrapper, Transaction, + TransactionWithHash, }; use starknet_types::traits::HashProducer; @@ -97,7 +98,7 @@ pub(crate) mod test_utils { ContractAddress::new(Felt::from_prefixed_hex_str("0xADD4E55").unwrap()).unwrap() } - pub(crate) fn dummy_declare_transaction_v1() -> DeclareTransactionV0V1 { + pub(crate) fn dummy_declare_transaction_v1() -> TransactionWithHash { let chain_id = DEVNET_DEFAULT_CHAIN_ID.to_felt(); let contract_class = dummy_cairo_0_contract_class(); let broadcasted_tx = BroadcastedDeclareTransactionV1::new( @@ -112,7 +113,13 @@ pub(crate) mod test_utils { let transaction_hash = broadcasted_tx.calculate_transaction_hash(&chain_id, &class_hash).unwrap(); - broadcasted_tx.create_declare(class_hash, transaction_hash) + TransactionWithHash::new( + transaction_hash, + Transaction::Declare(DeclareTransaction::V1(DeclareTransactionV0V1::new( + &broadcasted_tx, + class_hash, + ))), + ) } pub(crate) fn dummy_broadcasted_declare_transaction_v2( diff --git a/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs b/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs index 6ebb6a19c..318b70d92 100644 --- a/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs +++ b/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs @@ -1,6 +1,6 @@ use axum::{Extension, Json}; use starknet_types::rpc::messaging::{MessageToL1, MessageToL2}; -use starknet_types::rpc::transactions::L1HandlerTransaction; +use starknet_types::rpc::transactions::l1_handler_transaction::L1HandlerTransaction; use crate::api::http::error::HttpApiError; use crate::api::http::models::{ @@ -87,17 +87,13 @@ pub async fn postman_send_message_to_l2( ) -> HttpApiResult> { let mut starknet = state.api.starknet.write().await; - let chain_id = starknet.chain_id().to_felt(); - - let transaction = L1HandlerTransaction::try_from_message_to_l2(message) - .map_err(|_| HttpApiError::InvalidValueError { + let transaction = L1HandlerTransaction::try_from_message_to_l2(message).map_err(|_| { + HttpApiError::InvalidValueError { msg: "The `paid_fee_on_l1` is out of range, expecting u128 value".to_string(), - })? - .with_hash(chain_id); - - let transaction_hash = transaction.transaction_hash; + } + })?; - starknet + let transaction_hash = starknet .add_l1_handler_transaction(transaction) .map_err(|e| HttpApiError::MessagingError { msg: e.to_string() })?; diff --git a/crates/starknet-devnet-server/src/api/http/models.rs b/crates/starknet-devnet-server/src/api/http/models.rs index fac412b83..974f03d0d 100644 --- a/crates/starknet-devnet-server/src/api/http/models.rs +++ b/crates/starknet-devnet-server/src/api/http/models.rs @@ -6,7 +6,6 @@ use starknet_types::num_bigint::BigUint; use starknet_types::rpc::eth_address::EthAddressWrapper; use starknet_types::rpc::messaging::{MessageToL1, MessageToL2}; use starknet_types::rpc::transaction_receipt::FeeUnit; -use starknet_types::rpc::transactions::L1HandlerTransaction; use starknet_types::serde_helpers::dec_string::deserialize_biguint; use crate::api::http::error::HttpApiError; diff --git a/crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs b/crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs index 8ea86be13..d20ef5575 100644 --- a/crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs +++ b/crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs @@ -8,7 +8,6 @@ use starknet_types::rpc::state::StateUpdate; use starknet_types::rpc::transactions::{ BroadcastedTransaction, EventFilter, EventsChunk, FunctionCall, SimulationFlag, }; -use starknet_types::traits::ToHexString; use super::error::{ApiError, StrictRpcResult}; use super::models::{BlockHashAndNumberOutput, SyncingOutput, TransactionStatusOutput}; @@ -20,7 +19,7 @@ const DEFAULT_CONTINUATION_TOKEN: &str = "0"; impl JsonRpcHandler { /// starknet_specVersion pub fn spec_version(&self) -> StrictRpcResult { - Ok(StarknetResponse::SpecVersion(RPC_SPEC_VERSION.to_string())) + Ok(StarknetResponse::String(RPC_SPEC_VERSION.to_string())) } /// starknet_getBlockWithTxHashes @@ -33,7 +32,7 @@ impl JsonRpcHandler { }, )?; - Ok(StarknetResponse::BlockWithTransactionHashes(Block { + Ok(StarknetResponse::Block(Block { status: *block.status(), header: BlockHeader::from(&block), transactions: starknet_types::rpc::transactions::Transactions::Hashes( @@ -53,7 +52,7 @@ impl JsonRpcHandler { }, )?; - Ok(StarknetResponse::BlockWithFullTransactions(block)) + Ok(StarknetResponse::Block(block)) } /// starknet_getBlockWithReceipts @@ -67,7 +66,7 @@ impl JsonRpcHandler { }, )?; - Ok(StarknetResponse::BlockWithReceipts(block)) + Ok(StarknetResponse::Block(block)) } /// starknet_getStateUpdate @@ -112,7 +111,7 @@ impl JsonRpcHandler { unknown_error => ApiError::StarknetDevnetError(unknown_error), })?; - Ok(StarknetResponse::StorageAt(felt)) + Ok(StarknetResponse::Felt(felt)) } /// starknet_getTransactionByHash @@ -121,7 +120,7 @@ impl JsonRpcHandler { transaction_hash: TransactionHash, ) -> StrictRpcResult { match self.api.starknet.read().await.get_transaction_by_hash(transaction_hash) { - Ok(transaction) => Ok(StarknetResponse::TransactionByHash(transaction.clone())), + Ok(transaction) => Ok(StarknetResponse::Transaction(transaction.clone())), Err(Error::NoTransaction) => Err(ApiError::TransactionNotFound), Err(err) => Err(err.into()), } @@ -163,9 +162,7 @@ impl JsonRpcHandler { .await .get_transaction_by_block_id_and_index(block_id.as_ref(), index) { - Ok(transaction) => { - Ok(StarknetResponse::TransactionByBlockAndIndex(transaction.clone())) - } + Ok(transaction) => Ok(StarknetResponse::Transaction(transaction.clone())), Err(Error::InvalidTransactionIndexInBlock) => { Err(ApiError::InvalidTransactionIndexInBlock) } @@ -191,7 +188,7 @@ impl JsonRpcHandler { /// starknet_getClass pub async fn get_class(&self, block_id: BlockId, class_hash: ClassHash) -> StrictRpcResult { match self.api.starknet.write().await.get_class(block_id.as_ref(), class_hash) { - Ok(contract_class) => Ok(StarknetResponse::ClassByHash(contract_class.try_into()?)), + Ok(contract_class) => Ok(StarknetResponse::ContractClass(contract_class.try_into()?)), Err(e) => Err(match e { Error::NoBlock => ApiError::BlockNotFound, Error::StateError(_) => ApiError::ClassHashNotFound, @@ -208,9 +205,7 @@ impl JsonRpcHandler { contract_address: ContractAddress, ) -> StrictRpcResult { match self.api.starknet.write().await.get_class_at(block_id.as_ref(), contract_address) { - Ok(contract_class) => { - Ok(StarknetResponse::ClassAtContractAddress(contract_class.try_into()?)) - } + Ok(contract_class) => Ok(StarknetResponse::ContractClass(contract_class.try_into()?)), Err(Error::NoBlock) => Err(ApiError::BlockNotFound), Err(Error::ContractNotFound | Error::StateError(_)) => Err(ApiError::ContractNotFound), Err(e @ Error::NoStateAtBlock { .. }) => { @@ -228,7 +223,7 @@ impl JsonRpcHandler { ) -> StrictRpcResult { match self.api.starknet.write().await.get_class_hash_at(block_id.as_ref(), contract_address) { - Ok(class_hash) => Ok(StarknetResponse::ClassHashAtContractAddress(class_hash)), + Ok(class_hash) => Ok(StarknetResponse::Felt(class_hash)), Err(Error::NoBlock) => Err(ApiError::BlockNotFound), Err(Error::ContractNotFound) => Err(ApiError::ContractNotFound), Err(e @ Error::NoStateAtBlock { .. }) => { @@ -329,7 +324,7 @@ impl JsonRpcHandler { pub async fn chain_id(&self) -> StrictRpcResult { let chain_id = self.api.starknet.read().await.chain_id(); - Ok(StarknetResponse::ChainId(chain_id.to_felt().to_prefixed_hex_str())) + Ok(StarknetResponse::Felt(chain_id.to_felt())) } /// starknet_syncing @@ -381,7 +376,7 @@ impl JsonRpcHandler { unknown_error => ApiError::StarknetDevnetError(unknown_error), })?; - Ok(StarknetResponse::ContractNonce(nonce)) + Ok(StarknetResponse::Felt(nonce)) } /// starknet_simulateTransactions diff --git a/crates/starknet-devnet-server/src/api/json_rpc/mod.rs b/crates/starknet-devnet-server/src/api/json_rpc/mod.rs index 52b351579..66b0699eb 100644 --- a/crates/starknet-devnet-server/src/api/json_rpc/mod.rs +++ b/crates/starknet-devnet-server/src/api/json_rpc/mod.rs @@ -14,7 +14,7 @@ use models::{ }; use serde::{Deserialize, Serialize}; use starknet_rs_core::types::ContractClass as CodegenContractClass; -use starknet_types::felt::{ClassHash, Felt}; +use starknet_types::felt::Felt; use starknet_types::rpc::block::Block; use starknet_types::rpc::estimate_message_fee::{ EstimateMessageFeeRequestWrapper, FeeEstimateWrapper, @@ -22,7 +22,7 @@ use starknet_types::rpc::estimate_message_fee::{ use starknet_types::rpc::state::StateUpdate; use starknet_types::rpc::transaction_receipt::TransactionReceipt; use starknet_types::rpc::transactions::{ - BlockTransactionTrace, EventsChunk, SimulatedTransaction, Transaction, TransactionTrace, + BlockTransactionTrace, EventsChunk, SimulatedTransaction, TransactionTrace, TransactionWithHash, }; use starknet_types::starknet_api::block::BlockNumber; use tracing::{error, info, trace}; @@ -351,33 +351,26 @@ impl std::fmt::Display for StarknetRequest { #[cfg_attr(test, derive(Deserialize))] #[serde(untagged)] pub enum StarknetResponse { - BlockWithTransactionHashes(Block), - BlockWithFullTransactions(Block), - BlockWithReceipts(Block), + Block(Block), StateUpdate(StateUpdate), - StorageAt(Felt), - TransactionByHash(Transaction), - TransactionByBlockAndIndex(Transaction), + Felt(Felt), + Transaction(TransactionWithHash), TransactionReceiptByTransactionHash(Box), TransactionStatusByHash(TransactionStatusOutput), - ClassByHash(CodegenContractClass), - ClassHashAtContractAddress(ClassHash), - ClassAtContractAddress(CodegenContractClass), + ContractClass(CodegenContractClass), BlockTransactionCount(u64), Call(Vec), EstimateFee(Vec), BlockNumber(BlockNumber), BlockHashAndNumber(BlockHashAndNumberOutput), - ChainId(String), + String(String), Syncing(SyncingOutput), Events(EventsChunk), - ContractNonce(Felt), AddDeclareTransaction(DeclareTransactionOutput), AddDeployAccountTransaction(DeployAccountTransactionOutput), AddInvokeTransaction(InvokeTransactionOutput), EstimateMessageFee(FeeEstimateWrapper), SimulateTransactions(Vec), - SpecVersion(String), TraceTransaction(TransactionTrace), BlockTransactionTraces(Vec), } diff --git a/crates/starknet-devnet-server/src/api/json_rpc/spec_reader/mod.rs b/crates/starknet-devnet-server/src/api/json_rpc/spec_reader/mod.rs index e8f935ab5..b4fc3e7ea 100644 --- a/crates/starknet-devnet-server/src/api/json_rpc/spec_reader/mod.rs +++ b/crates/starknet-devnet-server/src/api/json_rpc/spec_reader/mod.rs @@ -203,9 +203,7 @@ mod tests { for _ in 0..1000 { for spec in specs.iter() { // Iterate over the methods in the spec - for method in - spec.methods.iter().filter(|m| m.name != "starknet_getBlockWithReceipts") - { + for method in spec.methods.iter() { // Create a JSON-RPC request for each method let request = generate_json_rpc_request(method, &combined_schema) .expect("Could not generate the JSON-RPC request"); @@ -245,21 +243,20 @@ mod tests { StarknetResponse::TransactionReceiptByTransactionHash(_) )); } - StarknetRequest::BlockWithTransactionHashes(_) => { - assert!(matches!( - sn_response, - StarknetResponse::BlockWithTransactionHashes(_) - )); + StarknetRequest::BlockWithTransactionHashes(_) + | StarknetRequest::BlockWithFullTransactions(_) + | StarknetRequest::BlockWithReceipts(_) => { + assert!(matches!(sn_response, StarknetResponse::Block(_))); } StarknetRequest::BlockHashAndNumber => { assert!(matches!(sn_response, StarknetResponse::BlockHashAndNumber(_))); } - StarknetRequest::BlockNumber - | StarknetRequest::BlockTransactionCount(_) => { + StarknetRequest::BlockTransactionCount(_) + | StarknetRequest::BlockNumber => { assert!(matches!( sn_response, - StarknetResponse::BlockNumber(_) - | StarknetResponse::BlockTransactionCount(_) + StarknetResponse::BlockTransactionCount(_) + | StarknetResponse::BlockNumber(_) )); } StarknetRequest::Call(_) => { @@ -267,11 +264,7 @@ mod tests { } StarknetRequest::ClassAtContractAddress(_) | StarknetRequest::ClassByHash(_) => { - assert!(matches!( - sn_response, - StarknetResponse::ClassAtContractAddress(_) - | StarknetResponse::ClassByHash(_) - )); + assert!(matches!(sn_response, StarknetResponse::ContractClass(_))); } StarknetRequest::EsimateFee(_) => { assert!(matches!(sn_response, StarknetResponse::EstimateFee(_))); @@ -318,13 +311,27 @@ mod tests { StarknetResponse::AddInvokeTransaction(_) )); } - _ => { - // Remaining responses are not implemented, because - // multiple requests return the same response format either u64, Felt, - // etc. so its impossible to know which - // response variant is generated based on - // serde untagged deserialization. This is due to the fact that the - // first variant which complies with the response format is returned + StarknetRequest::SpecVersion => { + assert!(matches!(sn_response, StarknetResponse::String(_))); + } + StarknetRequest::TransactionByHash(_) + | StarknetRequest::TransactionByBlockAndIndex(_) => { + assert!(matches!(sn_response, StarknetResponse::Transaction(_))); + } + StarknetRequest::ContractNonce(_) + | StarknetRequest::ChainId + | StarknetRequest::ClassHashAtContractAddress(_) + | StarknetRequest::StorageAt(_) => { + assert!(matches!(sn_response, StarknetResponse::Felt(_))); + } + StarknetRequest::TraceTransaction(_) => { + assert!(matches!(sn_response, StarknetResponse::TraceTransaction(_))); + } + StarknetRequest::BlockTransactionTraces(_) => { + assert!(matches!( + sn_response, + StarknetResponse::BlockTransactionTraces(_) + )); } } } diff --git a/crates/starknet-devnet-types/src/rpc/transactions.rs b/crates/starknet-devnet-types/src/rpc/transactions.rs index e3c5c2701..666734faa 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions.rs @@ -29,6 +29,7 @@ use self::declare_transaction_v3::DeclareTransactionV3; use self::deploy_account_transaction_v1::DeployAccountTransactionV1; use self::deploy_account_transaction_v3::DeployAccountTransactionV3; use self::invoke_transaction_v3::InvokeTransactionV3; +use self::l1_handler_transaction::L1HandlerTransaction; use super::estimate_message_fee::FeeEstimateWrapper; use super::messaging::{MessageToL1, OrderedMessageToL1}; use super::state::ThinStateDiff; @@ -71,7 +72,7 @@ pub mod l1_handler_transaction; #[serde(untagged)] pub enum Transactions { Hashes(Vec), - Full(Vec), + Full(Vec), FullWithReceipts(Vec), } @@ -91,7 +92,7 @@ pub enum TransactionType { } #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -#[serde(tag = "type")] +#[serde(tag = "type", deny_unknown_fields)] pub enum Transaction { #[serde(rename = "DECLARE")] Declare(DeclareTransaction), @@ -105,15 +106,24 @@ pub enum Transaction { L1Handler(L1HandlerTransaction), } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct TransactionWithReceipt { - pub receipt: TransactionReceipt, +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +pub struct TransactionWithHash { + transaction_hash: TransactionHash, + #[serde(flatten)] pub transaction: Transaction, } -impl Transaction { +impl TransactionWithHash { + pub fn new(transaction_hash: TransactionHash, transaction: Transaction) -> Self { + Self { transaction_hash, transaction } + } + + pub fn get_transaction_hash(&self) -> &TransactionHash { + &self.transaction_hash + } + pub fn get_type(&self) -> TransactionType { - match self { + match self.transaction { Transaction::Declare(_) => TransactionType::Declare, Transaction::DeployAccount(_) => TransactionType::DeployAccount, Transaction::Deploy(_) => TransactionType::Deploy, @@ -122,16 +132,6 @@ impl Transaction { } } - pub fn get_transaction_hash(&self) -> &TransactionHash { - match self { - Transaction::Declare(tx) => tx.get_transaction_hash(), - Transaction::L1Handler(tx) => tx.get_transaction_hash(), - Transaction::DeployAccount(tx) => tx.get_transaction_hash(), - Transaction::Invoke(tx) => tx.get_transaction_hash(), - Transaction::Deploy(tx) => tx.get_transaction_hash(), - } - } - #[allow(clippy::too_many_arguments)] pub fn create_common_receipt( &self, @@ -163,59 +163,40 @@ impl Transaction { } } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct TransactionWithReceipt { + pub receipt: TransactionReceipt, + pub transaction: Transaction, +} + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[serde(untagged)] pub enum DeclareTransaction { - Version1(DeclareTransactionV0V1), - Version2(DeclareTransactionV2), - Version3(DeclareTransactionV3), -} - -impl DeclareTransaction { - pub fn get_transaction_hash(&self) -> &TransactionHash { - match self { - DeclareTransaction::Version1(tx) => tx.get_transaction_hash(), - DeclareTransaction::Version2(tx) => tx.get_transaction_hash(), - DeclareTransaction::Version3(tx) => tx.get_transaction_hash(), - } - } + V1(DeclareTransactionV0V1), + V2(DeclareTransactionV2), + V3(DeclareTransactionV3), } #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(untagged)] pub enum InvokeTransaction { - Version1(InvokeTransactionV1), - Version3(InvokeTransactionV3), -} - -impl InvokeTransaction { - pub fn get_transaction_hash(&self) -> &TransactionHash { - match self { - InvokeTransaction::Version1(tx) => tx.get_transaction_hash(), - InvokeTransaction::Version3(tx) => tx.get_transaction_hash(), - } - } + V1(InvokeTransactionV1), + V3(InvokeTransactionV3), } #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(untagged)] pub enum DeployAccountTransaction { - Version1(Box), - Version3(Box), + V1(Box), + V3(Box), } impl DeployAccountTransaction { pub fn get_contract_address(&self) -> &ContractAddress { match self { - DeployAccountTransaction::Version1(tx) => &tx.contract_address, - DeployAccountTransaction::Version3(tx) => tx.get_contract_address(), - } - } - - pub fn get_transaction_hash(&self) -> &TransactionHash { - match self { - DeployAccountTransaction::Version1(tx) => tx.get_transaction_hash(), - DeployAccountTransaction::Version3(tx) => tx.get_transaction_hash(), + DeployAccountTransaction::V1(tx) => tx.get_contract_address(), + DeployAccountTransaction::V3(tx) => tx.get_contract_address(), } } } @@ -239,27 +220,6 @@ where s.serialize_str(&format!("{paid_fee_on_l1:#x}")) } -#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] -pub struct L1HandlerTransaction { - pub transaction_hash: TransactionHash, - pub version: TransactionVersion, - pub nonce: Nonce, - pub contract_address: ContractAddress, - pub entry_point_selector: EntryPointSelector, - pub calldata: Calldata, - #[serde( - serialize_with = "serialize_paid_fee_on_l1", - deserialize_with = "deserialize_paid_fee_on_l1" - )] - pub paid_fee_on_l1: u128, -} - -impl L1HandlerTransaction { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } -} - #[derive(Debug, Clone, Deserialize, Serialize)] pub struct EventFilter { pub from_block: Option, diff --git a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v1.rs index 19b03ca17..d90a5befb 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v1.rs @@ -19,7 +19,6 @@ use crate::error::DevnetResult; use crate::felt::{ ClassHash, Felt, Nonce, TransactionHash, TransactionSignature, TransactionVersion, }; -use crate::rpc::transactions::declare_transaction_v0v1::DeclareTransactionV0V1; use crate::rpc::transactions::BroadcastedTransactionCommon; use crate::traits::HashProducer; @@ -80,22 +79,6 @@ impl BroadcastedDeclareTransactionV1 { )?) } - pub fn create_declare( - &self, - class_hash: ClassHash, - transaction_hash: TransactionHash, - ) -> DeclareTransactionV0V1 { - DeclareTransactionV0V1 { - class_hash, - sender_address: self.sender_address, - nonce: self.common.nonce, - max_fee: self.common.max_fee, - version: self.common.version, - transaction_hash, - signature: self.common.signature.clone(), - } - } - pub fn generate_class_hash(&self) -> DevnetResult { self.contract_class.generate_hash() } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v2.rs b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v2.rs index 8ce6d31ee..71aa11a8c 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v2.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_declare_transaction_v2.rs @@ -9,11 +9,7 @@ use super::broadcasted_declare_transaction_v1::PREFIX_DECLARE; use crate::contract_address::ContractAddress; use crate::contract_class::{compute_sierra_class_hash, ContractClass}; use crate::error::DevnetResult; -use crate::felt::{ - ClassHash, CompiledClassHash, Felt, Nonce, TransactionHash, TransactionSignature, - TransactionVersion, -}; -use crate::rpc::transactions::declare_transaction_v2::DeclareTransactionV2; +use crate::felt::{CompiledClassHash, Felt, Nonce, TransactionSignature, TransactionVersion}; use crate::rpc::transactions::BroadcastedTransactionCommon; use crate::serde_helpers::rpc_sierra_contract_class_to_sierra_contract_class::deserialize_to_sierra_contract_class; @@ -51,23 +47,6 @@ impl BroadcastedDeclareTransactionV2 { } } - pub fn create_declare( - &self, - class_hash: ClassHash, - transaction_hash: TransactionHash, - ) -> DeclareTransactionV2 { - DeclareTransactionV2 { - class_hash, - compiled_class_hash: self.compiled_class_hash, - sender_address: self.sender_address, - nonce: self.common.nonce, - max_fee: self.common.max_fee, - version: self.common.version, - transaction_hash, - signature: self.common.signature.clone(), - } - } - pub fn create_blockifier_declare(&self, chain_id: Felt) -> DevnetResult { let sierra_class_hash: Felt = compute_sierra_class_hash(&self.contract_class)?; diff --git a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_deploy_account_transaction_v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_deploy_account_transaction_v1.rs index 8ba056585..599e57af7 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_deploy_account_transaction_v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_deploy_account_transaction_v1.rs @@ -6,12 +6,10 @@ use starknet_api::transaction::Fee; use starknet_rs_core::crypto::compute_hash_on_elements; use starknet_rs_ff::FieldElement; -use super::deploy_account_transaction_v1::DeployAccountTransactionV1; use crate::contract_address::ContractAddress; use crate::error::DevnetResult; use crate::felt::{ - Calldata, ClassHash, ContractAddressSalt, Felt, Nonce, TransactionHash, TransactionSignature, - TransactionVersion, + Calldata, ClassHash, ContractAddressSalt, Felt, Nonce, TransactionSignature, TransactionVersion, }; use crate::rpc::transactions::BroadcastedTransactionCommon; @@ -110,24 +108,6 @@ impl BroadcastedDeployAccountTransactionV1 { only_query, }) } - - pub fn compile_deploy_account_transaction_v1( - &self, - transaction_hash: &TransactionHash, - contract_address: ContractAddress, - ) -> DeployAccountTransactionV1 { - DeployAccountTransactionV1 { - transaction_hash: *transaction_hash, - max_fee: self.common.max_fee, - version: self.common.version, - signature: self.common.signature.clone(), - nonce: self.common.nonce, - class_hash: self.class_hash, - contract_address_salt: self.contract_address_salt, - constructor_calldata: self.constructor_calldata.clone(), - contract_address, - } - } } #[cfg(test)] diff --git a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_invoke_transaction_v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_invoke_transaction_v1.rs index 7bb7fa288..65242a60e 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_invoke_transaction_v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/broadcasted_invoke_transaction_v1.rs @@ -9,10 +9,7 @@ use starknet_rs_ff::FieldElement; use crate::contract_address::ContractAddress; use crate::error::DevnetResult; -use crate::felt::{ - Calldata, Felt, Nonce, TransactionHash, TransactionSignature, TransactionVersion, -}; -use crate::rpc::transactions::invoke_transaction_v1::InvokeTransactionV1; +use crate::felt::{Calldata, Felt, Nonce, TransactionSignature, TransactionVersion}; use crate::rpc::transactions::BroadcastedTransactionCommon; /// Cairo string for "invoke" from starknet-rs @@ -94,21 +91,6 @@ impl BroadcastedInvokeTransactionV1 { only_query, }) } - - pub fn create_invoke_transaction( - &self, - transaction_hash: TransactionHash, - ) -> InvokeTransactionV1 { - InvokeTransactionV1 { - transaction_hash, - max_fee: self.common.max_fee, - version: self.common.version, - signature: self.common.signature.clone(), - nonce: self.common.nonce, - sender_address: self.sender_address, - calldata: self.calldata.clone(), - } - } } #[cfg(test)] diff --git a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v0v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v0v1.rs index 7131d94fe..e46f807c1 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v0v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v0v1.rs @@ -1,13 +1,9 @@ use serde::{Deserialize, Serialize}; use starknet_api::transaction::Fee; +use super::broadcasted_declare_transaction_v1::BroadcastedDeclareTransactionV1; use crate::contract_address::ContractAddress; -use crate::error::{DevnetResult, Error}; -use crate::felt::{ - ClassHash, Felt, Nonce, TransactionHash, TransactionSignature, TransactionVersion, -}; -use crate::traits::HashProducer; - +use crate::felt::{ClassHash, Nonce, TransactionSignature, TransactionVersion}; #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct DeclareTransactionV0V1 { @@ -16,19 +12,18 @@ pub struct DeclareTransactionV0V1 { pub nonce: Nonce, pub max_fee: Fee, pub version: TransactionVersion, - pub transaction_hash: TransactionHash, pub signature: TransactionSignature, } impl DeclareTransactionV0V1 { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } -} - -impl HashProducer for DeclareTransactionV0V1 { - type Error = Error; - fn generate_hash(&self) -> DevnetResult { - Ok(self.transaction_hash) + pub fn new(broadcasted_txn: &BroadcastedDeclareTransactionV1, class_hash: ClassHash) -> Self { + Self { + class_hash, + sender_address: broadcasted_txn.sender_address, + nonce: broadcasted_txn.common.nonce, + max_fee: broadcasted_txn.common.max_fee, + version: broadcasted_txn.common.version, + signature: broadcasted_txn.common.signature.clone(), + } } } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v2.rs b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v2.rs index eacd6a5cc..a62b712ed 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v2.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v2.rs @@ -1,10 +1,9 @@ use serde::{Deserialize, Serialize}; use starknet_api::transaction::Fee; +use super::broadcasted_declare_transaction_v2::BroadcastedDeclareTransactionV2; use crate::contract_address::ContractAddress; -use crate::felt::{ - ClassHash, CompiledClassHash, Nonce, TransactionHash, TransactionSignature, TransactionVersion, -}; +use crate::felt::{ClassHash, CompiledClassHash, Nonce, TransactionSignature, TransactionVersion}; #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(deny_unknown_fields)] @@ -15,12 +14,19 @@ pub struct DeclareTransactionV2 { pub nonce: Nonce, pub max_fee: Fee, pub version: TransactionVersion, - pub transaction_hash: TransactionHash, pub signature: TransactionSignature, } impl DeclareTransactionV2 { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash + pub fn new(broadcasted_txn: &BroadcastedDeclareTransactionV2, class_hash: ClassHash) -> Self { + Self { + class_hash, + compiled_class_hash: broadcasted_txn.compiled_class_hash, + sender_address: broadcasted_txn.sender_address, + nonce: broadcasted_txn.common.nonce, + max_fee: broadcasted_txn.common.max_fee, + version: broadcasted_txn.common.version, + signature: broadcasted_txn.common.signature.clone(), + } } } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v3.rs b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v3.rs index 0fa4ca02c..d8fc41280 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v3.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/declare_transaction_v3.rs @@ -6,8 +6,7 @@ use super::broadcasted_declare_transaction_v3::BroadcastedDeclareTransactionV3; use super::ResourceBoundsWrapper; use crate::contract_address::ContractAddress; use crate::felt::{ - ClassHash, CompiledClassHash, Felt, Nonce, TransactionHash, TransactionSignature, - TransactionVersion, + ClassHash, CompiledClassHash, Felt, Nonce, TransactionSignature, TransactionVersion, }; #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] @@ -25,36 +24,26 @@ pub struct DeclareTransactionV3 { compiled_class_hash: CompiledClassHash, class_hash: ClassHash, account_deployment_data: Vec, - transaction_hash: TransactionHash, } impl DeclareTransactionV3 { - pub fn new( - broadcasted_txn: BroadcastedDeclareTransactionV3, - class_hash: ClassHash, - transaction_hash: TransactionHash, - ) -> Self { + pub fn new(broadcasted_txn: &BroadcastedDeclareTransactionV3, class_hash: ClassHash) -> Self { Self { version: broadcasted_txn.common.version, - signature: broadcasted_txn.common.signature, + signature: broadcasted_txn.common.signature.clone(), nonce: broadcasted_txn.common.nonce, - resource_bounds: broadcasted_txn.common.resource_bounds, + resource_bounds: broadcasted_txn.common.resource_bounds.clone(), tip: broadcasted_txn.common.tip, - paymaster_data: broadcasted_txn.common.paymaster_data, + paymaster_data: broadcasted_txn.common.paymaster_data.clone(), nonce_data_availability_mode: broadcasted_txn.common.nonce_data_availability_mode, fee_data_availability_mode: broadcasted_txn.common.fee_data_availability_mode, sender_address: broadcasted_txn.sender_address, - account_deployment_data: broadcasted_txn.account_deployment_data, - transaction_hash, + account_deployment_data: broadcasted_txn.account_deployment_data.clone(), compiled_class_hash: broadcasted_txn.compiled_class_hash, class_hash, } } - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } - pub fn get_class_hash(&self) -> &ClassHash { &self.class_hash } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v1.rs index 56c3d2fac..7190d5ad6 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v1.rs @@ -1,17 +1,15 @@ use serde::{Deserialize, Serialize}; use starknet_api::transaction::Fee; +use super::broadcasted_deploy_account_transaction_v1::BroadcastedDeployAccountTransactionV1; use crate::contract_address::ContractAddress; -use crate::error::{DevnetResult, Error}; use crate::felt::{ - Calldata, ClassHash, ContractAddressSalt, Felt, Nonce, TransactionHash, TransactionSignature, - TransactionVersion, + Calldata, ClassHash, ContractAddressSalt, Nonce, TransactionSignature, TransactionVersion, }; -use crate::traits::HashProducer; #[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] pub struct DeployAccountTransactionV1 { - pub transaction_hash: TransactionHash, pub max_fee: Fee, pub version: TransactionVersion, pub signature: TransactionSignature, @@ -24,14 +22,23 @@ pub struct DeployAccountTransactionV1 { } impl DeployAccountTransactionV1 { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash + pub fn new( + broadcasted_txn: &BroadcastedDeployAccountTransactionV1, + contract_address: ContractAddress, + ) -> Self { + Self { + max_fee: broadcasted_txn.common.max_fee, + version: broadcasted_txn.common.version, + signature: broadcasted_txn.common.signature.clone(), + nonce: broadcasted_txn.common.nonce, + class_hash: broadcasted_txn.class_hash, + contract_address_salt: broadcasted_txn.contract_address_salt, + constructor_calldata: broadcasted_txn.constructor_calldata.clone(), + contract_address, + } } -} -impl HashProducer for DeployAccountTransactionV1 { - type Error = Error; - fn generate_hash(&self) -> DevnetResult { - Ok(self.transaction_hash) + pub fn get_contract_address(&self) -> &ContractAddress { + &self.contract_address } } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v3.rs b/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v3.rs index 2a90503ea..41e866fe9 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v3.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/deploy_account_transaction_v3.rs @@ -6,8 +6,7 @@ use super::broadcasted_deploy_account_transaction_v3::BroadcastedDeployAccountTr use super::{BroadcastedTransactionCommonV3, ResourceBoundsWrapper}; use crate::contract_address::ContractAddress; use crate::felt::{ - Calldata, ClassHash, ContractAddressSalt, Felt, Nonce, TransactionHash, TransactionSignature, - TransactionVersion, + Calldata, ClassHash, ContractAddressSalt, Felt, Nonce, TransactionSignature, TransactionVersion, }; #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] @@ -26,34 +25,28 @@ pub struct DeployAccountTransactionV3 { class_hash: ClassHash, #[serde(skip)] contract_address: ContractAddress, - transaction_hash: TransactionHash, } impl DeployAccountTransactionV3 { pub fn new( - broadcasted_txn: BroadcastedDeployAccountTransactionV3, + broadcasted_txn: &BroadcastedDeployAccountTransactionV3, contract_address: ContractAddress, - transaction_hash: TransactionHash, ) -> Self { Self { version: broadcasted_txn.common.version, - signature: broadcasted_txn.common.signature, + signature: broadcasted_txn.common.signature.clone(), nonce: broadcasted_txn.common.nonce, - resource_bounds: broadcasted_txn.common.resource_bounds, + resource_bounds: broadcasted_txn.common.resource_bounds.clone(), tip: broadcasted_txn.common.tip, - paymaster_data: broadcasted_txn.common.paymaster_data, + paymaster_data: broadcasted_txn.common.paymaster_data.clone(), nonce_data_availability_mode: broadcasted_txn.common.nonce_data_availability_mode, fee_data_availability_mode: broadcasted_txn.common.fee_data_availability_mode, contract_address_salt: broadcasted_txn.contract_address_salt, - constructor_calldata: broadcasted_txn.constructor_calldata, + constructor_calldata: broadcasted_txn.constructor_calldata.clone(), class_hash: broadcasted_txn.class_hash, contract_address, - transaction_hash, } } - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } pub fn get_contract_address(&self) -> &ContractAddress { &self.contract_address diff --git a/crates/starknet-devnet-types/src/rpc/transactions/deploy_transaction.rs b/crates/starknet-devnet-types/src/rpc/transactions/deploy_transaction.rs index 12dd9cc95..c37556ace 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/deploy_transaction.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/deploy_transaction.rs @@ -1,18 +1,12 @@ use serde::{Deserialize, Serialize}; -use crate::felt::{Calldata, ClassHash, ContractAddressSalt, TransactionHash, TransactionVersion}; +use crate::felt::{Calldata, ClassHash, ContractAddressSalt, TransactionVersion}; #[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] pub struct DeployTransaction { - pub transaction_hash: TransactionHash, pub version: TransactionVersion, pub class_hash: ClassHash, pub contract_address_salt: ContractAddressSalt, pub constructor_calldata: Calldata, } - -impl DeployTransaction { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } -} diff --git a/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v1.rs b/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v1.rs index d59ab57a7..00e2cae5d 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v1.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v1.rs @@ -1,16 +1,13 @@ use serde::{Deserialize, Serialize}; use starknet_api::transaction::Fee; +use super::broadcasted_invoke_transaction_v1::BroadcastedInvokeTransactionV1; use crate::contract_address::ContractAddress; -use crate::error::{DevnetResult, Error}; -use crate::felt::{ - Calldata, Felt, Nonce, TransactionHash, TransactionSignature, TransactionVersion, -}; -use crate::traits::HashProducer; +use crate::felt::{Calldata, Nonce, TransactionSignature, TransactionVersion}; #[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] pub struct InvokeTransactionV1 { - pub transaction_hash: TransactionHash, pub max_fee: Fee, pub version: TransactionVersion, pub signature: TransactionSignature, @@ -20,14 +17,14 @@ pub struct InvokeTransactionV1 { } impl InvokeTransactionV1 { - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } -} - -impl HashProducer for InvokeTransactionV1 { - type Error = Error; - fn generate_hash(&self) -> DevnetResult { - Ok(self.transaction_hash) + pub fn new(broadcasted_txn: &BroadcastedInvokeTransactionV1) -> InvokeTransactionV1 { + InvokeTransactionV1 { + max_fee: broadcasted_txn.common.max_fee, + version: broadcasted_txn.common.version, + signature: broadcasted_txn.common.signature.clone(), + nonce: broadcasted_txn.common.nonce, + sender_address: broadcasted_txn.sender_address, + calldata: broadcasted_txn.calldata.clone(), + } } } diff --git a/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v3.rs b/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v3.rs index 1fce74156..968435938 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v3.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/invoke_transaction_v3.rs @@ -5,9 +5,7 @@ use starknet_api::transaction::Tip; use super::broadcasted_invoke_transaction_v3::BroadcastedInvokeTransactionV3; use super::{BroadcastedTransactionCommonV3, ResourceBoundsWrapper}; use crate::contract_address::ContractAddress; -use crate::felt::{ - Calldata, Felt, Nonce, TransactionHash, TransactionSignature, TransactionVersion, -}; +use crate::felt::{Calldata, Felt, Nonce, TransactionSignature, TransactionVersion}; #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(deny_unknown_fields)] @@ -23,32 +21,24 @@ pub struct InvokeTransactionV3 { account_deployment_data: Vec, sender_address: ContractAddress, calldata: Calldata, - transaction_hash: TransactionHash, } impl InvokeTransactionV3 { - pub fn new( - broadcasted_txn: BroadcastedInvokeTransactionV3, - transaction_hash: TransactionHash, - ) -> Self { + pub fn new(broadcasted_txn: &BroadcastedInvokeTransactionV3) -> Self { Self { version: broadcasted_txn.common.version, - signature: broadcasted_txn.common.signature, + signature: broadcasted_txn.common.signature.clone(), nonce: broadcasted_txn.common.nonce, - resource_bounds: broadcasted_txn.common.resource_bounds, + resource_bounds: broadcasted_txn.common.resource_bounds.clone(), tip: broadcasted_txn.common.tip, - paymaster_data: broadcasted_txn.common.paymaster_data, + paymaster_data: broadcasted_txn.common.paymaster_data.clone(), nonce_data_availability_mode: broadcasted_txn.common.nonce_data_availability_mode, fee_data_availability_mode: broadcasted_txn.common.fee_data_availability_mode, sender_address: broadcasted_txn.sender_address, - calldata: broadcasted_txn.calldata, - account_deployment_data: broadcasted_txn.account_deployment_data, - transaction_hash, + calldata: broadcasted_txn.calldata.clone(), + account_deployment_data: broadcasted_txn.account_deployment_data.clone(), } } - pub fn get_transaction_hash(&self) -> &TransactionHash { - &self.transaction_hash - } } impl From for BroadcastedInvokeTransactionV3 { diff --git a/crates/starknet-devnet-types/src/rpc/transactions/l1_handler_transaction.rs b/crates/starknet-devnet-types/src/rpc/transactions/l1_handler_transaction.rs index 624d8f74d..e1d105588 100644 --- a/crates/starknet-devnet-types/src/rpc/transactions/l1_handler_transaction.rs +++ b/crates/starknet-devnet-types/src/rpc/transactions/l1_handler_transaction.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; +use serde::{Deserialize, Serialize}; use starknet_api::core::{ ContractAddress as ApiContractAddress, EntryPointSelector as ApiEntryPointSelector, Nonce as ApiNonce, @@ -12,11 +13,11 @@ use starknet_api::transaction::{ use starknet_rs_core::crypto::compute_hash_on_elements; use starknet_rs_core::types::FieldElement; +use super::{deserialize_paid_fee_on_l1, serialize_paid_fee_on_l1}; use crate::contract_address::ContractAddress; use crate::error::{ConversionError, DevnetResult, Error}; -use crate::felt::Felt; +use crate::felt::{Calldata, EntryPointSelector, Felt, Nonce, TransactionVersion}; use crate::rpc::messaging::MessageToL2; -use crate::rpc::transactions::L1HandlerTransaction; /// Cairo string for "l1_handler" const PREFIX_L1_HANDLER: FieldElement = FieldElement::from_mont([ @@ -26,13 +27,22 @@ const PREFIX_L1_HANDLER: FieldElement = FieldElement::from_mont([ 157895833347907735, ]); -impl L1HandlerTransaction { - /// Instantiates a new `L1HandlerTransaction`. - pub fn with_hash(mut self, chain_id: Felt) -> Self { - self.transaction_hash = self.compute_hash(chain_id); - self - } +#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct L1HandlerTransaction { + pub version: TransactionVersion, + pub nonce: Nonce, + pub contract_address: ContractAddress, + pub entry_point_selector: EntryPointSelector, + pub calldata: Calldata, + #[serde( + serialize_with = "serialize_paid_fee_on_l1", + deserialize_with = "deserialize_paid_fee_on_l1" + )] + pub paid_fee_on_l1: u128, +} +impl L1HandlerTransaction { /// Computes the hash of a `L1HandlerTransaction`. /// /// # Arguments @@ -62,7 +72,10 @@ impl L1HandlerTransaction { } /// Creates a blockifier version of `L1HandlerTransaction`. - pub fn create_blockifier_transaction(&self) -> DevnetResult { + pub fn create_blockifier_transaction( + &self, + chain_id: Felt, + ) -> DevnetResult { let transaction = BlockifierL1HandlerTransaction { tx: ApiL1HandlerTransaction { contract_address: ApiContractAddress::try_from(self.contract_address)?, @@ -72,7 +85,7 @@ impl L1HandlerTransaction { version: ApiTransactionVersion(self.version.into()), }, paid_fee_on_l1: ApiFee(self.paid_fee_on_l1), - tx_hash: ApiTransactionHash(self.transaction_hash.into()), + tx_hash: ApiTransactionHash(self.compute_hash(chain_id).into()), }; Ok(transaction) @@ -198,13 +211,12 @@ mod tests { calldata, nonce: nonce.into(), paid_fee_on_l1: fee, - transaction_hash, ..Default::default() }; - let transaction = - L1HandlerTransaction::try_from_message_to_l2(message).unwrap().with_hash(chain_id); + let transaction = L1HandlerTransaction::try_from_message_to_l2(message).unwrap(); assert_eq!(transaction, expected_tx); + assert_eq!(transaction.compute_hash(chain_id), transaction_hash); } }