From 15ce646ee06b43c4bd91a738924a1294a01e48b9 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Tue, 27 Feb 2024 19:19:30 +0100 Subject: [PATCH] Reenable test --- tests/send_too_many_txs.rs | 107 +++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/tests/send_too_many_txs.rs b/tests/send_too_many_txs.rs index 1876596..35b0959 100644 --- a/tests/send_too_many_txs.rs +++ b/tests/send_too_many_txs.rs @@ -1,65 +1,92 @@ mod common; +use tx_sitter::client::ClientError; +use tx_sitter::server::ApiError; +use tx_sitter::types::{RelayerUpdate, TransactionPriority}; + use crate::common::prelude::*; +const MAX_QUEUED_TXS: usize = 20; + #[tokio::test] async fn send_too_many_txs() -> eyre::Result<()> { setup_tracing(); - panic!("UNIMPLEMENTED!"); - let (db_url, _db_container) = setup_db().await?; let anvil = AnvilBuilder::default().spawn().await?; let (_service, client) = ServiceBuilder::default().build(&anvil, &db_url).await?; - let CreateApiKeyResponse { api_key } = + let CreateApiKeyResponse { api_key: _api_key } = client.create_relayer_api_key(DEFAULT_RELAYER_ID).await?; + let CreateRelayerResponse { + relayer_id: secondary_relayer_id, + address: _secondary_relayer_address, + } = client + .create_relayer(&CreateRelayerRequest { + name: "Secondary Relayer".to_string(), + chain_id: DEFAULT_ANVIL_CHAIN_ID, + }) + .await?; + + let CreateApiKeyResponse { + api_key: secondary_api_key, + } = client.create_relayer_api_key(&secondary_relayer_id).await?; + + // Set max queued txs + client + .update_relayer( + &secondary_relayer_id, + RelayerUpdate::default().with_max_queued_txs(MAX_QUEUED_TXS as u64), + ) + .await?; + let provider = setup_provider(anvil.endpoint()).await?; // Send a transaction - let value: U256 = parse_units("10", "ether")?.into(); - let num_transfers = 10; - - let mut tasks = FuturesUnordered::new(); - for _ in 0..num_transfers { - let client = &client; - tasks.push(async { - client - .send_tx( - &api_key, - &SendTxRequest { - to: ARBITRARY_ADDRESS, - value, - gas_limit: U256::from(21_000), - ..Default::default() - }, - ) - .await?; - - Ok(()) - }); - } + let value: U256 = parse_units("0.01", "ether")?.into(); - while let Some(result) = tasks.next().await { - let result: eyre::Result<()> = result; - result?; + for _ in 0..=MAX_QUEUED_TXS { + client + .send_tx( + &secondary_api_key, + &SendTxRequest { + to: ARBITRARY_ADDRESS, + value, + data: None, + gas_limit: U256::from(21_000), + priority: TransactionPriority::Regular, + tx_id: None, + }, + ) + .await?; } - let expected_balance = value * num_transfers; - for _ in 0..50 { - let balance = provider.get_balance(ARBITRARY_ADDRESS, None).await?; + // Sending one more tx should fail + let result = client + .send_tx( + &secondary_api_key, + &SendTxRequest { + to: ARBITRARY_ADDRESS, + value, + data: None, + gas_limit: U256::from(21_000), + priority: TransactionPriority::Regular, + tx_id: None, + }, + ) + .await; - tracing::info!(?balance, ?expected_balance, "Checking balance"); - - if balance == expected_balance { - return Ok(()); - } else { - tokio::time::sleep(Duration::from_secs(5)).await; - } - } + assert!( + matches!( + result, + Err(ClientError::TxSitter(ApiError::TooManyTransactions { .. })) + ), + "Result {:?} should be too many transactions", + result + ); - panic!("Transactions were not sent") + Ok(()) }