Skip to content

Commit

Permalink
Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug committed Jan 7, 2025
1 parent c784e14 commit b976546
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/autopilot/src/infra/solvers/dto/reveal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Request {
/// Unique ID of the solution (per driver competition), to reveal.
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}

#[serde_as]
Expand Down
4 changes: 2 additions & 2 deletions crates/autopilot/src/infra/solvers/dto/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub struct Request {
/// The last block number in which the solution TX can be included
pub submission_deadline_latest_block: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}
2 changes: 1 addition & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ impl RunLoop {
let request = settle::Request {
solution_id,
submission_deadline_latest_block,
auction_id: None, // Requires 2-stage release for API-break change
auction_id,
};
driver
.settle(&request, self.config.max_settlement_transaction_wait)
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl RunLoop {
let revealed = driver
.reveal(&reveal::Request {
solution_id,
auction_id: None, // Requires 2-stage release for API-break change
auction_id: request.id,
})
.await
.map_err(Error::Reveal)?;
Expand Down
22 changes: 7 additions & 15 deletions crates/driver/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,14 @@ impl Competition {
pub async fn reveal(
&self,
solution_id: u64,
auction_id: Option<i64>,
auction_id: auction::Id,
) -> Result<Revealed, Error> {
let settlement = self
.settlements
.lock()
.unwrap()
.iter()
.find(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id.0 == id)
})
.find(|s| s.solution().get() == solution_id && s.auction_id == auction_id)
.cloned()
.ok_or(Error::SolutionNotAvailable)?;
Ok(Revealed {
Expand All @@ -347,7 +344,7 @@ impl Competition {
/// [`Competition::solve`] to generate the solution.
pub async fn settle(
&self,
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
) -> Result<Settled, Error> {
Expand Down Expand Up @@ -413,27 +410,22 @@ impl Competition {
tracing::error!(?err, "Failed to send /settle response");
}
}
.instrument(
tracing::info_span!("/settle", solver, auction_id = ?auction_id.map(|id| id.0)),
)
.instrument(tracing::info_span!("/settle", solver, %auction_id))
.await
}
}

async fn process_settle_request(
&self,
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
) -> Result<Settled, Error> {
let settlement = {
let mut lock = self.settlements.lock().unwrap();
let index = lock
.iter()
.position(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id == id)
})
.position(|s| s.solution().get() == solution_id && s.auction_id == auction_id)
.ok_or(Error::SolutionNotAvailable)?;
// remove settlement to ensure we can't settle it twice by accident
lock.swap_remove_front(index)
Expand Down Expand Up @@ -537,7 +529,7 @@ fn merge(solutions: impl Iterator<Item = Solution>, auction: &Auction) -> Vec<So
}

struct SettleRequest {
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
response_sender: oneshot::Sender<Result<Settled, Error>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub struct RevealRequest {
/// Unique ID of the solution (per driver competition), to reveal.
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}
15 changes: 10 additions & 5 deletions crates/driver/src/infra/api/routes/reveal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod dto;

use {
crate::infra::{
api::{Error, State},
observe,
crate::{
domain::competition::auction,
infra::{
api::{self, Error, State},
observe,
},
},
tracing::Instrument,
};
Expand All @@ -16,18 +19,20 @@ async fn route(
state: axum::extract::State<State>,
req: axum::Json<dto::RevealRequest>,
) -> Result<axum::Json<dto::RevealResponse>, (hyper::StatusCode, axum::Json<Error>)> {
let auction_id =
auction::Id::try_from(req.auction_id).map_err(Into::<api::routes::AuctionError>::into)?;
let handle_request = async {
observe::revealing();
let result = state
.competition()
.reveal(req.solution_id, req.auction_id)
.reveal(req.solution_id, auction_id)
.await;
observe::revealed(state.solver().name(), &result);
let result = result?;
Ok(axum::Json(dto::RevealResponse::new(result)))
};

handle_request
.instrument(tracing::info_span!("/reveal", solver = %state.solver().name(), req.auction_id))
.instrument(tracing::info_span!("/reveal", solver = %state.solver().name(), %auction_id))
.await
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub struct SettleRequest {
/// The last block number in which the solution TX can be included
pub submission_deadline_latest_block: u64,
/// Auction ID in which this solution is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}
9 changes: 3 additions & 6 deletions crates/driver/src/infra/api/routes/settle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ async fn route(
state: axum::extract::State<State>,
req: axum::Json<dto::SettleRequest>,
) -> Result<(), (hyper::StatusCode, axum::Json<Error>)> {
let auction_id = req
.auction_id
.map(auction::Id::try_from)
.transpose()
.map_err(Into::<api::routes::AuctionError>::into)?;
let auction_id =
auction::Id::try_from(req.auction_id).map_err(Into::<api::routes::AuctionError>::into)?;
let solver = state.solver().name().to_string();

let handle_request = async move {
Expand All @@ -39,7 +36,7 @@ async fn route(
observe::settled(state.solver().name(), &result);
result.map(|_| ()).map_err(Into::into)
}
.instrument(tracing::info_span!("/settle", solver, auction_id = ?auction_id.map(|id| id.0)));
.instrument(tracing::info_span!("/settle", solver, %auction_id));

// Handle `/settle` call in a background task to ensure that we correctly
// submit the settlement (or cancellation) on-chain even if the server
Expand Down

0 comments on commit b976546

Please sign in to comment.