Skip to content

Commit

Permalink
Use driver directly
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jan 30, 2025
1 parent 5fc831e commit 321f9bc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand All @@ -21,7 +21,6 @@ struct Inner {
banned_solvers: dashmap::DashMap<eth::Address, Instant>,
ttl: Duration,
last_auctions_count: u32,
db_validator_accepted_solvers: HashSet<eth::Address>,
}

impl Validator {
Expand All @@ -31,14 +30,12 @@ impl Validator {
settlement_updates_receiver: tokio::sync::mpsc::UnboundedReceiver<()>,
ttl: Duration,
last_auctions_count: u32,
db_validator_accepted_solvers: HashSet<eth::Address>,
) -> 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);
Expand Down Expand Up @@ -94,18 +91,18 @@ impl Validator {

#[async_trait::async_trait]
impl super::Validator for Validator {
async fn is_allowed(&self, solver: &eth::Address) -> anyhow::Result<bool> {
async fn is_allowed(&self, driver: &Driver) -> anyhow::Result<bool> {
// 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<eth::Address>,
) -> Self {
let mut validators: Vec<Box<dyn Validator + Send + Sync>> = Vec::new();

Expand All @@ -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));
}
Expand All @@ -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: &eth::Address) -> anyhow::Result<bool> {
pub async fn can_participate(&self, driver: &Driver) -> anyhow::Result<bool> {
for validator in &self.0.validators {
if !validator.is_allowed(solver).await? {
if !validator.is_allowed(driver).await? {
return Ok(false);
}
}
Expand All @@ -68,5 +65,5 @@ impl SolverParticipationGuard {

#[async_trait::async_trait]
trait Validator: Send + Sync {
async fn is_allowed(&self, solver: &eth::Address) -> anyhow::Result<bool>;
async fn is_allowed(&self, driver: &Driver) -> anyhow::Result<bool>;
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -8,12 +8,12 @@ pub(super) struct Validator {

#[async_trait::async_trait]
impl super::Validator for Validator {
async fn is_allowed(&self, solver: &eth::Address) -> anyhow::Result<bool> {
async fn is_allowed(&self, solver: &Driver) -> anyhow::Result<bool> {
Ok(self
.eth
.contracts()
.authenticator()
.is_solver(solver.0)
.is_solver(solver.submission_address.0)
.call()
.await?)
}
Expand Down
22 changes: 6 additions & 16 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use {
token_list::{AutoUpdatingTokenList, TokenListConfiguration},
},
std::{
collections::HashSet,
sync::{Arc, RwLock},
time::{Duration, Instant},
},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::<HashSet<_, _>>(),
);

let run = RunLoop::new(
run_loop_config,
eth,
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ impl RunLoop {
request: &solve::Request,
) -> Result<Vec<Result<competition::Solution, domain::competition::SolutionError>>, 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
}
Expand Down

0 comments on commit 321f9bc

Please sign in to comment.