From af0aebd368d27f3d7e31f1aad80d280c193abbff Mon Sep 17 00:00:00 2001 From: green Date: Fri, 13 Jan 2023 22:40:27 +0000 Subject: [PATCH] Address comments in teh PR: Implemented base chain height. Removed duplication of `DatabasePort` for tests. --- crates/fuel-core/src/database/relayer.rs | 9 ++-- crates/fuel-core/src/graphql_api/ports.rs | 20 ++------ crates/fuel-core/src/resource_query.rs | 47 ++++++++++--------- crates/fuel-core/src/schema/chain.rs | 13 +++-- .../src/service/adapters/graphql_api.rs | 13 +++++ .../src/service/adapters/producer.rs | 6 +-- crates/services/relayer/src/mock_db.rs | 11 ++--- crates/services/relayer/src/ports.rs | 9 ++-- crates/services/relayer/src/service.rs | 22 +++------ .../services/relayer/src/service/get_logs.rs | 4 +- .../relayer/src/service/get_logs/test.rs | 4 +- crates/services/relayer/src/service/run.rs | 9 +--- .../services/relayer/src/service/run/test.rs | 5 +- .../src/service/state/state_builder.rs | 7 ++- crates/services/relayer/src/service/test.rs | 12 ++--- crates/services/relayer/tests/integration.rs | 4 +- 16 files changed, 86 insertions(+), 109 deletions(-) diff --git a/crates/fuel-core/src/database/relayer.rs b/crates/fuel-core/src/database/relayer.rs index b54c38f9cc7..24830f1ebb0 100644 --- a/crates/fuel-core/src/database/relayer.rs +++ b/crates/fuel-core/src/database/relayer.rs @@ -21,7 +21,7 @@ use fuel_core_types::{ #[async_trait::async_trait] impl RelayerDb for Database { - async fn insert_message( + fn insert_message( &mut self, message: &CheckedMessage, ) -> StorageResult> { @@ -34,16 +34,13 @@ impl RelayerDb for Database { .insert(message.id(), message.message()) } - async fn set_finalized_da_height( - &mut self, - block: DaBlockHeight, - ) -> StorageResult<()> { + fn set_finalized_da_height(&mut self, block: DaBlockHeight) -> StorageResult<()> { let _: Option = self.insert(metadata::FINALIZED_DA_HEIGHT_KEY, Column::Metadata, block)?; Ok(()) } - async fn get_finalized_da_height(&self) -> StorageResult { + fn get_finalized_da_height(&self) -> StorageResult { self.get(metadata::FINALIZED_DA_HEIGHT_KEY, Column::Metadata)? .ok_or(not_found!("FinalizedDaHeight")) } diff --git a/crates/fuel-core/src/graphql_api/ports.rs b/crates/fuel-core/src/graphql_api/ports.rs index 1c53041a2fc..2e7958ad9c3 100644 --- a/crates/fuel-core/src/graphql_api/ports.rs +++ b/crates/fuel-core/src/graphql_api/ports.rs @@ -20,6 +20,7 @@ use fuel_core_types::{ blockchain::primitives::{ BlockHeight, BlockId, + DaBlockHeight, }, entities::message::Message, fuel_tx::{ @@ -40,7 +41,6 @@ use fuel_core_types::{ }; /// The database port expected by GraphQL API service. -#[cfg(not(test))] pub trait DatabasePort: Send + Sync @@ -53,22 +53,6 @@ pub trait DatabasePort: { } -/// The database port expected by GraphQL API service. -#[cfg(test)] -pub trait DatabasePort: - Send - + Sync - + DatabaseBlocks - + DatabaseTransactions - + DatabaseMessages - + DatabaseCoins - + DatabaseContracts - + DatabaseChain - + fuel_core_storage::StorageMutate - + fuel_core_storage::StorageMutate -{ -} - /// Trait that specifies all the getters required for blocks. pub trait DatabaseBlocks: StorageInspect @@ -143,4 +127,6 @@ pub trait DatabaseContracts: /// Trait that specifies all the getters required for chain metadata. pub trait DatabaseChain { fn chain_name(&self) -> StorageResult; + + fn base_chain_height(&self) -> StorageResult; } diff --git a/crates/fuel-core/src/resource_query.rs b/crates/fuel-core/src/resource_query.rs index c22cd9fdc28..fb1bb58d9ae 100644 --- a/crates/fuel-core/src/resource_query.rs +++ b/crates/fuel-core/src/resource_query.rs @@ -207,7 +207,8 @@ impl From for ResourceQueryError { #[cfg(test)] mod tests { use crate::{ - fuel_core_graphql_api::service::Database, + database::Database, + fuel_core_graphql_api::service::Database as ServiceDatabase, query::{ asset_query::{ AssetQuery, @@ -296,7 +297,7 @@ mod tests { fn query( spend_query: &[AssetSpendTarget], owner: &Address, - db: &Database, + db: &ServiceDatabase, ) -> Result>, ResourceQueryError> { let result: Vec<_> = spend_query .iter() @@ -324,7 +325,7 @@ mod tests { let resources = query( &[AssetSpendTarget::new(asset_id, target, u64::MAX)], &owner, - db.as_ref(), + &db.service_database(), ); // Transform result for convenience @@ -382,7 +383,7 @@ mod tests { let resources = query( &[AssetSpendTarget::new(asset_id, 6, 1)], &owner, - db.as_ref(), + &db.service_database(), ); assert_matches!(resources, Err(ResourceQueryError::MaxResourcesReached)); } @@ -413,7 +414,7 @@ mod tests { AssetSpendTarget::new(asset_ids[1], 6, u64::MAX), ], &owner, - db.as_ref(), + &db.service_database(), ); assert_matches!(resources, Ok(resources) if resources == vec![ @@ -441,7 +442,7 @@ mod tests { query_per_asset: Vec, owner: Address, asset_ids: &[AssetId], - db: &Database, + db: &ServiceDatabase, ) -> Result, ResourceQueryError> { let coins = random_improve(db, &SpendQuery::new(owner, &query_per_asset, None)?); @@ -473,7 +474,7 @@ mod tests { vec![AssetSpendTarget::new(asset_id, amount, u64::MAX)], owner, asset_ids, - db.as_ref(), + &db.service_database(), ); // Transform result for convenience @@ -524,7 +525,7 @@ mod tests { )], owner, asset_ids, - db.as_ref(), + &db.service_database(), ); assert_matches!(coins, Err(ResourceQueryError::MaxResourcesReached)); } @@ -565,7 +566,7 @@ mod tests { ], owner, asset_ids, - db.as_ref(), + &db.service_database(), ); assert_matches!(coins, Ok(ref coins) if coins.len() <= 6); let coins = coins.unwrap(); @@ -615,7 +616,7 @@ mod tests { excluded_ids: Vec| -> Result, ResourceQueryError> { let coins = random_improve( - db.as_ref(), + &db.service_database(), &SpendQuery::new(owner, &query_per_asset, Some(excluded_ids))?, ); @@ -808,7 +809,7 @@ mod tests { } let coins = random_improve( - db.as_ref(), + &db.service_database(), &SpendQuery::new( owner, &[AssetSpendTarget { @@ -838,13 +839,17 @@ mod tests { } impl TestDatabase { - pub fn new() -> Self { + fn new() -> Self { Self { - database: Box::::default(), + database: Default::default(), last_coin_index: Default::default(), last_message_index: Default::default(), } } + + fn service_database(&self) -> ServiceDatabase { + Box::new(self.database.clone()) + } } impl TestDatabase { @@ -867,7 +872,7 @@ mod tests { block_created: Default::default(), }; - let db = self.database.as_mut(); + let db = &mut self.database; StorageMutate::::insert(db, &id, &coin).unwrap(); coin.uncompress(id) @@ -887,14 +892,15 @@ mod tests { fuel_block_spend: None, }; - let db = self.database.as_mut(); + let db = &mut self.database; StorageMutate::::insert(db, &message.id(), &message).unwrap(); message } pub fn owned_coins(&self, owner: &Address) -> Vec { - let query = CoinQueryContext(&self.database); + let db = self.service_database(); + let query = CoinQueryContext(&db); query .owned_coins_ids(owner, None, IterDirection::Forward) .map(|res| res.map(|id| query.coin(id).unwrap())) @@ -903,7 +909,8 @@ mod tests { } pub fn owned_messages(&self, owner: &Address) -> Vec { - let query = MessageQueryContext(&self.database); + let db = self.service_database(); + let query = MessageQueryContext(&db); query .owned_message_ids(owner, None, IterDirection::Forward) .map(|res| res.map(|id| query.message(&id).unwrap())) @@ -911,10 +918,4 @@ mod tests { .unwrap() } } - - impl AsRef for TestDatabase { - fn as_ref(&self) -> &Database { - &self.database - } - } } diff --git a/crates/fuel-core/src/schema/chain.rs b/crates/fuel-core/src/schema/chain.rs index 6006311cf50..3bc5cb1dbe4 100644 --- a/crates/fuel-core/src/schema/chain.rs +++ b/crates/fuel-core/src/schema/chain.rs @@ -1,5 +1,8 @@ use crate::{ - fuel_core_graphql_api::Config as GraphQLConfig, + fuel_core_graphql_api::{ + service::Database, + Config as GraphQLConfig, + }, query::{ BlockQueryContext, ChainQueryContext, @@ -88,8 +91,12 @@ impl ChainInfo { Ok(latest_block) } - async fn base_chain_height(&self) -> U64 { - 0.into() + async fn base_chain_height(&self, ctx: &Context<'_>) -> U64 { + let height = ctx + .data_unchecked::() + .base_chain_height() + .unwrap_or_default(); + height.0.into() } async fn peer_count(&self) -> u16 { diff --git a/crates/fuel-core/src/service/adapters/graphql_api.rs b/crates/fuel-core/src/service/adapters/graphql_api.rs index fdb9be621f6..323e9dc6c68 100644 --- a/crates/fuel-core/src/service/adapters/graphql_api.rs +++ b/crates/fuel-core/src/service/adapters/graphql_api.rs @@ -31,6 +31,7 @@ use fuel_core_types::{ blockchain::primitives::{ BlockHeight, BlockId, + DaBlockHeight, }, entities::message::Message, fuel_tx::{ @@ -159,6 +160,18 @@ impl DatabaseChain for Database { .get_chain_name()? .unwrap_or_else(|| DEFAULT_NAME.to_string())) } + + fn base_chain_height(&self) -> StorageResult { + #[cfg(feature = "relayer")] + { + use fuel_core_relayer::ports::RelayerDb; + self.get_finalized_da_height() + } + #[cfg(not(feature = "relayer"))] + { + Ok(0u64.into()) + } + } } impl DatabasePort for Database {} diff --git a/crates/fuel-core/src/service/adapters/producer.rs b/crates/fuel-core/src/service/adapters/producer.rs index 6fb81cbf8b9..29fa034ce32 100644 --- a/crates/fuel-core/src/service/adapters/producer.rs +++ b/crates/fuel-core/src/service/adapters/producer.rs @@ -77,11 +77,7 @@ impl fuel_core_producer::ports::Relayer for MaybeRelayerAdapter { sync.await_synced().await?; } - Ok(self - .database - .get_finalized_da_height() - .await - .unwrap_or_default()) + Ok(self.database.get_finalized_da_height().unwrap_or_default()) } #[cfg(not(feature = "relayer"))] { diff --git a/crates/services/relayer/src/mock_db.rs b/crates/services/relayer/src/mock_db.rs index cc4d7902cbd..54d570c7be2 100644 --- a/crates/services/relayer/src/mock_db.rs +++ b/crates/services/relayer/src/mock_db.rs @@ -1,7 +1,6 @@ #![allow(missing_docs)] use crate::ports::RelayerDb; -use async_trait::async_trait; use fuel_core_storage::{ not_found, Result as StorageResult, @@ -43,9 +42,8 @@ impl MockDb { } } -#[async_trait] impl RelayerDb for MockDb { - async fn insert_message( + fn insert_message( &mut self, message: &CheckedMessage, ) -> StorageResult> { @@ -58,15 +56,12 @@ impl RelayerDb for MockDb { .insert(message_id, message)) } - async fn set_finalized_da_height( - &mut self, - height: DaBlockHeight, - ) -> StorageResult<()> { + fn set_finalized_da_height(&mut self, height: DaBlockHeight) -> StorageResult<()> { self.data.lock().unwrap().finalized_da_height = Some(height); Ok(()) } - async fn get_finalized_da_height(&self) -> StorageResult { + fn get_finalized_da_height(&self) -> StorageResult { self.data .lock() .unwrap() diff --git a/crates/services/relayer/src/ports.rs b/crates/services/relayer/src/ports.rs index cb0bc9d50f4..c4c5ad99327 100644 --- a/crates/services/relayer/src/ports.rs +++ b/crates/services/relayer/src/ports.rs @@ -14,17 +14,14 @@ use fuel_core_types::{ #[async_trait] pub trait RelayerDb: Send + Sync { /// Add bridge message to database. Messages are not revertible. - async fn insert_message( + fn insert_message( &mut self, message: &CheckedMessage, ) -> StorageResult>; /// set finalized da height that represent last block from da layer that got finalized. - async fn set_finalized_da_height( - &mut self, - block: DaBlockHeight, - ) -> StorageResult<()>; + fn set_finalized_da_height(&mut self, block: DaBlockHeight) -> StorageResult<()>; /// Assume it is always set as initialization of database. - async fn get_finalized_da_height(&self) -> StorageResult; + fn get_finalized_da_height(&self) -> StorageResult; } diff --git a/crates/services/relayer/src/service.rs b/crates/services/relayer/src/service.rs index 6e3dcb98646..c887571b75f 100644 --- a/crates/services/relayer/src/service.rs +++ b/crates/services/relayer/src/service.rs @@ -98,11 +98,10 @@ where P: Middleware + 'static, D: RelayerDb + 'static, { - async fn set_deploy_height(&mut self) { - if self.finalized().await.unwrap_or_default() < *self.config.da_deploy_height { + fn set_deploy_height(&mut self) { + if self.finalized().unwrap_or_default() < *self.config.da_deploy_height { self.database .set_finalized_da_height(self.config.da_deploy_height) - .await .expect("Should be able to set the finalized da height"); } } @@ -136,11 +135,8 @@ where write_logs(&mut self.database, logs).await } - async fn set_finalized_da_height( - &mut self, - height: DaBlockHeight, - ) -> StorageResult<()> { - self.database.set_finalized_da_height(height).await + fn set_finalized_da_height(&mut self, height: DaBlockHeight) -> StorageResult<()> { + self.database.set_finalized_da_height(height) } fn update_synced(&self, state: &state::EthState) { @@ -166,7 +162,7 @@ where } async fn into_task(mut self, _watcher: &StateWatcher) -> anyhow::Result { - self.set_deploy_height().await; + self.set_deploy_height(); Ok(self) } } @@ -239,12 +235,8 @@ where P: Middleware, D: RelayerDb + 'static, { - async fn finalized(&self) -> Option { - self.database - .get_finalized_da_height() - .await - .map(|h| *h) - .ok() + fn finalized(&self) -> Option { + self.database.get_finalized_da_height().map(|h| *h).ok() } } diff --git a/crates/services/relayer/src/service/get_logs.rs b/crates/services/relayer/src/service/get_logs.rs index 4589a9463f5..1aed48bff89 100644 --- a/crates/services/relayer/src/service/get_logs.rs +++ b/crates/services/relayer/src/service/get_logs.rs @@ -67,7 +67,7 @@ where match event { EthEventLog::Message(m) => { let m = Message::from(&m).check(); - if database.insert_message(&m).await?.is_some() { + if database.insert_message(&m)?.is_some() { // TODO: https://github.com/FuelLabs/fuel-core/issues/681 return Err(anyhow!( "The message for {:?} already existed", @@ -79,7 +79,7 @@ where EthEventLog::Ignored => (), } } - database.set_finalized_da_height(to_block.into()).await?; + database.set_finalized_da_height(to_block.into())?; } Ok(()) } diff --git a/crates/services/relayer/src/service/get_logs/test.rs b/crates/services/relayer/src/service/get_logs/test.rs index a5ebf71ee8f..524b6fd685b 100644 --- a/crates/services/relayer/src/service/get_logs/test.rs +++ b/crates/services/relayer/src/service/get_logs/test.rs @@ -167,11 +167,11 @@ async fn test_da_height_updates( stream: Vec), ProviderError>>, ) -> u64 { let mut mock_db = crate::mock_db::MockDb::default(); - mock_db.set_finalized_da_height(0u64.into()).await.unwrap(); + mock_db.set_finalized_da_height(0u64.into()).unwrap(); let logs = futures::stream::iter(stream); let _ = write_logs(&mut mock_db, logs).await; - *mock_db.get_finalized_da_height().await.unwrap() + *mock_db.get_finalized_da_height().unwrap() } diff --git a/crates/services/relayer/src/service/run.rs b/crates/services/relayer/src/service/run.rs index 04f73e9a42e..93946600b0b 100644 --- a/crates/services/relayer/src/service/run.rs +++ b/crates/services/relayer/src/service/run.rs @@ -30,10 +30,7 @@ pub trait RelayerData: EthRemote + EthLocal { ) -> anyhow::Result<()>; /// Set the finalized DA block height. - async fn set_finalized_da_height( - &mut self, - height: DaBlockHeight, - ) -> StorageResult<()>; + fn set_finalized_da_height(&mut self, height: DaBlockHeight) -> StorageResult<()>; /// Update the synced state. fn update_synced(&self, state: &EthState); @@ -56,9 +53,7 @@ where relayer.download_logs(ð_sync_gap).await?; // Update finalized height in database. - relayer - .set_finalized_da_height(eth_sync_gap.latest().into()) - .await?; + relayer.set_finalized_da_height(eth_sync_gap.latest().into())?; } // Update the synced state. diff --git a/crates/services/relayer/src/service/run/test.rs b/crates/services/relayer/src/service/run/test.rs index 1deb4b05979..5228b3614dd 100644 --- a/crates/services/relayer/src/service/run/test.rs +++ b/crates/services/relayer/src/service/run/test.rs @@ -57,9 +57,8 @@ mockall::mock! { fn finalization_period(&self) -> u64; } - #[async_trait] impl EthLocal for RelayerData { - async fn finalized(&self) -> Option; + fn finalized(&self) -> Option; } #[async_trait] @@ -71,7 +70,7 @@ mockall::mock! { eth_sync_gap: &state::EthSyncGap, ) -> anyhow::Result<()>; - async fn set_finalized_da_height(&mut self, height: DaBlockHeight) -> StorageResult<()>; + fn set_finalized_da_height(&mut self, height: DaBlockHeight) -> StorageResult<()>; fn update_synced(&self, state: &EthState); } diff --git a/crates/services/relayer/src/service/state/state_builder.rs b/crates/services/relayer/src/service/state/state_builder.rs index 05bbc7627ce..cc463200585 100644 --- a/crates/services/relayer/src/service/state/state_builder.rs +++ b/crates/services/relayer/src/service/state/state_builder.rs @@ -15,7 +15,7 @@ pub trait EthRemote { #[async_trait] pub trait EthLocal { /// The current finalized eth block that the relayer has seen. - async fn finalized(&self) -> Option; + fn finalized(&self) -> Option; } /// Build the Ethereum state. @@ -25,7 +25,7 @@ where { Ok(EthState { remote: EthHeights::new(t.current().await?, t.finalization_period()), - local: t.finalized().await, + local: t.finalized(), }) } @@ -49,9 +49,8 @@ pub mod test_builder { } } - #[async_trait] impl EthLocal for TestDataSource { - async fn finalized(&self) -> Option { + fn finalized(&self) -> Option { self.eth_local_finalized } } diff --git a/crates/services/relayer/src/service/test.rs b/crates/services/relayer/src/service/test.rs index f63725ed81e..5b28a653f61 100644 --- a/crates/services/relayer/src/service/test.rs +++ b/crates/services/relayer/src/service/test.rs @@ -44,7 +44,7 @@ async fn can_download_logs() { #[tokio::test] async fn deploy_height_does_not_override() { let mut mock_db = crate::mock_db::MockDb::default(); - mock_db.set_finalized_da_height(50u64.into()).await.unwrap(); + mock_db.set_finalized_da_height(50u64.into()).unwrap(); let config = Config { da_deploy_height: 20u64.into(), da_finalization: 1u64.into(), @@ -53,15 +53,15 @@ async fn deploy_height_does_not_override() { let eth_node = MockMiddleware::default(); let (tx, _) = watch::channel(false); let mut relayer = Task::new(tx, eth_node, mock_db.clone(), config); - relayer.set_deploy_height().await; + relayer.set_deploy_height(); - assert_eq!(*mock_db.get_finalized_da_height().await.unwrap(), 50); + assert_eq!(*mock_db.get_finalized_da_height().unwrap(), 50); } #[tokio::test] async fn deploy_height_does_override() { let mut mock_db = crate::mock_db::MockDb::default(); - mock_db.set_finalized_da_height(50u64.into()).await.unwrap(); + mock_db.set_finalized_da_height(50u64.into()).unwrap(); let config = Config { da_deploy_height: 52u64.into(), da_finalization: 1u64.into(), @@ -70,7 +70,7 @@ async fn deploy_height_does_override() { let eth_node = MockMiddleware::default(); let (tx, _) = watch::channel(false); let mut relayer = Task::new(tx, eth_node, mock_db.clone(), config); - relayer.set_deploy_height().await; + relayer.set_deploy_height(); - assert_eq!(*mock_db.get_finalized_da_height().await.unwrap(), 52); + assert_eq!(*mock_db.get_finalized_da_height().unwrap(), 52); } diff --git a/crates/services/relayer/tests/integration.rs b/crates/services/relayer/tests/integration.rs index 4eb6272dc36..7354c00c908 100644 --- a/crates/services/relayer/tests/integration.rs +++ b/crates/services/relayer/tests/integration.rs @@ -26,7 +26,7 @@ async fn can_set_da_height() { relayer.shared.await_synced().await.unwrap(); - assert_eq!(*mock_db.get_finalized_da_height().await.unwrap(), 100); + assert_eq!(*mock_db.get_finalized_da_height().unwrap(), 100); } #[tokio::test(start_paused = true)] @@ -103,5 +103,5 @@ async fn deploy_height_is_set() { relayer.shared.await_synced().await.unwrap(); rx.await.unwrap(); - assert_eq!(*mock_db.get_finalized_da_height().await.unwrap(), 53); + assert_eq!(*mock_db.get_finalized_da_height().unwrap(), 53); }