Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ app-data = { workspace = true, features = ["test_helpers"] }
contracts = { workspace = true }
ethrpc = { workspace = true, features = ["test-util"] }
maplit = { workspace = true }
mockall = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["process", "test-util"] }

Expand Down
55 changes: 35 additions & 20 deletions crates/driver/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use {
time::Instant,
},
tokio::{sync::mpsc, task},
tracing::{Instrument, instrument},
tracing::Instrument,
};

pub mod auction;
Expand Down Expand Up @@ -339,28 +339,51 @@ impl Competition {
Self::sort_orders(auction, solver_address, order_sorting_strategies)
});

// We can sort the orders and fetch auction data in parallel
// We can sort the orders and fetch auction data in parallel.
let (auction, balances, app_data) =
tokio::join!(sort_orders_future, tasks.balances, tasks.app_data);

let auction = Self::run_blocking_with_timer("update_orders", move || {
let mut auction = Self::run_blocking_with_timer("update_orders", move || {
// Same as before with sort_orders, we use spawn_blocking() because a lot of CPU
// bound computations are happening and we want to avoid blocking
// the runtime.
Self::update_orders(auction, balances, app_data, cow_amm_orders)
})
.await;

let risk_detector = self.risk_detector.clone();
let flashloans_enabled = self.solver.config().flashloans_enabled;
let liquidity_mode = self.solver.liquidity();

// We can run bad token filtering and liquidity fetching in parallel
let (liquidity, auction) = tokio::join!(
async {
match self.solver.liquidity() {
solver::Liquidity::Fetch => tasks.liquidity.await,
solver::Liquidity::Skip => Arc::new(Vec::new()),
let (auction, liquidity) = tokio::join!(
tokio::spawn(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokio::spawn returns detached JoinHandles. tokio::join! awaits them, but if the outer solve() future is dropped (e.g. client disconnect, overload shed), the spawned tasks keep running to completion. The previous inline tokio::join!(async { … }, future) would have been cancelled with the parent.

Copy link
Copy Markdown
Contributor Author

@metalurgical metalurgical May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. Though this behavior is not new here, it already exists with run_blocking_with_timer.

I would like to suggest that you have a look at #4379. The same concept there can be used for this as well, just couple it with a scopeguard. This is also probably better implemented there as well, else it is duplicating efforts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be solveable by simply using a cancellationtoken + dropguard?
The PR you're pointing to is huge and the scope/steps need to be discussed

Copy link
Copy Markdown
Contributor Author

@metalurgical metalurgical May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is it.

The PR I am pointing to already has the cancellation token threaded through. It appears large but much of it is tests and plumbing. Did subsequently open an issue for it, #4380. So it just needs the drop guard added to it (and an patched in here) then the above behavior mentioned here will be handled as well.

Happy to discuss scope and steps further.

async move {
risk_detector
.without_unsupported_orders(&mut auction.orders, flashloans_enabled)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, the quote path only ran token quality filtering. Flashloan filtering only happened in solve(). Now, without_unsupported_orders does both, so a solver with flashloans_enabled = false will start returning UnsupportedToken at quote time for flashloan orders. That's probably the right behavior (fail fast instead of silently quoting something the solver can't settle), but it's a semantic change that isn't called out in the PR description and isn't covered by an explicit test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to quote.rs is mentioned in the description, perhaps it was not explicit enough?

Logically the solver config should be used here, unless there is a specific reason not to? Could you confirm the exact behavior that is expected here?

It does not appear that a test can be written for this currently.

.await;
auction
}
},
self.without_unsupported_orders(auction)
.in_current_span(),
),
tokio::spawn(
async move {
match liquidity_mode {
solver::Liquidity::Fetch => tasks.liquidity.await,
solver::Liquidity::Skip => Arc::new(Vec::new()),
}
}
.in_current_span(),
),
);
let auction = auction.map_err(|err| {
tracing::error!(?err, "order filtering task failed");
Error::InternalError(err.to_string())
})?;
let liquidity = liquidity.map_err(|err| {
tracing::error!(?err, "liquidity fetch task failed");
Error::InternalError(err.to_string())
})?;

let elapsed = start.elapsed();
metrics::get()
Expand Down Expand Up @@ -951,16 +974,6 @@ impl Competition {
}
Ok(())
}

#[instrument(skip_all)]
async fn without_unsupported_orders(&self, mut auction: Auction) -> Auction {
if !self.solver.config().flashloans_enabled {
auction.orders.retain(|o| o.app_data.flashloan().is_none());
}
self.risk_detector
.filter_unsupported_orders_in_auction(auction)
.await
}
}

const MAX_SOLUTIONS_TO_MERGE: usize = 10;
Expand Down Expand Up @@ -1077,4 +1090,6 @@ pub enum Error {
NoValidOrdersFound,
#[error("could not parse the request")]
MalformedRequest,
#[error("internal error: {0}")]
InternalError(String),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use {
infra::{self, observe::metrics},
},
bad_tokens::{TokenQuality, trace_call::TraceCallDetectorRaw},
eth_domain_types as eth,
futures::FutureExt,
model::interaction::InteractionData,
request_sharing::BoxRequestSharing,
std::{
ops::Deref,
sync::Arc,
time::{Duration, Instant},
},
Expand All @@ -21,6 +23,29 @@ use {
#[derive(Clone)]
pub struct Detector(Arc<Inner>);

#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait DetectorApi: Send + Sync {
async fn determine_sell_token_quality(&self, order: &Order, now: Instant) -> Quality;
fn get_quality(&self, token: &eth::TokenAddress, now: Instant) -> Quality;
fn evict_outdated_entries(&self);
}

#[async_trait::async_trait]
impl DetectorApi for Detector {
async fn determine_sell_token_quality(&self, order: &Order, now: Instant) -> Quality {
self.determine_sell_token_quality(order, now).await
}

fn get_quality(&self, token: &eth::TokenAddress, now: Instant) -> Quality {
Deref::deref(self).get_quality(token, now)
}

fn evict_outdated_entries(&self) {
Deref::deref(self).evict_outdated_entries()
}
}

struct Inner {
cache: Cache,
detector: TraceCallDetectorRaw,
Expand Down Expand Up @@ -107,7 +132,7 @@ impl Detector {
}
}

impl std::ops::Deref for Detector {
impl Deref for Detector {
type Target = Cache;

fn deref(&self) -> &Self::Target {
Expand Down
Loading
Loading