diff --git a/crates/orderbook/src/app_data.rs b/crates/orderbook/src/app_data.rs index 033f273788..cd64ee36ed 100644 --- a/crates/orderbook/src/app_data.rs +++ b/crates/orderbook/src/app_data.rs @@ -1,5 +1,9 @@ use { - crate::database::{app_data::InsertError, Postgres}, + crate::{ + database::{app_data::InsertError, Postgres}, + ipfs_app_data::IpfsAppData, + }, + anyhow::{Context, Result}, model::app_data::AppDataHash, shared::app_data, }; @@ -8,14 +12,20 @@ use { pub struct Registry { validator: app_data::Validator, database: Postgres, + ipfs: Option, } impl Registry { /// Creates a new instance of an app-data registry. - pub fn new(validator: app_data::Validator, database: Postgres) -> Self { + pub fn new( + validator: app_data::Validator, + database: Postgres, + ipfs: Option, + ) -> Self { Self { validator, database, + ipfs, } } @@ -57,6 +67,32 @@ impl Registry { Err(InsertError::Other(err)) => Err(RegisterError::Other(err)), } } + + /// Finds full app data for an order that only has the contract app data + /// hash. + /// + /// The full app data can be located in the database or on IPFS. + pub async fn find(&self, contract_app_data: &AppDataHash) -> Result> { + // we reserve the 0 app data to indicate empty app data. + if contract_app_data.is_zero() { + return Ok(Some(app_data::EMPTY.to_string())); + } + + if let Some(app_data) = self + .database + .get_full_app_data(contract_app_data) + .await + .context("from database")? + { + tracing::debug!(?contract_app_data, "full app data in database"); + return Ok(Some(app_data)); + } + + let Some(ipfs) = &self.ipfs else { + return Ok(None); + }; + ipfs.fetch(contract_app_data).await.context("from ipfs") + } } #[derive(Debug)] diff --git a/crates/orderbook/src/orderbook.rs b/crates/orderbook/src/orderbook.rs index c42419f6b9..879ace9667 100644 --- a/crates/orderbook/src/orderbook.rs +++ b/crates/orderbook/src/orderbook.rs @@ -1,8 +1,8 @@ use { crate::{ + app_data, database::orders::{InsertionError, OrderStoring}, dto, - ipfs_app_data::IpfsAppData, }, anyhow::{Context, Result}, chrono::Utc, @@ -24,7 +24,6 @@ use { }, primitive_types::H160, shared::{ - app_data, metrics::LivenessChecking, order_validation::{OrderValidating, ValidationError}, }, @@ -176,7 +175,7 @@ pub struct Orderbook { settlement_contract: H160, database: crate::database::Postgres, order_validator: Arc, - ipfs: Option, + app_data: Arc, } impl Orderbook { @@ -186,7 +185,7 @@ impl Orderbook { settlement_contract: H160, database: crate::database::Postgres, order_validator: Arc, - ipfs: Option, + app_data: Arc, ) -> Self { Metrics::initialize(); Self { @@ -194,45 +193,16 @@ impl Orderbook { settlement_contract, database, order_validator, - ipfs, + app_data, } } - /// Finds full app data for an order that only has the contract app data - /// hash. - /// - /// The full app data can be located in the database or on IPFS. - pub async fn find_full_app_data( - &self, - contract_app_data: &AppDataHash, - ) -> Result> { - // we reserve the 0 app data to indicate empty app data. - if contract_app_data.is_zero() { - return Ok(Some(app_data::EMPTY.to_string())); - } - - if let Some(app_data) = self - .database - .get_full_app_data(contract_app_data) - .await - .context("from database")? - { - tracing::debug!(?contract_app_data, "full app data in database"); - return Ok(Some(app_data)); - } - - let Some(ipfs) = &self.ipfs else { - return Ok(None); - }; - ipfs.fetch(contract_app_data).await.context("from ipfs") - } - pub async fn add_order( &self, payload: OrderCreation, ) -> Result<(OrderUid, Option), AddOrderError> { let full_app_data_override = match payload.app_data { - OrderCreationAppData::Hash { hash } => self.find_full_app_data(&hash).await?, + OrderCreationAppData::Hash { hash } => self.app_data.find(&hash).await?, _ => None, }; @@ -497,12 +467,17 @@ mod tests { let database = crate::database::Postgres::new("postgresql://").unwrap(); database::clear_DANGER(&database.pool).await.unwrap(); database.insert_order(&old_order, None).await.unwrap(); + let app_data = Arc::new(app_data::Registry::new( + shared::app_data::Validator::new(8192), + database.clone(), + None, + )); let orderbook = Orderbook { database, order_validator: Arc::new(order_validator), domain_separator: Default::default(), settlement_contract: H160([0xba; 20]), - ipfs: None, + app_data, }; // App data does not encode cancellation. diff --git a/crates/orderbook/src/run.rs b/crates/orderbook/src/run.rs index e028fcc5cc..85098fe0c6 100644 --- a/crates/orderbook/src/run.rs +++ b/crates/orderbook/src/run.rs @@ -495,12 +495,17 @@ pub async fn run(args: Arguments) { ) }) .map(IpfsAppData::new); + let app_data = Arc::new(app_data::Registry::new( + app_data_validator, + postgres.clone(), + ipfs, + )); let orderbook = Arc::new(Orderbook::new( domain_separator, settlement_contract.address(), postgres.clone(), order_validator.clone(), - ipfs, + app_data.clone(), )); if let Some(uniswap_v3) = uniswap_v3_pool_fetcher { @@ -511,10 +516,6 @@ pub async fn run(args: Arguments) { check_database_connection(orderbook.as_ref()).await; let quotes = Arc::new(QuoteHandler::new(order_validator, optimal_quoter).with_fast_quoter(fast_quoter)); - let app_data = Arc::new(app_data::Registry::new( - app_data_validator, - postgres.clone(), - )); let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel(); let serve_api = serve_api(