Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Nov 22, 2023
1 parent cc02754 commit 6230f32
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# tx-sitter-service (proper name pending)
# Tx Sitter Monolith

A monolithized version of the [tx-sitter](https://github.com/worldcoin/tx-sitter-aws/).

## Testing locally

Expand All @@ -9,3 +11,10 @@ Copy `.env.example` to `.env` or set `RUST_LOG=info,service=debug` to have loggi
3. Start the service `cargo run`

This will use the `config.toml` configuration.

If you have [nushell](https://www.nushell.sh/) installed, `nu manual_test.nu` can be run to execute a basic test.

## Running tests
While you obviously can run tests with `cargo test --workspace` some tests take quite a long time (due to spinning up an anvil node, sending txs, etc.).

Therefore I recommend [cargo-nextest](https://nexte.st/) as it runs all the tests in parallel. Once installed `cargo nextest run --workspace` can be used instead.
6 changes: 5 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
6. [ ] Metrics
7. [ ] Tracing (add telemetry-batteries)
8. [ ] Automated testing
9. [ ] Parallelization in a few places
1. [x] Basic
2. [ ] Basic with contracts
3. [ ] Escalation testing
4. [ ] Reorg testing (how?!?)
9. [ ] Parallelization in a few places
3 changes: 3 additions & 0 deletions src/tasks/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::time::Duration;

use ethers::providers::{Http, Middleware, Provider};
use ethers::types::{Block, BlockNumber, H256, U256};
Expand Down Expand Up @@ -76,6 +77,8 @@ pub async fn index_blocks(app: Arc<App>) -> eyre::Result<()> {
)
.await?;
}
} else {
tokio::time::sleep(Duration::from_secs(5)).await;
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use service::config::{
};
use service::service::Service;
use tokio::task::JoinHandle;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;

pub type AppMiddleware = SignerMiddleware<Arc<Provider<Http>>, LocalWallet>;

Expand Down Expand Up @@ -47,6 +51,18 @@ impl DoubleAnvilHandle {
}
}

pub fn setup_tracing() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().pretty().compact())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
// Logging from fake_rpc can get very messy so we set it to warn only
.parse_lossy("info,fake_rpc=warn"),
)
.init();
}

pub async fn setup_db() -> eyre::Result<(String, DockerContainerGuard)> {
let db_container = postgres_docker_utils::setup().await?;
let db_socket_addr = db_container.address();
Expand Down
7 changes: 3 additions & 4 deletions tests/create_relayer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::time::Duration;

use common::setup_db;
use service::server::data::{CreateRelayerRequest, CreateRelayerResponse};

use crate::common::{
setup_double_anvil, setup_service, DEFAULT_ANVIL_CHAIN_ID,
};
use crate::common::*;

mod common;

const ESCALATION_INTERVAL: Duration = Duration::from_secs(30);

#[tokio::test]
async fn create_relayer() -> eyre::Result<()> {
setup_tracing();

let (db_url, _db_container) = setup_db().await?;
let double_anvil = setup_double_anvil().await?;

Expand Down
101 changes: 101 additions & 0 deletions tests/send_many_txs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::time::Duration;

use ethers::providers::Middleware;
use ethers::types::{Eip1559TransactionRequest, U256};
use ethers::utils::parse_units;
use service::server::data::{
CreateRelayerRequest, CreateRelayerResponse, SendTxRequest, SendTxResponse,
};

mod common;

use crate::common::*;

const ESCALATION_INTERVAL: Duration = Duration::from_secs(30);

#[tokio::test]
async fn send_many_txs() -> eyre::Result<()> {
setup_tracing();

let (db_url, _db_container) = setup_db().await?;
let double_anvil = setup_double_anvil().await?;

let service =
setup_service(&double_anvil.local_addr(), &db_url, ESCALATION_INTERVAL)
.await?;

let addr = service.local_addr();

let response = reqwest::Client::new()
.post(&format!("http://{}/1/relayer/create", addr))
.json(&CreateRelayerRequest {
name: "Test relayer".to_string(),
chain_id: DEFAULT_ANVIL_CHAIN_ID,
})
.send()
.await?;

let response: CreateRelayerResponse = response.json().await?;

// Fund the relayer
let middleware = setup_middleware(
format!("http://{}", double_anvil.local_addr()),
DEFAULT_ANVIL_PRIVATE_KEY,
)
.await?;

let amount: U256 = parse_units("1000", "ether")?.into();

middleware
.send_transaction(
Eip1559TransactionRequest {
to: Some(response.address.into()),
value: Some(amount),
..Default::default()
},
None,
)
.await?
.await?;

let provider = middleware.provider();

let current_balance = provider.get_balance(response.address, None).await?;
assert_eq!(current_balance, amount);

// Send a transaction
let value: U256 = parse_units("10", "ether")?.into();
let num_transfers = 10;
let relayer_id = response.relayer_id;

for _ in 0..num_transfers {
let response = reqwest::Client::new()
.post(&format!("http://{}/1/tx/send", addr))
.json(&SendTxRequest {
relayer_id: relayer_id.clone(),
to: ARBITRARY_ADDRESS,
value,
gas_limit: U256::from(21_000),
..Default::default()
})
.send()
.await?;

let _response: SendTxResponse = response.json().await?;
}

let expected_balance = value * num_transfers;
for _ in 0..50 {
let balance = provider.get_balance(ARBITRARY_ADDRESS, None).await?;

tracing::info!(?balance, ?expected_balance, "Checking balance");

if balance == expected_balance {
return Ok(());
} else {
tokio::time::sleep(Duration::from_secs(5)).await;
}
}

panic!("Transactions were not sent")
}
2 changes: 2 additions & 0 deletions tests/send_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const ESCALATION_INTERVAL: Duration = Duration::from_secs(30);

#[tokio::test]
async fn send_tx() -> eyre::Result<()> {
setup_tracing();

let (db_url, _db_container) = setup_db().await?;
let double_anvil = setup_double_anvil().await?;

Expand Down

0 comments on commit 6230f32

Please sign in to comment.