Skip to content

Commit

Permalink
Escalation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 21, 2023
1 parent 8793850 commit d951baa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,6 +45,17 @@ impl TxSitterClient {
Ok(response.json().await?)
}

async fn json_get<R>(&self, url: &str) -> eyre::Result<R>
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<Response> {
if !response.status().is_success() {
let body = response.text().await?;
Expand Down Expand Up @@ -77,6 +90,21 @@ impl TxSitterClient {
.await
}

pub async fn get_tx(
&self,
api_key: &ApiKey,
tx_id: &str,
) -> eyre::Result<GetTxResponse> {
Ok(self

Check failure on line 98 in src/client.rs

View workflow job for this annotation

GitHub Actions / cargo test

question mark operator is useless here

Check failure on line 98 in src/client.rs

View workflow job for this annotation

GitHub Actions / cargo test

question mark operator is useless here
.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,
Expand Down
43 changes: 40 additions & 3 deletions tests/escalation.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand All @@ -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 {
Expand All @@ -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<Http>,
value: U256,
) -> eyre::Result<()> {
for _ in 0..24 {
let balance = provider.get_balance(ARBITRARY_ADDRESS, None).await?;

Expand All @@ -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<H256> {
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;
}
}
}

0 comments on commit d951baa

Please sign in to comment.