From b97654686d9d06a33e1d9aa72fd276073285bc74 Mon Sep 17 00:00:00 2001 From: Michal Strug Date: Tue, 7 Jan 2025 13:45:49 +0100 Subject: [PATCH 1/2] Implementation --- .../autopilot/src/infra/solvers/dto/reveal.rs | 4 ++-- .../autopilot/src/infra/solvers/dto/settle.rs | 4 ++-- crates/autopilot/src/run_loop.rs | 2 +- crates/autopilot/src/shadow.rs | 2 +- crates/driver/src/domain/competition/mod.rs | 22 ++++++------------- .../api/routes/reveal/dto/reveal_request.rs | 4 ++-- .../driver/src/infra/api/routes/reveal/mod.rs | 15 ++++++++----- .../api/routes/settle/dto/settle_request.rs | 4 ++-- .../driver/src/infra/api/routes/settle/mod.rs | 9 +++----- 9 files changed, 30 insertions(+), 36 deletions(-) diff --git a/crates/autopilot/src/infra/solvers/dto/reveal.rs b/crates/autopilot/src/infra/solvers/dto/reveal.rs index 41971d28b1..6bda360d77 100644 --- a/crates/autopilot/src/infra/solvers/dto/reveal.rs +++ b/crates/autopilot/src/infra/solvers/dto/reveal.rs @@ -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")] - pub auction_id: Option, + #[serde_as(as = "serde_with::DisplayFromStr")] + pub auction_id: i64, } #[serde_as] diff --git a/crates/autopilot/src/infra/solvers/dto/settle.rs b/crates/autopilot/src/infra/solvers/dto/settle.rs index 5277eef76e..385ad68212 100644 --- a/crates/autopilot/src/infra/solvers/dto/settle.rs +++ b/crates/autopilot/src/infra/solvers/dto/settle.rs @@ -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")] - pub auction_id: Option, + #[serde_as(as = "serde_with::DisplayFromStr")] + pub auction_id: i64, } diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index 53b70b4100..3b55738364 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -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) diff --git a/crates/autopilot/src/shadow.rs b/crates/autopilot/src/shadow.rs index beddd86acc..44db5bd203 100644 --- a/crates/autopilot/src/shadow.rs +++ b/crates/autopilot/src/shadow.rs @@ -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)?; diff --git a/crates/driver/src/domain/competition/mod.rs b/crates/driver/src/domain/competition/mod.rs index 2ecde0fdc7..e8c8d964ed 100644 --- a/crates/driver/src/domain/competition/mod.rs +++ b/crates/driver/src/domain/competition/mod.rs @@ -318,17 +318,14 @@ impl Competition { pub async fn reveal( &self, solution_id: u64, - auction_id: Option, + auction_id: auction::Id, ) -> Result { 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 { @@ -347,7 +344,7 @@ impl Competition { /// [`Competition::solve`] to generate the solution. pub async fn settle( &self, - auction_id: Option, + auction_id: auction::Id, solution_id: u64, submission_deadline: BlockNo, ) -> Result { @@ -413,16 +410,14 @@ 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, solution_id: u64, submission_deadline: BlockNo, ) -> Result { @@ -430,10 +425,7 @@ impl Competition { 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) @@ -537,7 +529,7 @@ fn merge(solutions: impl Iterator, auction: &Auction) -> Vec, + auction_id: auction::Id, solution_id: u64, submission_deadline: BlockNo, response_sender: oneshot::Sender>, diff --git a/crates/driver/src/infra/api/routes/reveal/dto/reveal_request.rs b/crates/driver/src/infra/api/routes/reveal/dto/reveal_request.rs index c782ed57fc..e0e762dd47 100644 --- a/crates/driver/src/infra/api/routes/reveal/dto/reveal_request.rs +++ b/crates/driver/src/infra/api/routes/reveal/dto/reveal_request.rs @@ -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")] - pub auction_id: Option, + #[serde_as(as = "serde_with::DisplayFromStr")] + pub auction_id: i64, } diff --git a/crates/driver/src/infra/api/routes/reveal/mod.rs b/crates/driver/src/infra/api/routes/reveal/mod.rs index b96ce290f8..3ff64c0673 100644 --- a/crates/driver/src/infra/api/routes/reveal/mod.rs +++ b/crates/driver/src/infra/api/routes/reveal/mod.rs @@ -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, }; @@ -16,11 +19,13 @@ async fn route( state: axum::extract::State, req: axum::Json, ) -> Result, (hyper::StatusCode, axum::Json)> { + let auction_id = + auction::Id::try_from(req.auction_id).map_err(Into::::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?; @@ -28,6 +33,6 @@ async fn route( }; 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 } diff --git a/crates/driver/src/infra/api/routes/settle/dto/settle_request.rs b/crates/driver/src/infra/api/routes/settle/dto/settle_request.rs index 844a716738..6f5735fbce 100644 --- a/crates/driver/src/infra/api/routes/settle/dto/settle_request.rs +++ b/crates/driver/src/infra/api/routes/settle/dto/settle_request.rs @@ -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")] - pub auction_id: Option, + #[serde_as(as = "serde_with::DisplayFromStr")] + pub auction_id: i64, } diff --git a/crates/driver/src/infra/api/routes/settle/mod.rs b/crates/driver/src/infra/api/routes/settle/mod.rs index 516e3efb70..eecb98abfc 100644 --- a/crates/driver/src/infra/api/routes/settle/mod.rs +++ b/crates/driver/src/infra/api/routes/settle/mod.rs @@ -19,11 +19,8 @@ async fn route( state: axum::extract::State, req: axum::Json, ) -> Result<(), (hyper::StatusCode, axum::Json)> { - let auction_id = req - .auction_id - .map(auction::Id::try_from) - .transpose() - .map_err(Into::::into)?; + let auction_id = + auction::Id::try_from(req.auction_id).map_err(Into::::into)?; let solver = state.solver().name().to_string(); let handle_request = async move { @@ -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 From 98ae7041adb551006e51936735fdbfd39eadb5b0 Mon Sep 17 00:00:00 2001 From: Michal Strug Date: Wed, 8 Jan 2025 11:16:18 +0100 Subject: [PATCH 2/2] Updated errors conversion --- crates/driver/src/infra/api/routes/reveal/mod.rs | 2 +- crates/driver/src/infra/api/routes/settle/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/driver/src/infra/api/routes/reveal/mod.rs b/crates/driver/src/infra/api/routes/reveal/mod.rs index 3ff64c0673..7a2d6081e5 100644 --- a/crates/driver/src/infra/api/routes/reveal/mod.rs +++ b/crates/driver/src/infra/api/routes/reveal/mod.rs @@ -20,7 +20,7 @@ async fn route( req: axum::Json, ) -> Result, (hyper::StatusCode, axum::Json)> { let auction_id = - auction::Id::try_from(req.auction_id).map_err(Into::::into)?; + auction::Id::try_from(req.auction_id).map_err(api::routes::AuctionError::from)?; let handle_request = async { observe::revealing(); let result = state diff --git a/crates/driver/src/infra/api/routes/settle/mod.rs b/crates/driver/src/infra/api/routes/settle/mod.rs index eecb98abfc..1334693a6e 100644 --- a/crates/driver/src/infra/api/routes/settle/mod.rs +++ b/crates/driver/src/infra/api/routes/settle/mod.rs @@ -20,7 +20,7 @@ async fn route( req: axum::Json, ) -> Result<(), (hyper::StatusCode, axum::Json)> { let auction_id = - auction::Id::try_from(req.auction_id).map_err(Into::::into)?; + auction::Id::try_from(req.auction_id).map_err(api::routes::AuctionError::from)?; let solver = state.solver().name().to_string(); let handle_request = async move {