From f571e9ae256cfdd30f6a5b5d407d3159dfc9d6f8 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 12:27:08 -0300 Subject: [PATCH 1/7] await for transactions in tests --- Cargo.lock | 2 ++ .../bls_aggregation/src/bls_agg_test.rs | 31 +++++++++---------- testing/testing-utils/Cargo.toml | 2 ++ testing/testing-utils/src/transaction.rs | 24 ++++++++++++-- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf5ab8c1..202c3e7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2468,6 +2468,7 @@ dependencies = [ "alloy-network", "alloy-primitives", "alloy-provider", + "alloy-transport", "alloy-transport-http", "eigen-utils", "once_cell", @@ -2475,6 +2476,7 @@ dependencies = [ "serde_json", "testcontainers", "tokio", + "url", ] [[package]] diff --git a/crates/services/bls_aggregation/src/bls_agg_test.rs b/crates/services/bls_aggregation/src/bls_agg_test.rs index dd229d8a..bc74b89c 100644 --- a/crates/services/bls_aggregation/src/bls_agg_test.rs +++ b/crates/services/bls_aggregation/src/bls_agg_test.rs @@ -19,6 +19,7 @@ pub mod integration_test { get_registry_coordinator_address, get_service_manager_address, }, test_data::TestData, + transaction::wait_transaction, }; use eigen_types::{ avs::TaskIndex, @@ -34,7 +35,7 @@ pub mod integration_test { use serde::Deserialize; use sha2::{Digest, Sha256}; use std::time::Duration; - use tokio::{task, time::sleep}; + use tokio::task; use tokio_util::sync::CancellationToken; const PRIVATE_KEY_1: &str = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; // the owner addr @@ -164,7 +165,7 @@ pub mod integration_test { .unwrap(); // Register operator - avs_writer + let tx_hash = avs_writer .register_operator_in_quorum_with_avs_registry_coordinator( bls_key_pair.clone(), salt, @@ -174,9 +175,7 @@ pub mod integration_test { ) .await .unwrap(); - - // Sleep is needed so registered operators are accesible to the OperatorInfoServiceInMemory - sleep(Duration::from_secs(3)).await; + wait_transaction(http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = @@ -332,14 +331,14 @@ pub mod integration_test { let avs_writer = AvsRegistryChainWriter::build_avs_registry_chain_writer( get_test_logger(), - http_endpoint, + http_endpoint.clone(), PRIVATE_KEY_2.to_string(), registry_coordinator_address, operator_state_retriever_address, ) .await .unwrap(); - avs_writer + let tx_hash = avs_writer .register_operator_in_quorum_with_avs_registry_coordinator( bls_key_pair_2.clone(), salt, @@ -349,9 +348,7 @@ pub mod integration_test { ) .await .unwrap(); - - // Sleep is needed so registered operators are accesible to the OperatorInfoServiceInMemory - sleep(Duration::from_secs(3)).await; + wait_transaction(http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = @@ -521,7 +518,7 @@ pub mod integration_test { task::spawn(async move { operators_info_clone.start_service(&token_clone, 0, 0).await }); // Register operator - avs_writer + let tx_hash = avs_writer .register_operator_in_quorum_with_avs_registry_coordinator( bls_key_pair_1.clone(), salt, @@ -531,18 +528,22 @@ pub mod integration_test { ) .await .unwrap(); + wait_transaction(http_endpoint.clone(), tx_hash) + .await + .unwrap(); let avs_writer = AvsRegistryChainWriter::build_avs_registry_chain_writer( // TODO: check if needed get_test_logger(), - http_endpoint, + http_endpoint.clone(), PRIVATE_KEY_2.to_string(), registry_coordinator_address, operator_state_retriever_address, ) .await .unwrap(); - avs_writer + + let tx_hash = avs_writer .register_operator_in_quorum_with_avs_registry_coordinator( bls_key_pair_2.clone(), salt, @@ -552,9 +553,7 @@ pub mod integration_test { ) .await .unwrap(); - - // Sleep is needed so registered operators are accesible to the OperatorInfoServiceInMemory - sleep(Duration::from_secs(3)).await; + wait_transaction(http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = diff --git a/testing/testing-utils/Cargo.toml b/testing/testing-utils/Cargo.toml index 33c0c6e4..11f84baa 100644 --- a/testing/testing-utils/Cargo.toml +++ b/testing/testing-utils/Cargo.toml @@ -15,6 +15,7 @@ workspace = true alloy-network.workspace = true alloy-primitives.workspace = true alloy-provider.workspace = true +alloy-transport.workspace = true alloy-transport-http.workspace = true eigen-utils.workspace = true once_cell.workspace = true @@ -22,3 +23,4 @@ serde.workspace = true serde_json.workspace = true tokio.workspace = true testcontainers.workspace = true +url.workspace = true diff --git a/testing/testing-utils/src/transaction.rs b/testing/testing-utils/src/transaction.rs index f2ff357c..ef3ac2b3 100644 --- a/testing/testing-utils/src/transaction.rs +++ b/testing/testing-utils/src/transaction.rs @@ -1,12 +1,15 @@ -use alloy_primitives::FixedBytes; -use alloy_provider::Provider; +use alloy_primitives::{FixedBytes, TxHash}; +use alloy_provider::{PendingTransactionBuilder, Provider, ProviderBuilder}; +use alloy_transport::TransportResult; use eigen_utils::get_provider; use tokio::time::{sleep, Duration}; +use url::Url; /// Retrieves the status of a transaction from its hash. /// /// # Arguments /// +/// `rpc_url` - The RPC URL. /// `tx_hash` - The hash of the transaction. /// /// # Returns @@ -23,3 +26,20 @@ pub async fn get_transaction_status(rpc_url: String, tx_hash: FixedBytes<32>) -> .map(|receipt| receipt.status()) .unwrap_or(false) } + +/// Wait for a transaction to finish. +/// +/// # Arguments +/// +/// `rpc_url` - The RPC URL. +/// `tx_hash` - The hash of the transaction. +/// +/// # Returns +/// +/// A [`TransportResult`] containing the transaction hash. +pub async fn wait_transaction(rpc_url: String, tx_hash: FixedBytes<32>) -> TransportResult { + let url = Url::parse(&rpc_url).unwrap(); + let root_provider = ProviderBuilder::new().on_http(url); + let pending_tx = PendingTransactionBuilder::new(&root_provider, tx_hash); + pending_tx.watch().await +} From 7815460644f3f4627263102f6ce8d27a23c27324 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 12:31:33 -0300 Subject: [PATCH 2/7] remove sleep in elcontracts writer tests --- .../chainio/clients/elcontracts/src/writer.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/chainio/clients/elcontracts/src/writer.rs b/crates/chainio/clients/elcontracts/src/writer.rs index 7d9606a0..f49643bb 100644 --- a/crates/chainio/clients/elcontracts/src/writer.rs +++ b/crates/chainio/clients/elcontracts/src/writer.rs @@ -200,6 +200,7 @@ mod tests { self, get_delegation_manager_address, get_erc20_mock_strategy, get_service_manager_address, get_strategy_manager_address, }, + transaction::wait_transaction, }; use eigen_types::operator::Operator; use eigen_utils::{ @@ -329,9 +330,10 @@ mod tests { .register_as_operator(operator) .await .unwrap(); + wait_transaction(http_endpoint.clone(), tx_hash) + .await + .unwrap(); - // this sleep is needed so that we wait for the tx to be processed - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); @@ -353,22 +355,24 @@ mod tests { .update_operator_details(operator_modified) .await .unwrap(); + wait_transaction(http_endpoint.clone(), tx_hash) + .await + .unwrap(); - // this sleep is needed so that we wait for the tx to be processed - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); // Third test: deposit_erc20_into_strategy let amount = U256::from_str("100").unwrap(); - let strategy_addr = get_erc20_mock_strategy(http_endpoint).await; + let strategy_addr = get_erc20_mock_strategy(http_endpoint.clone()).await; let tx_hash = el_chain_writer .deposit_erc20_into_strategy(strategy_addr, amount) .await .unwrap(); + wait_transaction(http_endpoint.clone(), tx_hash) + .await + .unwrap(); - // this sleep is needed so that we wait for the tx to be processed - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); } From ba7dd220a9d7a680997598ab6cc05cfe318ec93e Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 12:34:35 -0300 Subject: [PATCH 3/7] clippy --- testing/testing-utils/src/transaction.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/testing/testing-utils/src/transaction.rs b/testing/testing-utils/src/transaction.rs index ef3ac2b3..9fc9ea91 100644 --- a/testing/testing-utils/src/transaction.rs +++ b/testing/testing-utils/src/transaction.rs @@ -1,6 +1,6 @@ use alloy_primitives::{FixedBytes, TxHash}; use alloy_provider::{PendingTransactionBuilder, Provider, ProviderBuilder}; -use alloy_transport::TransportResult; +use alloy_transport::{TransportErrorKind, TransportResult}; use eigen_utils::get_provider; use tokio::time::{sleep, Duration}; use url::Url; @@ -38,7 +38,8 @@ pub async fn get_transaction_status(rpc_url: String, tx_hash: FixedBytes<32>) -> /// /// A [`TransportResult`] containing the transaction hash. pub async fn wait_transaction(rpc_url: String, tx_hash: FixedBytes<32>) -> TransportResult { - let url = Url::parse(&rpc_url).unwrap(); + let url = + Url::parse(&rpc_url).map_err(|_| TransportErrorKind::custom_str("Invalid RPC URL"))?; let root_provider = ProviderBuilder::new().on_http(url); let pending_tx = PendingTransactionBuilder::new(&root_provider, tx_hash); pending_tx.watch().await From 3fabe7a07b1978f6eeba0387b9a2891a8bc0a217 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 12:46:44 -0300 Subject: [PATCH 4/7] remove sleep in operatorsinfo tests --- .../services/operatorsinfo/src/operatorsinfo_inmemory.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs b/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs index 54d98ede..da3d54af 100644 --- a/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs +++ b/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs @@ -296,6 +296,7 @@ mod tests { get_operator_state_retriever_address, get_registry_coordinator_address, get_strategy_manager_address, }; + use eigen_testing_utils::transaction::wait_transaction; use eigen_types::operator::Operator; use eigen_utils::get_provider; use std::str::FromStr; @@ -389,9 +390,6 @@ mod tests { ) .await; - // need to wait at least 3 seconds to get the event processed - tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; - cancel_token.clone().cancel(); let address = address!("f39fd6e51aad88f6f4ce6ab8827279cfffb92266"); @@ -448,7 +446,6 @@ mod tests { "8949062771264691130193054363356855357736539613420316273398900351143637925935", ) .await; - tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; // need to wait atleast 3 second to get the event processed cancel_token.clone().cancel(); @@ -528,7 +525,7 @@ mod tests { let quorum_numbers = Bytes::from_str("0x00").unwrap(); let socket = "socket"; - let _ = avs_registry_writer + let tx_hash = avs_registry_writer .register_operator_in_quorum_with_avs_registry_coordinator( bls_key_pair, salt, @@ -538,5 +535,6 @@ mod tests { ) .await .unwrap(); + wait_transaction(http_endpoint, tx_hash).await.unwrap(); } } From b3cb35e7d41987bb216ab636b7bbc66834f8f40a Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 12:47:37 -0300 Subject: [PATCH 5/7] remove clone --- crates/chainio/clients/elcontracts/src/writer.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/chainio/clients/elcontracts/src/writer.rs b/crates/chainio/clients/elcontracts/src/writer.rs index f49643bb..62f5a58c 100644 --- a/crates/chainio/clients/elcontracts/src/writer.rs +++ b/crates/chainio/clients/elcontracts/src/writer.rs @@ -369,9 +369,7 @@ mod tests { .deposit_erc20_into_strategy(strategy_addr, amount) .await .unwrap(); - wait_transaction(http_endpoint.clone(), tx_hash) - .await - .unwrap(); + wait_transaction(http_endpoint, tx_hash).await.unwrap(); let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); From cddff1688f81df3aa1bb9ebad0985c554f8cceb2 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 15:02:44 -0300 Subject: [PATCH 6/7] typo --- crates/services/bls_aggregation/src/bls_agg.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/services/bls_aggregation/src/bls_agg.rs b/crates/services/bls_aggregation/src/bls_agg.rs index 80147481..78ac3e21 100644 --- a/crates/services/bls_aggregation/src/bls_agg.rs +++ b/crates/services/bls_aggregation/src/bls_agg.rs @@ -152,7 +152,7 @@ impl BlsAggregatorService Ok(()) } - /// Processs signatures received from the channel and sends + /// Processes signatures received from the channel and sends /// the signed task response to the task channel. /// /// # Arguments From fab550c3eb6401c037236ded90d13eeae1cf917a Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 27 Sep 2024 15:12:28 -0300 Subject: [PATCH 7/7] refactor get_transaction_status util --- Cargo.lock | 1 - crates/chainio/clients/elcontracts/src/writer.rs | 10 +++------- crates/services/bls_aggregation/src/bls_agg_test.rs | 10 ++++------ .../operatorsinfo/src/operatorsinfo_inmemory.rs | 2 +- testing/testing-utils/Cargo.toml | 1 - testing/testing-utils/src/transaction.rs | 13 ++++++------- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 202c3e7d..b1d427df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2475,7 +2475,6 @@ dependencies = [ "serde", "serde_json", "testcontainers", - "tokio", "url", ] diff --git a/crates/chainio/clients/elcontracts/src/writer.rs b/crates/chainio/clients/elcontracts/src/writer.rs index 62f5a58c..4f2e3cfc 100644 --- a/crates/chainio/clients/elcontracts/src/writer.rs +++ b/crates/chainio/clients/elcontracts/src/writer.rs @@ -330,9 +330,7 @@ mod tests { .register_as_operator(operator) .await .unwrap(); - wait_transaction(http_endpoint.clone(), tx_hash) - .await - .unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); @@ -355,9 +353,7 @@ mod tests { .update_operator_details(operator_modified) .await .unwrap(); - wait_transaction(http_endpoint.clone(), tx_hash) - .await - .unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); @@ -369,7 +365,7 @@ mod tests { .deposit_erc20_into_strategy(strategy_addr, amount) .await .unwrap(); - wait_transaction(http_endpoint, tx_hash).await.unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap(); assert!(receipt.unwrap().status()); diff --git a/crates/services/bls_aggregation/src/bls_agg_test.rs b/crates/services/bls_aggregation/src/bls_agg_test.rs index bc74b89c..dcb29800 100644 --- a/crates/services/bls_aggregation/src/bls_agg_test.rs +++ b/crates/services/bls_aggregation/src/bls_agg_test.rs @@ -175,7 +175,7 @@ pub mod integration_test { ) .await .unwrap(); - wait_transaction(http_endpoint, tx_hash).await.unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = @@ -348,7 +348,7 @@ pub mod integration_test { ) .await .unwrap(); - wait_transaction(http_endpoint, tx_hash).await.unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = @@ -528,9 +528,7 @@ pub mod integration_test { ) .await .unwrap(); - wait_transaction(http_endpoint.clone(), tx_hash) - .await - .unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); let avs_writer = AvsRegistryChainWriter::build_avs_registry_chain_writer( // TODO: check if needed @@ -553,7 +551,7 @@ pub mod integration_test { ) .await .unwrap(); - wait_transaction(http_endpoint, tx_hash).await.unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); // Create aggregation service let avs_registry_service = diff --git a/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs b/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs index da3d54af..73953d1e 100644 --- a/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs +++ b/crates/services/operatorsinfo/src/operatorsinfo_inmemory.rs @@ -535,6 +535,6 @@ mod tests { ) .await .unwrap(); - wait_transaction(http_endpoint, tx_hash).await.unwrap(); + wait_transaction(&http_endpoint, tx_hash).await.unwrap(); } } diff --git a/testing/testing-utils/Cargo.toml b/testing/testing-utils/Cargo.toml index 11f84baa..5b5ae002 100644 --- a/testing/testing-utils/Cargo.toml +++ b/testing/testing-utils/Cargo.toml @@ -21,6 +21,5 @@ eigen-utils.workspace = true once_cell.workspace = true serde.workspace = true serde_json.workspace = true -tokio.workspace = true testcontainers.workspace = true url.workspace = true diff --git a/testing/testing-utils/src/transaction.rs b/testing/testing-utils/src/transaction.rs index 9fc9ea91..dfdfbf31 100644 --- a/testing/testing-utils/src/transaction.rs +++ b/testing/testing-utils/src/transaction.rs @@ -2,7 +2,6 @@ use alloy_primitives::{FixedBytes, TxHash}; use alloy_provider::{PendingTransactionBuilder, Provider, ProviderBuilder}; use alloy_transport::{TransportErrorKind, TransportResult}; use eigen_utils::get_provider; -use tokio::time::{sleep, Duration}; use url::Url; /// Retrieves the status of a transaction from its hash. @@ -16,10 +15,11 @@ use url::Url; /// /// A bool indicating wether the transaction was successful or not. pub async fn get_transaction_status(rpc_url: String, tx_hash: FixedBytes<32>) -> bool { - // this sleep is needed so that we wait for the tx to be processed - sleep(Duration::from_millis(500)).await; + if wait_transaction(&rpc_url, tx_hash).await.is_err() { + return false; + } + get_provider(&rpc_url) - .clone() .get_transaction_receipt(tx_hash) .await .unwrap_or(None) @@ -37,9 +37,8 @@ pub async fn get_transaction_status(rpc_url: String, tx_hash: FixedBytes<32>) -> /// # Returns /// /// A [`TransportResult`] containing the transaction hash. -pub async fn wait_transaction(rpc_url: String, tx_hash: FixedBytes<32>) -> TransportResult { - let url = - Url::parse(&rpc_url).map_err(|_| TransportErrorKind::custom_str("Invalid RPC URL"))?; +pub async fn wait_transaction(rpc_url: &str, tx_hash: FixedBytes<32>) -> TransportResult { + let url = Url::parse(rpc_url).map_err(|_| TransportErrorKind::custom_str("Invalid RPC URL"))?; let root_provider = ProviderBuilder::new().on_http(url); let pending_tx = PendingTransactionBuilder::new(&root_provider, tx_hash); pending_tx.watch().await