Skip to content

Commit

Permalink
Add legacy solver setup for e2e tests (#2284)
Browse files Browse the repository at this point in the history
# Description
Since the collocated driver upgrades, at Enso we've been running our e2e
tests against quite an old version of this repo. These small changes
would help us upgrade our `services` dependency to the latest commits.

# Changes
- Two new functions in the `e2e` setup module which help external
solvers use the e2e test harness in their own codebases.
- Increase `solve-deadline` to 11s from 2s so that external solvers have
a more realistic duration to return a solution in e2e tests.

## How to test
1. Run existing e2e tests.
2. Enso's e2e tests run correctly against this updated code.
  • Loading branch information
devanoneth authored Jan 17, 2024
1 parent 69a9683 commit 3e8db3f
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 30 deletions.
15 changes: 14 additions & 1 deletion crates/e2e/src/setup/colocation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{nodes::NODE_HOST, setup::*},
ethcontract::{H160, H256},
ethcontract::{H160, H256, U256},
reqwest::Url,
shared::sources::uniswap_v2::UNISWAP_INIT,
tokio::task::JoinHandle,
Expand All @@ -25,6 +25,19 @@ pub async fn start_naive_solver() -> Url {
start_solver(config_file, "naive".to_string()).await
}

pub async fn start_legacy_solver(solver_endpoint: Url, chain_id: Option<U256>) -> Url {
let chain_id = chain_id.unwrap_or(U256::from(1));
let config_file = config_tmp_file(format!(
r#"
chain-id = "{chain_id}"
solver-name = "legacy"
endpoint = "{solver_endpoint}"
"#,
));

start_solver(config_file, "legacy".to_string()).await
}

async fn start_solver(config_file: TempPath, solver_name: String) -> Url {
let args = vec![
"solvers".to_string(),
Expand Down
5 changes: 5 additions & 0 deletions crates/e2e/src/setup/onchain_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ impl OnchainComponents {

let forked_node_api = self.web3.api::<ForkedNodeApi<_>>();

forked_node_api
.set_balance(&auth_manager, to_wei(100))
.await
.expect("could not set auth_manager balance");

let auth_manager = forked_node_api
.impersonate(&auth_manager)
.await
Expand Down
67 changes: 59 additions & 8 deletions crates/e2e/src/setup/services.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::TestAccount,
super::{colocation::start_legacy_solver, TestAccount},
crate::setup::{
colocation::{self, SolverEngine},
wait_for_condition,
Expand All @@ -8,15 +8,15 @@ use {
},
autopilot::infra::persistence::dto,
clap::Parser,
ethcontract::H256,
ethcontract::{H256, U256},
model::{
app_data::{AppDataDocument, AppDataHash},
order::{Order, OrderCreation, OrderUid},
quote::{OrderQuoteRequest, OrderQuoteResponse},
solver_competition::SolverCompetitionAPI,
trade::Trade,
},
reqwest::{Client, StatusCode},
reqwest::{Client, StatusCode, Url},
sqlx::Connection,
std::time::Duration,
};
Expand Down Expand Up @@ -85,13 +85,19 @@ impl<'a> Services<'a> {
}

/// Start the autopilot service in a background task.
pub fn start_autopilot(&self, extra_args: Vec<String>) {
/// Optionally specify a solve deadline to use instead of the default 2s.
/// (note: specifying a larger solve deadline will impact test times as the
/// driver delays the submission of the solution until shortly before the
/// deadline in case the solution would start to revert at some point)
pub fn start_autopilot(&self, solve_deadline: Option<Duration>, extra_args: Vec<String>) {
let solve_deadline = solve_deadline.unwrap_or(Duration::from_secs(2));

let args = [
"autopilot".to_string(),
"--auction-update-interval=1s".to_string(),
format!("--ethflow-contract={:?}", self.contracts.ethflow.address()),
"--skip-event-sync=true".to_string(),
"--solve-deadline=2s".to_string(),
format!("--solve-deadline={solve_deadline:?}"),
]
.into_iter()
.chain(self.api_autopilot_solver_arguments())
Expand Down Expand Up @@ -140,15 +146,60 @@ impl<'a> Services<'a> {
endpoint: solver_endpoint,
}],
);
self.start_autopilot(vec![
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
]);
self.start_autopilot(
None,
vec!["--drivers=test_solver|http://localhost:11088/test_solver".to_string()],
);
self.start_api(vec![
"--price-estimation-drivers=test_solver|http://localhost:11088/test_solver".to_string(),
])
.await;
}

/// Starts a basic version of the protocol with a single legacy solver and
/// quoter.
pub async fn start_protocol_legacy_solver(
&self,
solver: TestAccount,
solver_endpoint: Option<Url>,
quoter_endpoint: Option<Url>,
chain_id: Option<U256>,
) {
let external_solver_endpoint =
solver_endpoint.unwrap_or("http://localhost:8000/solve".parse().unwrap());
let colocated_solver_endpoint =
start_legacy_solver(external_solver_endpoint, chain_id).await;

let external_quoter_endpoint =
quoter_endpoint.unwrap_or("http://localhost:8000/quote".parse().unwrap());
let colocated_quoter_endpoint =
start_legacy_solver(external_quoter_endpoint, chain_id).await;

colocation::start_driver(
self.contracts,
vec![
SolverEngine {
name: "test_solver".into(),
account: solver.clone(),
endpoint: colocated_solver_endpoint,
},
SolverEngine {
name: "test_quoter".into(),
account: solver,
endpoint: colocated_quoter_endpoint,
},
],
);
self.start_autopilot(
Some(Duration::from_secs(11)),
vec!["--drivers=test_solver|http://localhost:11088/test_solver".to_string()],
);
self.start_api(vec![
"--price-estimation-drivers=test_quoter|http://localhost:11088/test_quoter".to_string(),
])
.await;
}

async fn wait_for_api_to_come_up() {
let is_up = || async {
reqwest::get(format!("{API_HOST}{VERSION_ENDPOINT}"))
Expand Down
21 changes: 12 additions & 9 deletions crates/e2e/tests/e2e/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ async fn onchain_settlement_without_liquidity(web3: Web3) {
}],
);
let services = Services::new(onchain.contracts()).await;
services.start_autopilot(vec![
format!(
"--trusted-tokens={weth:#x},{token_a:#x},{token_b:#x}",
weth = onchain.contracts().weth.address(),
token_a = token_a.address(),
token_b = token_b.address()
),
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
]);
services.start_autopilot(
None,
vec![
format!(
"--trusted-tokens={weth:#x},{token_a:#x},{token_b:#x}",
weth = onchain.contracts().weth.address(),
token_a = token_a.address(),
token_b = token_b.address()
),
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
],
);
services.start_api(vec![]).await;

// Place Order
Expand Down
7 changes: 4 additions & 3 deletions crates/e2e/tests/e2e/onchain_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ async fn onchain_settlement(web3: Web3) {

// Only start the autopilot now to ensure that these orders are settled in a
// batch which seems to be expected in this test.
services.start_autopilot(vec![
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
]);
services.start_autopilot(
None,
vec!["--drivers=test_solver|http://localhost:11088/test_solver".to_string()],
);

let balance = token_b.balance_of(trader_a.address()).call().await.unwrap();
assert_eq!(balance, 0.into());
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e/tests/e2e/order_cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn order_cancellation(web3: Web3) {
);

let services = Services::new(onchain.contracts()).await;
services.start_autopilot(vec![]);
services.start_autopilot(None, vec![]);
services.start_api(vec![]).await;

let place_order = |salt: u8| {
Expand Down
13 changes: 8 additions & 5 deletions crates/e2e/tests/e2e/protocol_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,14 @@ async fn execute_test(
endpoint: solver_endpoint,
}],
);
services.start_autopilot(vec![
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
"--fee-policy-skip-market-orders=false".to_string(),
fee_policy.to_string(),
]);
services.start_autopilot(
None,
vec![
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(),
"--fee-policy-skip-market-orders=false".to_string(),
fee_policy.to_string(),
],
);
services
.start_api(vec![
"--price-estimation-drivers=test_solver|http://localhost:11088/test_solver".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e/tests/e2e/refunder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn refunder_tx(web3: Web3) {
.await;

let services = Services::new(onchain.contracts()).await;
services.start_autopilot(vec![]);
services.start_autopilot(None, vec![]);
services.start_api(vec![]).await;

// Get quote id for order placement
Expand Down
7 changes: 5 additions & 2 deletions crates/e2e/tests/e2e/solver_competition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ async fn solver_competition(web3: Web3) {
);

let services = Services::new(onchain.contracts()).await;
services.start_autopilot(vec![
services.start_autopilot(
None,
vec![
"--drivers=solver1|http://localhost:11088/solver1,solver2|http://localhost:11088/solver2"
.to_string(),
]);
],
);
services.start_api(vec![]).await;

// Place Order
Expand Down

0 comments on commit 3e8db3f

Please sign in to comment.