Skip to content

Commit

Permalink
Do not discard the whole auction
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jan 17, 2025
1 parent 56e21d4 commit beca1f5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
59 changes: 34 additions & 25 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use {
infra::{self, blockchain, config::file::OrderPriorityStrategy, observe, Ethereum},
util::{self, Bytes},
},
app_data::ValidatedAppData,
chrono::{Duration, Utc},
futures::future::{join_all, try_join, try_join_all, BoxFuture, FutureExt, Shared},
futures::future::{join, join_all, BoxFuture, FutureExt, Shared},
itertools::Itertools,
model::{order::OrderKind, signature::Signature},
shared::signature_validator::{Contracts, SignatureValidating},
Expand Down Expand Up @@ -150,40 +149,52 @@ impl AuctionProcessor {
/// Process the auction by prioritizing the orders and filtering out
/// unfillable orders. Fetches full app data for each order and returns an
/// auction with updated orders.
pub async fn process(&self, auction: Auction, solver: &eth::H160) -> Result<Auction, Error> {
let (app_data_by_order, mut prioritized_orders) = try_join(
pub async fn process(&self, auction: Auction, solver: &eth::H160) -> Auction {
let (app_data_by_order, mut prioritized_orders) = join(
self.collect_orders_app_data(&auction),
self.prioritize_orders(&auction, solver).map(Ok),
self.prioritize_orders(&auction, solver),
)
.await?;

for order in &mut prioritized_orders {
if let Some(Some(app_data)) = app_data_by_order.get(&order.uid) {
order.app_data = app_data.clone().into();
}
}
.await;

// Filter out orders that failed to fetch app data.
prioritized_orders.retain_mut(|order| {
app_data_by_order.get(&order.uid).map_or(true, |result| {
match result {
Err(err) => {
tracing::warn!(order_uid=?order.uid, ?err, "failed to fetch app data for order, excluding from auction");
false
}
Ok(Some(app_data)) => {
order.app_data = app_data.clone().into();
true
}
Ok(None) => true
}
})
});

Ok(Auction {
Auction {
orders: prioritized_orders,
..auction
})
}
}

/// Fetches the app data for all orders in the auction.
/// Returns a map from order UIDs to the result of fetching the app data.
async fn collect_orders_app_data(
&self,
auction: &Auction,
) -> Result<HashMap<order::Uid, Option<ValidatedAppData>>, Error> {
Ok(try_join_all(auction.orders.iter().map(|order| async {
self.app_data_retriever
.get(order.app_data.hash())
.await
.map(|app_data| (order.uid, app_data))
.map_err(|err| Error::AppDataFetching(order.uid, err))
) -> HashMap<
order::Uid,
Result<Option<app_data::ValidatedAppData>, order::app_data::FetchingError>,
> {
join_all(auction.orders.iter().map(|order| async {
let result = self.app_data_retriever.get(order.app_data.hash()).await;
(order.uid, result)
}))
.await?
.await
.into_iter()
.collect::<HashMap<_, _>>())
.collect::<HashMap<_, _>>()
}

/// Prioritize well priced and filter out unfillable orders from the given
Expand Down Expand Up @@ -672,6 +683,4 @@ pub enum Error {
InvalidAmounts,
#[error("blockchain error: {0:?}")]
Blockchain(#[from] blockchain::Error),
#[error("order {0:?} app data fetching error: {1}")]
AppDataFetching(order::Uid, order::app_data::FetchingError),
}
2 changes: 0 additions & 2 deletions crates/driver/src/domain/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use {
},
util,
},
anyhow::anyhow,
chrono::Utc,
std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -183,7 +182,6 @@ impl Order {
auction::Error::InvalidTokens => panic!("fake auction with invalid tokens"),
auction::Error::InvalidAmounts => panic!("fake auction with invalid amounts"),
auction::Error::Blockchain(e) => e.into(),
auction::Error::AppDataFetching(_, e) => Error::Boundary(anyhow!("{}", e)),
})
}

Expand Down
1 change: 0 additions & 1 deletion crates/driver/src/infra/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ impl From<api::routes::AuctionError> for (hyper::StatusCode, axum::Json<Error>)
api::routes::AuctionError::InvalidTokens => Kind::InvalidTokens,
api::routes::AuctionError::InvalidAmounts => Kind::InvalidAmounts,
api::routes::AuctionError::Blockchain(_) => Kind::Unknown,
api::routes::AuctionError::Internal => Kind::Unknown,
};
error.into()
}
Expand Down
3 changes: 0 additions & 3 deletions crates/driver/src/infra/api/routes/solve/dto/solve_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ pub enum Error {
InvalidAmounts,
#[error("blockchain error: {0:?}")]
Blockchain(#[source] crate::infra::blockchain::Error),
#[error("internal error")]
Internal,
}

impl From<auction::InvalidId> for Error {
Expand All @@ -193,7 +191,6 @@ impl From<auction::Error> for Error {
auction::Error::InvalidTokens => Self::InvalidTokens,
auction::Error::InvalidAmounts => Self::InvalidAmounts,
auction::Error::Blockchain(err) => Self::Blockchain(err),
auction::Error::AppDataFetching(_, _) => Self::Internal,
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/driver/src/infra/api/routes/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ async fn route(
let auction = state
.pre_processor()
.process(auction, &competition.solver.account().address())
.await
.map_err(|err| {
tracing::error!(?err, "unable to pre-process the auction");
Into::<AuctionError>::into(err)
})?;
.await;
let result = competition.solve(auction).await;
competition.ensure_settle_queue_capacity()?;
observe::solved(state.solver().name(), &result);
Expand Down

0 comments on commit beca1f5

Please sign in to comment.