From d951baa36c88a3c35674d4c2eaf44efe79bcce09 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Thu, 21 Dec 2023 15:22:44 +0100 Subject: [PATCH] Escalation test --- src/client.rs | 30 +++++++++++++++++++++++++++++- tests/escalation.rs | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index b479e5d..1ae30cf 100644 --- a/src/client.rs +++ b/src/client.rs @@ -5,7 +5,9 @@ use crate::server::routes::network::NewNetworkInfo; use crate::server::routes::relayer::{ CreateApiKeyResponse, CreateRelayerRequest, CreateRelayerResponse, }; -use crate::server::routes::transaction::{SendTxRequest, SendTxResponse}; +use crate::server::routes::transaction::{ + GetTxResponse, SendTxRequest, SendTxResponse, +}; pub struct TxSitterClient { client: reqwest::Client, @@ -43,6 +45,17 @@ impl TxSitterClient { Ok(response.json().await?) } + async fn json_get(&self, url: &str) -> eyre::Result + where + R: serde::de::DeserializeOwned, + { + let response = self.client.get(url).send().await?; + + let response = Self::validate_response(response).await?; + + Ok(response.json().await?) + } + async fn validate_response(response: Response) -> eyre::Result { if !response.status().is_success() { let body = response.text().await?; @@ -77,6 +90,21 @@ impl TxSitterClient { .await } + pub async fn get_tx( + &self, + api_key: &ApiKey, + tx_id: &str, + ) -> eyre::Result { + Ok(self + .json_get(&format!( + "{}/1/api/{api_key}/tx/{tx_id}", + self.url, + api_key = api_key, + tx_id = tx_id + )) + .await?) + } + pub async fn create_network( &self, chain_id: u64, diff --git a/tests/escalation.rs b/tests/escalation.rs index ae7ad0d..e446462 100644 --- a/tests/escalation.rs +++ b/tests/escalation.rs @@ -1,11 +1,15 @@ mod common; +use ethers::prelude::{Http, Provider}; +use ethers::types::H256; +use tx_sitter::api_key::ApiKey; +use tx_sitter::client::TxSitterClient; use tx_sitter::server::routes::relayer::CreateApiKeyResponse; use crate::common::prelude::*; const ESCALATION_INTERVAL: Duration = Duration::from_secs(2); -const ANVIL_BLOCK_TIME: u64 = 12; +const ANVIL_BLOCK_TIME: u64 = 6; #[tokio::test] async fn escalation() -> eyre::Result<()> { @@ -24,7 +28,7 @@ async fn escalation() -> eyre::Result<()> { // Send a transaction let value: U256 = parse_units("1", "ether")?.into(); - client + let tx = client .send_tx( &api_key, &SendTxRequest { @@ -36,6 +40,23 @@ async fn escalation() -> eyre::Result<()> { ) .await?; + let initial_tx_hash = get_tx_hash(&client, &api_key, &tx.tx_id).await?; + + await_balance(&provider, value).await?; + let final_tx_hash = get_tx_hash(&client, &api_key, &tx.tx_id).await?; + + assert_ne!( + initial_tx_hash, final_tx_hash, + "Escalation should have occurred" + ); + + Ok(()) +} + +async fn await_balance( + provider: &Provider, + value: U256, +) -> eyre::Result<()> { for _ in 0..24 { let balance = provider.get_balance(ARBITRARY_ADDRESS, None).await?; @@ -46,5 +67,21 @@ async fn escalation() -> eyre::Result<()> { } } - panic!("Transaction was not sent") + eyre::bail!("Balance not updated in time"); +} + +async fn get_tx_hash( + client: &TxSitterClient, + api_key: &ApiKey, + tx_id: &str, +) -> eyre::Result { + loop { + let tx = client.get_tx(&api_key, tx_id).await?; + + if let Some(tx_hash) = tx.tx_hash { + return Ok(tx_hash); + } else { + tokio::time::sleep(Duration::from_secs(3)).await; + } + } }