Skip to content

Commit

Permalink
Address comments in teh PR:
Browse files Browse the repository at this point in the history
Implemented base chain height.
Removed duplication of `DatabasePort` for tests.
  • Loading branch information
xgreenx committed Jan 13, 2023
1 parent e36125b commit af0aebd
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 109 deletions.
9 changes: 3 additions & 6 deletions crates/fuel-core/src/database/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Message>> {
Expand All @@ -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<BlockHeight> =
self.insert(metadata::FINALIZED_DA_HEIGHT_KEY, Column::Metadata, block)?;
Ok(())
}

async fn get_finalized_da_height(&self) -> StorageResult<DaBlockHeight> {
fn get_finalized_da_height(&self) -> StorageResult<DaBlockHeight> {
self.get(metadata::FINALIZED_DA_HEIGHT_KEY, Column::Metadata)?
.ok_or(not_found!("FinalizedDaHeight"))
}
Expand Down
20 changes: 3 additions & 17 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use fuel_core_types::{
blockchain::primitives::{
BlockHeight,
BlockId,
DaBlockHeight,
},
entities::message::Message,
fuel_tx::{
Expand All @@ -40,7 +41,6 @@ use fuel_core_types::{
};

/// The database port expected by GraphQL API service.
#[cfg(not(test))]
pub trait DatabasePort:
Send
+ Sync
Expand All @@ -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<Coins, Error = StorageError>
+ fuel_core_storage::StorageMutate<Messages, Error = StorageError>
{
}

/// Trait that specifies all the getters required for blocks.
pub trait DatabaseBlocks:
StorageInspect<FuelBlocks, Error = StorageError>
Expand Down Expand Up @@ -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<String>;

fn base_chain_height(&self) -> StorageResult<DaBlockHeight>;
}
47 changes: 24 additions & 23 deletions crates/fuel-core/src/resource_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ impl From<StorageError> 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,
Expand Down Expand Up @@ -296,7 +297,7 @@ mod tests {
fn query(
spend_query: &[AssetSpendTarget],
owner: &Address,
db: &Database,
db: &ServiceDatabase,
) -> Result<Vec<Vec<(AssetId, Word)>>, ResourceQueryError> {
let result: Vec<_> = spend_query
.iter()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -441,7 +442,7 @@ mod tests {
query_per_asset: Vec<AssetSpendTarget>,
owner: Address,
asset_ids: &[AssetId],
db: &Database,
db: &ServiceDatabase,
) -> Result<Vec<(AssetId, u64)>, ResourceQueryError> {
let coins =
random_improve(db, &SpendQuery::new(owner, &query_per_asset, None)?);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -524,7 +525,7 @@ mod tests {
)],
owner,
asset_ids,
db.as_ref(),
&db.service_database(),
);
assert_matches!(coins, Err(ResourceQueryError::MaxResourcesReached));
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -615,7 +616,7 @@ mod tests {
excluded_ids: Vec<ResourceId>|
-> Result<Vec<(AssetId, u64)>, ResourceQueryError> {
let coins = random_improve(
db.as_ref(),
&db.service_database(),
&SpendQuery::new(owner, &query_per_asset, Some(excluded_ids))?,
);

Expand Down Expand Up @@ -808,7 +809,7 @@ mod tests {
}

let coins = random_improve(
db.as_ref(),
&db.service_database(),
&SpendQuery::new(
owner,
&[AssetSpendTarget {
Expand Down Expand Up @@ -838,13 +839,17 @@ mod tests {
}

impl TestDatabase {
pub fn new() -> Self {
fn new() -> Self {
Self {
database: Box::<crate::database::Database>::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 {
Expand All @@ -867,7 +872,7 @@ mod tests {
block_created: Default::default(),
};

let db = self.database.as_mut();
let db = &mut self.database;
StorageMutate::<Coins>::insert(db, &id, &coin).unwrap();

coin.uncompress(id)
Expand All @@ -887,14 +892,15 @@ mod tests {
fuel_block_spend: None,
};

let db = self.database.as_mut();
let db = &mut self.database;
StorageMutate::<Messages>::insert(db, &message.id(), &message).unwrap();

message
}

pub fn owned_coins(&self, owner: &Address) -> Vec<Coin> {
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()))
Expand All @@ -903,18 +909,13 @@ mod tests {
}

pub fn owned_messages(&self, owner: &Address) -> Vec<Message> {
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()))
.try_collect()
.unwrap()
}
}

impl AsRef<Database> for TestDatabase {
fn as_ref(&self) -> &Database {
&self.database
}
}
}
13 changes: 10 additions & 3 deletions crates/fuel-core/src/schema/chain.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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::<Database>()
.base_chain_height()
.unwrap_or_default();
height.0.into()
}

async fn peer_count(&self) -> u16 {
Expand Down
13 changes: 13 additions & 0 deletions crates/fuel-core/src/service/adapters/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use fuel_core_types::{
blockchain::primitives::{
BlockHeight,
BlockId,
DaBlockHeight,
},
entities::message::Message,
fuel_tx::{
Expand Down Expand Up @@ -159,6 +160,18 @@ impl DatabaseChain for Database {
.get_chain_name()?
.unwrap_or_else(|| DEFAULT_NAME.to_string()))
}

fn base_chain_height(&self) -> StorageResult<DaBlockHeight> {
#[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 {}
6 changes: 1 addition & 5 deletions crates/fuel-core/src/service/adapters/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand Down
11 changes: 3 additions & 8 deletions crates/services/relayer/src/mock_db.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -43,9 +42,8 @@ impl MockDb {
}
}

#[async_trait]
impl RelayerDb for MockDb {
async fn insert_message(
fn insert_message(
&mut self,
message: &CheckedMessage,
) -> StorageResult<Option<Message>> {
Expand All @@ -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<DaBlockHeight> {
fn get_finalized_da_height(&self) -> StorageResult<DaBlockHeight> {
self.data
.lock()
.unwrap()
Expand Down
9 changes: 3 additions & 6 deletions crates/services/relayer/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Message>>;

/// 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<DaBlockHeight>;
fn get_finalized_da_height(&self) -> StorageResult<DaBlockHeight>;
}
Loading

0 comments on commit af0aebd

Please sign in to comment.