From 321f9bc73ac2fe87d79f6ae5683c57926961c432 Mon Sep 17 00:00:00 2001 From: ilya Date: Thu, 30 Jan 2025 17:17:55 +0000 Subject: [PATCH] Use driver directly --- .../competition/participation_guard/db.rs | 13 +++++------ .../competition/participation_guard/mod.rs | 13 +++++------ .../participation_guard/onchain.rs | 6 ++--- crates/autopilot/src/run.rs | 22 +++++-------------- crates/autopilot/src/run_loop.rs | 2 +- 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/crates/autopilot/src/domain/competition/participation_guard/db.rs b/crates/autopilot/src/domain/competition/participation_guard/db.rs index f765b39418..0a8dba9f7d 100644 --- a/crates/autopilot/src/domain/competition/participation_guard/db.rs +++ b/crates/autopilot/src/domain/competition/participation_guard/db.rs @@ -2,10 +2,10 @@ use { crate::{ database::Postgres, domain::{eth, Metrics}, + infra::Driver, }, ethrpc::block_stream::CurrentBlockWatcher, std::{ - collections::HashSet, sync::Arc, time::{Duration, Instant}, }, @@ -21,7 +21,6 @@ struct Inner { banned_solvers: dashmap::DashMap, ttl: Duration, last_auctions_count: u32, - db_validator_accepted_solvers: HashSet, } impl Validator { @@ -31,14 +30,12 @@ impl Validator { settlement_updates_receiver: tokio::sync::mpsc::UnboundedReceiver<()>, ttl: Duration, last_auctions_count: u32, - db_validator_accepted_solvers: HashSet, ) -> Self { let self_ = Self(Arc::new(Inner { db, banned_solvers: Default::default(), ttl, last_auctions_count, - db_validator_accepted_solvers, })); self_.start_maintenance(settlement_updates_receiver, current_block); @@ -94,18 +91,18 @@ impl Validator { #[async_trait::async_trait] impl super::Validator for Validator { - async fn is_allowed(&self, solver: ð::Address) -> anyhow::Result { + async fn is_allowed(&self, driver: &Driver) -> anyhow::Result { // Check if solver accepted this feature. This should be removed once a CIP is // approved. - if !self.0.db_validator_accepted_solvers.contains(solver) { + if !driver.accepts_unsettled_blocking { return Ok(true); } - if let Some(entry) = self.0.banned_solvers.get(solver) { + if let Some(entry) = self.0.banned_solvers.get(&driver.submission_address) { if Instant::now().duration_since(*entry.value()) < self.0.ttl { return Ok(false); } else { - self.0.banned_solvers.remove(solver); + self.0.banned_solvers.remove(&driver.submission_address); } } diff --git a/crates/autopilot/src/domain/competition/participation_guard/mod.rs b/crates/autopilot/src/domain/competition/participation_guard/mod.rs index d0127e68d5..7b4bf53157 100644 --- a/crates/autopilot/src/domain/competition/participation_guard/mod.rs +++ b/crates/autopilot/src/domain/competition/participation_guard/mod.rs @@ -5,10 +5,9 @@ use { crate::{ arguments::DbBasedSolverParticipationGuardConfig, database::Postgres, - domain::eth, - infra::Ethereum, + infra::{Driver, Ethereum}, }, - std::{collections::HashSet, sync::Arc}, + std::sync::Arc, }; /// This struct checks whether a solver can participate in the competition by @@ -27,7 +26,6 @@ impl SolverParticipationGuard { db: Postgres, settlement_updates_receiver: tokio::sync::mpsc::UnboundedReceiver<()>, db_based_validator_config: DbBasedSolverParticipationGuardConfig, - db_validator_accepted_solvers: HashSet, ) -> Self { let mut validators: Vec> = Vec::new(); @@ -39,7 +37,6 @@ impl SolverParticipationGuard { settlement_updates_receiver, db_based_validator_config.solver_blacklist_cache_ttl, db_based_validator_config.solver_last_auctions_participation_count, - db_validator_accepted_solvers, ); validators.push(Box::new(database_solver_participation_validator)); } @@ -55,9 +52,9 @@ impl SolverParticipationGuard { /// the following order: /// 1. DB-based validator: operates fast since it uses in-memory cache. /// 2. Onchain-based validator: only then calls the Authenticator contract. - pub async fn can_participate(&self, solver: ð::Address) -> anyhow::Result { + pub async fn can_participate(&self, driver: &Driver) -> anyhow::Result { for validator in &self.0.validators { - if !validator.is_allowed(solver).await? { + if !validator.is_allowed(driver).await? { return Ok(false); } } @@ -68,5 +65,5 @@ impl SolverParticipationGuard { #[async_trait::async_trait] trait Validator: Send + Sync { - async fn is_allowed(&self, solver: ð::Address) -> anyhow::Result; + async fn is_allowed(&self, driver: &Driver) -> anyhow::Result; } diff --git a/crates/autopilot/src/domain/competition/participation_guard/onchain.rs b/crates/autopilot/src/domain/competition/participation_guard/onchain.rs index 82d0ef3fb7..3e472b298e 100644 --- a/crates/autopilot/src/domain/competition/participation_guard/onchain.rs +++ b/crates/autopilot/src/domain/competition/participation_guard/onchain.rs @@ -1,4 +1,4 @@ -use crate::{domain::eth, infra::Ethereum}; +use crate::infra::{Driver, Ethereum}; /// Calls Authenticator contract to check if a solver has a sufficient /// permission. @@ -8,12 +8,12 @@ pub(super) struct Validator { #[async_trait::async_trait] impl super::Validator for Validator { - async fn is_allowed(&self, solver: ð::Address) -> anyhow::Result { + async fn is_allowed(&self, solver: &Driver) -> anyhow::Result { Ok(self .eth .contracts() .authenticator() - .is_solver(solver.0) + .is_solver(solver.submission_address.0) .call() .await?) } diff --git a/crates/autopilot/src/run.rs b/crates/autopilot/src/run.rs index 8c8f2b7834..2f426a1967 100644 --- a/crates/autopilot/src/run.rs +++ b/crates/autopilot/src/run.rs @@ -52,7 +52,6 @@ use { token_list::{AutoUpdatingTokenList, TokenListConfiguration}, }, std::{ - collections::HashSet, sync::{Arc, RwLock}, time::{Duration, Instant}, }, @@ -369,6 +368,12 @@ pub async fn run(args: Arguments) { let (settlement_updates_sender, settlement_updates_receiver) = tokio::sync::mpsc::unbounded_channel(); + let solver_participation_guard = SolverParticipationGuard::new( + eth.clone(), + db.clone(), + settlement_updates_receiver, + args.db_based_solver_participation_guard, + ); let persistence = infra::persistence::Persistence::new(args.s3.into().unwrap(), Arc::new(db.clone())).await; let settlement_observer = crate::domain::settlement::Observer::new( @@ -578,21 +583,6 @@ pub async fn run(args: Arguments) { .into_iter() .collect(); - let solver_participation_guard = SolverParticipationGuard::new( - eth.clone(), - db.clone(), - settlement_updates_receiver, - args.db_based_solver_participation_guard, - drivers - .iter() - .filter_map(|driver| { - driver - .accepts_unsettled_blocking - .then_some(driver.submission_address) - }) - .collect::>(), - ); - let run = RunLoop::new( run_loop_config, eth, diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index 492ca18751..4102f675ea 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -736,7 +736,7 @@ impl RunLoop { request: &solve::Request, ) -> Result>, SolveError> { - let can_participate = self.solver_participation_guard.can_participate(&driver.submission_address).await.map_err(|err| { + let can_participate = self.solver_participation_guard.can_participate(driver).await.map_err(|err| { tracing::error!(?err, driver = %driver.name, ?driver.submission_address, "solver participation check failed"); SolveError::SolverDenyListed }