diff --git a/Cargo.lock b/Cargo.lock index d3b2a62b90..cfdf3eef5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "Inflector" @@ -10383,6 +10383,7 @@ name = "starknet_mempool_p2p_types" version = "0.0.0" dependencies = [ "async-trait", + "mockall", "papyrus_network_types", "papyrus_proc_macros", "serde", diff --git a/crates/starknet_mempool/Cargo.toml b/crates/starknet_mempool/Cargo.toml index efd35caccd..6c692229d9 100644 --- a/crates/starknet_mempool/Cargo.toml +++ b/crates/starknet_mempool/Cargo.toml @@ -28,6 +28,7 @@ rstest.workspace = true starknet_api = { workspace = true, features = ["testing"] } # Enable test utils feature for integration tests. starknet_mempool = { workspace = true, features = ["testing"] } +starknet_mempool_p2p_types = { workspace = true, features = ["testing"] } [features] testing = ["mempool_test_utils", "pretty_assertions", "starknet-types-core"] diff --git a/crates/starknet_mempool/src/mempool_test.rs b/crates/starknet_mempool/src/mempool_test.rs index 4a257c5a4b..e9d41d3a23 100644 --- a/crates/starknet_mempool/src/mempool_test.rs +++ b/crates/starknet_mempool/src/mempool_test.rs @@ -687,3 +687,61 @@ fn test_update_gas_price_threshold_decreases_threshold() { .build(); expected_mempool_content.assert_eq(&mempool); } + +#[rstest] +#[tokio::test] +async fn test_new_tx_sent_to_p2p(mempool: Mempool) { + // add_tx_input! creates an Invoke Transaction + let tx_args = add_tx_input!(tx_hash: 1, address: "0x0", tx_nonce: 2, account_nonce: 2); + let propagateor_args = + AddTransactionArgsWrapper { args: tx_args.clone(), p2p_message_metadata: None }; + // TODO: use regular conversion once we have a compiler component + let rpc_tx = match tx_args.tx { + AccountTransaction::Declare(_declare_tx) => { + panic!("No implementation for converting DeclareTransaction to an RpcTransaction") + } + AccountTransaction::DeployAccount(deploy_account_transaction) => { + RpcTransaction::DeployAccount(RpcDeployAccountTransaction::V3( + deploy_account_transaction.clone().into(), + )) + } + AccountTransaction::Invoke(invoke_transaction) => { + RpcTransaction::Invoke(RpcInvokeTransaction::V3(invoke_transaction.clone().into())) + } + }; + + let mut mock_mempool_p2p_propagator_client = MockMempoolP2pPropagatorClient::new(); + mock_mempool_p2p_propagator_client + .expect_add_transaction() + .times(1) + .with(predicate::eq(rpc_tx)) + .returning(|_| Ok(())); + let mut mempool_wrapper = + MempoolCommunicationWrapper::new(mempool, Arc::new(mock_mempool_p2p_propagator_client)); + + mempool_wrapper.add_tx(propagateor_args).await.unwrap(); +} + +#[rstest] +#[tokio::test] +async fn test_propagated_tx_sent_to_p2p(mempool: Mempool) { + // add_tx_input! creates an Invoke Transaction + let tx_args = add_tx_input!(tx_hash: 2, address: "0x0", tx_nonce: 3, account_nonce: 2); + let expected_message_metadata = BroadcastedMessageMetadata::get_test_instance(&mut get_rng()); + let propagated_args = AddTransactionArgsWrapper { + args: tx_args.clone(), + p2p_message_metadata: Some(expected_message_metadata.clone()), + }; + + let mut mock_mempool_p2p_propagator_client = MockMempoolP2pPropagatorClient::new(); + mock_mempool_p2p_propagator_client + .expect_continue_propagation() + .times(1) + .with(predicate::eq(expected_message_metadata.clone())) + .returning(|_| Ok(())); + + let mut mempool_wrapper = + MempoolCommunicationWrapper::new(mempool, Arc::new(mock_mempool_p2p_propagator_client)); + + mempool_wrapper.add_tx(propagated_args).await.unwrap(); +} diff --git a/crates/starknet_mempool_p2p_types/Cargo.toml b/crates/starknet_mempool_p2p_types/Cargo.toml index 352de1ab63..7554957fc9 100644 --- a/crates/starknet_mempool_p2p_types/Cargo.toml +++ b/crates/starknet_mempool_p2p_types/Cargo.toml @@ -10,9 +10,16 @@ workspace = true [dependencies] async-trait.workspace = true +mockall = { workspace = true, optional = true } papyrus_network_types.workspace = true papyrus_proc_macros.workspace = true serde = { workspace = true, features = ["derive"] } starknet_api.workspace = true starknet_sequencer_infra.workspace = true thiserror.workspace = true + +[dev-dependencies] +mockall.workspace = true + +[features] +testing = ["mockall"] \ No newline at end of file diff --git a/crates/starknet_mempool_p2p_types/src/communication.rs b/crates/starknet_mempool_p2p_types/src/communication.rs index 09878fe764..42a9a190d6 100644 --- a/crates/starknet_mempool_p2p_types/src/communication.rs +++ b/crates/starknet_mempool_p2p_types/src/communication.rs @@ -19,6 +19,7 @@ use thiserror::Error; use crate::errors::MempoolP2pPropagatorError; use crate::mempool_p2p_types::MempoolP2pPropagatorResult; +#[cfg_attr(any(feature = "testing", test), mockall::automock)] #[async_trait] pub trait MempoolP2pPropagatorClient: Send + Sync { /// Adds a transaction to be propagated to other peers. This should only be called on a new