Skip to content

Commit

Permalink
Move app data structs
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jan 21, 2025
1 parent 917761e commit 55b18a7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 69 deletions.
2 changes: 1 addition & 1 deletion crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl AuctionProcessor {
},
kind: order::Kind::Limit,
side: template.order.kind.into(),
app_data: order::AppDataHash(Bytes(template.order.app_data.0)).into(),
app_data: order::app_data::AppDataHash(Bytes(template.order.app_data.0)).into(),
buy_token_balance: template.order.buy_token_balance.into(),
sell_token_balance: template.order.sell_token_balance.into(),
partial: match template.order.partially_fillable {
Expand Down
71 changes: 64 additions & 7 deletions crates/driver/src/domain/competition/order/app_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use {
crate::util::Bytes,
derive_more::From,
futures::FutureExt,
moka::future::Cache,
reqwest::StatusCode,
Expand Down Expand Up @@ -26,12 +28,10 @@ pub struct AppDataRetriever(Arc<Inner>);
struct Inner {
client: reqwest::Client,
base_url: Url,
request_sharing: BoxRequestSharing<
super::AppDataHash,
Result<Option<app_data::ValidatedAppData>, FetchingError>,
>,
request_sharing:
BoxRequestSharing<AppDataHash, Result<Option<app_data::ValidatedAppData>, FetchingError>>,
app_data_validator: app_data::Validator,
cache: Cache<super::AppDataHash, Option<app_data::ValidatedAppData>>,
cache: Cache<AppDataHash, Option<app_data::ValidatedAppData>>,
}

impl AppDataRetriever {
Expand All @@ -48,13 +48,13 @@ impl AppDataRetriever {
/// Retrieves the full app-data for the given `app_data` hash, if exists.
pub async fn get(
&self,
app_data: super::AppDataHash,
app_data: AppDataHash,
) -> Result<Option<app_data::ValidatedAppData>, FetchingError> {
if let Some(app_data) = self.0.cache.get(&app_data).await {
return Ok(app_data.clone());
}

let app_data_fut = move |app_data: &super::AppDataHash| {
let app_data_fut = move |app_data: &AppDataHash| {
let app_data = *app_data;
let self_ = self.clone();

Expand Down Expand Up @@ -90,6 +90,63 @@ impl AppDataRetriever {
}
}

/// The app data associated with an order.
#[derive(Debug, Clone, From)]
pub enum AppData {
/// App data hash.
Hash(AppDataHash),
/// Validated full app data.
Full(Box<::app_data::ValidatedAppData>),
}

impl Default for AppData {
fn default() -> Self {
Self::Hash(Default::default())
}
}

impl AppData {
pub fn hash(&self) -> AppDataHash {
match self {
Self::Hash(hash) => *hash,
Self::Full(data) => AppDataHash(data.hash.0.into()),
}
}
}

impl From<[u8; APP_DATA_LEN]> for AppData {
fn from(app_data_hash: [u8; APP_DATA_LEN]) -> Self {
Self::Hash(app_data_hash.into())
}
}

impl From<::app_data::ValidatedAppData> for AppData {
fn from(value: ::app_data::ValidatedAppData) -> Self {
Self::Full(Box::new(value))
}
}

/// The length of the app data hash in bytes.
pub const APP_DATA_LEN: usize = 32;

/// This is a hash allowing arbitrary user data to be associated with an order.
/// While this type holds the hash, the data itself is uploaded to IPFS. This
/// hash is signed along with the order.
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub struct AppDataHash(pub Bytes<[u8; APP_DATA_LEN]>);

impl From<[u8; APP_DATA_LEN]> for AppDataHash {
fn from(inner: [u8; APP_DATA_LEN]) -> Self {
Self(inner.into())
}
}

impl From<AppDataHash> for [u8; APP_DATA_LEN] {
fn from(app_data: AppDataHash) -> Self {
app_data.0.into()
}
}

#[derive(Error, Debug)]
pub enum FetchingError {
#[error("error while sending a request: {0}")]
Expand Down
61 changes: 2 additions & 59 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Order {
pub sell: eth::Asset,
pub side: Side,
pub kind: Kind,
pub app_data: AppData,
pub app_data: app_data::AppData,
pub partial: Partial,
/// The onchain calls to run before sending user funds to the settlement
/// contract.
Expand All @@ -51,42 +51,6 @@ pub struct Order {
pub quote: Option<Quote>,
}

/// The app data associated with an order.
#[derive(Debug, Clone, From)]
pub enum AppData {
/// App data hash.
Hash(AppDataHash),
/// Validated full app data.
Full(Box<::app_data::ValidatedAppData>),
}

impl Default for AppData {
fn default() -> Self {
Self::Hash(Default::default())
}
}

impl AppData {
pub fn hash(&self) -> AppDataHash {
match self {
Self::Hash(hash) => *hash,
Self::Full(data) => AppDataHash(data.hash.0.into()),
}
}
}

impl From<[u8; APP_DATA_LEN]> for AppData {
fn from(app_data_hash: [u8; APP_DATA_LEN]) -> Self {
Self::Hash(app_data_hash.into())
}
}

impl From<::app_data::ValidatedAppData> for AppData {
fn from(value: ::app_data::ValidatedAppData) -> Self {
Self::Full(Box::new(value))
}
}

/// An amount denominated in the sell token of an [`Order`].
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, From, Into)]
pub struct SellAmount(pub eth::U256);
Expand Down Expand Up @@ -304,27 +268,6 @@ impl From<Uid> for [u8; UID_LEN] {
}
}

/// The length of the app data hash in bytes.
pub const APP_DATA_LEN: usize = 32;

/// This is a hash allowing arbitrary user data to be associated with an order.
/// While this type holds the hash, the data itself is uploaded to IPFS. This
/// hash is signed along with the order.
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub struct AppDataHash(pub Bytes<[u8; APP_DATA_LEN]>);

impl From<[u8; APP_DATA_LEN]> for AppDataHash {
fn from(inner: [u8; APP_DATA_LEN]) -> Self {
Self(inner.into())
}
}

impl From<AppDataHash> for [u8; APP_DATA_LEN] {
fn from(app_data: AppDataHash) -> Self {
app_data.0.into()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
/// Order intended to be immediately executed. This is the "regular" type of
Expand Down Expand Up @@ -425,7 +368,7 @@ pub struct Jit {
pub receiver: eth::Address,
pub valid_to: util::Timestamp,
pub partially_fillable: bool,
pub app_data: AppDataHash,
pub app_data: app_data::AppDataHash,
pub side: Side,
pub sell_token_balance: SellTokenBalance,
pub buy_token_balance: BuyTokenBalance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ struct Order {
buy_token_balance: BuyTokenBalance,
class: Class,
#[serde_as(as = "serialize::Hex")]
app_data: [u8; order::APP_DATA_LEN],
app_data: [u8; order::app_data::APP_DATA_LEN],
signing_scheme: SigningScheme,
#[serde_as(as = "serialize::Hex")]
signature: Vec<u8>,
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/infra/solver/dto/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ struct JitOrder {
partially_fillable: bool,
valid_to: u32,
#[serde_as(as = "serialize::Hex")]
app_data: [u8; order::APP_DATA_LEN],
app_data: [u8; order::app_data::APP_DATA_LEN],
kind: Kind,
sell_token_balance: SellTokenBalance,
buy_token_balance: BuyTokenBalance,
Expand Down

0 comments on commit 55b18a7

Please sign in to comment.