Skip to content

Commit

Permalink
Solution_id is integer (#3071)
Browse files Browse the repository at this point in the history
# Description
Related to #3064

This PR enables driver to send either string or integer for
`solution_id`.

This PR marks the start of transition period. Once the PR is merged,
external solvers will be asked to:
1. Start sending `solution_id` as integer in `/solve` response.
2. Implement receiving either string or integer for `solution_id` on
`/reveal` and on `/settle`.

Once done, backend will have a [follow
up](#3072) where we will
definitely switch to using integers in all three endpoints and
#3064 will be fixed.

## How to test
Manually checked that both versions are properly
serialized/deserialized.

<!--
## Related Issues

Fixes #
-->
  • Loading branch information
sunce86 authored Oct 18, 2024
1 parent 170ccef commit 3690ba5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
33 changes: 32 additions & 1 deletion crates/autopilot/src/infra/solvers/dto/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub enum Side {
pub struct Solution {
/// Unique ID of the solution (per driver competition), used to identify
/// it in subsequent requests (reveal, settle).
#[serde_as(as = "serde_with::DisplayFromStr")]
#[serde(deserialize_with = "deserialize_solution_id")]
pub solution_id: u64,
#[serde_as(as = "HexOrDecimalU256")]
pub score: U256,
Expand All @@ -182,6 +182,37 @@ pub struct Solution {
pub gas: Option<u64>,
}

fn deserialize_solution_id<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: serde::Deserializer<'de>,
{
struct SolutionIdVisitor;

impl<'de> serde::de::Visitor<'de> 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<E>(self, value: u64) -> Result<u64, E>
where
E: serde::de::Error,
{
Ok(value)
}

fn visit_str<E>(self, value: &str) -> Result<u64, E>
where
E: serde::de::Error,
{
value.parse::<u64>().map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_any(SolutionIdVisitor)
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Response {
Expand Down
4 changes: 2 additions & 2 deletions crates/driver/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ components:
The unique identifier of the solution.
This id is used to identify the solution when executing it.
type: string
example: "1"
type: integer
example: 1
score:
description: |
The objective value of the solution.
Expand Down
1 change: 0 additions & 1 deletion crates/driver/src/infra/api/routes/solve/dto/solved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ type OrderId = [u8; order::UID_LEN];
pub struct Solution {
/// Unique ID of the solution (per driver competition), used to identify it
/// in subsequent requests (reveal, settle).
#[serde_as(as = "serde_with::DisplayFromStr")]
solution_id: u64,
#[serde_as(as = "serialize::U256")]
score: eth::U256,
Expand Down
3 changes: 2 additions & 1 deletion crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,8 @@ impl<'a> SolveOk<'a> {
solution
.get("solutionId")
.unwrap()
.as_str()
.as_u64()
.map(|id| id.to_string())
.unwrap()
.to_owned()
}
Expand Down

0 comments on commit 3690ba5

Please sign in to comment.