From 789f457c15aa30afd2388c703f2c3b768df424c4 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Thu, 3 Oct 2024 16:11:35 +0530 Subject: [PATCH 01/25] feat: declare v0 working --- crates/client/mempool/src/lib.rs | 19 +++++++- .../rpc/src/providers/forward_to_provider.rs | 8 +++- crates/client/rpc/src/providers/mempool.rs | 6 +++ crates/client/rpc/src/providers/mod.rs | 3 ++ crates/client/rpc/src/test_utils.rs | 6 ++- crates/client/rpc/src/versions/v0_7_1/api.rs | 13 +++-- .../src/versions/v0_7_1/methods/write/mod.rs | 19 +++++++- crates/primitives/class/src/lib.rs | 9 ++++ .../src/broadcasted_to_blockifier.rs | 48 ++++++++++++++++++- .../src/from_broadcasted_transaction.rs | 33 +++++++++++-- crates/primitives/transactions/src/lib.rs | 18 ++++++- crates/tests/src/lib.rs | 2 + 12 files changed, 170 insertions(+), 14 deletions(-) diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index d3f264d84..3c31b7986 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -14,7 +14,7 @@ use mp_block::BlockId; use mp_block::BlockTag; use mp_block::MadaraPendingBlockInfo; use mp_class::ConvertedClass; -use mp_transactions::broadcasted_to_blockifier; +use mp_transactions::{broadcasted_to_blockifier, BroadcastedDeclareTransactionV0, broadcasted_to_blockifier_v0}; use mp_transactions::BroadcastedToBlockifierError; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::transaction::TransactionHash; @@ -63,6 +63,7 @@ impl Error { #[cfg_attr(test, mockall::automock)] pub trait MempoolProvider: Send + Sync { fn accept_invoke_tx(&self, tx: BroadcastedInvokeTransaction) -> Result; + fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result; fn accept_declare_tx(&self, tx: BroadcastedDeclareTransaction) -> Result; fn accept_deploy_account_tx( &self, @@ -181,6 +182,22 @@ impl MempoolProvider for Mempool { Ok(res) } + fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { + log::info!("Checkpoint 3: accept_declare_v0_tx: {:?}", tx); + let (tx, classes) = broadcasted_to_blockifier_v0( + tx, + self.chain_id(), + self.backend.chain_config().latest_protocol_version, + )?; + + let res = DeclareTransactionResult { + transaction_hash: transaction_hash(&tx), + class_hash: declare_class_hash(&tx).expect("Created transaction should be declare"), + }; + self.accept_tx(tx, classes)?; + Ok(res) + } + fn accept_declare_tx(&self, tx: BroadcastedDeclareTransaction) -> Result { let (tx, classes) = broadcasted_to_blockifier( BroadcastedTransaction::Declare(tx), diff --git a/crates/client/rpc/src/providers/forward_to_provider.rs b/crates/client/rpc/src/providers/forward_to_provider.rs index e9d09445b..9d644ad76 100644 --- a/crates/client/rpc/src/providers/forward_to_provider.rs +++ b/crates/client/rpc/src/providers/forward_to_provider.rs @@ -4,7 +4,7 @@ use starknet_core::types::{ DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; use starknet_providers::{Provider, ProviderError}; - +use mp_transactions::BroadcastedDeclareTransactionV0; use crate::{bail_internal_server_error, errors::StarknetRpcApiError}; use super::AddTransactionProvider; @@ -21,6 +21,12 @@ impl ForwardToProvider

{ #[async_trait] impl AddTransactionProvider for ForwardToProvider

{ + + async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + // panic here, because we can't really forward it to the real FGW, or shall we enable it so that another madara full node is able to use it? + // maybe a flag for this? as discussed + unimplemented!() + } async fn add_declare_transaction( &self, declare_transaction: BroadcastedDeclareTransaction, diff --git a/crates/client/rpc/src/providers/mempool.rs b/crates/client/rpc/src/providers/mempool.rs index 26623e310..eaac340c9 100644 --- a/crates/client/rpc/src/providers/mempool.rs +++ b/crates/client/rpc/src/providers/mempool.rs @@ -8,6 +8,7 @@ use starknet_core::types::{ DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; use std::sync::Arc; +use mp_transactions::BroadcastedDeclareTransactionV0; /// This [`AddTransactionProvider`] adds the received transactions to a mempool. pub struct MempoolAddTxProvider { @@ -43,6 +44,11 @@ impl From for StarknetRpcApiError { #[async_trait] impl AddTransactionProvider for MempoolAddTxProvider { + + async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + log::info!("Checkpoint 2: add_declare_v0_transaction: {:?}", declare_v0_transaction); + Ok(self.mempool.accept_declare_v0_tx(declare_v0_transaction).map_err(StarknetRpcApiError::from)?) + } async fn add_declare_transaction( &self, declare_transaction: BroadcastedDeclareTransaction, diff --git a/crates/client/rpc/src/providers/mod.rs b/crates/client/rpc/src/providers/mod.rs index 1f89ca62b..dbf1b4187 100644 --- a/crates/client/rpc/src/providers/mod.rs +++ b/crates/client/rpc/src/providers/mod.rs @@ -9,9 +9,12 @@ use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; +use mp_transactions::BroadcastedDeclareTransactionV0; #[async_trait] pub trait AddTransactionProvider: Send + Sync { + + async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult; async fn add_declare_transaction( &self, declare_transaction: BroadcastedDeclareTransaction, diff --git a/crates/client/rpc/src/test_utils.rs b/crates/client/rpc/src/test_utils.rs index 22401286d..1490e7b01 100644 --- a/crates/client/rpc/src/test_utils.rs +++ b/crates/client/rpc/src/test_utils.rs @@ -13,7 +13,7 @@ use mp_state_update::{ ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, ReplacedClassItem, StateDiff, StorageEntry, }; -use mp_transactions::{InvokeTransaction, InvokeTransactionV0, Transaction}; +use mp_transactions::{BroadcastedDeclareTransactionV0, InvokeTransaction, InvokeTransactionV0, Transaction}; use rstest::fixture; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, @@ -29,6 +29,10 @@ pub struct TestTransactionProvider; #[cfg(test)] #[async_trait] impl AddTransactionProvider for TestTransactionProvider { + + async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + unimplemented!() + } async fn add_declare_transaction( &self, _declare_transaction: BroadcastedDeclareTransaction, diff --git a/crates/client/rpc/src/versions/v0_7_1/api.rs b/crates/client/rpc/src/versions/v0_7_1/api.rs index 8dbab251d..40adf9824 100644 --- a/crates/client/rpc/src/versions/v0_7_1/api.rs +++ b/crates/client/rpc/src/versions/v0_7_1/api.rs @@ -11,7 +11,7 @@ use starknet_core::types::{ use starknet_types_core::felt::Felt; use m_proc_macros::versioned_starknet_rpc; - +use mp_transactions::BroadcastedDeclareTransactionV0; // Starknet RPC API trait and types // // Starkware maintains [a description of the Starknet API](https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json) @@ -28,19 +28,26 @@ pub trait StarknetWriteRpcApi { invoke_transaction: BroadcastedInvokeTransaction, ) -> RpcResult; - /// Submit a new class declaration transaction + /// Submit a new deploy account transaction #[method(name = "addDeployAccountTransaction")] async fn add_deploy_account_transaction( &self, deploy_account_transaction: BroadcastedDeployAccountTransaction, ) -> RpcResult; - /// Submit a new deploy account transaction + /// Submit a new class declaration transaction #[method(name = "addDeclareTransaction")] async fn add_declare_transaction( &self, declare_transaction: BroadcastedDeclareTransaction, ) -> RpcResult; + + /// Submit a new class v0 declaration transaction + #[method(name = "addDeclareV0Transaction")] + async fn add_declare_v0_transaction( + &self, + declare_transaction_v0: BroadcastedDeclareTransactionV0, + ) -> RpcResult; } #[versioned_starknet_rpc("V0_7_1")] diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs index 95b7796f4..a68b50443 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs @@ -3,11 +3,28 @@ use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; - +use mp_transactions::BroadcastedDeclareTransactionV0; use crate::{versions::v0_7_1::StarknetWriteRpcApiV0_7_1Server, Starknet}; #[async_trait] impl StarknetWriteRpcApiV0_7_1Server for Starknet { + /// Submit a new declare transaction to be added to the chain + /// + /// # Arguments + /// + /// * `declare_v0_transaction` - the declare v0 transaction to be added to the chain + /// + /// # Returns + /// + /// * `declare_transaction_result` - the result of the declare transaction + async fn add_declare_v0_transaction( + &self, + declare_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult { + log::info!("add_declare_v0_transaction: {:?}", declare_transaction); + Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?) + } + /// Submit a new declare transaction to be added to the chain /// /// # Arguments diff --git a/crates/primitives/class/src/lib.rs b/crates/primitives/class/src/lib.rs index a8dd3fac2..b82f35cb5 100644 --- a/crates/primitives/class/src/lib.rs +++ b/crates/primitives/class/src/lib.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::Arc}; use starknet_types_core::felt::Felt; + pub mod class_hash; pub mod class_update; pub mod compile; @@ -178,6 +179,7 @@ pub struct LegacyContractEntryPoint { } #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(untagged)] pub enum LegacyContractAbiEntry { Function(LegacyFunctionAbiEntry), Event(LegacyEventAbiEntry), @@ -190,6 +192,7 @@ pub struct LegacyFunctionAbiEntry { pub name: String, pub inputs: Vec, pub outputs: Vec, + #[serde(rename = "stateMutability")] pub state_mutability: Option, } @@ -224,23 +227,29 @@ pub struct LegacyTypedParameter { #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum LegacyFunctionAbiType { + #[serde(rename = "function")] Function, + #[serde(rename = "l1_handler")] L1Handler, + #[serde(rename = "constructor")] Constructor, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum LegacyEventAbiType { + #[serde(rename = "event")] Event, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum LegacyStructAbiType { + #[serde(rename = "struct")] Struct, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum FunctionStateMutability { + #[serde(rename = "view")] View, } diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index 96cb5e30f..665bdbf01 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use crate::{into_starknet_api::TransactionApiError, Transaction, TransactionWithHash}; +use crate::{BroadcastedDeclareTransactionV0, into_starknet_api::TransactionApiError, Transaction, TransactionWithHash}; use blockifier::{execution::errors::ContractClassError, transaction::errors::TransactionExecutionError}; use mp_chain_config::StarknetVersion; use mp_class::{ @@ -30,6 +30,52 @@ pub enum BroadcastedToBlockifierError { CompiledClassHashMismatch { expected: Felt, compilation: Felt }, } +pub fn broadcasted_to_blockifier_v0( + transaction: BroadcastedDeclareTransactionV0, + chain_id: Felt, + starknet_version: StarknetVersion, +) -> Result< + (blockifier::transaction::transaction_execution::Transaction, Option), + BroadcastedToBlockifierError, +> { + let (class_info, class_hash, extra_class_info) = { + let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); + let class_hash = compressed_legacy_class.compute_class_hash().unwrap(); + log::debug!("Computed legacy class hash: {:?}", class_hash); + let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); + let class_blockifier = compressed_legacy_class + .to_blockifier_class() + .map_err(BroadcastedToBlockifierError::CompilationFailed)?; + let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) }; + + ( + Some(blockifier::execution::contract_class::ClassInfo::new(&class_blockifier, 0, 0)?), + Some(class_hash), + Some(ConvertedClass::Legacy(LegacyConvertedClass { class_hash, info: class_info })), + ) }; + + let is_query = transaction.is_query; + let TransactionWithHash { transaction, hash } = + TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash); + let deployed_address = match &transaction { + Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), + _ => None, + }; + let transaction: starknet_api::transaction::Transaction = transaction.try_into()?; + + Ok(( + blockifier::transaction::transaction_execution::Transaction::from_api( + transaction, + TransactionHash(hash), + class_info, + None, + deployed_address.map(|address| address.try_into().unwrap()), + is_query, + )?, + extra_class_info, + )) +} + pub fn broadcasted_to_blockifier( transaction: starknet_core::types::BroadcastedTransaction, chain_id: Felt, diff --git a/crates/primitives/transactions/src/from_broadcasted_transaction.rs b/crates/primitives/transactions/src/from_broadcasted_transaction.rs index 9cc80a4ca..657330886 100644 --- a/crates/primitives/transactions/src/from_broadcasted_transaction.rs +++ b/crates/primitives/transactions/src/from_broadcasted_transaction.rs @@ -1,11 +1,7 @@ use mp_chain_config::StarknetVersion; use starknet_types_core::felt::Felt; -use crate::{ - DeclareTransaction, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction, - DeployAccountTransactionV1, DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1, - InvokeTransactionV3, Transaction, TransactionWithHash, -}; +use crate::{BroadcastedDeclareTransactionV0, DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV1, DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1, InvokeTransactionV3, Transaction, TransactionWithHash}; // class_hash is required for DeclareTransaction impl TransactionWithHash { @@ -26,6 +22,18 @@ impl TransactionWithHash { let hash = transaction.compute_hash(chain_id, starknet_version, is_query); Self { hash, transaction } } + + pub fn from_broadcasted_v0( + tx: BroadcastedDeclareTransactionV0, + chain_id: Felt, + starknet_version: StarknetVersion, + class_hash: Option, + ) -> Self { + let is_query = tx.is_query; + let transaction: Transaction = Transaction::Declare(DeclareTransaction::from_broadcasted_v0(tx, class_hash.unwrap())); + let hash = transaction.compute_hash(chain_id, starknet_version, is_query); + Self { hash, transaction } + } } impl From for InvokeTransaction { @@ -80,6 +88,21 @@ impl DeclareTransaction { } } } + + fn from_broadcasted_v0(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { + DeclareTransaction::V0(DeclareTransactionV0::from_broadcasted(tx, class_hash)) + } +} + +impl DeclareTransactionV0 { + fn from_broadcasted(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { + Self { + sender_address: tx.sender_address, + max_fee: tx.max_fee, + signature: tx.signature, + class_hash, + } + } } impl DeclareTransactionV1 { diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index 22bbbd523..8837155bd 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -1,3 +1,4 @@ +use std::sync::Arc; use mp_convert::ToFelt; use starknet_api::transaction::TransactionVersion; use starknet_types_core::{felt::Felt, hash::StarkHash}; @@ -12,7 +13,8 @@ mod to_starknet_core; pub mod compute_hash; pub mod utils; -pub use broadcasted_to_blockifier::{broadcasted_to_blockifier, BroadcastedToBlockifierError}; +pub use broadcasted_to_blockifier::{broadcasted_to_blockifier, BroadcastedToBlockifierError, broadcasted_to_blockifier_v0}; +use mp_class::CompressedLegacyContractClass; const SIMULATE_TX_VERSION_OFFSET: Felt = Felt::from_hex_unchecked("0x100000000000000000000000000000000"); @@ -38,6 +40,20 @@ impl TransactionWithHash { } } +#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct BroadcastedDeclareTransactionV0 { + /// The address of the account contract sending the declaration transaction + pub sender_address: Felt, + /// The maximal fee that can be charged for including the transaction + pub max_fee: Felt, + /// Signature + pub signature: Vec, + /// The class to be declared + pub contract_class: Arc, + /// If set to `true`, uses a query-only transaction version that's invalid for execution + pub is_query: bool, +} + #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Transaction { Invoke(InvokeTransaction), diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index f43b23514..4a602aab3 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -235,6 +235,8 @@ async fn madara_can_sync_a_few_blocks() { node.wait_for_ready().await; node.wait_for_sync_to(19).await; + println!("we have block till: {:?}", node.json_rpc().block_hash_and_number().await.unwrap()); + assert_eq!( node.json_rpc().block_hash_and_number().await.unwrap(), BlockHashAndNumber { From d448b65381733b47b07e94739c07b93e1392f251 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 4 Oct 2024 16:35:10 +0530 Subject: [PATCH 02/25] debug: debuging declare v0 --- crates/client/db/src/class_db.rs | 3 +++ crates/client/mempool/src/block_production.rs | 5 +++++ crates/client/mempool/src/inner.rs | 5 +++++ crates/client/mempool/src/lib.rs | 15 ++++++++++++++- crates/client/rpc/src/providers/mempool.rs | 2 +- .../rpc/src/versions/v0_7_1/methods/write/mod.rs | 2 +- .../primitives/chain_config/src/chain_config.rs | 2 ++ .../src/broadcasted_to_blockifier.rs | 16 ++++++++++++++-- 8 files changed, 45 insertions(+), 5 deletions(-) diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 5a8a54631..d8eb23ce2 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -28,10 +28,12 @@ impl MadaraBackend { log::debug!("get encoded {key:#x}"); let key_encoded = bincode::serialize(key)?; + log::debug!("serailization passed, issue somewhere"); // Get from pending db, then normal db if not found. if is_pending { let col = self.db.get_column(pending_col); if let Some(res) = self.db.get_pinned_cf(&col, &key_encoded)? { + log::debug!("got some result and next step is to deserialize"); return Ok(Some(bincode::deserialize(&res)?)); // found in pending } } @@ -60,6 +62,7 @@ impl MadaraBackend { Column::ClassInfo, )? else { + log::debug!("returning none because some error with getting the class"); return Ok(None); }; diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index b1ec49dbd..a8aa68f59 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -232,6 +232,8 @@ impl BlockProductionTask { // This does not need to be outside the loop, but that saves an allocation let mut executed_txs = Vec::with_capacity(batch_size); + log::debug!("just before the loop on the transactions"); + loop { // Take transactions from mempool. let to_take = batch_size.saturating_sub(txs_to_process.len()); @@ -249,6 +251,7 @@ impl BlockProductionTask { txs_to_process_blockifier .extend(txs_to_process.iter().map(|tx| Transaction::AccountTransaction(clone_account_tx(&tx.tx)))); + log::debug!("just before executing the transactions"); // Execute the transactions. let all_results = self.executor.execute_txs(&txs_to_process_blockifier); // When the bouncer cap is reached, blockifier will return fewer results than what we asked for. @@ -390,6 +393,8 @@ impl BlockProductionTask { let block_n = self.block_n(); log::debug!("closing block #{}", block_n); + + // Complete the block with full bouncer capacity. let start_time = Instant::now(); let (new_state_diff, _n_executed) = diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 8faf8a725..46fc3bf77 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -229,7 +229,12 @@ impl MempoolInner { pub fn insert_tx(&mut self, mempool_tx: MempoolTransaction, force: bool) -> Result<(), TxInsersionError> { // Get the nonce chain for the contract + log::debug!("Checkpoint: inside the insert tx"); + + let contract_addr = mempool_tx.contract_address(); + log::debug!("Checkpoint: contract address here is: {:?}", contract_addr); + let arrived_at = mempool_tx.arrived_at; let deployed_contract_address = diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index 3c31b7986..031fabbd9 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -93,6 +93,8 @@ impl Mempool { fn accept_tx(&self, tx: Transaction, converted_class: Option) -> Result<(), Error> { let Transaction::AccountTransaction(tx) = tx else { panic!("L1HandlerTransaction not supported yet") }; + log::debug!("Checkpoint: we are inside the accept tx and we made sure that we have account txn only"); + // The timestamp *does not* take the transaction validation time into account. let arrived_at = ArrivedAtTimestamp::now(); @@ -101,6 +103,8 @@ impl Mempool { block } else { // No current pending block, we'll make an unsaved empty one for the sake of validating this tx. + log::debug!("Checkpoint: this should be triggered because we are trying for a genesis block/ or maybe not"); + let parent_block_hash = self .backend .get_block_hash(&BlockId::Tag(BlockTag::Latest))? @@ -125,12 +129,16 @@ impl Mempool { } else { None }; + log::debug!("Checkpoint: now sending the tx for the execution context"); // Perform validations let exec_context = ExecutionContext::new_in_block(Arc::clone(&self.backend), &pending_block_info)?; let mut validator = exec_context.tx_validator(); let _ = validator.perform_validations(clone_account_tx(&tx), deploy_account_tx_hash.is_some()); + log::debug!("Checkpoint: validation performed"); + + if !is_only_query(&tx) { // Finally, add it to the nonce chain for the account nonce let force = false; @@ -183,17 +191,22 @@ impl MempoolProvider for Mempool { } fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { - log::info!("Checkpoint 3: accept_declare_v0_tx: {:?}", tx); + log::debug!("Checkpoint 3: accept_declare_v0_tx"); let (tx, classes) = broadcasted_to_blockifier_v0( tx, self.chain_id(), self.backend.chain_config().latest_protocol_version, )?; + log::debug!("Checkpoint declare v0 tx"); + + let res = DeclareTransactionResult { transaction_hash: transaction_hash(&tx), class_hash: declare_class_hash(&tx).expect("Created transaction should be declare"), }; + log::debug!("sending txn to the accept tx"); + self.accept_tx(tx, classes)?; Ok(res) } diff --git a/crates/client/rpc/src/providers/mempool.rs b/crates/client/rpc/src/providers/mempool.rs index eaac340c9..791d644b5 100644 --- a/crates/client/rpc/src/providers/mempool.rs +++ b/crates/client/rpc/src/providers/mempool.rs @@ -46,7 +46,7 @@ impl From for StarknetRpcApiError { impl AddTransactionProvider for MempoolAddTxProvider { async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { - log::info!("Checkpoint 2: add_declare_v0_transaction: {:?}", declare_v0_transaction); + log::info!("Checkpoint 2: add_declare_v0_transaction"); Ok(self.mempool.accept_declare_v0_tx(declare_v0_transaction).map_err(StarknetRpcApiError::from)?) } async fn add_declare_transaction( diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs index a68b50443..c7119e47b 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs @@ -21,7 +21,7 @@ impl StarknetWriteRpcApiV0_7_1Server for Starknet { &self, declare_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { - log::info!("add_declare_v0_transaction: {:?}", declare_transaction); + log::info!("add_declare_v0_transaction"); Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?) } diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index 1e098052c..a77e105d3 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -212,6 +212,8 @@ impl ChainConfig { chain_name: "Madara".into(), chain_id: ChainId::Other("MADARA_DEVNET".into()), sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), + block_time: Duration::from_secs(3), + pending_block_update_time: Duration::from_secs(2), ..ChainConfig::starknet_sepolia() } } diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index 665bdbf01..f80f0ec8a 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -38,15 +38,20 @@ pub fn broadcasted_to_blockifier_v0( (blockifier::transaction::transaction_execution::Transaction, Option), BroadcastedToBlockifierError, > { + log::debug!("Checkpoint 4: broadcasted to blockifier v0"); + let (class_info, class_hash, extra_class_info) = { let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); let class_hash = compressed_legacy_class.compute_class_hash().unwrap(); - log::debug!("Computed legacy class hash: {:?}", class_hash); + log::debug!("Checkpoint 5: Computed legacy class hash: {:?}", class_hash); let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); let class_blockifier = compressed_legacy_class .to_blockifier_class() .map_err(BroadcastedToBlockifierError::CompilationFailed)?; - let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) }; + + log::debug!("Checkpoint 5: class blockfier sorted"); + + let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) }; ( Some(blockifier::execution::contract_class::ClassInfo::new(&class_blockifier, 0, 0)?), @@ -54,9 +59,16 @@ pub fn broadcasted_to_blockifier_v0( Some(ConvertedClass::Legacy(LegacyConvertedClass { class_hash, info: class_info })), ) }; + log::debug!("Checkpoint 6: we have the class info, class hash and the extra class info"); + + let is_query = transaction.is_query; let TransactionWithHash { transaction, hash } = TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash); + + log::debug!("Checkpoint 7: tx hash is: {:?}", hash); + + let deployed_address = match &transaction { Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), _ => None, From 3da2f1a224498f3dda1220b4444b61aeae2f8fa1 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Mon, 7 Oct 2024 13:46:11 +0530 Subject: [PATCH 03/25] adding comments for debugging --- crates/client/db/src/class_db.rs | 11 ++++++++++- crates/primitives/chain_config/src/chain_config.rs | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 2c261048b..8e0a2fbef 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -43,6 +43,8 @@ impl MadaraBackend { let Some(val) = self.db.get_pinned_cf(&col, &key_encoded)? else { return Ok(None) }; let val = bincode::deserialize(&val)?; + log::debug!("got some value of class hash"); + Ok(Some(val)) } @@ -117,6 +119,9 @@ impl MadaraBackend { col_info: Column, col_compiled: Column, ) -> Result<(), MadaraStorageError> { + log::debug!("store classes has been called"); + log::debug!("converted classes length: {:?}", converted_classes.len()); + let mut writeopts = WriteOptions::new(); writeopts.disable_wal(true); @@ -158,7 +163,7 @@ impl MadaraBackend { |col, chunk| { let mut batch = WriteBatchWithTransaction::default(); for (key, value) in chunk { - log::trace!("Class compiled store key={key:#x}"); + log::debug!("Class compiled store key={key:#x}"); let key_bin = bincode::serialize(key)?; // TODO: find a way to avoid this allocation batch.put_cf(col, &key_bin, bincode::serialize(&value)?); @@ -167,6 +172,10 @@ impl MadaraBackend { Ok::<_, MadaraStorageError>(()) }, )?; + + // get the class info from the db, class hash is here: [Legacy(LegacyConvertedClass { class_hash: 0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6, offset: 0, r#type: "legacy_contract_class" })] + let class_info = self.get_class_info(&block_id, &Felt::from_hex("0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6").unwrap())?; + log::debug!("class info: {:?}", class_info); Ok(()) } diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index a77e105d3..3bc886162 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -212,7 +212,7 @@ impl ChainConfig { chain_name: "Madara".into(), chain_id: ChainId::Other("MADARA_DEVNET".into()), sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), - block_time: Duration::from_secs(3), + block_time: Duration::from_secs(10), pending_block_update_time: Duration::from_secs(2), ..ChainConfig::starknet_sepolia() } From 2938bef429be10a2744857e6b04b2040ea3a5cad Mon Sep 17 00:00:00 2001 From: mohiiit Date: Mon, 7 Oct 2024 21:35:29 +0530 Subject: [PATCH 04/25] debugging with bootstraper --- crates/client/db/src/class_db.rs | 4 ---- crates/client/eth/src/l1_messaging.rs | 7 ++++++- .../rpc/src/versions/v0_7_1/methods/read/get_nonce.rs | 2 ++ crates/primitives/chain_config/src/chain_config.rs | 3 ++- crates/primitives/class/src/lib.rs | 1 - 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 8e0a2fbef..28b2ea222 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -172,10 +172,6 @@ impl MadaraBackend { Ok::<_, MadaraStorageError>(()) }, )?; - - // get the class info from the db, class hash is here: [Legacy(LegacyConvertedClass { class_hash: 0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6, offset: 0, r#type: "legacy_contract_class" })] - let class_info = self.get_class_info(&block_id, &Felt::from_hex("0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6").unwrap())?; - log::debug!("class info: {:?}", class_info); Ok(()) } diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index b9df9c56a..22658afe9 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -2,13 +2,15 @@ use alloy::eips::BlockNumberOrTag; use anyhow::Context; use futures::StreamExt; use std::sync::Arc; - +use std::thread::sleep; +use std::time::Duration; use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; use alloy::primitives::{keccak256, FixedBytes, U256}; use alloy::sol_types::SolValue; use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; +use log::Level::Debug; use mc_db::{l1_db::LastSyncedEventBlock, MadaraBackend}; use mp_utils::channel_wait_or_graceful_shutdown; use starknet_api::core::{ChainId, ContractAddress, EntryPointSelector, Nonce}; @@ -17,6 +19,7 @@ use starknet_api::transaction::{ }; use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; +use url::ParseError::SetHostOnCannotBeABaseUrl; impl EthereumClient { /// Get cancellation status of an L1 to L2 message @@ -62,7 +65,9 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C .into_stream(); while let Some(event_result) = channel_wait_or_graceful_shutdown(event_stream.next()).await { + log::debug!("inside the l1 messages"); if let Ok((event, meta)) = event_result { + log::debug!("we got some logs"); tracing::info!( "⟠ Processing L1 Message from block: {:?}, transaction_hash: {:?}, log_index: {:?}, fromAddress: {:?}", meta.block_number, diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs index c4ea2b728..759e92de9 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs @@ -23,6 +23,8 @@ use crate::Starknet; /// specific issue. pub fn get_nonce(starknet: &Starknet, block_id: BlockId, contract_address: Felt) -> StarknetRpcResult { + + log::debug!("inside get nonce block id is: {:?}, and contract address is: {:?}", block_id, contract_address); // Check if block exists. We have to return a different error in that case. let block_exists = starknet.backend.contains_block(&block_id).or_internal_server_error("Checking if block is in database")?; diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index 3bc886162..b8f651933 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -212,8 +212,9 @@ impl ChainConfig { chain_name: "Madara".into(), chain_id: ChainId::Other("MADARA_DEVNET".into()), sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), - block_time: Duration::from_secs(10), + block_time: Duration::from_secs(8), pending_block_update_time: Duration::from_secs(2), + eth_core_contract_address: "0x5fc8d32690cc91d4c39d9d3abcbd16989f875707".parse().expect("parsing a constant"), ..ChainConfig::starknet_sepolia() } } diff --git a/crates/primitives/class/src/lib.rs b/crates/primitives/class/src/lib.rs index 0443d68eb..d344d464e 100644 --- a/crates/primitives/class/src/lib.rs +++ b/crates/primitives/class/src/lib.rs @@ -180,7 +180,6 @@ pub struct LegacyContractEntryPoint { } #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(untagged)] pub enum LegacyContractAbiEntry { Function(LegacyFunctionAbiEntry), Event(LegacyEventAbiEntry), From b8c554cd7828b0e63187bd65991c38fea3922f2e Mon Sep 17 00:00:00 2001 From: mohiiit Date: Tue, 8 Oct 2024 18:03:20 +0530 Subject: [PATCH 05/25] making l1 handler execute in the mempool --- Cargo.lock | 319 ++++++++++++------ Cargo.toml | 2 +- crates/client/eth/src/client.rs | 2 + crates/client/eth/src/l1_messaging.rs | 38 ++- crates/client/eth/src/state_update.rs | 16 +- crates/client/eth/src/sync.rs | 24 +- crates/client/mempool/src/inner.rs | 7 +- crates/client/mempool/src/lib.rs | 85 +++-- crates/node/src/main.rs | 6 +- crates/node/src/service/l1.rs | 10 +- .../chain_config/src/chain_config.rs | 2 +- 11 files changed, 340 insertions(+), 171 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e8f112c2..c4c31ad94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,15 +58,16 @@ checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "alloy" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f4a4aaae80afd4be443a6aecd92a6b255dcdd000f97996928efb33d8a71e100" +checksum = "056f2c01b2aed86e15b43c47d109bfc8b82553dc34e66452875e51247ec31ab2" dependencies = [ "alloy-consensus", "alloy-contract", "alloy-core", "alloy-eips", "alloy-genesis", + "alloy-network", "alloy-node-bindings", "alloy-provider", "alloy-rpc-client", @@ -88,23 +89,25 @@ dependencies = [ [[package]] name = "alloy-consensus" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04c309895995eaa4bfcc345f5515a39c7df9447798645cc8bf462b6c5bf1dc96" +checksum = "705687d5bfd019fee57cf9e206b27b30a9a9617535d5590a02b171e813208f8e" dependencies = [ "alloy-eips", "alloy-primitives", "alloy-rlp", "alloy-serde", + "auto_impl", "c-kzg", + "derive_more 1.0.0", "serde", ] [[package]] name = "alloy-contract" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f4e0ef72b0876ae3068b2ed7dfae9ae1779ce13cfaec2ee1f08f5bd0348dc57" +checksum = "917f7d12cf3971dc8c11c9972f732b35ccb9aaaf5f28f2f87e9e6523bee3a8ad" dependencies = [ "alloy-dyn-abi", "alloy-json-abi", @@ -122,21 +125,22 @@ dependencies = [ [[package]] name = "alloy-core" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "529fc6310dc1126c8de51c376cbc59c79c7f662bd742be7dc67055d5421a81b4" +checksum = "3cf9b7166dd6aee2236646457b81fa032af8a67c25f3965d56e48881658bc85f" dependencies = [ "alloy-dyn-abi", "alloy-json-abi", "alloy-primitives", + "alloy-rlp", "alloy-sol-types", ] [[package]] name = "alloy-dyn-abi" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413902aa18a97569e60f679c23f46a18db1656d87ab4d4e49d0e1e52042f66df" +checksum = "1109c57718022ac84c194f775977a534e1b3969b405e55693a61c42187cc0612" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -149,16 +153,41 @@ dependencies = [ "winnow 0.6.13", ] +[[package]] +name = "alloy-eip2930" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0069cf0642457f87a01a014f6dc29d5d893cd4fd8fddf0c3cdfad1bb3ebafc41" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "serde", +] + +[[package]] +name = "alloy-eip7702" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea59dc42102bc9a1905dc57901edc6dd48b9f38115df86c7d252acba70d71d04" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "serde", +] + [[package]] name = "alloy-eips" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9431c99a3b3fe606ede4b3d4043bdfbcb780c45b8d8d226c3804e2b75cfbe68" +checksum = "6ffb906284a1e1f63c4607da2068c8197458a352d0b3e9796e67353d72a9be85" dependencies = [ + "alloy-eip2930", + "alloy-eip7702", "alloy-primitives", "alloy-rlp", "alloy-serde", "c-kzg", + "derive_more 1.0.0", "once_cell", "serde", "sha2", @@ -166,9 +195,9 @@ dependencies = [ [[package]] name = "alloy-genesis" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79614dfe86144328da11098edcc7bc1a3f25ad8d3134a9eb9e857e06f0d9840d" +checksum = "8429cf4554eed9b40feec7f4451113e76596086447550275e3def933faf47ce3" dependencies = [ "alloy-primitives", "alloy-serde", @@ -177,9 +206,9 @@ dependencies = [ [[package]] name = "alloy-json-abi" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc05b04ac331a9f07e3a4036ef7926e49a8bf84a99a1ccfc7e2ab55a5fcbb372" +checksum = "c4cc0e59c803dd44d14fc0cfa9fea1f74cfa8fd9fb60ca303ced390c58c28d4e" dependencies = [ "alloy-primitives", "alloy-sol-type-parser", @@ -189,9 +218,9 @@ dependencies = [ [[package]] name = "alloy-json-rpc" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57e2865c4c3bb4cdad3f0d9ec1ab5c0c657ba69a375651bd35e32fb6c180ccc2" +checksum = "f8fa8a1a3c4cbd221f2b8e3693aeb328fca79a757fe556ed08e47bbbc2a70db7" dependencies = [ "alloy-primitives", "alloy-sol-types", @@ -203,9 +232,9 @@ dependencies = [ [[package]] name = "alloy-network" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e701fc87ef9a3139154b0b4ccb935b565d27ffd9de020fe541bf2dec5ae4ede" +checksum = "85fa23a6a9d612b52e402c995f2d582c25165ec03ac6edf64c861a76bc5b87cd" dependencies = [ "alloy-consensus", "alloy-eips", @@ -224,10 +253,12 @@ dependencies = [ [[package]] name = "alloy-network-primitives" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec9d5a0f9170b10988b6774498a022845e13eda94318440d17709d50687f67f9" +checksum = "801492711d4392b2ccf5fc0bc69e299fa1aab15167d74dcaa9aab96a54f684bd" dependencies = [ + "alloy-consensus", + "alloy-eips", "alloy-primitives", "alloy-serde", "serde", @@ -235,13 +266,14 @@ dependencies = [ [[package]] name = "alloy-node-bindings" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16faebb9ea31a244fd6ce3288d47df4be96797d9c3c020144b8f2c31543a4512" +checksum = "4f1334a738aa1710cb8227441b3fcc319202ce78e967ef37406940242df4a454" dependencies = [ "alloy-genesis", "alloy-primitives", "k256", + "rand", "serde_json", "tempfile", "thiserror", @@ -251,31 +283,37 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccb3ead547f4532bc8af961649942f0b9c16ee9226e26caa3f38420651cc0bf4" +checksum = "a289ffd7448036f2f436b377f981c79ce0b2090877bad938d43387dc09931877" dependencies = [ "alloy-rlp", "bytes", "cfg-if", "const-hex", - "derive_more", + "derive_more 1.0.0", + "foldhash", + "hashbrown 0.15.0", "hex-literal", + "indexmap 2.6.0", "itoa", "k256", "keccak-asm", + "paste", "proptest", "rand", "ruint", + "rustc-hash 2.0.0", "serde", + "sha3", "tiny-keccak", ] [[package]] name = "alloy-provider" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9c0ab10b93de601a6396fc7ff2ea10d3b28c46f079338fa562107ebf9857c8" +checksum = "fcfaa4ffec0af04e3555686b8aacbcdf7d13638133a0672749209069750f78a6" dependencies = [ "alloy-chains", "alloy-consensus", @@ -294,7 +332,7 @@ dependencies = [ "async-stream", "async-trait", "auto_impl", - "dashmap", + "dashmap 6.1.0", "futures", "futures-utils-wasm", "lru", @@ -302,6 +340,7 @@ dependencies = [ "reqwest 0.12.5", "serde", "serde_json", + "thiserror", "tokio", "tracing", "url", @@ -331,11 +370,12 @@ dependencies = [ [[package]] name = "alloy-rpc-client" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b38e3ffdb285df5d9f60cb988d336d9b8e3505acb78750c3bc60336a7af41d3" +checksum = "370143ed581aace6e663342d21d209c6b2e34ee6142f7d6675adb518deeaf0dc" dependencies = [ "alloy-json-rpc", + "alloy-primitives", "alloy-transport", "alloy-transport-http", "futures", @@ -345,17 +385,18 @@ dependencies = [ "serde_json", "tokio", "tokio-stream", - "tower", + "tower 0.5.1", "tracing", "url", ] [[package]] name = "alloy-rpc-types" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c31a3750b8f5a350d17354e46a52b0f2f19ec5f2006d816935af599dedc521" +checksum = "9ffc534b7919e18f35e3aa1f507b6f3d9d92ec298463a9f6beaac112809d8d06" dependencies = [ + "alloy-primitives", "alloy-rpc-types-eth", "alloy-serde", "serde", @@ -363,9 +404,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-anvil" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ab6509cd38b2e8c8da726e0f61c1e314a81df06a38d37ddec8bced3f8d25ed" +checksum = "d780adaa5d95b07ad92006b2feb68ecfa7e2015f7d5976ceaac4c906c73ebd07" dependencies = [ "alloy-primitives", "alloy-serde", @@ -374,9 +415,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-eth" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81e18424d962d7700a882fe423714bd5b9dde74c7a7589d4255ea64068773aef" +checksum = "413f4aa3ccf2c3e4234a047c5fa4727916d7daf25a89f9b765df0ba09784fd87" dependencies = [ "alloy-consensus", "alloy-eips", @@ -385,17 +426,17 @@ dependencies = [ "alloy-rlp", "alloy-serde", "alloy-sol-types", + "derive_more 1.0.0", "itertools 0.13.0", "serde", "serde_json", - "thiserror", ] [[package]] name = "alloy-serde" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e33feda6a53e6079895aed1d08dcb98a1377b000d80d16370fbbdb8155d547ef" +checksum = "9dff0ab1cdd43ca001e324dc27ee0e8606bd2161d6623c63e0e0b8c4dfc13600" dependencies = [ "alloy-primitives", "serde", @@ -404,9 +445,9 @@ dependencies = [ [[package]] name = "alloy-signer" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "740a25b92e849ed7b0fa013951fe2f64be9af1ad5abe805037b44fb7770c5c47" +checksum = "2fd4e0ad79c81a27ca659be5d176ca12399141659fef2bcbfdc848da478f4504" dependencies = [ "alloy-primitives", "async-trait", @@ -418,9 +459,9 @@ dependencies = [ [[package]] name = "alloy-signer-local" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b0707d4f63e4356a110b30ef3add8732ab6d181dd7be4607bf79b8777105cee" +checksum = "494e0a256f3e99f2426f994bcd1be312c02cb8f88260088dacb33a8b8936475f" dependencies = [ "alloy-consensus", "alloy-network", @@ -434,13 +475,13 @@ dependencies = [ [[package]] name = "alloy-sol-macro" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b40397ddcdcc266f59f959770f601ce1280e699a91fc1862f29cef91707cd09" +checksum = "0409e3ba5d1de409997a7db8b8e9d679d52088c1dee042a85033affd3cadeab4" dependencies = [ "alloy-sol-macro-expander", "alloy-sol-macro-input", - "proc-macro-error", + "proc-macro-error2", "proc-macro2", "quote", "syn 2.0.66", @@ -448,16 +489,16 @@ dependencies = [ [[package]] name = "alloy-sol-macro-expander" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "867a5469d61480fea08c7333ffeca52d5b621f5ca2e44f271b117ec1fc9a0525" +checksum = "a18372ef450d59f74c7a64a738f546ba82c92f816597fed1802ef559304c81f1" dependencies = [ "alloy-json-abi", "alloy-sol-macro-input", "const-hex", "heck 0.5.0", - "indexmap 2.2.6", - "proc-macro-error", + "indexmap 2.6.0", + "proc-macro-error2", "proc-macro2", "quote", "syn 2.0.66", @@ -467,9 +508,9 @@ dependencies = [ [[package]] name = "alloy-sol-macro-input" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e482dc33a32b6fadbc0f599adea520bd3aaa585c141a80b404d0a3e3fa72528" +checksum = "f7bad89dd0d5f109e8feeaf787a9ed7a05a91a9a0efc6687d147a70ebca8eff7" dependencies = [ "alloy-json-abi", "const-hex", @@ -484,9 +525,9 @@ dependencies = [ [[package]] name = "alloy-sol-type-parser" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbcba3ca07cf7975f15d871b721fb18031eec8bce51103907f6dcce00b255d98" +checksum = "dbd3548d5262867c2c4be6223fe4f2583e21ade0ca1c307fd23bc7f28fca479e" dependencies = [ "serde", "winnow 0.6.13", @@ -494,9 +535,9 @@ dependencies = [ [[package]] name = "alloy-sol-types" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a91ca40fa20793ae9c3841b83e74569d1cc9af29a2f5237314fd3452d51e38c7" +checksum = "4aa666f1036341b46625e72bd36878bf45ad0185f1b88601223e1ec6ed4b72b1" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -507,9 +548,9 @@ dependencies = [ [[package]] name = "alloy-transport" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d0590afbdacf2f8cca49d025a2466f3b6584a016a8b28f532f29f8da1007bae" +checksum = "2ac3e97dad3d31770db0fc89bd6a63b789fbae78963086733f960cf32c483904" dependencies = [ "alloy-json-rpc", "base64 0.22.1", @@ -519,22 +560,22 @@ dependencies = [ "serde_json", "thiserror", "tokio", - "tower", + "tower 0.5.1", "tracing", "url", ] [[package]] name = "alloy-transport-http" -version = "0.2.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2437d145d80ea1aecde8574d2058cceb8b3c9cba05f6aea8e67907c660d46698" +checksum = "b367dcccada5b28987c2296717ee04b9a5637aacd78eacb1726ef211678b5212" dependencies = [ "alloy-json-rpc", "alloy-transport", "reqwest 0.12.5", "serde_json", - "tower", + "tower 0.5.1", "tracing", "url", ] @@ -1204,7 +1245,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "rustc-hash", + "rustc-hash 1.1.0", "shlex", "syn 2.0.66", ] @@ -1283,8 +1324,8 @@ dependencies = [ "cairo-lang-starknet-classes", "cairo-lang-utils 2.7.0", "cairo-vm", - "derive_more", - "indexmap 2.2.6", + "derive_more 0.99.18", + "indexmap 2.6.0", "itertools 0.10.5", "keccak", "log", @@ -1339,7 +1380,7 @@ version = "0.1.0" source = "git+https://github.com/cchudant/bonsai-trie.git?branch=fix_inserts_remove_leaks#1a77166d1a3c9afa590b337d8d70670b8d43eadd" dependencies = [ "bitvec", - "derive_more", + "derive_more 0.99.18", "hashbrown 0.14.5", "log", "parity-scale-codec", @@ -3114,7 +3155,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8bd5c8c127b9362a12ffb9dede38e792c81b4ded5a98b448baec157b745f47d1" dependencies = [ "hashbrown 0.14.5", - "indexmap 2.2.6", + "indexmap 2.6.0", "itertools 0.12.1", "num-bigint", "num-traits 0.2.19", @@ -3526,6 +3567,20 @@ dependencies = [ "parking_lot_core 0.9.10", ] +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core 0.9.10", +] + [[package]] name = "data-encoding" version = "2.6.0" @@ -3576,6 +3631,27 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "unicode-xid", +] + [[package]] name = "diff" version = "0.1.13" @@ -3954,6 +4030,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + [[package]] name = "foreign-types" version = "0.3.2" @@ -4230,7 +4312,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68a7f542ee6b35af73b06abc0dad1c1bae89964e4e253bc4b587b91c9637867b" dependencies = [ "cfg-if", - "dashmap", + "dashmap 5.5.3", "futures", "futures-timer", "no-std-compat", @@ -4266,7 +4348,7 @@ dependencies = [ "futures-sink", "futures-util", "http 0.2.12", - "indexmap 2.2.6", + "indexmap 2.6.0", "slab", "tokio", "tokio-util", @@ -4285,7 +4367,7 @@ dependencies = [ "futures-core", "futures-sink", "http 1.1.0", - "indexmap 2.2.6", + "indexmap 2.6.0", "slab", "tokio", "tokio-util", @@ -4319,6 +4401,16 @@ dependencies = [ "serde", ] +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +dependencies = [ + "foldhash", + "serde", +] + [[package]] name = "heck" version = "0.3.3" @@ -4599,7 +4691,7 @@ dependencies = [ "pin-project-lite", "socket2 0.5.7", "tokio", - "tower", + "tower 0.4.13", "tower-service", "tracing", ] @@ -4842,12 +4934,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.6" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.14.5", + "hashbrown 0.15.0", "serde", ] @@ -5009,7 +5101,7 @@ dependencies = [ "jsonrpsee-types", "parking_lot 0.12.3", "rand", - "rustc-hash", + "rustc-hash 1.1.0", "serde", "serde_json", "thiserror", @@ -5050,7 +5142,7 @@ dependencies = [ "tokio", "tokio-stream", "tokio-util", - "tower", + "tower 0.4.13", "tracing", ] @@ -5382,7 +5474,7 @@ dependencies = [ "starknet_api", "thiserror", "tokio", - "tower", + "tower 0.4.13", "tower-http", "url", ] @@ -6413,7 +6505,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ "fixedbitset", - "indexmap 2.2.6", + "indexmap 2.6.0", ] [[package]] @@ -6646,27 +6738,25 @@ dependencies = [ ] [[package]] -name = "proc-macro-error" -version = "1.0.4" +name = "proc-macro-error-attr2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" dependencies = [ - "proc-macro-error-attr", "proc-macro2", "quote", - "syn 1.0.109", - "version_check", ] [[package]] -name = "proc-macro-error-attr" -version = "1.0.4" +name = "proc-macro-error2" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" dependencies = [ + "proc-macro-error-attr2", "proc-macro2", "quote", - "version_check", + "syn 2.0.66", ] [[package]] @@ -6775,6 +6865,7 @@ dependencies = [ "libc", "rand_chacha", "rand_core", + "serde", ] [[package]] @@ -7179,6 +7270,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hash" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" + [[package]] name = "rustc-hex" version = "2.1.0" @@ -7337,7 +7434,7 @@ dependencies = [ "log", "oorandom", "parking_lot 0.11.2", - "rustc-hash", + "rustc-hash 1.1.0", "salsa-macros", "smallvec", ] @@ -7629,7 +7726,7 @@ dependencies = [ "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.2.6", + "indexmap 2.6.0", "serde", "serde_derive", "serde_json", @@ -7667,7 +7764,7 @@ version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.6.0", "itoa", "ryu", "serde", @@ -8211,9 +8308,9 @@ checksum = "1b505c9c076d9fce854304bd743c93ea540ebea6b16ec96819b07343a3aa2c7c" dependencies = [ "bitvec", "cairo-lang-starknet-classes", - "derive_more", + "derive_more 0.99.18", "hex", - "indexmap 2.2.6", + "indexmap 2.6.0", "itertools 0.12.1", "once_cell", "primitive-types", @@ -8426,9 +8523,9 @@ dependencies = [ [[package]] name = "syn-solidity" -version = "0.7.7" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c837dc8852cb7074e46b444afb81783140dab12c58867b49fb3898fbafedf7ea" +checksum = "f3a850d65181df41b83c6be01a7d91f5e9377c43d48faa5af7d95816f437f5a3" dependencies = [ "paste", "proc-macro2", @@ -8773,7 +8870,7 @@ version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.6.0", "toml_datetime", "winnow 0.5.40", ] @@ -8784,7 +8881,7 @@ version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.6.0", "serde", "serde_spanned", "toml_datetime", @@ -8807,6 +8904,20 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 0.1.2", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-http" version = "0.4.4" @@ -8827,15 +8938,15 @@ dependencies = [ [[package]] name = "tower-layer" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" [[package]] name = "tower-service" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" diff --git a/Cargo.toml b/Cargo.toml index 48aa3bf90..a70532ca0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,7 +128,7 @@ starknet_api = "=0.13.0-rc.1" cairo-lang-starknet-classes = "=2.7.0" cairo-lang-utils = "=2.7.0" -alloy = { version = "0.2.0", features = [ +alloy = { version = "0.4.0", features = [ "node-bindings", "rpc-types", "provider-http", diff --git a/crates/client/eth/src/client.rs b/crates/client/eth/src/client.rs index ebf9629f1..7e9b85d8f 100644 --- a/crates/client/eth/src/client.rs +++ b/crates/client/eth/src/client.rs @@ -67,6 +67,8 @@ impl EthereumClient { pub async fn new(url: Url, l1_core_address: Address, l1_block_metrics: L1BlockMetrics) -> anyhow::Result { let provider = ProviderBuilder::new().on_http(url); + log::debug!("core contract address over here is: {:?}", l1_core_address); + EthereumClient::assert_core_contract_exists(&provider, l1_core_address).await?; let core_contract = StarknetCoreContract::new(l1_core_address, provider.clone()); diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index 22658afe9..d7d1b2623 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -7,7 +7,8 @@ use std::time::Duration; use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; -use alloy::primitives::{keccak256, FixedBytes, U256}; +use alloy::primitives::{keccak256, FixedBytes, U256, b256, address}; +use alloy::rpc::types::Filter; use alloy::sol_types::SolValue; use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; use log::Level::Debug; @@ -17,9 +18,12 @@ use starknet_api::core::{ChainId, ContractAddress, EntryPointSelector, Nonce}; use starknet_api::transaction::{ Calldata, Fee, L1HandlerTransaction, Transaction, TransactionHash, TransactionVersion, }; +use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; use url::ParseError::SetHostOnCannotBeABaseUrl; +use mc_mempool::Mempool; +use alloy::providers::Provider; impl EthereumClient { /// Get cancellation status of an L1 to L2 message @@ -42,7 +46,7 @@ impl EthereumClient { } } -pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &ChainId) -> anyhow::Result<()> { +pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &ChainId, mempool: Arc) -> anyhow::Result<()> { tracing::info!("⟠ Starting L1 Messages Syncing..."); let last_synced_event_block = match backend.messaging_last_synced_l1_block_with_event() { @@ -55,15 +59,30 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C return Err(e.into()); } }; + log::debug!("we are inside sync and we will be calling the event filter now and last synced event block is: {:?}", last_synced_event_block); let event_filter = client.l1_core_contract.event_filter::(); + log::debug!("event filter here is: {:?}", event_filter); + + let address = client.l1_core_contract.address().clone(); + let transfer_event_signature = + b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b"); + let filter = Filter::new().event_signature(transfer_event_signature).from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number)).address(address); + // You could also use the event name instead of the event signature like so: + // .event("Transfer(address,address,uint256)") + + // Get all logs from the latest block that match the filter. + let logs = client.provider.get_logs(&filter).await?; + + for log in logs { + log::debug!("Transfer event: {log:?}"); + } let mut event_stream = event_filter .from_block(last_synced_event_block.block_number) - .select(BlockNumberOrTag::Finalized) .watch() .await .context("Failed to watch event filter")? .into_stream(); - + // log::debug!("event stream here is: {:?}", event_stream); while let Some(event_result) = channel_wait_or_graceful_shutdown(event_stream.next()).await { log::debug!("inside the l1 messages"); if let Ok((event, meta)) = event_result { @@ -97,7 +116,7 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C continue; } - match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id).await { + match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id, mempool.clone()).await { Ok(Some(tx_hash)) => { tracing::info!( "⟠ L1 Message from block: {:?}, transaction_hash: {:?}, log_index: {:?} submitted, \ @@ -132,6 +151,7 @@ async fn process_l1_message( l1_block_number: &Option, event_index: &Option, chain_id: &ChainId, + mempool: Arc ) -> anyhow::Result> { let transaction = parse_handle_l1_message_transaction(event)?; let tx_nonce = transaction.nonce; @@ -152,9 +172,10 @@ async fn process_l1_message( }; let tx_hash = get_transaction_hash(&Transaction::L1Handler(transaction.clone()), chain_id, &transaction.version)?; - let blockifier_transaction: BlockifierL1HandlerTransaction = + let blockifier_transaction = BlockifierL1HandlerTransaction { tx: transaction.clone(), tx_hash, paid_fee_on_l1: Fee(event.fee.try_into()?) }; + mempool.accept_tx(BlockifierTransation::L1HandlerTransaction(blockifier_transaction), None); // TODO: submit tx to mempool // TODO: remove unwraps @@ -162,7 +183,7 @@ async fn process_l1_message( backend.messaging_update_last_synced_l1_block_with_event(block_sent)?; // TODO: replace by tx hash from mempool - Ok(Some(blockifier_transaction.tx_hash)) + Ok(Some(tx_hash)) } pub fn parse_handle_l1_message_transaction(event: &LogMessageToL2) -> anyhow::Result { @@ -241,7 +262,7 @@ mod l1_messaging_tests { use tempfile::TempDir; use tracing_test::traced_test; use url::Url; - + use mc_mempool::Mempool; use crate::l1_messaging::sync; use self::DummyContract::DummyContractInstance; @@ -253,6 +274,7 @@ mod l1_messaging_tests { db_service: Arc, dummy_contract: DummyContractInstance, RootProvider>>, eth_client: EthereumClient, + mempool: Arc } // LogMessageToL2 from https://etherscan.io/tx/0x21980d6674d33e50deee43c6c30ef3b439bd148249b4539ce37b7856ac46b843 diff --git a/crates/client/eth/src/state_update.rs b/crates/client/eth/src/state_update.rs index f8ef4b361..a5fc6095f 100644 --- a/crates/client/eth/src/state_update.rs +++ b/crates/client/eth/src/state_update.rs @@ -9,7 +9,9 @@ use mc_db::MadaraBackend; use mp_transactions::MAIN_CHAIN_ID; use mp_utils::channel_wait_or_graceful_shutdown; use serde::Deserialize; +use starknet_api::core::ChainId; use starknet_types_core::felt::Felt; +use mp_convert::ToFelt; #[derive(Debug, Clone, Deserialize, PartialEq)] pub struct L1StateUpdate { @@ -33,7 +35,7 @@ pub async fn listen_and_update_state( eth_client: &EthereumClient, backend: &MadaraBackend, block_metrics: &L1BlockMetrics, - chain_id: Felt, + chain_id: ChainId, ) -> anyhow::Result<()> { let event_filter = eth_client.l1_core_contract.event_filter::(); @@ -43,7 +45,7 @@ pub async fn listen_and_update_state( let log = event_result.context("listening for events")?; let format_event: L1StateUpdate = convert_log_state_update(log.0.clone()).context("formatting event into an L1StateUpdate")?; - update_l1(backend, format_event, block_metrics, chain_id)?; + update_l1(backend, format_event, block_metrics, chain_id.clone())?; } Ok(()) @@ -53,12 +55,12 @@ pub fn update_l1( backend: &MadaraBackend, state_update: L1StateUpdate, block_metrics: &L1BlockMetrics, - chain_id: Felt, + chain_id: ChainId, ) -> anyhow::Result<()> { // This is a provisory check to avoid updating the state with an L1StateUpdate that should not have been detected // // TODO: Remove this check when the L1StateUpdate is properly verified - if state_update.block_number > 500000u64 || chain_id == MAIN_CHAIN_ID { + if state_update.block_number > 500000u64 || chain_id.to_felt() == MAIN_CHAIN_ID { log::info!( "🔄 Updated L1 head #{} ({}) with state root ({})", state_update.block_number, @@ -80,7 +82,7 @@ pub fn update_l1( pub async fn state_update_worker( backend: &MadaraBackend, eth_client: &EthereumClient, - chain_id: Felt, + chain_id: ChainId, ) -> anyhow::Result<()> { // Clear L1 confirmed block at startup backend.clear_last_confirmed_block().context("Clearing l1 last confirmed block number")?; @@ -90,7 +92,7 @@ pub async fn state_update_worker( // ideally here there would be one service which will update the l1 gas prices and another one for messages and one that's already present is state update // Get and store the latest verified state let initial_state = get_initial_state(eth_client).await.context("Getting initial ethereum state")?; - update_l1(backend, initial_state, ð_client.l1_block_metrics, chain_id)?; + update_l1(backend, initial_state, ð_client.l1_block_metrics, chain_id.clone())?; // Listen to LogStateUpdate (0x77552641) update and send changes continusly listen_and_update_state(eth_client, backend, ð_client.l1_block_metrics, chain_id) @@ -190,7 +192,7 @@ mod eth_client_event_subscription_test { ð_client, db.backend(), ð_client.l1_block_metrics, - chain_info.chain_id.clone().to_felt(), + chain_info.chain_id.clone(), ) .await }) diff --git a/crates/client/eth/src/sync.rs b/crates/client/eth/src/sync.rs index d730adf27..73dddb4b5 100644 --- a/crates/client/eth/src/sync.rs +++ b/crates/client/eth/src/sync.rs @@ -1,26 +1,34 @@ +use std::sync::Arc; use crate::client::EthereumClient; use crate::l1_gas_price::gas_price_worker; use crate::state_update::state_update_worker; -use mc_mempool::GasPriceProvider; +use mc_mempool::{GasPriceProvider, Mempool}; use starknet_types_core::felt::Felt; use std::time::Duration; +use starknet_api::core::ChainId; +use crate::l1_messaging::sync; use mc_db::MadaraBackend; pub async fn l1_sync_worker( backend: &MadaraBackend, eth_client: &EthereumClient, - chain_id: Felt, + chain_id: ChainId, l1_gas_provider: GasPriceProvider, gas_price_sync_disabled: bool, gas_price_poll_ms: Duration, + mempool: Arc ) -> anyhow::Result<()> { - tokio::try_join!(state_update_worker(backend, eth_client, chain_id), async { - if !gas_price_sync_disabled { - gas_price_worker(eth_client, l1_gas_provider, gas_price_poll_ms).await?; - } - Ok(()) - })?; + tokio::try_join!( + state_update_worker(backend, eth_client, chain_id.clone()), + async { + if !gas_price_sync_disabled { + gas_price_worker(eth_client, l1_gas_provider, gas_price_poll_ms).await?; + } + Ok(()) + }, + sync(backend, eth_client, &chain_id, mempool) + )?; Ok(()) } diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 46fc3bf77..336cb63a8 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -6,7 +6,7 @@ //! TODO: mempool size limits //! TODO(perf): should we box the MempoolTransaction? -use crate::{clone_account_tx, contract_addr, nonce, tx_hash}; +use crate::{clone_transaction, contract_addr, nonce, tx_hash}; use blockifier::transaction::account_transaction::AccountTransaction; use mp_class::ConvertedClass; use starknet_api::{ @@ -19,12 +19,13 @@ use std::{ iter, time::SystemTime, }; +use blockifier::transaction::transaction_execution::Transaction; pub type ArrivedAtTimestamp = SystemTime; #[derive(Debug)] pub struct MempoolTransaction { - pub tx: AccountTransaction, + pub tx: Transaction, pub arrived_at: ArrivedAtTimestamp, pub converted_class: Option, } @@ -32,7 +33,7 @@ pub struct MempoolTransaction { impl Clone for MempoolTransaction { fn clone(&self) -> Self { Self { - tx: clone_account_tx(&self.tx), + tx: clone_transaction(&self.tx), arrived_at: self.arrived_at, converted_class: self.converted_class.clone(), } diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index 031fabbd9..ee0a6804f 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -1,7 +1,7 @@ use blockifier::blockifier::stateful_validator::StatefulValidatorError; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transaction_execution::Transaction; -use blockifier::transaction::transactions::DeclareTransaction; +use blockifier::transaction::transactions::{DeclareTransaction, L1HandlerTransaction}; use blockifier::transaction::transactions::DeployAccountTransaction; use blockifier::transaction::transactions::InvokeTransaction; use header::make_pending_header; @@ -90,8 +90,8 @@ impl Mempool { Mempool { backend, l1_data_provider, inner: Default::default() } } - fn accept_tx(&self, tx: Transaction, converted_class: Option) -> Result<(), Error> { - let Transaction::AccountTransaction(tx) = tx else { panic!("L1HandlerTransaction not supported yet") }; + pub fn accept_tx(&self, tx: Transaction, converted_class: Option) -> Result<(), Error> { + // let Transaction::AccountTransaction(tx) = tx else { panic!("L1HandlerTransaction not supported yet") }; log::debug!("Checkpoint: we are inside the accept tx and we made sure that we have account txn only"); @@ -119,7 +119,7 @@ impl Mempool { // If the contract has been deployed for the same block is is invoked, we need to skip validations. // NB: the lock is NOT taken the entire time the tx is being validated. As such, the deploy tx // may appear during that time - but it is not a problem. - let deploy_account_tx_hash = if let AccountTransaction::Invoke(tx) = &tx { + let deploy_account_tx_hash = if let Transaction::AccountTransaction(AccountTransaction::Invoke(tx)) = &tx { let mempool = self.inner.read().expect("Poisoned lock"); if mempool.has_deployed_contract(&tx.tx.sender_address()) { Some(tx.tx_hash) // we return the wrong tx hash here but it's ok because the actual hash is unused by blockifier @@ -134,7 +134,10 @@ impl Mempool { // Perform validations let exec_context = ExecutionContext::new_in_block(Arc::clone(&self.backend), &pending_block_info)?; let mut validator = exec_context.tx_validator(); - let _ = validator.perform_validations(clone_account_tx(&tx), deploy_account_tx_hash.is_some()); + + if let Transaction::AccountTransaction(account_tx) = clone_transaction(&tx) { + let _ = validator.perform_validations(account_tx, deploy_account_tx_hash.is_some()); + } log::debug!("Checkpoint: validation performed"); @@ -150,6 +153,7 @@ impl Mempool { Ok(()) } + } pub fn transaction_hash(tx: &Transaction) -> Felt { @@ -266,11 +270,14 @@ impl MempoolProvider for Mempool { } } -pub(crate) fn is_only_query(tx: &AccountTransaction) -> bool { +pub(crate) fn is_only_query(tx: &Transaction) -> bool { match tx { - AccountTransaction::Declare(tx) => tx.only_query(), - AccountTransaction::DeployAccount(tx) => tx.only_query, - AccountTransaction::Invoke(tx) => tx.only_query, + Transaction::AccountTransaction(account_tx) => match account_tx { + AccountTransaction::Declare(tx) => tx.only_query(), + AccountTransaction::DeployAccount(tx) => tx.only_query, + AccountTransaction::Invoke(tx) => tx.only_query, + }, + Transaction::L1HandlerTransaction(_) => false, } } @@ -290,35 +297,43 @@ pub(crate) fn nonce(tx: &AccountTransaction) -> Nonce { } } -pub(crate) fn tx_hash(tx: &AccountTransaction) -> TransactionHash { +pub(crate) fn tx_hash(tx: &Transaction) -> TransactionHash { match tx { - AccountTransaction::Declare(tx) => tx.tx_hash, - AccountTransaction::DeployAccount(tx) => tx.tx_hash, - AccountTransaction::Invoke(tx) => tx.tx_hash, + Transaction::AccountTransaction(account_tx) => match account_tx { + AccountTransaction::Declare(tx) => tx.tx_hash, + AccountTransaction::DeployAccount(tx) => tx.tx_hash, + AccountTransaction::Invoke(tx) => tx.tx_hash, + }, + Transaction::L1HandlerTransaction(tx) => tx.tx_hash, } } // AccountTransaction does not implement Clone :( -pub(crate) fn clone_account_tx(tx: &AccountTransaction) -> AccountTransaction { - match tx { - // Declare has a private field :( - AccountTransaction::Declare(tx) => AccountTransaction::Declare(match tx.only_query() { - // These should never fail - true => DeclareTransaction::new_for_query(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) - .expect("Making blockifier transaction for query"), - false => DeclareTransaction::new(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) - .expect("Making blockifier transaction"), - }), - AccountTransaction::DeployAccount(tx) => AccountTransaction::DeployAccount(DeployAccountTransaction { - tx: tx.tx.clone(), - tx_hash: tx.tx_hash, - contract_address: tx.contract_address, - only_query: tx.only_query, - }), - AccountTransaction::Invoke(tx) => AccountTransaction::Invoke(InvokeTransaction { - tx: tx.tx.clone(), - tx_hash: tx.tx_hash, - only_query: tx.only_query, - }), + pub(crate) fn clone_transaction(tx: &Transaction) -> Transaction { + match tx { + Transaction::AccountTransaction(account_tx) => Transaction::AccountTransaction(match account_tx { + AccountTransaction::Declare(tx) => AccountTransaction::Declare(match tx.only_query() { + true => DeclareTransaction::new_for_query(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) + .expect("Making blockifier transaction for query"), + false => DeclareTransaction::new(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) + .expect("Making blockifier transaction"), + }), + AccountTransaction::DeployAccount(tx) => AccountTransaction::DeployAccount(DeployAccountTransaction { + tx: tx.tx.clone(), + tx_hash: tx.tx_hash, + contract_address: tx.contract_address, + only_query: tx.only_query, + }), + AccountTransaction::Invoke(tx) => AccountTransaction::Invoke(InvokeTransaction { + tx: tx.tx.clone(), + tx_hash: tx.tx_hash, + only_query: tx.only_query, + }), + }), + Transaction::L1HandlerTransaction(tx) => Transaction::L1HandlerTransaction(L1HandlerTransaction { + tx: tx.tx.clone(), + tx_hash: tx.tx_hash, + paid_fee_on_l1: tx.paid_fee_on_l1 + }), + } } -} diff --git a/crates/node/src/main.rs b/crates/node/src/main.rs index e9a516a5a..d8cab33e7 100644 --- a/crates/node/src/main.rs +++ b/crates/node/src/main.rs @@ -99,6 +99,9 @@ async fn main() -> anyhow::Result<()> { run_cmd.l1_sync_params.gas_price_sync_disabled = true; } + // declare mempool here so that it can be used to process l1->l2 messages in the l1 service + let mempool = Arc::new(Mempool::new(Arc::clone(db_service.backend()), Arc::clone(&l1_data_provider))); + let l1_service = L1SyncService::new( &run_cmd.l1_sync_params, &db_service, @@ -107,6 +110,7 @@ async fn main() -> anyhow::Result<()> { chain_config.chain_id.clone(), chain_config.eth_core_contract_address, run_cmd.is_sequencer(), + Arc::clone(&mempool) ) .await .context("Initializing the l1 sync service")?; @@ -117,7 +121,7 @@ async fn main() -> anyhow::Result<()> { match run_cmd.is_sequencer() { // Block production service. (authority) true => { - let mempool = Arc::new(Mempool::new(Arc::clone(db_service.backend()), Arc::clone(&l1_data_provider))); + // let mempool = Arc::new(Mempool::new(Arc::clone(db_service.backend()), Arc::clone(&l1_data_provider))); let block_production_service = BlockProductionService::new( &run_cmd.block_production_params, diff --git a/crates/node/src/service/l1.rs b/crates/node/src/service/l1.rs index f60616ce6..5567d1058 100644 --- a/crates/node/src/service/l1.rs +++ b/crates/node/src/service/l1.rs @@ -3,7 +3,7 @@ use alloy::primitives::Address; use anyhow::Context; use mc_db::{DatabaseService, MadaraBackend}; use mc_eth::client::{EthereumClient, L1BlockMetrics}; -use mc_mempool::GasPriceProvider; +use mc_mempool::{GasPriceProvider, Mempool}; use mc_metrics::MetricsRegistry; use mp_block::H160; use mp_convert::ToFelt; @@ -21,6 +21,7 @@ pub struct L1SyncService { chain_id: ChainId, gas_price_sync_disabled: bool, gas_price_poll: Duration, + mempool: Arc } impl L1SyncService { @@ -32,6 +33,7 @@ impl L1SyncService { chain_id: ChainId, l1_core_address: H160, authority: bool, + mempool: Arc ) -> anyhow::Result { let eth_client = if !config.sync_l1_disabled { if let Some(l1_rpc_url) = &config.l1_endpoint { @@ -73,6 +75,7 @@ impl L1SyncService { chain_id, gas_price_sync_disabled: !gas_price_sync_enabled, gas_price_poll, + mempool }) } } @@ -80,7 +83,7 @@ impl L1SyncService { #[async_trait::async_trait] impl Service for L1SyncService { async fn start(&mut self, join_set: &mut JoinSet>) -> anyhow::Result<()> { - let L1SyncService { l1_gas_provider, chain_id, gas_price_sync_disabled, gas_price_poll, .. } = self.clone(); + let L1SyncService { l1_gas_provider, chain_id, gas_price_sync_disabled, gas_price_poll, mempool, .. } = self.clone(); if let Some(eth_client) = self.eth_client.take() { // enabled @@ -90,10 +93,11 @@ impl Service for L1SyncService { mc_eth::sync::l1_sync_worker( &db_backend, ð_client, - chain_id.to_felt(), + chain_id, l1_gas_provider, gas_price_sync_disabled, gas_price_poll, + mempool ) .await }); diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index b8f651933..a258c5f02 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -214,7 +214,7 @@ impl ChainConfig { sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), block_time: Duration::from_secs(8), pending_block_update_time: Duration::from_secs(2), - eth_core_contract_address: "0x5fc8d32690cc91d4c39d9d3abcbd16989f875707".parse().expect("parsing a constant"), + eth_core_contract_address: "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512".parse().expect("parsing a constant"), ..ChainConfig::starknet_sepolia() } } From 62e174fe99ad8c8915e29b27d112391aef840474 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Wed, 9 Oct 2024 19:54:19 +0530 Subject: [PATCH 06/25] feat: refactored Mempool to accept L1Handler txn --- crates/client/mempool/src/block_production.rs | 8 +-- crates/client/mempool/src/inner.rs | 6 +-- crates/client/mempool/src/lib.rs | 24 +++++---- .../primitives/receipt/src/from_blockifier.rs | 54 ++++++++++++++----- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index a8aa68f59..b3104639a 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -28,7 +28,7 @@ use std::time::Instant; use crate::close_block::close_block; use crate::header::make_pending_header; -use crate::{clone_account_tx, L1DataProvider, MempoolProvider, MempoolTransaction}; +use crate::{clone_transaction, L1DataProvider, MempoolProvider, MempoolTransaction}; #[derive(Default, Clone)] struct ContinueBlockStats { @@ -249,7 +249,7 @@ impl BlockProductionTask { stats.n_batches += 1; txs_to_process_blockifier - .extend(txs_to_process.iter().map(|tx| Transaction::AccountTransaction(clone_account_tx(&tx.tx)))); + .extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); log::debug!("just before executing the transactions"); // Execute the transactions. @@ -275,9 +275,9 @@ impl BlockProductionTask { self.block.inner.receipts.push(from_blockifier_execution_info( &execution_info, - &Transaction::AccountTransaction(clone_account_tx(&mempool_tx.tx)), + &clone_transaction(&mempool_tx.tx), )); - let converted_tx = TransactionWithHash::from(clone_account_tx(&mempool_tx.tx)); // TODO: too many tx clones! + let converted_tx = TransactionWithHash::from(clone_transaction(&mempool_tx.tx)); // TODO: too many tx clones! self.block.info.tx_hashes.push(converted_tx.hash); self.block.inner.transactions.push(converted_tx.transaction); } diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 336cb63a8..9c5ef74d5 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -219,7 +219,7 @@ impl MempoolInner { debug_assert!(tx_queue.is_empty()); let mut deployed_contracts = self.deployed_contracts.clone(); for contract in self.nonce_chains.values().flat_map(|chain| &chain.transactions) { - if let AccountTransaction::DeployAccount(tx) = &contract.0.tx { + if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &contract.0.tx { debug_assert!(deployed_contracts.remove(&tx.contract_address)) }; } @@ -239,7 +239,7 @@ impl MempoolInner { let arrived_at = mempool_tx.arrived_at; let deployed_contract_address = - if let AccountTransaction::DeployAccount(tx) = &mempool_tx.tx { Some(tx.contract_address) } else { None }; + if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx { Some(tx.contract_address) } else { None }; if let Some(contract_address) = &deployed_contract_address { if !self.deployed_contracts.insert(*contract_address) && !force { @@ -325,7 +325,7 @@ impl MempoolInner { } // Update deployed contracts. - if let AccountTransaction::DeployAccount(tx) = &mempool_tx.tx { + if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx { let removed = self.deployed_contracts.remove(&tx.contract_address); debug_assert!(removed); } diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index ee0a6804f..a65557a2a 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -281,19 +281,25 @@ pub(crate) fn is_only_query(tx: &Transaction) -> bool { } } -pub(crate) fn contract_addr(tx: &AccountTransaction) -> ContractAddress { +pub(crate) fn contract_addr(tx: &Transaction) -> ContractAddress { match tx { - AccountTransaction::Declare(tx) => tx.tx.sender_address(), - AccountTransaction::DeployAccount(tx) => tx.contract_address, - AccountTransaction::Invoke(tx) => tx.tx.sender_address(), + Transaction::AccountTransaction(account_tx) => (match account_tx { + AccountTransaction::Declare(tx) => tx.tx.sender_address(), + AccountTransaction::DeployAccount(tx) => tx.contract_address, + AccountTransaction::Invoke(tx) => tx.tx.sender_address(), + }), + Transaction::L1HandlerTransaction(tx) => tx.tx.contract_address, } } -pub(crate) fn nonce(tx: &AccountTransaction) -> Nonce { +pub(crate) fn nonce(tx: &Transaction) -> Nonce { match tx { - AccountTransaction::Declare(tx) => tx.tx.nonce(), - AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), - AccountTransaction::Invoke(tx) => tx.tx.nonce(), + Transaction::AccountTransaction(account_tx) => (match account_tx { + AccountTransaction::Declare(tx) => tx.tx.nonce(), + AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), + AccountTransaction::Invoke(tx) => tx.tx.nonce(), + }), + Transaction::L1HandlerTransaction(tx) => tx.tx.nonce, } } @@ -336,4 +342,4 @@ pub(crate) fn tx_hash(tx: &Transaction) -> TransactionHash { paid_fee_on_l1: tx.paid_fee_on_l1 }), } - } + } \ No newline at end of file diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index 719baf443..6e6d5d272 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -5,12 +5,10 @@ use blockifier::transaction::{ transaction_execution::Transaction, }; use cairo_vm::types::builtin_name::BuiltinName; +use starknet_core::types::MsgToL2; use starknet_types_core::felt::Felt; -use crate::{ - DeclareTransactionReceipt, DeployAccountTransactionReceipt, Event, ExecutionResources, ExecutionResult, FeePayment, - InvokeTransactionReceipt, L1Gas, MsgToL1, PriceUnit, TransactionReceipt, -}; +use crate::{DeclareTransactionReceipt, DeployAccountTransactionReceipt, Event, ExecutionResources, ExecutionResult, FeePayment, InvokeTransactionReceipt, L1Gas, L1HandlerTransactionReceipt, MsgToL1, PriceUnit, TransactionReceipt}; fn blockifier_tx_fee_type(tx: &Transaction) -> FeeType { match tx { @@ -51,6 +49,35 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans }) }) .collect(); + + // if the txn type is L1HandlerTransaction, then the message hash is a function call of l1_handler_tx + // let message: MsgToL2 = match tx { + // Transaction::L1HandlerTransaction(tx) => MsgToL2 { + // nonce: tx.tx.nonce.into(), + // payload: tx.tx.calldata.0, + // from_address: tx.tx.contract_address, + // to_address: tx.tx.to_address, + // selector: tx.tx.entry_point_selector + // }, + // _ => todo!(), + // }; + + // pub fn parse_msg_to_l2(&self) -> Result { + // self.calldata.split_first().map_or( + // Err(ParseMsgToL2Error::EmptyCalldata), + // |(from_address, payload)| { + // Ok(MsgToL2 { + // from_address: (*from_address) + // .try_into() + // .map_err(|_| ParseMsgToL2Error::FromAddressOutOfRange)?, + // to_address: self.contract_address, + // selector: self.entry_point_selector, + // payload: payload.into(), + // nonce: self.nonce, + // }) + // }, + // ) + // } let get_applications = |resource| { res.non_optional_call_infos() @@ -113,16 +140,15 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans execution_result, }) } - Transaction::L1HandlerTransaction(_tx) => unimplemented!("l1 handler tx"), - // Transaction::L1HandlerTransactionv(tx) => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { - // transaction_hash, - // actual_fee, - // messages_sent, - // events, - // execution_resources, - // execution_result, - // message_hash: todo!(), - // }), + Transaction::L1HandlerTransaction(tx) => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { + transaction_hash, + actual_fee, + messages_sent, + events, + execution_resources, + execution_result, + message_hash: Felt::from(1) + }), } } From 4c16c8ec342420d7947f94e912f2cdf057c64814 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Thu, 10 Oct 2024 11:42:12 +0530 Subject: [PATCH 07/25] fixing test for l1 message and mempool inner --- crates/client/eth/src/l1_messaging.rs | 24 ++++++++++++++---------- crates/client/exec/src/lib.rs | 2 +- crates/client/mempool/src/inner.rs | 24 +++++++++++++++++------- crates/tests/src/lib.rs | 6 +++--- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index d7d1b2623..3c07f72fc 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -176,13 +176,11 @@ async fn process_l1_message( BlockifierL1HandlerTransaction { tx: transaction.clone(), tx_hash, paid_fee_on_l1: Fee(event.fee.try_into()?) }; mempool.accept_tx(BlockifierTransation::L1HandlerTransaction(blockifier_transaction), None); - // TODO: submit tx to mempool // TODO: remove unwraps let block_sent = LastSyncedEventBlock::new(l1_block_number.unwrap(), event_index.unwrap()); backend.messaging_update_last_synced_l1_block_with_event(block_sent)?; - // TODO: replace by tx hash from mempool Ok(Some(tx_hash)) } @@ -262,7 +260,7 @@ mod l1_messaging_tests { use tempfile::TempDir; use tracing_test::traced_test; use url::Url; - use mc_mempool::Mempool; + use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool}; use crate::l1_messaging::sync; use self::DummyContract::DummyContractInstance; @@ -375,6 +373,12 @@ mod l1_messaging_tests { .expect("Failed to create database service"), ); + + let l1_gas_setter = GasPriceProvider::new(); + let l1_data_provider: Arc = Arc::new(l1_gas_setter.clone()); + + let mempool = Arc::new(Mempool::new(Arc::clone(db.backend()), Arc::clone(&l1_data_provider))); + // Set up metrics service let prometheus_service = MetricsService::new(true, false, 9615).unwrap(); let l1_block_metrics = L1BlockMetrics::register(prometheus_service.registry()).unwrap(); @@ -394,7 +398,7 @@ mod l1_messaging_tests { l1_block_metrics: l1_block_metrics.clone(), }; - TestRunner { anvil, chain_config, db_service: db, dummy_contract: contract, eth_client } + TestRunner { anvil, chain_config, db_service: db, dummy_contract: contract, eth_client, mempool } } /// Test the basic workflow of l1 -> l2 messaging @@ -413,13 +417,13 @@ mod l1_messaging_tests { #[traced_test] #[tokio::test] async fn e2e_test_basic_workflow(#[future] setup_test_env: TestRunner) { - let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil } = + let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil, mempool } = setup_test_env.await; // Start worker let worker_handle = { let db = Arc::clone(&db); - tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id).await }) + tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id, mempool).await }) }; let _ = contract.setIsCanceled(false).send().await; @@ -465,13 +469,13 @@ mod l1_messaging_tests { #[traced_test] #[tokio::test] async fn e2e_test_already_processed_event(#[future] setup_test_env: TestRunner) { - let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil } = + let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil, mempool } = setup_test_env.await; // Start worker let worker_handle = { let db = Arc::clone(&db); - tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id).await }) + tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id, mempool).await }) }; let _ = contract.setIsCanceled(false).send().await; @@ -512,13 +516,13 @@ mod l1_messaging_tests { #[traced_test] #[tokio::test] async fn e2e_test_message_canceled(#[future] setup_test_env: TestRunner) { - let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil } = + let TestRunner { chain_config, db_service: db, dummy_contract: contract, eth_client, anvil: _anvil, mempool } = setup_test_env.await; // Start worker let worker_handle = { let db = Arc::clone(&db); - tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id).await }) + tokio::spawn(async move { sync(db.backend(), ð_client, &chain_config.chain_id, mempool).await }) }; // Mock cancelled message diff --git a/crates/client/exec/src/lib.rs b/crates/client/exec/src/lib.rs index 12ef6e9ac..2bc1880b7 100644 --- a/crates/client/exec/src/lib.rs +++ b/crates/client/exec/src/lib.rs @@ -13,7 +13,7 @@ use starknet_types_core::felt::Felt; mod block_context; mod blockifier_state_adapter; mod call; -mod execution; +pub mod execution; mod fee; mod trace; diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 9c5ef74d5..a720be4cb 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -351,7 +351,7 @@ mod tests { use blockifier::{ execution::contract_class::ClassInfo, test_utils::{contracts::FeatureContract, CairoVersion}, - transaction::transactions::{DeclareTransaction, InvokeTransaction}, + transaction::transactions::{DeclareTransaction, InvokeTransaction, L1HandlerTransaction}, }; use proptest::prelude::*; use proptest_derive::Arbitrary; @@ -360,9 +360,13 @@ mod tests { transaction::{DeclareTransactionV3, InvokeTransactionV3}, }; use starknet_types_core::felt::Felt; + use mc_exec::execution::TxInfo; use super::*; use std::fmt; + use starknet_api::transaction::Fee; + // use blockifier::transaction::transaction_execution::Transaction::L1HandlerTransaction; + // use starknet_api::transaction::L1HandlerTransaction; #[derive(PartialEq, Eq, Hash)] struct AFelt(Felt); @@ -409,6 +413,7 @@ mod tests { Declare, DeployAccount, InvokeFunction, + L1Handler, } <(TxTy, SystemTime, AFelt, AFelt, u64, bool)>::arbitrary() @@ -421,7 +426,7 @@ mod tests { let dummy_class_info = ClassInfo::new(&dummy_contract_class.get_class(), 100, 100).unwrap(); let tx = match ty { - TxTy::Declare => AccountTransaction::Declare( + TxTy::Declare => Transaction::AccountTransaction(AccountTransaction::Declare( DeclareTransaction::new( starknet_api::transaction::DeclareTransaction::V3(DeclareTransactionV3 { resource_bounds: Default::default(), @@ -440,8 +445,8 @@ mod tests { dummy_class_info, ) .unwrap(), - ), - TxTy::DeployAccount => AccountTransaction::Declare( + )), + TxTy::DeployAccount => Transaction::AccountTransaction(AccountTransaction::Declare( DeclareTransaction::new( starknet_api::transaction::DeclareTransaction::V3(DeclareTransactionV3 { resource_bounds: Default::default(), @@ -460,8 +465,8 @@ mod tests { dummy_class_info, ) .unwrap(), - ), - TxTy::InvokeFunction => AccountTransaction::Invoke(InvokeTransaction::new( + )), + TxTy::InvokeFunction => Transaction::AccountTransaction(AccountTransaction::Invoke(InvokeTransaction::new( starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 { resource_bounds: Default::default(), tip: Default::default(), @@ -475,7 +480,12 @@ mod tests { account_deployment_data: Default::default(), }), tx_hash, - )), + ))), + TxTy::L1Handler => Transaction::L1HandlerTransaction(L1HandlerTransaction::create_for_testing( + Fee(0), ContractAddress::from(1u16) + ) + + ), }; Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force) diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index d7ee38ccf..6abcec387 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -246,7 +246,7 @@ async fn madara_can_sync_a_few_blocks() { let mut node = cmd_builder.run(); node.wait_for_ready().await; - node.wait_for_sync_to(9).await; + node.wait_for_sync_to(19).await; println!("we have block till: {:?}", node.json_rpc().block_hash_and_number().await.unwrap()); @@ -254,8 +254,8 @@ async fn madara_can_sync_a_few_blocks() { node.json_rpc().block_hash_and_number().await.unwrap(), BlockHashAndNumber { // https://sepolia.voyager.online/block/0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c - block_hash: Felt::from_hex_unchecked("0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c"), - block_number: 9 + block_hash: Felt::from_hex_unchecked("0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5,"), + block_number: 19 } ); } From 50dcc7aa52bb7f6489ca6482370c18cf974f85ba Mon Sep 17 00:00:00 2001 From: mohiiit Date: Thu, 10 Oct 2024 11:47:39 +0530 Subject: [PATCH 08/25] formatting and linting --- crates/client/db/src/class_db.rs | 2 +- crates/client/eth/src/l1_messaging.rs | 53 ++++++----- crates/client/eth/src/state_update.rs | 2 +- crates/client/eth/src/sync.rs | 8 +- crates/client/mempool/src/block_production.rs | 19 ++-- crates/client/mempool/src/inner.rs | 52 ++++++----- crates/client/mempool/src/lib.rs | 88 +++++++++---------- .../rpc/src/providers/forward_to_provider.rs | 10 ++- crates/client/rpc/src/providers/mempool.rs | 8 +- crates/client/rpc/src/providers/mod.rs | 8 +- crates/client/rpc/src/test_utils.rs | 6 +- .../versions/v0_7_1/methods/read/get_nonce.rs | 1 - .../src/versions/v0_7_1/methods/write/mod.rs | 4 +- crates/node/src/main.rs | 2 +- crates/node/src/service/l1.rs | 11 +-- .../chain_config/src/chain_config.rs | 4 +- crates/primitives/class/src/lib.rs | 1 - .../primitives/receipt/src/from_blockifier.rs | 9 +- .../src/broadcasted_to_blockifier.rs | 28 +++--- .../src/from_broadcasted_transaction.rs | 17 ++-- crates/primitives/transactions/src/lib.rs | 8 +- 21 files changed, 182 insertions(+), 159 deletions(-) diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 564397cbe..851f88daf 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -121,7 +121,7 @@ impl MadaraBackend { ) -> Result<(), MadaraStorageError> { log::debug!("store classes has been called"); log::debug!("converted classes length: {:?}", converted_classes.len()); - + let mut writeopts = WriteOptions::new(); writeopts.disable_wal(true); diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index 3c07f72fc..d7b915b84 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -1,29 +1,29 @@ -use alloy::eips::BlockNumberOrTag; -use anyhow::Context; -use futures::StreamExt; -use std::sync::Arc; -use std::thread::sleep; -use std::time::Duration; use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; -use alloy::primitives::{keccak256, FixedBytes, U256, b256, address}; +use alloy::eips::BlockNumberOrTag; +use alloy::primitives::{address, b256, keccak256, FixedBytes, U256}; +use alloy::providers::Provider; use alloy::rpc::types::Filter; use alloy::sol_types::SolValue; +use anyhow::Context; +use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; +use futures::StreamExt; use log::Level::Debug; use mc_db::{l1_db::LastSyncedEventBlock, MadaraBackend}; +use mc_mempool::Mempool; use mp_utils::channel_wait_or_graceful_shutdown; use starknet_api::core::{ChainId, ContractAddress, EntryPointSelector, Nonce}; use starknet_api::transaction::{ Calldata, Fee, L1HandlerTransaction, Transaction, TransactionHash, TransactionVersion, }; -use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; +use std::sync::Arc; +use std::thread::sleep; +use std::time::Duration; use url::ParseError::SetHostOnCannotBeABaseUrl; -use mc_mempool::Mempool; -use alloy::providers::Provider; impl EthereumClient { /// Get cancellation status of an L1 to L2 message @@ -46,7 +46,12 @@ impl EthereumClient { } } -pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &ChainId, mempool: Arc) -> anyhow::Result<()> { +pub async fn sync( + backend: &MadaraBackend, + client: &EthereumClient, + chain_id: &ChainId, + mempool: Arc, +) -> anyhow::Result<()> { tracing::info!("⟠ Starting L1 Messages Syncing..."); let last_synced_event_block = match backend.messaging_last_synced_l1_block_with_event() { @@ -59,14 +64,19 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C return Err(e.into()); } }; - log::debug!("we are inside sync and we will be calling the event filter now and last synced event block is: {:?}", last_synced_event_block); + log::debug!( + "we are inside sync and we will be calling the event filter now and last synced event block is: {:?}", + last_synced_event_block + ); let event_filter = client.l1_core_contract.event_filter::(); log::debug!("event filter here is: {:?}", event_filter); let address = client.l1_core_contract.address().clone(); - let transfer_event_signature = - b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b"); - let filter = Filter::new().event_signature(transfer_event_signature).from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number)).address(address); + let transfer_event_signature = b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b"); + let filter = Filter::new() + .event_signature(transfer_event_signature) + .from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number)) + .address(address); // You could also use the event name instead of the event signature like so: // .event("Transfer(address,address,uint256)") @@ -116,7 +126,9 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C continue; } - match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id, mempool.clone()).await { + match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id, mempool.clone()) + .await + { Ok(Some(tx_hash)) => { tracing::info!( "⟠ L1 Message from block: {:?}, transaction_hash: {:?}, log_index: {:?} submitted, \ @@ -151,7 +163,7 @@ async fn process_l1_message( l1_block_number: &Option, event_index: &Option, chain_id: &ChainId, - mempool: Arc + mempool: Arc, ) -> anyhow::Result> { let transaction = parse_handle_l1_message_transaction(event)?; let tx_nonce = transaction.nonce; @@ -236,6 +248,7 @@ mod l1_messaging_tests { use std::{sync::Arc, time::Duration}; use super::Felt; + use crate::l1_messaging::sync; use crate::{ client::{ EthereumClient, L1BlockMetrics, @@ -253,6 +266,7 @@ mod l1_messaging_tests { transports::http::{Client, Http}, }; use mc_db::DatabaseService; + use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool}; use mc_metrics::{MetricsRegistry, MetricsService}; use mp_chain_config::ChainConfig; use rstest::*; @@ -260,8 +274,6 @@ mod l1_messaging_tests { use tempfile::TempDir; use tracing_test::traced_test; use url::Url; - use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool}; - use crate::l1_messaging::sync; use self::DummyContract::DummyContractInstance; @@ -272,7 +284,7 @@ mod l1_messaging_tests { db_service: Arc, dummy_contract: DummyContractInstance, RootProvider>>, eth_client: EthereumClient, - mempool: Arc + mempool: Arc, } // LogMessageToL2 from https://etherscan.io/tx/0x21980d6674d33e50deee43c6c30ef3b439bd148249b4539ce37b7856ac46b843 @@ -373,7 +385,6 @@ mod l1_messaging_tests { .expect("Failed to create database service"), ); - let l1_gas_setter = GasPriceProvider::new(); let l1_data_provider: Arc = Arc::new(l1_gas_setter.clone()); diff --git a/crates/client/eth/src/state_update.rs b/crates/client/eth/src/state_update.rs index a5fc6095f..548580b49 100644 --- a/crates/client/eth/src/state_update.rs +++ b/crates/client/eth/src/state_update.rs @@ -6,12 +6,12 @@ use crate::{ use anyhow::Context; use futures::StreamExt; use mc_db::MadaraBackend; +use mp_convert::ToFelt; use mp_transactions::MAIN_CHAIN_ID; use mp_utils::channel_wait_or_graceful_shutdown; use serde::Deserialize; use starknet_api::core::ChainId; use starknet_types_core::felt::Felt; -use mp_convert::ToFelt; #[derive(Debug, Clone, Deserialize, PartialEq)] pub struct L1StateUpdate { diff --git a/crates/client/eth/src/sync.rs b/crates/client/eth/src/sync.rs index 73dddb4b5..151c18f81 100644 --- a/crates/client/eth/src/sync.rs +++ b/crates/client/eth/src/sync.rs @@ -1,12 +1,12 @@ -use std::sync::Arc; use crate::client::EthereumClient; use crate::l1_gas_price::gas_price_worker; +use crate::l1_messaging::sync; use crate::state_update::state_update_worker; use mc_mempool::{GasPriceProvider, Mempool}; +use starknet_api::core::ChainId; use starknet_types_core::felt::Felt; +use std::sync::Arc; use std::time::Duration; -use starknet_api::core::ChainId; -use crate::l1_messaging::sync; use mc_db::MadaraBackend; @@ -17,7 +17,7 @@ pub async fn l1_sync_worker( l1_gas_provider: GasPriceProvider, gas_price_sync_disabled: bool, gas_price_poll_ms: Duration, - mempool: Arc + mempool: Arc, ) -> anyhow::Result<()> { tokio::try_join!( state_update_worker(backend, eth_client, chain_id.clone()), diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index 251e62a49..397599dd4 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -242,12 +242,8 @@ impl BlockProductionTask { if to_take > 0 { self.mempool.take_txs_chunk(/* extend */ &mut txs_to_process, batch_size); - txs_to_process_blockifier.extend( - txs_to_process - .iter() - .skip(cur_len) - .map(|tx| clone_transaction(&tx.tx)), - ); + txs_to_process_blockifier + .extend(txs_to_process.iter().skip(cur_len).map(|tx| clone_transaction(&tx.tx))); } if txs_to_process.is_empty() { @@ -257,8 +253,7 @@ impl BlockProductionTask { stats.n_batches += 1; - txs_to_process_blockifier - .extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); + txs_to_process_blockifier.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); // Execute the transactions. let all_results = self.executor.execute_txs(&txs_to_process_blockifier); @@ -284,10 +279,10 @@ impl BlockProductionTask { self.declared_classes.push(class); } - self.block.inner.receipts.push(from_blockifier_execution_info( - &execution_info, - &clone_transaction(&mempool_tx.tx), - )); + self.block + .inner + .receipts + .push(from_blockifier_execution_info(&execution_info, &clone_transaction(&mempool_tx.tx))); let converted_tx = TransactionWithHash::from(clone_transaction(&mempool_tx.tx)); // TODO: too many tx clones! self.block.info.tx_hashes.push(converted_tx.hash); self.block.inner.transactions.push(converted_tx.transaction); diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index a720be4cb..6d60e57fd 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -8,6 +8,7 @@ use crate::{clone_transaction, contract_addr, nonce, tx_hash}; use blockifier::transaction::account_transaction::AccountTransaction; +use blockifier::transaction::transaction_execution::Transaction; use mp_class::ConvertedClass; use starknet_api::{ core::{ContractAddress, Nonce}, @@ -19,7 +20,6 @@ use std::{ iter, time::SystemTime, }; -use blockifier::transaction::transaction_execution::Transaction; pub type ArrivedAtTimestamp = SystemTime; @@ -232,14 +232,17 @@ impl MempoolInner { log::debug!("Checkpoint: inside the insert tx"); - let contract_addr = mempool_tx.contract_address(); log::debug!("Checkpoint: contract address here is: {:?}", contract_addr); let arrived_at = mempool_tx.arrived_at; let deployed_contract_address = - if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx { Some(tx.contract_address) } else { None }; + if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx { + Some(tx.contract_address) + } else { + None + }; if let Some(contract_address) = &deployed_contract_address { if !self.deployed_contracts.insert(*contract_address) && !force { @@ -353,6 +356,7 @@ mod tests { test_utils::{contracts::FeatureContract, CairoVersion}, transaction::transactions::{DeclareTransaction, InvokeTransaction, L1HandlerTransaction}, }; + use mc_exec::execution::TxInfo; use proptest::prelude::*; use proptest_derive::Arbitrary; use starknet_api::{ @@ -360,11 +364,10 @@ mod tests { transaction::{DeclareTransactionV3, InvokeTransactionV3}, }; use starknet_types_core::felt::Felt; - use mc_exec::execution::TxInfo; use super::*; - use std::fmt; use starknet_api::transaction::Fee; + use std::fmt; // use blockifier::transaction::transaction_execution::Transaction::L1HandlerTransaction; // use starknet_api::transaction::L1HandlerTransaction; @@ -466,26 +469,27 @@ mod tests { ) .unwrap(), )), - TxTy::InvokeFunction => Transaction::AccountTransaction(AccountTransaction::Invoke(InvokeTransaction::new( - starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 { - resource_bounds: Default::default(), - tip: Default::default(), - signature: Default::default(), - nonce, - sender_address: contract_addr, - calldata: Default::default(), - nonce_data_availability_mode: DataAvailabilityMode::L1, - fee_data_availability_mode: DataAvailabilityMode::L1, - paymaster_data: Default::default(), - account_deployment_data: Default::default(), - }), - tx_hash, - ))), + TxTy::InvokeFunction => { + Transaction::AccountTransaction(AccountTransaction::Invoke(InvokeTransaction::new( + starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 { + resource_bounds: Default::default(), + tip: Default::default(), + signature: Default::default(), + nonce, + sender_address: contract_addr, + calldata: Default::default(), + nonce_data_availability_mode: DataAvailabilityMode::L1, + fee_data_availability_mode: DataAvailabilityMode::L1, + paymaster_data: Default::default(), + account_deployment_data: Default::default(), + }), + tx_hash, + ))) + } TxTy::L1Handler => Transaction::L1HandlerTransaction(L1HandlerTransaction::create_for_testing( - Fee(0), ContractAddress::from(1u16) - ) - - ), + Fee(0), + ContractAddress::from(1u16), + )), }; Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force) diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index a65557a2a..a99e59f3d 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -1,9 +1,9 @@ use blockifier::blockifier::stateful_validator::StatefulValidatorError; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transaction_execution::Transaction; -use blockifier::transaction::transactions::{DeclareTransaction, L1HandlerTransaction}; use blockifier::transaction::transactions::DeployAccountTransaction; use blockifier::transaction::transactions::InvokeTransaction; +use blockifier::transaction::transactions::{DeclareTransaction, L1HandlerTransaction}; use header::make_pending_header; use inner::MempoolInner; use mc_db::db_block_id::DbBlockId; @@ -14,8 +14,8 @@ use mp_block::BlockId; use mp_block::BlockTag; use mp_block::MadaraPendingBlockInfo; use mp_class::ConvertedClass; -use mp_transactions::{broadcasted_to_blockifier, BroadcastedDeclareTransactionV0, broadcasted_to_blockifier_v0}; use mp_transactions::BroadcastedToBlockifierError; +use mp_transactions::{broadcasted_to_blockifier, broadcasted_to_blockifier_v0, BroadcastedDeclareTransactionV0}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::transaction::TransactionHash; use starknet_core::types::BroadcastedDeclareTransaction; @@ -134,14 +134,13 @@ impl Mempool { // Perform validations let exec_context = ExecutionContext::new_in_block(Arc::clone(&self.backend), &pending_block_info)?; let mut validator = exec_context.tx_validator(); - + if let Transaction::AccountTransaction(account_tx) = clone_transaction(&tx) { let _ = validator.perform_validations(account_tx, deploy_account_tx_hash.is_some()); } log::debug!("Checkpoint: validation performed"); - if !is_only_query(&tx) { // Finally, add it to the nonce chain for the account nonce let force = false; @@ -153,7 +152,6 @@ impl Mempool { Ok(()) } - } pub fn transaction_hash(tx: &Transaction) -> Felt { @@ -196,15 +194,11 @@ impl MempoolProvider for Mempool { fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { log::debug!("Checkpoint 3: accept_declare_v0_tx"); - let (tx, classes) = broadcasted_to_blockifier_v0( - tx, - self.chain_id(), - self.backend.chain_config().latest_protocol_version, - )?; + let (tx, classes) = + broadcasted_to_blockifier_v0(tx, self.chain_id(), self.backend.chain_config().latest_protocol_version)?; log::debug!("Checkpoint declare v0 tx"); - let res = DeclareTransactionResult { transaction_hash: transaction_hash(&tx), class_hash: declare_class_hash(&tx).expect("Created transaction should be declare"), @@ -283,22 +277,26 @@ pub(crate) fn is_only_query(tx: &Transaction) -> bool { pub(crate) fn contract_addr(tx: &Transaction) -> ContractAddress { match tx { - Transaction::AccountTransaction(account_tx) => (match account_tx { - AccountTransaction::Declare(tx) => tx.tx.sender_address(), - AccountTransaction::DeployAccount(tx) => tx.contract_address, - AccountTransaction::Invoke(tx) => tx.tx.sender_address(), - }), + Transaction::AccountTransaction(account_tx) => { + (match account_tx { + AccountTransaction::Declare(tx) => tx.tx.sender_address(), + AccountTransaction::DeployAccount(tx) => tx.contract_address, + AccountTransaction::Invoke(tx) => tx.tx.sender_address(), + }) + } Transaction::L1HandlerTransaction(tx) => tx.tx.contract_address, } } pub(crate) fn nonce(tx: &Transaction) -> Nonce { match tx { - Transaction::AccountTransaction(account_tx) => (match account_tx { - AccountTransaction::Declare(tx) => tx.tx.nonce(), - AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), - AccountTransaction::Invoke(tx) => tx.tx.nonce(), - }), + Transaction::AccountTransaction(account_tx) => { + (match account_tx { + AccountTransaction::Declare(tx) => tx.tx.nonce(), + AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), + AccountTransaction::Invoke(tx) => tx.tx.nonce(), + }) + } Transaction::L1HandlerTransaction(tx) => tx.tx.nonce, } } @@ -315,31 +313,31 @@ pub(crate) fn tx_hash(tx: &Transaction) -> TransactionHash { } // AccountTransaction does not implement Clone :( - pub(crate) fn clone_transaction(tx: &Transaction) -> Transaction { - match tx { - Transaction::AccountTransaction(account_tx) => Transaction::AccountTransaction(match account_tx { - AccountTransaction::Declare(tx) => AccountTransaction::Declare(match tx.only_query() { - true => DeclareTransaction::new_for_query(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) - .expect("Making blockifier transaction for query"), - false => DeclareTransaction::new(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) - .expect("Making blockifier transaction"), - }), - AccountTransaction::DeployAccount(tx) => AccountTransaction::DeployAccount(DeployAccountTransaction { - tx: tx.tx.clone(), - tx_hash: tx.tx_hash, - contract_address: tx.contract_address, - only_query: tx.only_query, - }), - AccountTransaction::Invoke(tx) => AccountTransaction::Invoke(InvokeTransaction { - tx: tx.tx.clone(), - tx_hash: tx.tx_hash, - only_query: tx.only_query, - }), +pub(crate) fn clone_transaction(tx: &Transaction) -> Transaction { + match tx { + Transaction::AccountTransaction(account_tx) => Transaction::AccountTransaction(match account_tx { + AccountTransaction::Declare(tx) => AccountTransaction::Declare(match tx.only_query() { + true => DeclareTransaction::new_for_query(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) + .expect("Making blockifier transaction for query"), + false => DeclareTransaction::new(tx.tx.clone(), tx.tx_hash, tx.class_info.clone()) + .expect("Making blockifier transaction"), }), - Transaction::L1HandlerTransaction(tx) => Transaction::L1HandlerTransaction(L1HandlerTransaction { + AccountTransaction::DeployAccount(tx) => AccountTransaction::DeployAccount(DeployAccountTransaction { tx: tx.tx.clone(), tx_hash: tx.tx_hash, - paid_fee_on_l1: tx.paid_fee_on_l1 + contract_address: tx.contract_address, + only_query: tx.only_query, }), - } - } \ No newline at end of file + AccountTransaction::Invoke(tx) => AccountTransaction::Invoke(InvokeTransaction { + tx: tx.tx.clone(), + tx_hash: tx.tx_hash, + only_query: tx.only_query, + }), + }), + Transaction::L1HandlerTransaction(tx) => Transaction::L1HandlerTransaction(L1HandlerTransaction { + tx: tx.tx.clone(), + tx_hash: tx.tx_hash, + paid_fee_on_l1: tx.paid_fee_on_l1, + }), + } +} diff --git a/crates/client/rpc/src/providers/forward_to_provider.rs b/crates/client/rpc/src/providers/forward_to_provider.rs index 9d644ad76..baa1e6814 100644 --- a/crates/client/rpc/src/providers/forward_to_provider.rs +++ b/crates/client/rpc/src/providers/forward_to_provider.rs @@ -1,11 +1,11 @@ +use crate::{bail_internal_server_error, errors::StarknetRpcApiError}; use jsonrpsee::core::{async_trait, RpcResult}; +use mp_transactions::BroadcastedDeclareTransactionV0; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; use starknet_providers::{Provider, ProviderError}; -use mp_transactions::BroadcastedDeclareTransactionV0; -use crate::{bail_internal_server_error, errors::StarknetRpcApiError}; use super::AddTransactionProvider; @@ -21,8 +21,10 @@ impl ForwardToProvider

{ #[async_trait] impl AddTransactionProvider for ForwardToProvider

{ - - async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + async fn add_declare_v0_transaction( + &self, + declare_v0_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult { // panic here, because we can't really forward it to the real FGW, or shall we enable it so that another madara full node is able to use it? // maybe a flag for this? as discussed unimplemented!() diff --git a/crates/client/rpc/src/providers/mempool.rs b/crates/client/rpc/src/providers/mempool.rs index 791d644b5..01c27da15 100644 --- a/crates/client/rpc/src/providers/mempool.rs +++ b/crates/client/rpc/src/providers/mempool.rs @@ -3,12 +3,12 @@ use crate::{errors::StarknetRpcApiError, utils::display_internal_server_error}; use jsonrpsee::core::{async_trait, RpcResult}; use mc_mempool::Mempool; use mc_mempool::MempoolProvider; +use mp_transactions::BroadcastedDeclareTransactionV0; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; use std::sync::Arc; -use mp_transactions::BroadcastedDeclareTransactionV0; /// This [`AddTransactionProvider`] adds the received transactions to a mempool. pub struct MempoolAddTxProvider { @@ -44,8 +44,10 @@ impl From for StarknetRpcApiError { #[async_trait] impl AddTransactionProvider for MempoolAddTxProvider { - - async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + async fn add_declare_v0_transaction( + &self, + declare_v0_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult { log::info!("Checkpoint 2: add_declare_v0_transaction"); Ok(self.mempool.accept_declare_v0_tx(declare_v0_transaction).map_err(StarknetRpcApiError::from)?) } diff --git a/crates/client/rpc/src/providers/mod.rs b/crates/client/rpc/src/providers/mod.rs index dbf1b4187..e5ee2fd7e 100644 --- a/crates/client/rpc/src/providers/mod.rs +++ b/crates/client/rpc/src/providers/mod.rs @@ -5,16 +5,18 @@ pub use forward_to_provider::*; pub use mempool::*; use jsonrpsee::core::{async_trait, RpcResult}; +use mp_transactions::BroadcastedDeclareTransactionV0; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; -use mp_transactions::BroadcastedDeclareTransactionV0; #[async_trait] pub trait AddTransactionProvider: Send + Sync { - - async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult; + async fn add_declare_v0_transaction( + &self, + declare_v0_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult; async fn add_declare_transaction( &self, declare_transaction: BroadcastedDeclareTransaction, diff --git a/crates/client/rpc/src/test_utils.rs b/crates/client/rpc/src/test_utils.rs index 35c42768a..e5d5e8284 100644 --- a/crates/client/rpc/src/test_utils.rs +++ b/crates/client/rpc/src/test_utils.rs @@ -29,8 +29,10 @@ pub struct TestTransactionProvider; #[cfg(test)] #[async_trait] impl AddTransactionProvider for TestTransactionProvider { - - async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult { + async fn add_declare_v0_transaction( + &self, + declare_v0_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult { unimplemented!() } async fn add_declare_transaction( diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs index 759e92de9..4d95f58b6 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs @@ -23,7 +23,6 @@ use crate::Starknet; /// specific issue. pub fn get_nonce(starknet: &Starknet, block_id: BlockId, contract_address: Felt) -> StarknetRpcResult { - log::debug!("inside get nonce block id is: {:?}, and contract address is: {:?}", block_id, contract_address); // Check if block exists. We have to return a different error in that case. let block_exists = diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs index c7119e47b..f81106dd7 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs @@ -1,10 +1,10 @@ +use crate::{versions::v0_7_1::StarknetWriteRpcApiV0_7_1Server, Starknet}; use jsonrpsee::core::{async_trait, RpcResult}; +use mp_transactions::BroadcastedDeclareTransactionV0; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, }; -use mp_transactions::BroadcastedDeclareTransactionV0; -use crate::{versions::v0_7_1::StarknetWriteRpcApiV0_7_1Server, Starknet}; #[async_trait] impl StarknetWriteRpcApiV0_7_1Server for Starknet { diff --git a/crates/node/src/main.rs b/crates/node/src/main.rs index d8cab33e7..2b62d54ed 100644 --- a/crates/node/src/main.rs +++ b/crates/node/src/main.rs @@ -110,7 +110,7 @@ async fn main() -> anyhow::Result<()> { chain_config.chain_id.clone(), chain_config.eth_core_contract_address, run_cmd.is_sequencer(), - Arc::clone(&mempool) + Arc::clone(&mempool), ) .await .context("Initializing the l1 sync service")?; diff --git a/crates/node/src/service/l1.rs b/crates/node/src/service/l1.rs index 5567d1058..deaa26309 100644 --- a/crates/node/src/service/l1.rs +++ b/crates/node/src/service/l1.rs @@ -21,7 +21,7 @@ pub struct L1SyncService { chain_id: ChainId, gas_price_sync_disabled: bool, gas_price_poll: Duration, - mempool: Arc + mempool: Arc, } impl L1SyncService { @@ -33,7 +33,7 @@ impl L1SyncService { chain_id: ChainId, l1_core_address: H160, authority: bool, - mempool: Arc + mempool: Arc, ) -> anyhow::Result { let eth_client = if !config.sync_l1_disabled { if let Some(l1_rpc_url) = &config.l1_endpoint { @@ -75,7 +75,7 @@ impl L1SyncService { chain_id, gas_price_sync_disabled: !gas_price_sync_enabled, gas_price_poll, - mempool + mempool, }) } } @@ -83,7 +83,8 @@ impl L1SyncService { #[async_trait::async_trait] impl Service for L1SyncService { async fn start(&mut self, join_set: &mut JoinSet>) -> anyhow::Result<()> { - let L1SyncService { l1_gas_provider, chain_id, gas_price_sync_disabled, gas_price_poll, mempool, .. } = self.clone(); + let L1SyncService { l1_gas_provider, chain_id, gas_price_sync_disabled, gas_price_poll, mempool, .. } = + self.clone(); if let Some(eth_client) = self.eth_client.take() { // enabled @@ -97,7 +98,7 @@ impl Service for L1SyncService { l1_gas_provider, gas_price_sync_disabled, gas_price_poll, - mempool + mempool, ) .await }); diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index 84524ac33..70dddce89 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -228,7 +228,9 @@ impl ChainConfig { sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), block_time: Duration::from_secs(8), pending_block_update_time: Duration::from_secs(2), - eth_core_contract_address: "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512".parse().expect("parsing a constant"), + eth_core_contract_address: "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512" + .parse() + .expect("parsing a constant"), ..ChainConfig::starknet_sepolia() } } diff --git a/crates/primitives/class/src/lib.rs b/crates/primitives/class/src/lib.rs index d344d464e..29336dda7 100644 --- a/crates/primitives/class/src/lib.rs +++ b/crates/primitives/class/src/lib.rs @@ -2,7 +2,6 @@ use std::{collections::HashMap, sync::Arc}; use starknet_types_core::felt::Felt; - pub mod class_hash; pub mod class_update; pub mod compile; diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index 6e6d5d272..ff41f0f17 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -8,7 +8,10 @@ use cairo_vm::types::builtin_name::BuiltinName; use starknet_core::types::MsgToL2; use starknet_types_core::felt::Felt; -use crate::{DeclareTransactionReceipt, DeployAccountTransactionReceipt, Event, ExecutionResources, ExecutionResult, FeePayment, InvokeTransactionReceipt, L1Gas, L1HandlerTransactionReceipt, MsgToL1, PriceUnit, TransactionReceipt}; +use crate::{ + DeclareTransactionReceipt, DeployAccountTransactionReceipt, Event, ExecutionResources, ExecutionResult, FeePayment, + InvokeTransactionReceipt, L1Gas, L1HandlerTransactionReceipt, MsgToL1, PriceUnit, TransactionReceipt, +}; fn blockifier_tx_fee_type(tx: &Transaction) -> FeeType { match tx { @@ -49,7 +52,7 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans }) }) .collect(); - + // if the txn type is L1HandlerTransaction, then the message hash is a function call of l1_handler_tx // let message: MsgToL2 = match tx { // Transaction::L1HandlerTransaction(tx) => MsgToL2 { @@ -147,7 +150,7 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans events, execution_resources, execution_result, - message_hash: Felt::from(1) + message_hash: Felt::from(1), }), } } diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index f80f0ec8a..ef81ab6c3 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -1,6 +1,8 @@ use std::sync::Arc; -use crate::{BroadcastedDeclareTransactionV0, into_starknet_api::TransactionApiError, Transaction, TransactionWithHash}; +use crate::{ + into_starknet_api::TransactionApiError, BroadcastedDeclareTransactionV0, Transaction, TransactionWithHash, +}; use blockifier::{execution::errors::ContractClassError, transaction::errors::TransactionExecutionError}; use mp_chain_config::StarknetVersion; use mp_class::{ @@ -42,33 +44,31 @@ pub fn broadcasted_to_blockifier_v0( let (class_info, class_hash, extra_class_info) = { let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); - let class_hash = compressed_legacy_class.compute_class_hash().unwrap(); - log::debug!("Checkpoint 5: Computed legacy class hash: {:?}", class_hash); - let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); - let class_blockifier = compressed_legacy_class - .to_blockifier_class() - .map_err(BroadcastedToBlockifierError::CompilationFailed)?; + let class_hash = compressed_legacy_class.compute_class_hash().unwrap(); + log::debug!("Checkpoint 5: Computed legacy class hash: {:?}", class_hash); + let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); + let class_blockifier = + compressed_legacy_class.to_blockifier_class().map_err(BroadcastedToBlockifierError::CompilationFailed)?; log::debug!("Checkpoint 5: class blockfier sorted"); let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) }; - ( - Some(blockifier::execution::contract_class::ClassInfo::new(&class_blockifier, 0, 0)?), - Some(class_hash), - Some(ConvertedClass::Legacy(LegacyConvertedClass { class_hash, info: class_info })), - ) }; + ( + Some(blockifier::execution::contract_class::ClassInfo::new(&class_blockifier, 0, 0)?), + Some(class_hash), + Some(ConvertedClass::Legacy(LegacyConvertedClass { class_hash, info: class_info })), + ) + }; log::debug!("Checkpoint 6: we have the class info, class hash and the extra class info"); - let is_query = transaction.is_query; let TransactionWithHash { transaction, hash } = TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash); log::debug!("Checkpoint 7: tx hash is: {:?}", hash); - let deployed_address = match &transaction { Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), _ => None, diff --git a/crates/primitives/transactions/src/from_broadcasted_transaction.rs b/crates/primitives/transactions/src/from_broadcasted_transaction.rs index 657330886..1d771ca51 100644 --- a/crates/primitives/transactions/src/from_broadcasted_transaction.rs +++ b/crates/primitives/transactions/src/from_broadcasted_transaction.rs @@ -1,7 +1,12 @@ use mp_chain_config::StarknetVersion; use starknet_types_core::felt::Felt; -use crate::{BroadcastedDeclareTransactionV0, DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV1, DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1, InvokeTransactionV3, Transaction, TransactionWithHash}; +use crate::{ + BroadcastedDeclareTransactionV0, DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, + DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV1, + DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1, InvokeTransactionV3, Transaction, + TransactionWithHash, +}; // class_hash is required for DeclareTransaction impl TransactionWithHash { @@ -30,7 +35,8 @@ impl TransactionWithHash { class_hash: Option, ) -> Self { let is_query = tx.is_query; - let transaction: Transaction = Transaction::Declare(DeclareTransaction::from_broadcasted_v0(tx, class_hash.unwrap())); + let transaction: Transaction = + Transaction::Declare(DeclareTransaction::from_broadcasted_v0(tx, class_hash.unwrap())); let hash = transaction.compute_hash(chain_id, starknet_version, is_query); Self { hash, transaction } } @@ -96,12 +102,7 @@ impl DeclareTransaction { impl DeclareTransactionV0 { fn from_broadcasted(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { - Self { - sender_address: tx.sender_address, - max_fee: tx.max_fee, - signature: tx.signature, - class_hash, - } + Self { sender_address: tx.sender_address, max_fee: tx.max_fee, signature: tx.signature, class_hash } } } diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index f40fa355b..8c56bbe28 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -1,5 +1,5 @@ -use std::sync::Arc; use starknet_types_core::{felt::Felt, hash::StarkHash}; +use std::sync::Arc; mod broadcasted_to_blockifier; mod from_blockifier; @@ -14,10 +14,12 @@ pub mod utils; use mp_convert::{hex_serde::U128AsHex, hex_serde::U64AsHex, ToFelt}; // pub use from_starknet_provider::TransactionTypeError; +pub use broadcasted_to_blockifier::{ + broadcasted_to_blockifier, broadcasted_to_blockifier_v0, BroadcastedToBlockifierError, +}; +use mp_class::CompressedLegacyContractClass; use serde_with::serde_as; use starknet_api::transaction::TransactionVersion; -pub use broadcasted_to_blockifier::{broadcasted_to_blockifier, BroadcastedToBlockifierError, broadcasted_to_blockifier_v0}; -use mp_class::CompressedLegacyContractClass; const SIMULATE_TX_VERSION_OFFSET: Felt = Felt::from_hex_unchecked("0x100000000000000000000000000000000"); From f9650f5a33dc85f94fd948230aa49e6d42f40ccb Mon Sep 17 00:00:00 2001 From: mohiiit Date: Thu, 10 Oct 2024 18:43:30 +0530 Subject: [PATCH 09/25] formatting and changelog --- .gitignore | 3 ++ CHANGELOG.md | 1 + crates/client/db/src/class_db.rs | 10 +--- crates/client/eth/src/client.rs | 2 - crates/client/eth/src/l1_messaging.rs | 51 ++++--------------- crates/client/eth/src/state_update.rs | 1 - crates/client/eth/src/sync.rs | 1 - crates/client/mempool/src/block_production.rs | 4 +- crates/client/mempool/src/inner.rs | 24 +++++---- crates/client/mempool/src/lib.rs | 49 +++++++----------- .../rpc/src/providers/forward_to_provider.rs | 2 +- crates/client/rpc/src/providers/mempool.rs | 1 - crates/client/rpc/src/test_utils.rs | 2 +- .../versions/v0_7_1/methods/read/get_nonce.rs | 1 - .../src/versions/v0_7_1/methods/write/mod.rs | 1 - crates/node/src/main.rs | 2 - crates/node/src/service/l1.rs | 2 +- .../primitives/receipt/src/from_blockifier.rs | 4 +- .../src/broadcasted_to_blockifier.rs | 13 +---- crates/primitives/transactions/src/lib.rs | 7 +++ crates/tests/src/lib.rs | 4 +- 21 files changed, 66 insertions(+), 119 deletions(-) diff --git a/.gitignore b/.gitignore index 07d1eefc0..bf4f3d9f7 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,9 @@ madara.log starknet-e2e-test/contracts/cache starknet-e2e-test/contracts/build +# proptest report +**/proptest-regressions/ + # vscode settings .vscode/settings.json diff --git a/CHANGELOG.md b/CHANGELOG.md index be1ac67f4..7a4bf3038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- feat: declare v0, l1 handler support added - fix(cleanup): Updated EditorConfig to 4-space indents - fix(tests): Fixed local testing scripts - fix: override chain config diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 851f88daf..3285e8220 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -28,7 +28,6 @@ impl MadaraBackend { log::debug!("get encoded {key:#x}"); let key_encoded = bincode::serialize(key)?; - log::debug!("serailization passed, issue somewhere"); // Get from pending db, then normal db if not found. if is_pending { let col = self.db.get_column(pending_col); @@ -37,14 +36,11 @@ impl MadaraBackend { return Ok(Some(bincode::deserialize(&res)?)); // found in pending } } - log::debug!("get encoded: not in pending"); let col = self.db.get_column(nonpending_col); let Some(val) = self.db.get_pinned_cf(&col, &key_encoded)? else { return Ok(None) }; let val = bincode::deserialize(&val)?; - log::debug!("got some value of class hash"); - Ok(Some(val)) } @@ -64,7 +60,6 @@ impl MadaraBackend { Column::ClassInfo, )? else { - log::debug!("returning none because some error with getting the class"); return Ok(None); }; @@ -119,9 +114,6 @@ impl MadaraBackend { col_info: Column, col_compiled: Column, ) -> Result<(), MadaraStorageError> { - log::debug!("store classes has been called"); - log::debug!("converted classes length: {:?}", converted_classes.len()); - let mut writeopts = WriteOptions::new(); writeopts.disable_wal(true); @@ -163,7 +155,7 @@ impl MadaraBackend { |col, chunk| { let mut batch = WriteBatchWithTransaction::default(); for (key, value) in chunk { - log::debug!("Class compiled store key={key:#x}"); + log::trace!("Class compiled store key={key:#x}"); let key_bin = bincode::serialize(key)?; // TODO: find a way to avoid this allocation batch.put_cf(col, &key_bin, bincode::serialize(&value)?); diff --git a/crates/client/eth/src/client.rs b/crates/client/eth/src/client.rs index 7e9b85d8f..ebf9629f1 100644 --- a/crates/client/eth/src/client.rs +++ b/crates/client/eth/src/client.rs @@ -67,8 +67,6 @@ impl EthereumClient { pub async fn new(url: Url, l1_core_address: Address, l1_block_metrics: L1BlockMetrics) -> anyhow::Result { let provider = ProviderBuilder::new().on_http(url); - log::debug!("core contract address over here is: {:?}", l1_core_address); - EthereumClient::assert_core_contract_exists(&provider, l1_core_address).await?; let core_contract = StarknetCoreContract::new(l1_core_address, provider.clone()); diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index d7b915b84..8018e14b7 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -1,29 +1,20 @@ use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; -use alloy::eips::BlockNumberOrTag; -use alloy::primitives::{address, b256, keccak256, FixedBytes, U256}; -use alloy::providers::Provider; -use alloy::rpc::types::Filter; +use alloy::primitives::{keccak256, FixedBytes, U256}; use alloy::sol_types::SolValue; use anyhow::Context; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; use futures::StreamExt; -use log::Level::Debug; use mc_db::{l1_db::LastSyncedEventBlock, MadaraBackend}; -use mc_mempool::Mempool; +use mc_mempool::{Mempool, MempoolProvider}; use mp_utils::channel_wait_or_graceful_shutdown; use starknet_api::core::{ChainId, ContractAddress, EntryPointSelector, Nonce}; -use starknet_api::transaction::{ - Calldata, Fee, L1HandlerTransaction, Transaction, TransactionHash, TransactionVersion, -}; +use starknet_api::transaction::{Calldata, Fee, L1HandlerTransaction, Transaction, TransactionVersion}; use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; use std::sync::Arc; -use std::thread::sleep; -use std::time::Duration; -use url::ParseError::SetHostOnCannotBeABaseUrl; impl EthereumClient { /// Get cancellation status of an L1 to L2 message @@ -64,39 +55,16 @@ pub async fn sync( return Err(e.into()); } }; - log::debug!( - "we are inside sync and we will be calling the event filter now and last synced event block is: {:?}", - last_synced_event_block - ); let event_filter = client.l1_core_contract.event_filter::(); - log::debug!("event filter here is: {:?}", event_filter); - - let address = client.l1_core_contract.address().clone(); - let transfer_event_signature = b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b"); - let filter = Filter::new() - .event_signature(transfer_event_signature) - .from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number)) - .address(address); - // You could also use the event name instead of the event signature like so: - // .event("Transfer(address,address,uint256)") - - // Get all logs from the latest block that match the filter. - let logs = client.provider.get_logs(&filter).await?; - - for log in logs { - log::debug!("Transfer event: {log:?}"); - } + let mut event_stream = event_filter .from_block(last_synced_event_block.block_number) .watch() .await .context("Failed to watch event filter")? .into_stream(); - // log::debug!("event stream here is: {:?}", event_stream); while let Some(event_result) = channel_wait_or_graceful_shutdown(event_stream.next()).await { - log::debug!("inside the l1 messages"); if let Ok((event, meta)) = event_result { - log::debug!("we got some logs"); tracing::info!( "⟠ Processing L1 Message from block: {:?}, transaction_hash: {:?}, log_index: {:?}, fromAddress: {:?}", meta.block_number, @@ -164,7 +132,7 @@ async fn process_l1_message( event_index: &Option, chain_id: &ChainId, mempool: Arc, -) -> anyhow::Result> { +) -> anyhow::Result> { let transaction = parse_handle_l1_message_transaction(event)?; let tx_nonce = transaction.nonce; @@ -184,16 +152,17 @@ async fn process_l1_message( }; let tx_hash = get_transaction_hash(&Transaction::L1Handler(transaction.clone()), chain_id, &transaction.version)?; - let blockifier_transaction = + let blockifier_transaction: BlockifierL1HandlerTransaction = BlockifierL1HandlerTransaction { tx: transaction.clone(), tx_hash, paid_fee_on_l1: Fee(event.fee.try_into()?) }; - mempool.accept_tx(BlockifierTransation::L1HandlerTransaction(blockifier_transaction), None); + let res = mempool.accept_l1_handler_tx(BlockifierTransation::L1HandlerTransaction(blockifier_transaction))?; // TODO: remove unwraps + // Ques: shall it panic if no block number of event_index? let block_sent = LastSyncedEventBlock::new(l1_block_number.unwrap(), event_index.unwrap()); backend.messaging_update_last_synced_l1_block_with_event(block_sent)?; - Ok(Some(tx_hash)) + Ok(Some(res.transaction_hash)) } pub fn parse_handle_l1_message_transaction(event: &LogMessageToL2) -> anyhow::Result { @@ -247,7 +216,6 @@ mod l1_messaging_tests { use std::{sync::Arc, time::Duration}; - use super::Felt; use crate::l1_messaging::sync; use crate::{ client::{ @@ -271,6 +239,7 @@ mod l1_messaging_tests { use mp_chain_config::ChainConfig; use rstest::*; use starknet_api::core::Nonce; + use starknet_types_core::felt::Felt; use tempfile::TempDir; use tracing_test::traced_test; use url::Url; diff --git a/crates/client/eth/src/state_update.rs b/crates/client/eth/src/state_update.rs index 548580b49..2031a8395 100644 --- a/crates/client/eth/src/state_update.rs +++ b/crates/client/eth/src/state_update.rs @@ -111,7 +111,6 @@ mod eth_client_event_subscription_test { use mc_db::DatabaseService; use mc_metrics::{MetricsRegistry, MetricsService}; use mp_chain_config::ChainConfig; - use mp_convert::ToFelt; use rstest::*; use tempfile::TempDir; use url::Url; diff --git a/crates/client/eth/src/sync.rs b/crates/client/eth/src/sync.rs index 151c18f81..6d9d6196d 100644 --- a/crates/client/eth/src/sync.rs +++ b/crates/client/eth/src/sync.rs @@ -4,7 +4,6 @@ use crate::l1_messaging::sync; use crate::state_update::state_update_worker; use mc_mempool::{GasPriceProvider, Mempool}; use starknet_api::core::ChainId; -use starknet_types_core::felt::Felt; use std::sync::Arc; use std::time::Duration; diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index 397599dd4..c6519fd98 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -5,7 +5,7 @@ use blockifier::bouncer::{Bouncer, BouncerWeights, BuiltinCount}; use blockifier::state::cached_state::CommitmentStateDiff; use blockifier::state::state_api::StateReader; use blockifier::transaction::errors::TransactionExecutionError; -use blockifier::transaction::transaction_execution::Transaction; +// use blockifier::transaction::transaction_execution::Transaction; use mc_block_import::{BlockImportError, BlockImporter}; use mc_db::db_block_id::DbBlockId; use mc_db::{MadaraBackend, MadaraStorageError}; @@ -253,7 +253,7 @@ impl BlockProductionTask { stats.n_batches += 1; - txs_to_process_blockifier.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); + // txs_to_process_blockifier.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); // Execute the transactions. let all_results = self.executor.execute_txs(&txs_to_process_blockifier); diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 6d60e57fd..c2a0a86cc 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -230,10 +230,7 @@ impl MempoolInner { pub fn insert_tx(&mut self, mempool_tx: MempoolTransaction, force: bool) -> Result<(), TxInsersionError> { // Get the nonce chain for the contract - log::debug!("Checkpoint: inside the insert tx"); - let contract_addr = mempool_tx.contract_address(); - log::debug!("Checkpoint: contract address here is: {:?}", contract_addr); let arrived_at = mempool_tx.arrived_at; @@ -366,10 +363,9 @@ mod tests { use starknet_types_core::felt::Felt; use super::*; - use starknet_api::transaction::Fee; + use blockifier::abi::abi_utils::selector_from_name; + use starknet_api::transaction::{Fee, TransactionVersion}; use std::fmt; - // use blockifier::transaction::transaction_execution::Transaction::L1HandlerTransaction; - // use starknet_api::transaction::L1HandlerTransaction; #[derive(PartialEq, Eq, Hash)] struct AFelt(Felt); @@ -486,10 +482,18 @@ mod tests { tx_hash, ))) } - TxTy::L1Handler => Transaction::L1HandlerTransaction(L1HandlerTransaction::create_for_testing( - Fee(0), - ContractAddress::from(1u16), - )), + // TODO: maybe update the values? + TxTy::L1Handler => Transaction::L1HandlerTransaction(L1HandlerTransaction { + tx: starknet_api::transaction::L1HandlerTransaction { + version: TransactionVersion::ZERO, + nonce: Nonce::default(), + contract_address: contract_addr, + entry_point_selector: selector_from_name("l1_handler_set_value"), + calldata: Default::default(), + }, + tx_hash, + paid_fee_on_l1: Fee::default(), + }), }; Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force) diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index a99e59f3d..56c955c03 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -14,8 +14,8 @@ use mp_block::BlockId; use mp_block::BlockTag; use mp_block::MadaraPendingBlockInfo; use mp_class::ConvertedClass; -use mp_transactions::BroadcastedToBlockifierError; use mp_transactions::{broadcasted_to_blockifier, broadcasted_to_blockifier_v0, BroadcastedDeclareTransactionV0}; +use mp_transactions::{BroadcastedToBlockifierError, L1HandlerTransactionResult}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::transaction::TransactionHash; use starknet_core::types::BroadcastedDeclareTransaction; @@ -69,6 +69,7 @@ pub trait MempoolProvider: Send + Sync { &self, tx: BroadcastedDeployAccountTransaction, ) -> Result; + fn accept_l1_handler_tx(&self, tx: Transaction) -> Result; fn take_txs_chunk + 'static>(&self, dest: &mut I, n: usize) where Self: Sized; @@ -90,11 +91,7 @@ impl Mempool { Mempool { backend, l1_data_provider, inner: Default::default() } } - pub fn accept_tx(&self, tx: Transaction, converted_class: Option) -> Result<(), Error> { - // let Transaction::AccountTransaction(tx) = tx else { panic!("L1HandlerTransaction not supported yet") }; - - log::debug!("Checkpoint: we are inside the accept tx and we made sure that we have account txn only"); - + fn accept_tx(&self, tx: Transaction, converted_class: Option) -> Result<(), Error> { // The timestamp *does not* take the transaction validation time into account. let arrived_at = ArrivedAtTimestamp::now(); @@ -103,8 +100,6 @@ impl Mempool { block } else { // No current pending block, we'll make an unsaved empty one for the sake of validating this tx. - log::debug!("Checkpoint: this should be triggered because we are trying for a genesis block/ or maybe not"); - let parent_block_hash = self .backend .get_block_hash(&BlockId::Tag(BlockTag::Latest))? @@ -129,8 +124,6 @@ impl Mempool { } else { None }; - log::debug!("Checkpoint: now sending the tx for the execution context"); - // Perform validations let exec_context = ExecutionContext::new_in_block(Arc::clone(&self.backend), &pending_block_info)?; let mut validator = exec_context.tx_validator(); @@ -139,8 +132,6 @@ impl Mempool { let _ = validator.perform_validations(account_tx, deploy_account_tx_hash.is_some()); } - log::debug!("Checkpoint: validation performed"); - if !is_only_query(&tx) { // Finally, add it to the nonce chain for the account nonce let force = false; @@ -193,22 +184,24 @@ impl MempoolProvider for Mempool { } fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { - log::debug!("Checkpoint 3: accept_declare_v0_tx"); let (tx, classes) = broadcasted_to_blockifier_v0(tx, self.chain_id(), self.backend.chain_config().latest_protocol_version)?; - log::debug!("Checkpoint declare v0 tx"); - let res = DeclareTransactionResult { transaction_hash: transaction_hash(&tx), class_hash: declare_class_hash(&tx).expect("Created transaction should be declare"), }; - log::debug!("sending txn to the accept tx"); self.accept_tx(tx, classes)?; Ok(res) } + fn accept_l1_handler_tx(&self, tx: Transaction) -> Result { + let res = L1HandlerTransactionResult { transaction_hash: transaction_hash(&tx) }; + self.accept_tx(tx, None)?; + Ok(res) + } + fn accept_declare_tx(&self, tx: BroadcastedDeclareTransaction) -> Result { let (tx, classes) = broadcasted_to_blockifier( BroadcastedTransaction::Declare(tx), @@ -277,26 +270,22 @@ pub(crate) fn is_only_query(tx: &Transaction) -> bool { pub(crate) fn contract_addr(tx: &Transaction) -> ContractAddress { match tx { - Transaction::AccountTransaction(account_tx) => { - (match account_tx { - AccountTransaction::Declare(tx) => tx.tx.sender_address(), - AccountTransaction::DeployAccount(tx) => tx.contract_address, - AccountTransaction::Invoke(tx) => tx.tx.sender_address(), - }) - } + Transaction::AccountTransaction(account_tx) => match account_tx { + AccountTransaction::Declare(tx) => tx.tx.sender_address(), + AccountTransaction::DeployAccount(tx) => tx.contract_address, + AccountTransaction::Invoke(tx) => tx.tx.sender_address(), + }, Transaction::L1HandlerTransaction(tx) => tx.tx.contract_address, } } pub(crate) fn nonce(tx: &Transaction) -> Nonce { match tx { - Transaction::AccountTransaction(account_tx) => { - (match account_tx { - AccountTransaction::Declare(tx) => tx.tx.nonce(), - AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), - AccountTransaction::Invoke(tx) => tx.tx.nonce(), - }) - } + Transaction::AccountTransaction(account_tx) => match account_tx { + AccountTransaction::Declare(tx) => tx.tx.nonce(), + AccountTransaction::DeployAccount(tx) => tx.tx.nonce(), + AccountTransaction::Invoke(tx) => tx.tx.nonce(), + }, Transaction::L1HandlerTransaction(tx) => tx.tx.nonce, } } diff --git a/crates/client/rpc/src/providers/forward_to_provider.rs b/crates/client/rpc/src/providers/forward_to_provider.rs index baa1e6814..8c48f12de 100644 --- a/crates/client/rpc/src/providers/forward_to_provider.rs +++ b/crates/client/rpc/src/providers/forward_to_provider.rs @@ -23,7 +23,7 @@ impl ForwardToProvider

{ impl AddTransactionProvider for ForwardToProvider

{ async fn add_declare_v0_transaction( &self, - declare_v0_transaction: BroadcastedDeclareTransactionV0, + _declare_v0_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { // panic here, because we can't really forward it to the real FGW, or shall we enable it so that another madara full node is able to use it? // maybe a flag for this? as discussed diff --git a/crates/client/rpc/src/providers/mempool.rs b/crates/client/rpc/src/providers/mempool.rs index 01c27da15..ed8fa1f9f 100644 --- a/crates/client/rpc/src/providers/mempool.rs +++ b/crates/client/rpc/src/providers/mempool.rs @@ -48,7 +48,6 @@ impl AddTransactionProvider for MempoolAddTxProvider { &self, declare_v0_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { - log::info!("Checkpoint 2: add_declare_v0_transaction"); Ok(self.mempool.accept_declare_v0_tx(declare_v0_transaction).map_err(StarknetRpcApiError::from)?) } async fn add_declare_transaction( diff --git a/crates/client/rpc/src/test_utils.rs b/crates/client/rpc/src/test_utils.rs index e5d5e8284..f70a0dc41 100644 --- a/crates/client/rpc/src/test_utils.rs +++ b/crates/client/rpc/src/test_utils.rs @@ -31,7 +31,7 @@ pub struct TestTransactionProvider; impl AddTransactionProvider for TestTransactionProvider { async fn add_declare_v0_transaction( &self, - declare_v0_transaction: BroadcastedDeclareTransactionV0, + _declare_v0_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { unimplemented!() } diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs index 4d95f58b6..c4ea2b728 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/read/get_nonce.rs @@ -23,7 +23,6 @@ use crate::Starknet; /// specific issue. pub fn get_nonce(starknet: &Starknet, block_id: BlockId, contract_address: Felt) -> StarknetRpcResult { - log::debug!("inside get nonce block id is: {:?}, and contract address is: {:?}", block_id, contract_address); // Check if block exists. We have to return a different error in that case. let block_exists = starknet.backend.contains_block(&block_id).or_internal_server_error("Checking if block is in database")?; diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs index f81106dd7..7b45e5f20 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs @@ -21,7 +21,6 @@ impl StarknetWriteRpcApiV0_7_1Server for Starknet { &self, declare_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { - log::info!("add_declare_v0_transaction"); Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?) } diff --git a/crates/node/src/main.rs b/crates/node/src/main.rs index 2b62d54ed..c38eb75e6 100644 --- a/crates/node/src/main.rs +++ b/crates/node/src/main.rs @@ -121,8 +121,6 @@ async fn main() -> anyhow::Result<()> { match run_cmd.is_sequencer() { // Block production service. (authority) true => { - // let mempool = Arc::new(Mempool::new(Arc::clone(db_service.backend()), Arc::clone(&l1_data_provider))); - let block_production_service = BlockProductionService::new( &run_cmd.block_production_params, &db_service, diff --git a/crates/node/src/service/l1.rs b/crates/node/src/service/l1.rs index deaa26309..ba2531c48 100644 --- a/crates/node/src/service/l1.rs +++ b/crates/node/src/service/l1.rs @@ -6,7 +6,6 @@ use mc_eth::client::{EthereumClient, L1BlockMetrics}; use mc_mempool::{GasPriceProvider, Mempool}; use mc_metrics::MetricsRegistry; use mp_block::H160; -use mp_convert::ToFelt; use mp_utils::service::Service; use starknet_api::core::ChainId; use std::sync::Arc; @@ -25,6 +24,7 @@ pub struct L1SyncService { } impl L1SyncService { + #[allow(clippy::too_many_arguments)] pub async fn new( config: &L1SyncParams, db: &DatabaseService, diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index ff41f0f17..df8855286 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -5,7 +5,7 @@ use blockifier::transaction::{ transaction_execution::Transaction, }; use cairo_vm::types::builtin_name::BuiltinName; -use starknet_core::types::MsgToL2; +// use starknet_core::types::MsgToL2; use starknet_types_core::felt::Felt; use crate::{ @@ -143,7 +143,7 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans execution_result, }) } - Transaction::L1HandlerTransaction(tx) => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { + Transaction::L1HandlerTransaction(_tx) => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { transaction_hash, actual_fee, messages_sent, diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index ef81ab6c3..cb4ffd821 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -40,18 +40,13 @@ pub fn broadcasted_to_blockifier_v0( (blockifier::transaction::transaction_execution::Transaction, Option), BroadcastedToBlockifierError, > { - log::debug!("Checkpoint 4: broadcasted to blockifier v0"); - let (class_info, class_hash, extra_class_info) = { - let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); + let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone(); let class_hash = compressed_legacy_class.compute_class_hash().unwrap(); - log::debug!("Checkpoint 5: Computed legacy class hash: {:?}", class_hash); - let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into(); + let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone(); let class_blockifier = compressed_legacy_class.to_blockifier_class().map_err(BroadcastedToBlockifierError::CompilationFailed)?; - log::debug!("Checkpoint 5: class blockfier sorted"); - let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) }; ( @@ -61,14 +56,10 @@ pub fn broadcasted_to_blockifier_v0( ) }; - log::debug!("Checkpoint 6: we have the class info, class hash and the extra class info"); - let is_query = transaction.is_query; let TransactionWithHash { transaction, hash } = TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash); - log::debug!("Checkpoint 7: tx hash is: {:?}", hash); - let deployed_address = match &transaction { Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), _ => None, diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index 8c56bbe28..9b1422404 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use starknet_types_core::{felt::Felt, hash::StarkHash}; use std::sync::Arc; @@ -59,6 +60,12 @@ pub struct BroadcastedDeclareTransactionV0 { pub is_query: bool, } +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct L1HandlerTransactionResult { + /// The hash of the invoke transaction + pub transaction_hash: Felt, +} + #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Transaction { Invoke(InvokeTransaction), diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index 30b7c89bf..93d59446a 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -233,7 +233,7 @@ async fn madara_can_sync_a_few_blocks() { "sepolia", "--no-sync-polling", "--n-blocks-to-sync", - "10", + "20", "--no-l1-sync", ]); @@ -247,7 +247,7 @@ async fn madara_can_sync_a_few_blocks() { node.json_rpc().block_hash_and_number().await.unwrap(), BlockHashAndNumber { // https://sepolia.voyager.online/block/0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c - block_hash: Felt::from_hex_unchecked("0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5,"), + block_hash: Felt::from_hex_unchecked("0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5"), block_number: 19 } ); From 334c2584c90945766877f2ada993019cff21318e Mon Sep 17 00:00:00 2001 From: mohiiit Date: Thu, 10 Oct 2024 21:32:30 +0530 Subject: [PATCH 10/25] comments removed --- crates/client/db/src/class_db.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/client/db/src/class_db.rs b/crates/client/db/src/class_db.rs index 3285e8220..a12719b5a 100644 --- a/crates/client/db/src/class_db.rs +++ b/crates/client/db/src/class_db.rs @@ -32,7 +32,6 @@ impl MadaraBackend { if is_pending { let col = self.db.get_column(pending_col); if let Some(res) = self.db.get_pinned_cf(&col, &key_encoded)? { - log::debug!("got some result and next step is to deserialize"); return Ok(Some(bincode::deserialize(&res)?)); // found in pending } } From 7c6339677d3b41d9290a4cad098511f783d3d15f Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 11 Oct 2024 16:27:33 +0530 Subject: [PATCH 11/25] validation added for txns --- crates/client/mempool/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index 56c955c03..799f558b4 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -129,7 +129,7 @@ impl Mempool { let mut validator = exec_context.tx_validator(); if let Transaction::AccountTransaction(account_tx) = clone_transaction(&tx) { - let _ = validator.perform_validations(account_tx, deploy_account_tx_hash.is_some()); + validator.perform_validations(account_tx, deploy_account_tx_hash.is_some())? } if !is_only_query(&tx) { From c857b79cdfd6acea16267310f752bc0b0d50aace Mon Sep 17 00:00:00 2001 From: mohiiit Date: Tue, 15 Oct 2024 23:18:29 +0530 Subject: [PATCH 12/25] resolving issues --- crates/client/eth/src/l1_messaging.rs | 6 ++---- crates/client/mempool/src/block_production.rs | 3 --- crates/client/mempool/src/inner.rs | 5 ++--- crates/client/mempool/src/lib.rs | 4 ++-- crates/client/rpc/src/providers/forward_to_provider.rs | 4 +--- crates/primitives/chain_config/src/chain_config.rs | 5 ----- crates/primitives/class/src/lib.rs | 6 ------ .../transactions/src/broadcasted_to_blockifier.rs | 4 ++-- .../transactions/src/from_broadcasted_transaction.rs | 10 +++++----- crates/primitives/transactions/src/lib.rs | 2 +- crates/tests/src/lib.rs | 2 -- 11 files changed, 15 insertions(+), 36 deletions(-) diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index 8018e14b7..e2fe02770 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -152,10 +152,8 @@ async fn process_l1_message( }; let tx_hash = get_transaction_hash(&Transaction::L1Handler(transaction.clone()), chain_id, &transaction.version)?; - let blockifier_transaction: BlockifierL1HandlerTransaction = - BlockifierL1HandlerTransaction { tx: transaction.clone(), tx_hash, paid_fee_on_l1: Fee(event.fee.try_into()?) }; - - let res = mempool.accept_l1_handler_tx(BlockifierTransation::L1HandlerTransaction(blockifier_transaction))?; + let blockifier_transaction = BlockifierTransation::from_api(Transaction::L1Handler(transaction), tx_hash, None, Some(event.fee.try_into()?), None, false)?; + let res = mempool.accept_l1_handler_tx(blockifier_transaction)?; // TODO: remove unwraps // Ques: shall it panic if no block number of event_index? diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index c1997836f..a69b119c3 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -5,7 +5,6 @@ use blockifier::bouncer::{Bouncer, BouncerWeights, BuiltinCount}; use blockifier::state::cached_state::CommitmentStateDiff; use blockifier::state::state_api::StateReader; use blockifier::transaction::errors::TransactionExecutionError; -// use blockifier::transaction::transaction_execution::Transaction; use mc_block_import::{BlockImportError, BlockImporter}; use mc_db::db_block_id::DbBlockId; use mc_db::{MadaraBackend, MadaraStorageError}; @@ -253,8 +252,6 @@ impl BlockProductionTask { stats.n_batches += 1; - // txs_to_process_blockifier.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx))); - // Execute the transactions. let all_results = self.executor.execute_txs(&txs_to_process_blockifier); // When the bouncer cap is reached, blockifier will return fewer results than what we asked for. diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 30cfe8fa2..368b9e87c 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -536,7 +536,7 @@ mod tests { TxTy::L1Handler => starknet_api::transaction::Transaction::L1Handler( starknet_api::transaction::L1HandlerTransaction { version: TransactionVersion::ZERO, - nonce: Nonce::default(), + nonce, contract_address: contract_addr, entry_point_selector: selector_from_name("l1_handler_set_value"), calldata: Default::default(), @@ -560,11 +560,10 @@ mod tests { let tx_hash = tx.calculate_transaction_hash(&ChainId::Mainnet, &TransactionVersion::THREE).unwrap(); + // Note: sending paid l1 gas as none as of now let tx = Transaction::from_api(tx, tx_hash, Some(DUMMY_CLASS.clone()), None, deployed, false).unwrap(); - // let Transaction::AccountTransaction(tx) = tx else { unimplemented!() }; - Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force) }) .boxed() diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index 20560eb2a..4015e2075 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -15,7 +15,7 @@ use mp_block::BlockTag; use mp_block::MadaraPendingBlockInfo; use mp_class::ConvertedClass; use mp_convert::ToFelt; -use mp_transactions::{broadcasted_to_blockifier, broadcasted_to_blockifier_v0, BroadcastedDeclareTransactionV0}; +use mp_transactions::{broadcasted_to_blockifier, broadcasted_declare_v0_to_blockifier, BroadcastedDeclareTransactionV0}; use mp_transactions::{BroadcastedToBlockifierError, L1HandlerTransactionResult}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::transaction::TransactionHash; @@ -190,7 +190,7 @@ impl MempoolProvider for Mempool { fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { let (tx, classes) = - broadcasted_to_blockifier_v0(tx, self.chain_id(), self.backend.chain_config().latest_protocol_version)?; + broadcasted_declare_v0_to_blockifier(tx, self.chain_id(), self.backend.chain_config().latest_protocol_version)?; let res = DeclareTransactionResult { transaction_hash: transaction_hash(&tx), diff --git a/crates/client/rpc/src/providers/forward_to_provider.rs b/crates/client/rpc/src/providers/forward_to_provider.rs index 8c48f12de..9bead7924 100644 --- a/crates/client/rpc/src/providers/forward_to_provider.rs +++ b/crates/client/rpc/src/providers/forward_to_provider.rs @@ -25,9 +25,7 @@ impl AddTransactionProvider for ForwardToProvider

&self, _declare_v0_transaction: BroadcastedDeclareTransactionV0, ) -> RpcResult { - // panic here, because we can't really forward it to the real FGW, or shall we enable it so that another madara full node is able to use it? - // maybe a flag for this? as discussed - unimplemented!() + Err(StarknetRpcApiError::UnimplementedMethod.into()) } async fn add_declare_transaction( &self, diff --git a/crates/primitives/chain_config/src/chain_config.rs b/crates/primitives/chain_config/src/chain_config.rs index d810318da..c55ec327f 100644 --- a/crates/primitives/chain_config/src/chain_config.rs +++ b/crates/primitives/chain_config/src/chain_config.rs @@ -263,11 +263,6 @@ impl ChainConfig { chain_name: "Madara".into(), chain_id: ChainId::Other("MADARA_DEVNET".into()), sequencer_address: Felt::from_hex_unchecked("0x123").try_into().unwrap(), - block_time: Duration::from_secs(8), - pending_block_update_time: Duration::from_secs(2), - eth_core_contract_address: "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512" - .parse() - .expect("parsing a constant"), ..ChainConfig::starknet_sepolia() } } diff --git a/crates/primitives/class/src/lib.rs b/crates/primitives/class/src/lib.rs index aec47382b..197843e9b 100644 --- a/crates/primitives/class/src/lib.rs +++ b/crates/primitives/class/src/lib.rs @@ -227,32 +227,26 @@ pub struct LegacyTypedParameter { #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum LegacyFunctionAbiType { - #[serde(rename = "function")] Function, - #[serde(rename = "l1_handler")] L1Handler, - #[serde(rename = "constructor")] Constructor, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum LegacyEventAbiType { - #[serde(rename = "event")] Event, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum LegacyStructAbiType { - #[serde(rename = "struct")] Struct, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum FunctionStateMutability { - #[serde(rename = "view")] View, } diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index cb4ffd821..596e8f239 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -32,7 +32,7 @@ pub enum BroadcastedToBlockifierError { CompiledClassHashMismatch { expected: Felt, compilation: Felt }, } -pub fn broadcasted_to_blockifier_v0( +pub fn broadcasted_declare_v0_to_blockifier( transaction: BroadcastedDeclareTransactionV0, chain_id: Felt, starknet_version: StarknetVersion, @@ -58,7 +58,7 @@ pub fn broadcasted_to_blockifier_v0( let is_query = transaction.is_query; let TransactionWithHash { transaction, hash } = - TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash); + TransactionWithHash::from_broadcasted_declare_v0(transaction, chain_id, starknet_version, class_hash); let deployed_address = match &transaction { Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), diff --git a/crates/primitives/transactions/src/from_broadcasted_transaction.rs b/crates/primitives/transactions/src/from_broadcasted_transaction.rs index 1d771ca51..63e60ac1a 100644 --- a/crates/primitives/transactions/src/from_broadcasted_transaction.rs +++ b/crates/primitives/transactions/src/from_broadcasted_transaction.rs @@ -28,7 +28,7 @@ impl TransactionWithHash { Self { hash, transaction } } - pub fn from_broadcasted_v0( + pub fn from_broadcasted_declare_v0( tx: BroadcastedDeclareTransactionV0, chain_id: Felt, starknet_version: StarknetVersion, @@ -36,7 +36,7 @@ impl TransactionWithHash { ) -> Self { let is_query = tx.is_query; let transaction: Transaction = - Transaction::Declare(DeclareTransaction::from_broadcasted_v0(tx, class_hash.unwrap())); + Transaction::Declare(DeclareTransaction::from_broadcasted_declare_v0(tx, class_hash.unwrap())); let hash = transaction.compute_hash(chain_id, starknet_version, is_query); Self { hash, transaction } } @@ -95,13 +95,13 @@ impl DeclareTransaction { } } - fn from_broadcasted_v0(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { - DeclareTransaction::V0(DeclareTransactionV0::from_broadcasted(tx, class_hash)) + fn from_broadcasted_declare_v0(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { + DeclareTransaction::V0(DeclareTransactionV0::from_broadcasted_declare_v0(tx, class_hash)) } } impl DeclareTransactionV0 { - fn from_broadcasted(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { + fn from_broadcasted_declare_v0(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self { Self { sender_address: tx.sender_address, max_fee: tx.max_fee, signature: tx.signature, class_hash } } } diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index 9b1422404..9e1ba6296 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -16,7 +16,7 @@ pub mod utils; use mp_convert::{hex_serde::U128AsHex, hex_serde::U64AsHex, ToFelt}; // pub use from_starknet_provider::TransactionTypeError; pub use broadcasted_to_blockifier::{ - broadcasted_to_blockifier, broadcasted_to_blockifier_v0, BroadcastedToBlockifierError, + broadcasted_to_blockifier, broadcasted_declare_v0_to_blockifier, BroadcastedToBlockifierError, }; use mp_class::CompressedLegacyContractClass; use serde_with::serde_as; diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index 8883b3c53..c93fdae9e 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -242,8 +242,6 @@ async fn madara_can_sync_a_few_blocks() { node.wait_for_ready().await; node.wait_for_sync_to(19).await; - println!("we have block till: {:?}", node.json_rpc().block_hash_and_number().await.unwrap()); - assert_eq!( node.json_rpc().block_hash_and_number().await.unwrap(), BlockHashAndNumber { From 8e510c97f30bf1b75afd6c6563c0425ca9c3c526 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Wed, 16 Oct 2024 18:58:20 +0530 Subject: [PATCH 13/25] refactor: declare v0 refactored with different rpc namespace --- Cargo.lock | 1 + crates/client/eth/src/l1_messaging.rs | 10 +++++-- crates/client/mempool/src/inner.rs | 4 +-- crates/client/mempool/src/lib.rs | 11 +++++--- crates/client/rpc/src/lib.rs | 12 +++++++-- crates/client/rpc/src/macros.rs | 5 +++- crates/client/rpc/src/versions/v0_7_1/api.rs | 27 +++++++++++-------- .../versions/v0_7_1/methods/internal/mod.rs | 23 ++++++++++++++++ .../rpc/src/versions/v0_7_1/methods/mod.rs | 1 + .../src/versions/v0_7_1/methods/write/mod.rs | 17 ------------ crates/node/src/service/rpc.rs | 8 +++--- crates/node/src/service/rpc/middleware.rs | 8 +++--- crates/node/src/service/rpc/server.rs | 3 +++ crates/primitives/transactions/src/lib.rs | 2 +- crates/proc-macros/Cargo.toml | 1 + crates/proc-macros/src/lib.rs | 13 ++++++--- 16 files changed, 95 insertions(+), 51 deletions(-) create mode 100644 crates/client/rpc/src/versions/v0_7_1/methods/internal/mod.rs diff --git a/Cargo.lock b/Cargo.lock index adf2c1280..c66d63036 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5209,6 +5209,7 @@ dependencies = [ name = "m-proc-macros" version = "0.7.0" dependencies = [ + "log", "proc-macro2", "quote", "syn 2.0.79", diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index e2fe02770..a148638b6 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -5,7 +5,6 @@ use alloy::primitives::{keccak256, FixedBytes, U256}; use alloy::sol_types::SolValue; use anyhow::Context; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; -use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction; use futures::StreamExt; use mc_db::{l1_db::LastSyncedEventBlock, MadaraBackend}; use mc_mempool::{Mempool, MempoolProvider}; @@ -152,7 +151,14 @@ async fn process_l1_message( }; let tx_hash = get_transaction_hash(&Transaction::L1Handler(transaction.clone()), chain_id, &transaction.version)?; - let blockifier_transaction = BlockifierTransation::from_api(Transaction::L1Handler(transaction), tx_hash, None, Some(event.fee.try_into()?), None, false)?; + let blockifier_transaction = BlockifierTransation::from_api( + Transaction::L1Handler(transaction), + tx_hash, + None, + Some(Fee(event.fee.try_into()?)), + None, + false, + )?; let res = mempool.accept_l1_handler_tx(blockifier_transaction)?; // TODO: remove unwraps diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 368b9e87c..8b4ba2df4 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -418,7 +418,6 @@ mod tests { use blockifier::{ execution::contract_class::ClassInfo, test_utils::{contracts::FeatureContract, CairoVersion}, - transaction::transactions::{DeclareTransaction, InvokeTransaction, L1HandlerTransaction}, transaction::{transaction_execution::Transaction, transaction_types::TransactionType}, }; use mc_exec::execution::TxInfo; @@ -429,14 +428,13 @@ mod tests { core::{calculate_contract_address, ChainId}, data_availability::DataAvailabilityMode, transaction::{ - ContractAddressSalt, DeclareTransactionV3, DeployAccountTransactionV3, Fee, InvokeTransactionV3, Resource, + ContractAddressSalt, DeclareTransactionV3, DeployAccountTransactionV3, InvokeTransactionV3, Resource, ResourceBounds, ResourceBoundsMapping, TransactionHasher, TransactionVersion, }, }; use starknet_types_core::felt::Felt; use blockifier::abi::abi_utils::selector_from_name; - use lazy_static::*; use std::{collections::HashSet, fmt, time::Duration}; lazy_static::lazy_static! { diff --git a/crates/client/mempool/src/lib.rs b/crates/client/mempool/src/lib.rs index 4015e2075..1b4ce7494 100644 --- a/crates/client/mempool/src/lib.rs +++ b/crates/client/mempool/src/lib.rs @@ -15,7 +15,9 @@ use mp_block::BlockTag; use mp_block::MadaraPendingBlockInfo; use mp_class::ConvertedClass; use mp_convert::ToFelt; -use mp_transactions::{broadcasted_to_blockifier, broadcasted_declare_v0_to_blockifier, BroadcastedDeclareTransactionV0}; +use mp_transactions::{ + broadcasted_declare_v0_to_blockifier, broadcasted_to_blockifier, BroadcastedDeclareTransactionV0, +}; use mp_transactions::{BroadcastedToBlockifierError, L1HandlerTransactionResult}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::transaction::TransactionHash; @@ -189,8 +191,11 @@ impl MempoolProvider for Mempool { } fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result { - let (tx, classes) = - broadcasted_declare_v0_to_blockifier(tx, self.chain_id(), self.backend.chain_config().latest_protocol_version)?; + let (tx, classes) = broadcasted_declare_v0_to_blockifier( + tx, + self.chain_id(), + self.backend.chain_config().latest_protocol_version, + )?; let res = DeclareTransactionResult { transaction_hash: transaction_hash(&tx), diff --git a/crates/client/rpc/src/lib.rs b/crates/client/rpc/src/lib.rs index 42e981afc..4b46530a4 100644 --- a/crates/client/rpc/src/lib.rs +++ b/crates/client/rpc/src/lib.rs @@ -93,14 +93,22 @@ impl Starknet { } /// Returns the RpcModule merged with all the supported RPC versions. -pub fn versioned_rpc_api(starknet: &Starknet, read: bool, write: bool, trace: bool) -> anyhow::Result> { +pub fn versioned_rpc_api( + starknet: &Starknet, + read: bool, + write: bool, + trace: bool, + internal: bool, +) -> anyhow::Result> { let mut rpc_api = RpcModule::new(()); merge_rpc_versions!( - rpc_api, starknet, read, write, trace, + rpc_api, starknet, read, write, trace, internal, v0_7_1, // We can add new versions by adding the version module below // , v0_8_0 (for example) ); + log::debug!("rpc here is: {:?}", rpc_api); + Ok(rpc_api) } diff --git a/crates/client/rpc/src/macros.rs b/crates/client/rpc/src/macros.rs index 2eddd1129..cb46b06dc 100644 --- a/crates/client/rpc/src/macros.rs +++ b/crates/client/rpc/src/macros.rs @@ -1,6 +1,6 @@ #[macro_export] macro_rules! merge_rpc_versions { - ($rpc_api:expr, $starknet:expr, $read:expr, $write:expr, $trace:expr, $($version:ident),+ $(,)?) => { + ($rpc_api:expr, $starknet:expr, $read:expr, $write:expr, $trace:expr, $internal:expr, $($version:ident),+ $(,)?) => { $( paste::paste! { if $read { @@ -12,6 +12,9 @@ macro_rules! merge_rpc_versions { if $trace { $rpc_api.merge(versions::[<$version>]::[]::into_rpc($starknet.clone()))?; } + if $internal { + $rpc_api.merge(versions::[<$version>]::[]::into_rpc($starknet.clone()))?; + } } )+ }; diff --git a/crates/client/rpc/src/versions/v0_7_1/api.rs b/crates/client/rpc/src/versions/v0_7_1/api.rs index 40adf9824..40dcb202c 100644 --- a/crates/client/rpc/src/versions/v0_7_1/api.rs +++ b/crates/client/rpc/src/versions/v0_7_1/api.rs @@ -10,7 +10,7 @@ use starknet_core::types::{ }; use starknet_types_core::felt::Felt; -use m_proc_macros::versioned_starknet_rpc; +use m_proc_macros::versioned_rpc; use mp_transactions::BroadcastedDeclareTransactionV0; // Starknet RPC API trait and types // @@ -19,7 +19,19 @@ use mp_transactions::BroadcastedDeclareTransactionV0; // This crate uses `jsonrpsee` to define such an API in Rust terms. /// Starknet write rpc interface. -#[versioned_starknet_rpc("V0_7_1")] +/// + +#[versioned_rpc("V0_7_1", "madara")] +pub trait MadaraWriteRpcApi { + /// Submit a new class v0 declaration transaction + #[method(name = "addDeclareV0Transaction")] + async fn add_declare_v0_transaction( + &self, + declare_transaction_v0: BroadcastedDeclareTransactionV0, + ) -> RpcResult; +} + +#[versioned_rpc("V0_7_1", "starknet")] pub trait StarknetWriteRpcApi { /// Submit a new transaction to be added to the chain #[method(name = "addInvokeTransaction")] @@ -41,16 +53,9 @@ pub trait StarknetWriteRpcApi { &self, declare_transaction: BroadcastedDeclareTransaction, ) -> RpcResult; - - /// Submit a new class v0 declaration transaction - #[method(name = "addDeclareV0Transaction")] - async fn add_declare_v0_transaction( - &self, - declare_transaction_v0: BroadcastedDeclareTransactionV0, - ) -> RpcResult; } -#[versioned_starknet_rpc("V0_7_1")] +#[versioned_rpc("V0_7_1", "starknet")] pub trait StarknetReadRpcApi { /// Get the Version of the StarkNet JSON-RPC Specification Being Used #[method(name = "specVersion")] @@ -151,7 +156,7 @@ pub trait StarknetReadRpcApi { fn get_state_update(&self, block_id: BlockId) -> RpcResult; } -#[versioned_starknet_rpc("V0_7_1")] +#[versioned_rpc("V0_7_1", "starknet")] pub trait StarknetTraceRpcApi { /// Returns the execution trace of a transaction by simulating it in the runtime. #[method(name = "simulateTransactions")] diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/internal/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/internal/mod.rs new file mode 100644 index 000000000..5638b0d0b --- /dev/null +++ b/crates/client/rpc/src/versions/v0_7_1/methods/internal/mod.rs @@ -0,0 +1,23 @@ +use crate::{versions::v0_7_1::MadaraWriteRpcApiV0_7_1Server, Starknet}; +use jsonrpsee::core::{async_trait, RpcResult}; +use mp_transactions::BroadcastedDeclareTransactionV0; +use starknet_core::types::DeclareTransactionResult; + +#[async_trait] +impl MadaraWriteRpcApiV0_7_1Server for Starknet { + /// Submit a new declare transaction to be added to the chain + /// + /// # Arguments + /// + /// * `declare_v0_transaction` - the declare v0 transaction to be added to the chain + /// + /// # Returns + /// + /// * `declare_transaction_result` - the result of the declare transaction + async fn add_declare_v0_transaction( + &self, + declare_transaction: BroadcastedDeclareTransactionV0, + ) -> RpcResult { + Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?) + } +} diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/mod.rs index 7d8625fb7..083aeccfb 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/mod.rs @@ -1,3 +1,4 @@ +pub mod internal; pub mod read; pub mod trace; pub mod write; diff --git a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs index 7b45e5f20..df7dff155 100644 --- a/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs +++ b/crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs @@ -1,6 +1,5 @@ use crate::{versions::v0_7_1::StarknetWriteRpcApiV0_7_1Server, Starknet}; use jsonrpsee::core::{async_trait, RpcResult}; -use mp_transactions::BroadcastedDeclareTransactionV0; use starknet_core::types::{ BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult, @@ -8,22 +7,6 @@ use starknet_core::types::{ #[async_trait] impl StarknetWriteRpcApiV0_7_1Server for Starknet { - /// Submit a new declare transaction to be added to the chain - /// - /// # Arguments - /// - /// * `declare_v0_transaction` - the declare v0 transaction to be added to the chain - /// - /// # Returns - /// - /// * `declare_transaction_result` - the result of the declare transaction - async fn add_declare_v0_transaction( - &self, - declare_transaction: BroadcastedDeclareTransactionV0, - ) -> RpcResult { - Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?) - } - /// Submit a new declare transaction to be added to the chain /// /// # Arguments diff --git a/crates/node/src/service/rpc.rs b/crates/node/src/service/rpc.rs index 4653d6072..7254a437f 100644 --- a/crates/node/src/service/rpc.rs +++ b/crates/node/src/service/rpc.rs @@ -34,7 +34,7 @@ impl RpcService { return Ok(Self { server_config: None, server_handle: None }); } - let (rpcs, _node_operator) = match (config.rpc_methods, config.rpc_external) { + let (rpcs, node_operator) = match (config.rpc_methods, config.rpc_external) { (RpcMethods::Safe, _) => (true, false), (RpcMethods::Unsafe, _) => (true, true), (RpcMethods::Auto, false) => (true, true), @@ -46,8 +46,8 @@ impl RpcService { (true, false) } }; - let (read, write, trace) = (rpcs, rpcs, rpcs); - let starknet = Starknet::new(Arc::clone(db.backend()), chain_config.clone(), add_txs_method_provider); + let (read, write, trace, internal) = (rpcs, rpcs, rpcs, node_operator); + let starknet = Starknet::new(Arc::clone(db.backend()), chain_config.clone(), add_txs_method_provider.clone()); let metrics = RpcMetrics::register(metrics_handle)?; Ok(Self { @@ -59,7 +59,7 @@ impl RpcService { max_payload_out_mb: config.rpc_max_response_size, max_subs_per_conn: config.rpc_max_subscriptions_per_connection, message_buffer_capacity: config.rpc_message_buffer_capacity_per_connection, - rpc_api: versioned_rpc_api(&starknet, read, write, trace)?, + rpc_api: versioned_rpc_api(&starknet, read, write, trace, internal)?, metrics, cors: config.cors(), rate_limit: config.rpc_rate_limit, diff --git a/crates/node/src/service/rpc/middleware.rs b/crates/node/src/service/rpc/middleware.rs index e1aeef3b8..951cad05f 100644 --- a/crates/node/src/service/rpc/middleware.rs +++ b/crates/node/src/service/rpc/middleware.rs @@ -275,9 +275,11 @@ async fn add_rpc_version_to_method(req: &mut hyper::Request) -> Result<(), for item in items.iter_mut() { if let Some(method) = item.get_mut("method").as_deref().and_then(Value::as_str) { - let new_method = - format!("starknet_{}_{}", version.name(), method.strip_prefix("starknet_").unwrap_or(method)); - + let new_method = if let Some((prefix, suffix)) = method.split_once('_') { + format!("{}_{}_{}", prefix, version.name(), suffix) + } else { + format!("starknet_{}_{}", version.name(), method) + }; item["method"] = Value::String(new_method); } // we don't need to throw an error here, the request will be rejected later if the method is not supported diff --git a/crates/node/src/service/rpc/server.rs b/crates/node/src/service/rpc/server.rs index 379244203..b7d70243b 100644 --- a/crates/node/src/service/rpc/server.rs +++ b/crates/node/src/service/rpc/server.rs @@ -129,6 +129,7 @@ pub async fn start_server( let rate_limit_whitelisted_ips = rate_limit_whitelisted_ips.clone(); Ok::<_, Infallible>(service_fn(move |req| { + log::debug!("req: {:?}", req); let proxy_ip = if rate_limit_trust_proxy_headers { get_proxy_ip(&req) } else { None }; let rate_limit_cfg = if rate_limit_whitelisted_ips @@ -224,6 +225,8 @@ pub(crate) fn host_filtering(enabled: bool, addr: Option) -> Option< pub(crate) fn build_rpc_api(mut rpc_api: RpcModule) -> RpcModule { let mut available_methods = rpc_api.method_names().collect::>(); + log::debug!("available_methods: {:?}", available_methods); + // The "rpc_methods" is defined below and we want it to be part of the reported methods. // The available methods will be prefixed by their version, example: // * starknet_V0_7_1_blockNumber, diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index 9e1ba6296..acedb2fbd 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -16,7 +16,7 @@ pub mod utils; use mp_convert::{hex_serde::U128AsHex, hex_serde::U64AsHex, ToFelt}; // pub use from_starknet_provider::TransactionTypeError; pub use broadcasted_to_blockifier::{ - broadcasted_to_blockifier, broadcasted_declare_v0_to_blockifier, BroadcastedToBlockifierError, + broadcasted_declare_v0_to_blockifier, broadcasted_to_blockifier, BroadcastedToBlockifierError, }; use mp_class::CompressedLegacyContractClass; use serde_with::serde_as; diff --git a/crates/proc-macros/Cargo.toml b/crates/proc-macros/Cargo.toml index 05003920a..915680d41 100644 --- a/crates/proc-macros/Cargo.toml +++ b/crates/proc-macros/Cargo.toml @@ -15,6 +15,7 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] +log = { workspace = true } proc-macro2 = "1.0.86" quote = "1.0.26" syn = { version = "2.0.39", features = ["full"] } diff --git a/crates/proc-macros/src/lib.rs b/crates/proc-macros/src/lib.rs index 0e09189f9..bd24958f7 100644 --- a/crates/proc-macros/src/lib.rs +++ b/crates/proc-macros/src/lib.rs @@ -35,11 +35,14 @@ use syn::{parse::Parse, parse_macro_input, Attribute, Ident, ItemTrait, LitStr, struct VersionedRpcAttr { version: String, + namespace: String, } impl Parse for VersionedRpcAttr { fn parse(input: syn::parse::ParseStream) -> syn::Result { let version = input.parse::()?.value(); + input.parse::()?; + let namespace = input.parse::()?.value(); if !version.starts_with('V') { return Err(syn::Error::new(Span::call_site(), "Version must start with 'V'")); @@ -60,7 +63,7 @@ impl Parse for VersionedRpcAttr { } } - Ok(VersionedRpcAttr { version }) + Ok(VersionedRpcAttr { version, namespace }) } } @@ -79,8 +82,8 @@ fn version_method_name(attr: &Attribute, version: &str) -> syn::Result TokenStream { - let VersionedRpcAttr { version } = parse_macro_input!(attr as VersionedRpcAttr); +pub fn versioned_rpc(attr: TokenStream, input: TokenStream) -> TokenStream { + let VersionedRpcAttr { version, namespace } = parse_macro_input!(attr as VersionedRpcAttr); let mut item_trait = parse_macro_input!(input as ItemTrait); let trait_name = &item_trait.ident; @@ -103,11 +106,13 @@ pub fn versioned_starknet_rpc(attr: TokenStream, input: TokenStream) -> TokenStr } let versioned_trait = ItemTrait { - attrs: vec![syn::parse_quote!(#[rpc(server, namespace = "starknet")])], + attrs: vec![syn::parse_quote!(#[rpc(server, namespace = #namespace)])], ident: versioned_trait_name, ..item_trait }; + log::debug!("versioned_trait: {:?}", versioned_trait); + quote! { #versioned_trait } From 2ad4d197f409b578fd90161d8c83ccdd0416839c Mon Sep 17 00:00:00 2001 From: mohiiit Date: Wed, 16 Oct 2024 21:41:29 +0530 Subject: [PATCH 14/25] msgL2 hash added --- Cargo.lock | 2 +- crates/client/mempool/src/inner.rs | 11 ++- crates/node/src/service/rpc/server.rs | 3 - crates/primitives/receipt/Cargo.toml | 1 + .../primitives/receipt/src/from_blockifier.rs | 77 +++++++++++-------- crates/proc-macros/Cargo.toml | 1 - crates/proc-macros/src/lib.rs | 2 - 7 files changed, 57 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6eec38c8..f0c4d7c09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5210,7 +5210,6 @@ dependencies = [ name = "m-proc-macros" version = "0.7.0" dependencies = [ - "log", "proc-macro2", "quote", "syn 2.0.79", @@ -5788,6 +5787,7 @@ dependencies = [ "starknet-providers", "starknet-types-core", "starknet_api", + "thiserror", ] [[package]] diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 8b4ba2df4..005e2ad86 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -435,6 +435,7 @@ mod tests { use starknet_types_core::felt::Felt; use blockifier::abi::abi_utils::selector_from_name; + use starknet_api::transaction::Fee; use std::{collections::HashSet, fmt, time::Duration}; lazy_static::lazy_static! { @@ -556,11 +557,17 @@ mod tests { None }; + // providing dummy l1 gas for now + let l1_gas_paid = match &tx { + starknet_api::transaction::Transaction::L1Handler(_) => Some(Fee(1)), + _ => None, + }; + let tx_hash = tx.calculate_transaction_hash(&ChainId::Mainnet, &TransactionVersion::THREE).unwrap(); - // Note: sending paid l1 gas as none as of now let tx = - Transaction::from_api(tx, tx_hash, Some(DUMMY_CLASS.clone()), None, deployed, false).unwrap(); + Transaction::from_api(tx, tx_hash, Some(DUMMY_CLASS.clone()), l1_gas_paid, deployed, false) + .unwrap(); Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force) }) diff --git a/crates/node/src/service/rpc/server.rs b/crates/node/src/service/rpc/server.rs index b7d70243b..379244203 100644 --- a/crates/node/src/service/rpc/server.rs +++ b/crates/node/src/service/rpc/server.rs @@ -129,7 +129,6 @@ pub async fn start_server( let rate_limit_whitelisted_ips = rate_limit_whitelisted_ips.clone(); Ok::<_, Infallible>(service_fn(move |req| { - log::debug!("req: {:?}", req); let proxy_ip = if rate_limit_trust_proxy_headers { get_proxy_ip(&req) } else { None }; let rate_limit_cfg = if rate_limit_whitelisted_ips @@ -225,8 +224,6 @@ pub(crate) fn host_filtering(enabled: bool, addr: Option) -> Option< pub(crate) fn build_rpc_api(mut rpc_api: RpcModule) -> RpcModule { let mut available_methods = rpc_api.method_names().collect::>(); - log::debug!("available_methods: {:?}", available_methods); - // The "rpc_methods" is defined below and we want it to be part of the reported methods. // The available methods will be prefixed by their version, example: // * starknet_V0_7_1_blockNumber, diff --git a/crates/primitives/receipt/Cargo.toml b/crates/primitives/receipt/Cargo.toml index 5d032923e..780ba942c 100644 --- a/crates/primitives/receipt/Cargo.toml +++ b/crates/primitives/receipt/Cargo.toml @@ -27,6 +27,7 @@ starknet-core = { workspace = true } starknet-providers = { workspace = true } starknet-types-core = { workspace = true } starknet_api = { workspace = true } +thiserror = { workspace = true } # Other serde = { workspace = true, features = ["derive"] } diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index df8855286..ba2f5df5d 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -5,8 +5,9 @@ use blockifier::transaction::{ transaction_execution::Transaction, }; use cairo_vm::types::builtin_name::BuiltinName; -// use starknet_core::types::MsgToL2; +use starknet_core::types::MsgToL2; use starknet_types_core::felt::Felt; +use thiserror::Error; use crate::{ DeclareTransactionReceipt, DeployAccountTransactionReceipt, Event, ExecutionResources, ExecutionResult, FeePayment, @@ -30,6 +31,38 @@ fn blockifier_tx_hash(tx: &Transaction) -> Felt { } } +#[derive(Debug, Error)] +pub enum L1HandlerMessageError { + #[error("Empty calldata")] + EmptyCalldata, + #[error("From address out of range")] + FromAddressOutOfRange, + #[error("Invalid nonce")] + InvalidNonce, +} + +fn get_l1_handler_message_hash(tx: &Transaction) -> Result { + match tx { + Transaction::L1HandlerTransaction(tx) => { + let (from_address, payload) = tx.tx.calldata.0.split_first().ok_or(L1HandlerMessageError::EmptyCalldata)?; + + let from_address = (*from_address).try_into().map_err(|_| L1HandlerMessageError::FromAddressOutOfRange)?; + + let nonce = tx.tx.nonce.0.to_bigint().try_into().map_err(|_| L1HandlerMessageError::InvalidNonce)?; + + let message = MsgToL2 { + from_address, + to_address: tx.tx.contract_address.into(), + selector: tx.tx.entry_point_selector.0, + payload: payload.into(), + nonce, + }; + Ok(Felt::from_bytes_le(message.hash().as_bytes())) + } + _ => Err(L1HandlerMessageError::EmptyCalldata), // or another appropriate error + } +} + pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Transaction) -> TransactionReceipt { let price_unit = match blockifier_tx_fee_type(tx) { FeeType::Eth => PriceUnit::Wei, @@ -42,6 +75,17 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans let mut events: Vec = Vec::new(); get_events_from_call_info(res.execute_call_info.as_ref(), 0, &mut events); + let message_hash = match tx { + Transaction::L1HandlerTransaction(_) => match get_l1_handler_message_hash(tx) { + Ok(hash) => Some(hash), + Err(_err) => { + // ideal this should panic + None + } + }, + _ => None, + }; + let messages_sent = res .non_optional_call_infos() .flat_map(|call| { @@ -53,35 +97,6 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans }) .collect(); - // if the txn type is L1HandlerTransaction, then the message hash is a function call of l1_handler_tx - // let message: MsgToL2 = match tx { - // Transaction::L1HandlerTransaction(tx) => MsgToL2 { - // nonce: tx.tx.nonce.into(), - // payload: tx.tx.calldata.0, - // from_address: tx.tx.contract_address, - // to_address: tx.tx.to_address, - // selector: tx.tx.entry_point_selector - // }, - // _ => todo!(), - // }; - - // pub fn parse_msg_to_l2(&self) -> Result { - // self.calldata.split_first().map_or( - // Err(ParseMsgToL2Error::EmptyCalldata), - // |(from_address, payload)| { - // Ok(MsgToL2 { - // from_address: (*from_address) - // .try_into() - // .map_err(|_| ParseMsgToL2Error::FromAddressOutOfRange)?, - // to_address: self.contract_address, - // selector: self.entry_point_selector, - // payload: payload.into(), - // nonce: self.nonce, - // }) - // }, - // ) - // } - let get_applications = |resource| { res.non_optional_call_infos() .map(|call| call.resources.builtin_instance_counter.get(resource).map(|el| *el as u64)) @@ -150,7 +165,7 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans events, execution_resources, execution_result, - message_hash: Felt::from(1), + message_hash: message_hash.unwrap_or_else(|| Felt::from(0)), }), } } diff --git a/crates/proc-macros/Cargo.toml b/crates/proc-macros/Cargo.toml index 915680d41..05003920a 100644 --- a/crates/proc-macros/Cargo.toml +++ b/crates/proc-macros/Cargo.toml @@ -15,7 +15,6 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] -log = { workspace = true } proc-macro2 = "1.0.86" quote = "1.0.26" syn = { version = "2.0.39", features = ["full"] } diff --git a/crates/proc-macros/src/lib.rs b/crates/proc-macros/src/lib.rs index bd24958f7..32ba8baff 100644 --- a/crates/proc-macros/src/lib.rs +++ b/crates/proc-macros/src/lib.rs @@ -111,8 +111,6 @@ pub fn versioned_rpc(attr: TokenStream, input: TokenStream) -> TokenStream { ..item_trait }; - log::debug!("versioned_trait: {:?}", versioned_trait); - quote! { #versioned_trait } From fc16ff3f8b0966dcfa7365e046e760d4c640896e Mon Sep 17 00:00:00 2001 From: mohiiit Date: Wed, 16 Oct 2024 23:57:08 +0530 Subject: [PATCH 15/25] formatting and linting --- crates/client/rpc/src/lib.rs | 2 - .../primitives/receipt/src/from_blockifier.rs | 40 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/crates/client/rpc/src/lib.rs b/crates/client/rpc/src/lib.rs index 4b46530a4..f63ff9e8e 100644 --- a/crates/client/rpc/src/lib.rs +++ b/crates/client/rpc/src/lib.rs @@ -108,7 +108,5 @@ pub fn versioned_rpc_api( // , v0_8_0 (for example) ); - log::debug!("rpc here is: {:?}", rpc_api); - Ok(rpc_api) } diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index ba2f5df5d..13d66f89b 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -2,7 +2,7 @@ use blockifier::execution::call_info::CallInfo; use blockifier::transaction::{ account_transaction::AccountTransaction, objects::{FeeType, GasVector, HasRelatedFeeType, TransactionExecutionInfo}, - transaction_execution::Transaction, + transaction_execution::Transaction, transactions::L1HandlerTransaction }; use cairo_vm::types::builtin_name::BuiltinName; use starknet_core::types::MsgToL2; @@ -41,26 +41,21 @@ pub enum L1HandlerMessageError { InvalidNonce, } -fn get_l1_handler_message_hash(tx: &Transaction) -> Result { - match tx { - Transaction::L1HandlerTransaction(tx) => { - let (from_address, payload) = tx.tx.calldata.0.split_first().ok_or(L1HandlerMessageError::EmptyCalldata)?; +fn get_l1_handler_message_hash(tx: &L1HandlerTransaction) -> Result { + let (from_address, payload) = tx.tx.calldata.0.split_first().ok_or(L1HandlerMessageError::EmptyCalldata)?; - let from_address = (*from_address).try_into().map_err(|_| L1HandlerMessageError::FromAddressOutOfRange)?; + let from_address = (*from_address).try_into().map_err(|_| L1HandlerMessageError::FromAddressOutOfRange)?; - let nonce = tx.tx.nonce.0.to_bigint().try_into().map_err(|_| L1HandlerMessageError::InvalidNonce)?; + let nonce = tx.tx.nonce.0.to_bigint().try_into().map_err(|_| L1HandlerMessageError::InvalidNonce)?; - let message = MsgToL2 { - from_address, - to_address: tx.tx.contract_address.into(), - selector: tx.tx.entry_point_selector.0, - payload: payload.into(), - nonce, - }; - Ok(Felt::from_bytes_le(message.hash().as_bytes())) - } - _ => Err(L1HandlerMessageError::EmptyCalldata), // or another appropriate error - } + let message = MsgToL2 { + from_address, + to_address: tx.tx.contract_address.into(), + selector: tx.tx.entry_point_selector.0, + payload: payload.into(), + nonce, + }; + Ok(Felt::from_bytes_le(message.hash().as_bytes())) } pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Transaction) -> TransactionReceipt { @@ -76,11 +71,10 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans get_events_from_call_info(res.execute_call_info.as_ref(), 0, &mut events); let message_hash = match tx { - Transaction::L1HandlerTransaction(_) => match get_l1_handler_message_hash(tx) { + Transaction::L1HandlerTransaction(tx) => match get_l1_handler_message_hash(tx) { Ok(hash) => Some(hash), - Err(_err) => { - // ideal this should panic - None + Err(err) => { + panic!("Error getting l1 handler message hash: {:?}", err); } }, _ => None, @@ -165,7 +159,7 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans events, execution_resources, execution_result, - message_hash: message_hash.unwrap_or_else(|| Felt::from(0)), + message_hash: message_hash.unwrap(), // it's a safe unwrap because it would've panicked earlier if it was Err }), } } From 22098855fcb507c99abcaf1cf74b6d8b6b6e86e3 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 18 Oct 2024 16:13:25 +0530 Subject: [PATCH 16/25] tests added for proc macros, error added for empty namespace, removed default addeing of starknet namespace in middleware --- Cargo.lock | 1 + Cargo.toml | 1 + crates/node/src/service/rpc/middleware.rs | 2 +- .../primitives/receipt/src/from_blockifier.rs | 3 +- crates/proc-macros/Cargo.toml | 1 + crates/proc-macros/src/lib.rs | 65 +++++++++++++++++++ crates/tests/src/lib.rs | 2 +- 7 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0c4d7c09..7d1a3af8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5210,6 +5210,7 @@ dependencies = [ name = "m-proc-macros" version = "0.7.0" dependencies = [ + "indoc 2.0.5", "proc-macro2", "quote", "syn 2.0.79", diff --git a/Cargo.toml b/Cargo.toml index 27dded7e1..ae7018dab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -174,6 +174,7 @@ num-traits = "0.2" num-bigint = "0.4" primitive-types = "0.12" rand = "0.8" +indoc = "2" reqwest = { version = "0.12", features = ["blocking", "json"] } rstest = "0.18" serde = { version = "1.0", default-features = false, features = ["std"] } diff --git a/crates/node/src/service/rpc/middleware.rs b/crates/node/src/service/rpc/middleware.rs index 951cad05f..3aa7d9135 100644 --- a/crates/node/src/service/rpc/middleware.rs +++ b/crates/node/src/service/rpc/middleware.rs @@ -278,7 +278,7 @@ async fn add_rpc_version_to_method(req: &mut hyper::Request) -> Result<(), let new_method = if let Some((prefix, suffix)) = method.split_once('_') { format!("{}_{}_{}", prefix, version.name(), suffix) } else { - format!("starknet_{}_{}", version.name(), method) + format!("{}_{}", version.name(), method) }; item["method"] = Value::String(new_method); } diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index 13d66f89b..312479dce 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -2,7 +2,8 @@ use blockifier::execution::call_info::CallInfo; use blockifier::transaction::{ account_transaction::AccountTransaction, objects::{FeeType, GasVector, HasRelatedFeeType, TransactionExecutionInfo}, - transaction_execution::Transaction, transactions::L1HandlerTransaction + transaction_execution::Transaction, + transactions::L1HandlerTransaction, }; use cairo_vm::types::builtin_name::BuiltinName; use starknet_core::types::MsgToL2; diff --git a/crates/proc-macros/Cargo.toml b/crates/proc-macros/Cargo.toml index 05003920a..ad6b33cec 100644 --- a/crates/proc-macros/Cargo.toml +++ b/crates/proc-macros/Cargo.toml @@ -15,6 +15,7 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] +indoc = { workspace = true } proc-macro2 = "1.0.86" quote = "1.0.26" syn = { version = "2.0.39", features = ["full"] } diff --git a/crates/proc-macros/src/lib.rs b/crates/proc-macros/src/lib.rs index 32ba8baff..83abd1df3 100644 --- a/crates/proc-macros/src/lib.rs +++ b/crates/proc-macros/src/lib.rs @@ -33,6 +33,7 @@ use proc_macro2::Span; use quote::quote; use syn::{parse::Parse, parse_macro_input, Attribute, Ident, ItemTrait, LitStr, TraitItem}; +#[derive(Debug)] struct VersionedRpcAttr { version: String, namespace: String, @@ -63,6 +64,19 @@ impl Parse for VersionedRpcAttr { } } + if namespace.trim().is_empty() { + return Err(syn::Error::new( + Span::call_site(), + indoc::indoc!( + " + Namespace cannot be empty. + Please provide a non-empty namespace string. + Example: #[versioned_rpc(\"V0_7_1\", \"starknet\")] + " + ), + )); + } + Ok(VersionedRpcAttr { version, namespace }) } } @@ -116,3 +130,54 @@ pub fn versioned_rpc(attr: TokenStream, input: TokenStream) -> TokenStream { } .into() } + +#[cfg(test)] +mod tests { + use super::*; + use quote::{quote, ToTokens}; + use syn::parse_quote; + + #[test] + fn test_versioned_rpc_attribute_parsing() { + let attr: VersionedRpcAttr = parse_quote!("V0_7_1", "starknet"); + assert_eq!(attr.version, "V0_7_1"); + assert_eq!(attr.namespace, "starknet"); + } + + #[test] + fn test_versioned_rpc_attribute_parsing_invalid_version() { + let result: syn::Result = syn::parse2(quote!("0_7_1", "starknet")); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "Version must start with 'V'"); + } + + #[test] + fn test_versioned_rpc_attribute_parsing_invalid_parts() { + let result: syn::Result = syn::parse2(quote!("V0_7", "starknet")); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "Version must have exactly three parts (VMAJOR_MINOR_PATCH)"); + } + + #[test] + fn test_versioned_rpc_attribute_parsing_empty_namespace() { + let result: syn::Result = syn::parse2(quote!("V0_7_1", "")); + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().to_string(), + indoc::indoc!( + " + Namespace cannot be empty. + Please provide a non-empty namespace string. + Example: #[versioned_rpc(\"V0_7_1\", \"starknet\")] + " + ) + ); + } + + #[test] + fn test_version_method_name() { + let attr: Attribute = parse_quote!(#[method(name = "blockNumber")]); + let result = version_method_name(&attr, "V0_7_1").unwrap(); + assert_eq!(result.to_token_stream().to_string(), "# [method (name = \"V0_7_1_blockNumber\")]"); + } +} diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index c93fdae9e..25a31ac4d 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -245,7 +245,7 @@ async fn madara_can_sync_a_few_blocks() { assert_eq!( node.json_rpc().block_hash_and_number().await.unwrap(), BlockHashAndNumber { - // https://sepolia.voyager.online/block/0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c + // https://sepolia.voyager.online/block/0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5 block_hash: Felt::from_hex_unchecked("0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5"), block_number: 19 } From ed5b6c00d42ced75652946357b0b7bb48926fb4d Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 18 Oct 2024 16:27:58 +0530 Subject: [PATCH 17/25] fixing comments --- crates/client/mempool/src/inner.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 005e2ad86..558072dff 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -146,6 +146,7 @@ impl NonceChain { ) -> Result<(InsertedPosition, ReplacedState), TxInsersionError> { let mempool_tx_arrived_at = mempool_tx.arrived_at; let mempool_tx_nonce = mempool_tx.nonce(); + #[cfg(debug_assertions)] let mempool_tx_hash = mempool_tx.tx_hash(); let replaced = if force { @@ -176,7 +177,7 @@ impl NonceChain { self.front_nonce = mempool_tx_nonce; #[cfg(debug_assertions)] { - self.front_tx_hash = mempool_tx.tx_hash(); + self.front_tx_hash = mempool_tx_hash; } InsertedPosition::Front { former_head_arrived_at } } else { From f21baadfa05c6be88846d3c967d6fc4c35021ffc Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 18 Oct 2024 16:43:12 +0530 Subject: [PATCH 18/25] fix: build fix --- crates/client/mempool/src/inner.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 558072dff..83ae13d59 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -159,6 +159,7 @@ impl NonceChain { match self.transactions.entry(OrderMempoolTransactionByNonce(mempool_tx.clone())) { btree_map::Entry::Occupied(entry) => { // duplicate nonce, either it's because the hash is duplicated or nonce conflict with another tx. + #[cfg(debug_assertions)] if entry.key().0.tx_hash() == mempool_tx_hash { return Err(TxInsersionError::DuplicateTxn); } else { From 77038a2f54c41076d9223645315157847241f75f Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 18 Oct 2024 19:25:02 +0530 Subject: [PATCH 19/25] feat: added error when namespace is not present or method is not present in the rpc call --- crates/node/src/service/rpc/middleware.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/node/src/service/rpc/middleware.rs b/crates/node/src/service/rpc/middleware.rs index 3aa7d9135..75a6a4d7e 100644 --- a/crates/node/src/service/rpc/middleware.rs +++ b/crates/node/src/service/rpc/middleware.rs @@ -176,6 +176,10 @@ enum VersionMiddlewareError { InvalidVersion, #[error("Unsupported version specified")] UnsupportedVersion, + #[error("Invalid method format. Namespace required: {0}")] + InvalidMethodFormat(String), + #[error("Missing method in RPC request")] + MissingMethod, } impl From for VersionMiddlewareError { @@ -278,11 +282,12 @@ async fn add_rpc_version_to_method(req: &mut hyper::Request) -> Result<(), let new_method = if let Some((prefix, suffix)) = method.split_once('_') { format!("{}_{}_{}", prefix, version.name(), suffix) } else { - format!("{}_{}", version.name(), method) + return Err(VersionMiddlewareError::InvalidMethodFormat(method.to_string())); }; item["method"] = Value::String(new_method); + } else { + return Err(VersionMiddlewareError::MissingMethod); } - // we don't need to throw an error here, the request will be rejected later if the method is not supported } let response = if batched_request { serde_json::to_vec(&items)? } else { serde_json::to_vec(&items[0])? }; From 76696702544e88285da7f7589a54c71a00df46b7 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Fri, 18 Oct 2024 22:06:56 +0530 Subject: [PATCH 20/25] feat: de/serialize tests added for BroadcastedDeclareTransactionV0, comments resolved --- Cargo.lock | 2 + crates/primitives/receipt/Cargo.toml | 1 + .../primitives/receipt/src/from_blockifier.rs | 3 +- crates/primitives/transactions/Cargo.toml | 1 + .../src/broadcasted_to_blockifier.rs | 8 +-- .../src/from_broadcasted_transaction.rs | 11 +++- crates/primitives/transactions/src/lib.rs | 55 +++++++++++++++++++ 7 files changed, 71 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d1a3af8f..7c8b128f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5781,6 +5781,7 @@ dependencies = [ "bincode 1.3.3", "blockifier", "cairo-vm", + "log", "mp-convert", "rstest 0.18.2", "serde", @@ -5820,6 +5821,7 @@ dependencies = [ "mp-convert", "num-bigint", "serde", + "serde_json", "serde_with 3.11.0", "starknet-core 0.11.0", "starknet-providers", diff --git a/crates/primitives/receipt/Cargo.toml b/crates/primitives/receipt/Cargo.toml index 780ba942c..e4f01873b 100644 --- a/crates/primitives/receipt/Cargo.toml +++ b/crates/primitives/receipt/Cargo.toml @@ -22,6 +22,7 @@ mp-convert = { workspace = true } # Starknet blockifier = { workspace = true } cairo-vm = { workspace = true } +log = { workspace = true } rstest.workspace = true starknet-core = { workspace = true } starknet-providers = { workspace = true } diff --git a/crates/primitives/receipt/src/from_blockifier.rs b/crates/primitives/receipt/src/from_blockifier.rs index 312479dce..dfa8f51bf 100644 --- a/crates/primitives/receipt/src/from_blockifier.rs +++ b/crates/primitives/receipt/src/from_blockifier.rs @@ -75,7 +75,8 @@ pub fn from_blockifier_execution_info(res: &TransactionExecutionInfo, tx: &Trans Transaction::L1HandlerTransaction(tx) => match get_l1_handler_message_hash(tx) { Ok(hash) => Some(hash), Err(err) => { - panic!("Error getting l1 handler message hash: {:?}", err); + log::error!("Error getting l1 handler message hash: {:?}", err); + None } }, _ => None, diff --git a/crates/primitives/transactions/Cargo.toml b/crates/primitives/transactions/Cargo.toml index ffdcdfc59..59450f7c7 100644 --- a/crates/primitives/transactions/Cargo.toml +++ b/crates/primitives/transactions/Cargo.toml @@ -42,3 +42,4 @@ thiserror = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } +serde_json = { workspace = true } diff --git a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs index 596e8f239..8fab80793 100644 --- a/crates/primitives/transactions/src/broadcasted_to_blockifier.rs +++ b/crates/primitives/transactions/src/broadcasted_to_blockifier.rs @@ -60,10 +60,6 @@ pub fn broadcasted_declare_v0_to_blockifier( let TransactionWithHash { transaction, hash } = TransactionWithHash::from_broadcasted_declare_v0(transaction, chain_id, starknet_version, class_hash); - let deployed_address = match &transaction { - Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()), - _ => None, - }; let transaction: starknet_api::transaction::Transaction = transaction.try_into()?; Ok(( @@ -72,7 +68,7 @@ pub fn broadcasted_declare_v0_to_blockifier( TransactionHash(hash), class_info, None, - deployed_address.map(|address| address.try_into().unwrap()), + None, is_query, )?, extra_class_info, @@ -178,7 +174,7 @@ pub fn broadcasted_to_blockifier( TransactionHash(hash), class_info, None, - deployed_address.map(|address| address.try_into().unwrap()), + deployed_address.map(|address| address.try_into().expect("Address conversion should never fail")), is_query, )?, extra_class_info, diff --git a/crates/primitives/transactions/src/from_broadcasted_transaction.rs b/crates/primitives/transactions/src/from_broadcasted_transaction.rs index 63e60ac1a..a6f5e54cc 100644 --- a/crates/primitives/transactions/src/from_broadcasted_transaction.rs +++ b/crates/primitives/transactions/src/from_broadcasted_transaction.rs @@ -20,7 +20,10 @@ impl TransactionWithHash { let transaction: Transaction = match tx { starknet_core::types::BroadcastedTransaction::Invoke(tx) => Transaction::Invoke(tx.into()), starknet_core::types::BroadcastedTransaction::Declare(tx) => { - Transaction::Declare(DeclareTransaction::from_broadcasted(tx, class_hash.unwrap())) + Transaction::Declare(DeclareTransaction::from_broadcasted( + tx, + class_hash.expect("Class hash must be provided for DeclareTransaction"), + )) } starknet_core::types::BroadcastedTransaction::DeployAccount(tx) => Transaction::DeployAccount(tx.into()), }; @@ -35,8 +38,10 @@ impl TransactionWithHash { class_hash: Option, ) -> Self { let is_query = tx.is_query; - let transaction: Transaction = - Transaction::Declare(DeclareTransaction::from_broadcasted_declare_v0(tx, class_hash.unwrap())); + let transaction: Transaction = Transaction::Declare(DeclareTransaction::from_broadcasted_declare_v0( + tx, + class_hash.expect("Class hash must be provided for DeclareTransactionV0"), + )); let hash = transaction.compute_hash(chain_id, starknet_version, is_query); Self { hash, transaction } } diff --git a/crates/primitives/transactions/src/lib.rs b/crates/primitives/transactions/src/lib.rs index acedb2fbd..141461fef 100644 --- a/crates/primitives/transactions/src/lib.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -726,6 +726,7 @@ impl From for DataAvailabilityMode { #[cfg(test)] mod tests { use super::*; + use mp_class::{CompressedLegacyContractClass, LegacyEntryPointsByType}; #[test] fn test_tx_with_hash() { @@ -996,6 +997,60 @@ mod tests { assert_eq!(da_mode, da_mode_back); } + #[test] + fn test_broadcasted_declare_transaction_v0_serialization() { + let contract_class = CompressedLegacyContractClass { + program: Vec::new(), + entry_points_by_type: LegacyEntryPointsByType { + constructor: Vec::new(), + external: Vec::new(), + l1_handler: Vec::new(), + }, + abi: None, + }; + + let tx = BroadcastedDeclareTransactionV0 { + sender_address: Felt::from(1), + max_fee: Felt::from(1000), + signature: vec![Felt::from(2), Felt::from(3)], + contract_class: Arc::new(contract_class), + is_query: false, + }; + + let serialized = serde_json::to_string(&tx).unwrap(); + let deserialized: BroadcastedDeclareTransactionV0 = serde_json::from_str(&serialized).unwrap(); + + assert_eq!(tx, deserialized); + } + + #[test] + fn test_broadcasted_declare_transaction_v0_deserialization() { + let json = r#" + { + "sender_address": "0x1", + "max_fee": "0x3e8", + "signature": ["0x2", "0x3"], + "contract_class": { + "program": [], + "entry_points_by_type": { + "CONSTRUCTOR": [], + "EXTERNAL": [], + "L1_HANDLER": [] + }, + "abi": null + }, + "is_query": false + } + "#; + + let deserialized: BroadcastedDeclareTransactionV0 = serde_json::from_str(json).unwrap(); + + assert_eq!(deserialized.sender_address, Felt::from(1)); + assert_eq!(deserialized.max_fee, Felt::from(1000)); + assert_eq!(deserialized.signature, vec![Felt::from(2), Felt::from(3)]); + assert!(!deserialized.is_query); + } + pub(crate) fn dummy_tx_invoke_v0() -> InvokeTransactionV0 { InvokeTransactionV0 { max_fee: Felt::from(1), From 51054f8ab2f7ec501a76ae5940a69cabf8b8ee77 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Sat, 19 Oct 2024 12:12:58 +0530 Subject: [PATCH 21/25] fix: state diff special address should have current_block - 10 block number --- crates/client/mempool/src/block_production.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/client/mempool/src/block_production.rs b/crates/client/mempool/src/block_production.rs index c28b4b2b1..e69141073 100644 --- a/crates/client/mempool/src/block_production.rs +++ b/crates/client/mempool/src/block_production.rs @@ -422,7 +422,7 @@ impl BlockProductionTask { let address = Felt::ONE; new_state_diff.storage_diffs.push(ContractStorageDiffItem { address, - storage_entries: vec![StorageEntry { key: Felt::from(block_n), value: prev_block_hash }], + storage_entries: vec![StorageEntry { key: Felt::from(prev_block_number), value: prev_block_hash }], }); } From b7a2a3effcb1cc900949ee393d2751c24594179d Mon Sep 17 00:00:00 2001 From: mohiiit Date: Sat, 19 Oct 2024 14:21:44 +0530 Subject: [PATCH 22/25] feat: updating filter of l1 message to check between last and finalized block --- crates/client/eth/src/l1_messaging.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index a148638b6..9a6bfd117 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -14,6 +14,7 @@ use starknet_api::transaction::{Calldata, Fee, L1HandlerTransaction, Transaction use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; use std::sync::Arc; +use alloy::providers::Provider; impl EthereumClient { /// Get cancellation status of an L1 to L2 message @@ -55,9 +56,11 @@ pub async fn sync( } }; let event_filter = client.l1_core_contract.event_filter::(); + let current_block_number = client.provider.get_block_number().await?; let mut event_stream = event_filter .from_block(last_synced_event_block.block_number) + .to_block(current_block_number - 64) .watch() .await .context("Failed to watch event filter")? From 8ed287c01e36dd7c239d01cdce4ea9d624b68ac3 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Sat, 19 Oct 2024 14:32:16 +0530 Subject: [PATCH 23/25] linting and formatting --- crates/client/eth/src/l1_messaging.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index 9a6bfd117..be3a9d8f7 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -2,6 +2,7 @@ use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; use alloy::primitives::{keccak256, FixedBytes, U256}; +use alloy::providers::Provider; use alloy::sol_types::SolValue; use anyhow::Context; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; @@ -14,7 +15,6 @@ use starknet_api::transaction::{Calldata, Fee, L1HandlerTransaction, Transaction use starknet_api::transaction_hash::get_transaction_hash; use starknet_types_core::felt::Felt; use std::sync::Arc; -use alloy::providers::Provider; impl EthereumClient { /// Get cancellation status of an L1 to L2 message From 638c6454b76ff32d7b9c43f9632b773ae73e816e Mon Sep 17 00:00:00 2001 From: mohiiit Date: Sat, 19 Oct 2024 15:22:14 +0530 Subject: [PATCH 24/25] refactor: reverting the event filter in l1 messaging --- crates/client/eth/src/l1_messaging.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/client/eth/src/l1_messaging.rs b/crates/client/eth/src/l1_messaging.rs index be3a9d8f7..d1576ff40 100644 --- a/crates/client/eth/src/l1_messaging.rs +++ b/crates/client/eth/src/l1_messaging.rs @@ -1,8 +1,8 @@ use crate::client::StarknetCoreContract::LogMessageToL2; use crate::client::{EthereumClient, StarknetCoreContract}; use crate::utils::u256_to_felt; +use alloy::eips::BlockNumberOrTag; use alloy::primitives::{keccak256, FixedBytes, U256}; -use alloy::providers::Provider; use alloy::sol_types::SolValue; use anyhow::Context; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation; @@ -56,11 +56,10 @@ pub async fn sync( } }; let event_filter = client.l1_core_contract.event_filter::(); - let current_block_number = client.provider.get_block_number().await?; let mut event_stream = event_filter .from_block(last_synced_event_block.block_number) - .to_block(current_block_number - 64) + .to_block(BlockNumberOrTag::Finalized) .watch() .await .context("Failed to watch event filter")? From 2fa3b46a70f95e5d87a65dfc898a3ac211bf1bf2 Mon Sep 17 00:00:00 2001 From: mohiiit Date: Mon, 21 Oct 2024 17:52:57 +0530 Subject: [PATCH 25/25] extra clones removed --- crates/client/mempool/src/inner.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/client/mempool/src/inner.rs b/crates/client/mempool/src/inner.rs index 83ae13d59..bc430e7d1 100644 --- a/crates/client/mempool/src/inner.rs +++ b/crates/client/mempool/src/inner.rs @@ -150,13 +150,13 @@ impl NonceChain { let mempool_tx_hash = mempool_tx.tx_hash(); let replaced = if force { - if self.transactions.insert(OrderMempoolTransactionByNonce(mempool_tx.clone()), ()).is_some() { + if self.transactions.insert(OrderMempoolTransactionByNonce(mempool_tx), ()).is_some() { ReplacedState::Replaced } else { ReplacedState::NotReplaced } } else { - match self.transactions.entry(OrderMempoolTransactionByNonce(mempool_tx.clone())) { + match self.transactions.entry(OrderMempoolTransactionByNonce(mempool_tx)) { btree_map::Entry::Occupied(entry) => { // duplicate nonce, either it's because the hash is duplicated or nonce conflict with another tx. #[cfg(debug_assertions)]