From 2bb146f4732937ad6667bfa5fe5140aabb98f6b1 Mon Sep 17 00:00:00 2001 From: Dusan Stanivukovic Date: Tue, 10 Dec 2024 11:21:34 +0100 Subject: [PATCH] Reveal and settle to receive integer solution id (#3152) # Description A follow up for https://github.com/cowprotocol/services/pull/3071 This PR allows reference driver to receive both integer and strings for solution id, on `/reveal` and `/settle` endpoints. Note how this PR implements task 2 from the [linked pull request](https://github.com/cowprotocol/services/pull/3071). This is done in this PR as a separate step to give time to colocated solvers using reference driver to update on time. If we were to just switch to using integer, colocated drivers would incur downtime. Next step is final step to use integer ONLY in both autopilot and driver, once we made sure all colocated solvers updated their driver. # Changes - [ ] Driver able to receive both integer and string on `/reveal` and `/settle` - [ ] ... ## How to test The code is copy paste of already tested code. --- crates/driver/src/infra/api/routes/mod.rs | 31 +++++++++++++++++++ .../infra/api/routes/reveal/dto/solution.rs | 4 +-- .../infra/api/routes/settle/dto/solution.rs | 4 +-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/crates/driver/src/infra/api/routes/mod.rs b/crates/driver/src/infra/api/routes/mod.rs index ee1027b0e9..434846385e 100644 --- a/crates/driver/src/infra/api/routes/mod.rs +++ b/crates/driver/src/infra/api/routes/mod.rs @@ -15,3 +15,34 @@ pub(super) use { settle::settle, solve::{solve, AuctionError}, }; + +pub(crate) fn deserialize_solution_id<'de, D>(deserializer: D) -> Result +where + D: serde::Deserializer<'de>, +{ + struct SolutionIdVisitor; + + impl serde::de::Visitor<'_> for SolutionIdVisitor { + type Value = u64; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("a string or integer representing a solution ID") + } + + fn visit_u64(self, value: u64) -> Result + where + E: serde::de::Error, + { + Ok(value) + } + + fn visit_str(self, value: &str) -> Result + where + E: serde::de::Error, + { + value.parse::().map_err(serde::de::Error::custom) + } + } + + deserializer.deserialize_any(SolutionIdVisitor) +} diff --git a/crates/driver/src/infra/api/routes/reveal/dto/solution.rs b/crates/driver/src/infra/api/routes/reveal/dto/solution.rs index 77a80e28b1..0c8d55fa49 100644 --- a/crates/driver/src/infra/api/routes/reveal/dto/solution.rs +++ b/crates/driver/src/infra/api/routes/reveal/dto/solution.rs @@ -1,11 +1,11 @@ -use {serde::Deserialize, serde_with::serde_as}; +use {super::super::super::deserialize_solution_id, serde::Deserialize, serde_with::serde_as}; #[serde_as] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Solution { /// Unique ID of the solution (per driver competition), to reveal. - #[serde_as(as = "serde_with::DisplayFromStr")] + #[serde(deserialize_with = "deserialize_solution_id")] pub solution_id: u64, /// Auction ID in which the specified solution ID is competing. #[serde_as(as = "Option")] diff --git a/crates/driver/src/infra/api/routes/settle/dto/solution.rs b/crates/driver/src/infra/api/routes/settle/dto/solution.rs index cbe0441ac3..6c62cd0cdd 100644 --- a/crates/driver/src/infra/api/routes/settle/dto/solution.rs +++ b/crates/driver/src/infra/api/routes/settle/dto/solution.rs @@ -1,11 +1,11 @@ -use {serde::Deserialize, serde_with::serde_as}; +use {super::super::super::deserialize_solution_id, serde::Deserialize, serde_with::serde_as}; #[serde_as] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Solution { /// Unique ID of the solution (per driver competition), to settle. - #[serde_as(as = "serde_with::DisplayFromStr")] + #[serde(deserialize_with = "deserialize_solution_id")] pub solution_id: u64, /// The last block number in which the solution TX can be included pub submission_deadline_latest_block: u64,