Skip to content

Commit

Permalink
Leave dap.rs in the fuel-core level for now (#899)
Browse files Browse the repository at this point in the history
Removed unused `OwnedTransactionIndexCursor` from the schema.
Extracted `dap.rs` into the `fuel-core` level.
Renamed `TempDatabase` into `Database`.
  • Loading branch information
xgreenx authored Jan 12, 2023
1 parent 0d97e98 commit ade0a04
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 65 deletions.
20 changes: 7 additions & 13 deletions crates/fuel-core/src/graphql_api/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::{
fuel_core_graphql_api::ports::DatabasePort,
graphql_api::Config,
schema::{
build_schema,
dap,
CoreSchema,
CoreSchemaBuilder,
},
service::metrics::metrics,
};
Expand Down Expand Up @@ -67,11 +66,7 @@ use tower_http::{

pub type Service = fuel_core_services::ServiceRunner<NotInitializedTask>;

// TODO: When the port of DB will exist we need to replace it with `Box<dyn DatabasePort>
pub type Database = crate::database::Database;
// TODO: When all ports will be ready, we need to remove `Database`(and related code)
// and rename `DatabaseTemp` into `Database`
pub type DatabaseTemp = Box<dyn DatabasePort>;
pub type Database = Box<dyn DatabasePort>;
// TODO: When the port for `Executor` will exist we need to replace it with `Box<dyn ExecutorPort>
pub type Executor = crate::service::adapters::ExecutorAdapter;
// TODO: When the port of BlockProducer will exist we need to replace it with
Expand Down Expand Up @@ -145,21 +140,20 @@ impl RunnableTask for Task {
pub fn new_service(
config: Config,
database: Database,
schema: CoreSchemaBuilder,
producer: BlockProducer,
txpool: TxPool,
executor: Executor,
) -> anyhow::Result<Service> {
let database_temp: DatabaseTemp = Box::new(database.clone());
let network_addr = config.addr;
let params = config.transaction_parameters;
let schema = build_schema()
let schema = schema
.data(config)
.data(database)
.data(database_temp)
.data(producer)
.data(txpool)
.data(executor);
let schema = dap::init(schema, params).extension(Tracing).finish();
.data(executor)
.extension(Tracing)
.finish();

let router = Router::new()
.route("/playground", get(graphql_playground))
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
fuel_core_graphql_api::service::DatabaseTemp,
fuel_core_graphql_api::service::Database,
state::IterDirection,
};
use asset_query::{
Expand All @@ -23,7 +23,7 @@ use std::{

pub mod asset_query;

pub struct BalanceQueryContext<'a>(pub &'a DatabaseTemp);
pub struct BalanceQueryContext<'a>(pub &'a Database);

impl BalanceQueryContext<'_> {
pub fn balance(
Expand Down
10 changes: 5 additions & 5 deletions crates/fuel-core/src/query/balance/asset_query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
fuel_core_graphql_api::service::DatabaseTemp,
fuel_core_graphql_api::service::Database,
query::{
CoinQueryContext,
MessageQueryContext,
Expand Down Expand Up @@ -71,15 +71,15 @@ pub struct AssetsQuery<'a> {
pub owner: &'a Address,
pub assets: Option<HashSet<&'a AssetId>>,
pub exclude: Option<&'a Exclude>,
pub database: &'a DatabaseTemp,
pub database: &'a Database,
}

impl<'a> AssetsQuery<'a> {
pub fn new(
owner: &'a Address,
assets: Option<HashSet<&'a AssetId>>,
exclude: Option<&'a Exclude>,
database: &'a DatabaseTemp,
database: &'a Database,
) -> Self {
Self {
owner,
Expand Down Expand Up @@ -161,7 +161,7 @@ pub struct AssetQuery<'a> {
pub owner: &'a Address,
pub asset: &'a AssetSpendTarget,
pub exclude: Option<&'a Exclude>,
pub database: &'a DatabaseTemp,
pub database: &'a Database,
query: AssetsQuery<'a>,
}

Expand All @@ -170,7 +170,7 @@ impl<'a> AssetQuery<'a> {
owner: &'a Address,
asset: &'a AssetSpendTarget,
exclude: Option<&'a Exclude>,
database: &'a DatabaseTemp,
database: &'a Database,
) -> Self {
let mut allowed = HashSet::new();
allowed.insert(&asset.id);
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
fuel_core_graphql_api::service::DatabaseTemp,
fuel_core_graphql_api::service::Database,
state::IterDirection,
};
use fuel_core_storage::{
Expand All @@ -20,7 +20,7 @@ use fuel_core_types::blockchain::{
},
};

pub struct BlockQueryContext<'a>(pub &'a DatabaseTemp);
pub struct BlockQueryContext<'a>(pub &'a Database);

impl BlockQueryContext<'_> {
pub fn block(&self, id: &BlockId) -> StorageResult<CompressedBlock> {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fuel_core_graphql_api::service::DatabaseTemp;
use crate::fuel_core_graphql_api::service::Database;
use fuel_core_storage::Result as StorageResult;

pub struct ChainQueryContext<'a>(pub &'a DatabaseTemp);
pub struct ChainQueryContext<'a>(pub &'a Database);

impl ChainQueryContext<'_> {
pub fn name(&self) -> StorageResult<String> {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/coin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
graphql_api::service::DatabaseTemp,
graphql_api::service::Database,
state::IterDirection,
};
use fuel_core_storage::{
Expand All @@ -14,7 +14,7 @@ use fuel_core_types::{
fuel_types::Address,
};

pub struct CoinQueryContext<'a>(pub &'a DatabaseTemp);
pub struct CoinQueryContext<'a>(pub &'a Database);

impl<'a> CoinQueryContext<'a> {
pub fn coin(&self, utxo_id: UtxoId) -> StorageResult<Coin> {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
graphql_api::service::DatabaseTemp,
graphql_api::service::Database,
state::IterDirection,
};
use fuel_core_storage::{
Expand All @@ -21,7 +21,7 @@ use fuel_core_types::{
services::graphql_api::ContractBalance,
};

pub struct ContractQueryContext<'a>(pub &'a DatabaseTemp);
pub struct ContractQueryContext<'a>(pub &'a Database);

impl ContractQueryContext<'_> {
pub fn contract_id(&self, id: ContractId) -> StorageResult<ContractId> {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/message_proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
fuel_core_graphql_api::{
service::DatabaseTemp,
service::Database,
IntoApiResult,
},
query::{
Expand Down Expand Up @@ -48,7 +48,7 @@ use std::borrow::Cow;
#[cfg(test)]
mod test;

pub struct MessageQueryContext<'a>(pub &'a DatabaseTemp);
pub struct MessageQueryContext<'a>(pub &'a Database);

impl<'a> MessageQueryContext<'a> {
pub fn message(&self, message_id: &MessageId) -> StorageResult<Message> {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
fuel_core_graphql_api::service::DatabaseTemp,
fuel_core_graphql_api::service::Database,
state::IterDirection,
};
use fuel_core_storage::{
Expand All @@ -22,7 +22,7 @@ use fuel_core_types::{
services::txpool::TransactionStatus,
};

pub struct TransactionQueryContext<'a>(pub &'a DatabaseTemp);
pub struct TransactionQueryContext<'a>(pub &'a Database);

impl TransactionQueryContext<'_> {
pub fn transaction(&self, tx_id: &TxId) -> StorageResult<Transaction> {
Expand Down
34 changes: 22 additions & 12 deletions crates/fuel-core/src/resource_query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
fuel_core_graphql_api::service::DatabaseTemp,
fuel_core_graphql_api::service::Database,
query::asset_query::{
AssetQuery,
AssetSpendTarget,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl SpendQuery {
}

/// Return [`AssetQuery`]s.
pub fn asset_queries<'a>(&'a self, db: &'a DatabaseTemp) -> Vec<AssetQuery<'a>> {
pub fn asset_queries<'a>(&'a self, db: &'a Database) -> Vec<AssetQuery<'a>> {
self.query_per_asset
.iter()
.map(|asset| AssetQuery::new(&self.owner, asset, Some(&self.exclude), db))
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn largest_first(query: &AssetQuery) -> Result<Vec<Resource>, ResourceQueryE

// An implementation of the method described on: https://iohk.io/en/blog/posts/2018/07/03/self-organisation-in-coin-selection/
pub fn random_improve(
db: &DatabaseTemp,
db: &Database,
spend_query: &SpendQuery,
) -> Result<Vec<Vec<Resource>>, ResourceQueryError> {
let mut resources_per_asset = vec![];
Expand Down Expand Up @@ -207,11 +207,21 @@ impl From<StorageError> for ResourceQueryError {
#[cfg(test)]
mod tests {
use crate::{
database::Database,
fuel_core_graphql_api::service::Database,
query::{
asset_query::{
AssetQuery,
AssetSpendTarget,
},
CoinQueryContext,
MessageQueryContext,
},
resource_query::{
largest_first,
random_improve,
ResourceQueryError,
SpendQuery,
},
state::IterDirection,
};
use assert_matches::assert_matches;
Expand All @@ -236,8 +246,7 @@ mod tests {
fuel_tx::*,
};
use itertools::Itertools;

use super::*;
use std::cmp::Reverse;

fn setup_coins() -> (Address, [AssetId; 2], TestDatabase) {
let owner = Address::default();
Expand Down Expand Up @@ -287,7 +296,7 @@ mod tests {
fn query(
spend_query: &[AssetSpendTarget],
owner: &Address,
db: &DatabaseTemp,
db: &Database,
) -> Result<Vec<Vec<(AssetId, Word)>>, ResourceQueryError> {
let result: Vec<_> = spend_query
.iter()
Expand Down Expand Up @@ -432,7 +441,7 @@ mod tests {
query_per_asset: Vec<AssetSpendTarget>,
owner: Address,
asset_ids: &[AssetId],
db: &DatabaseTemp,
db: &Database,
) -> Result<Vec<(AssetId, u64)>, ResourceQueryError> {
let coins =
random_improve(db, &SpendQuery::new(owner, &query_per_asset, None)?);
Expand Down Expand Up @@ -592,6 +601,7 @@ mod tests {

mod exclusion {
use super::*;
use fuel_core_types::entities::resource::ResourceId;

fn exclusion_assert(
owner: Address,
Expand Down Expand Up @@ -822,15 +832,15 @@ mod tests {
}

pub struct TestDatabase {
database: DatabaseTemp,
database: Database,
last_coin_index: u64,
last_message_index: u64,
}

impl TestDatabase {
pub fn new() -> Self {
Self {
database: Box::<Database>::default(),
database: Box::<crate::database::Database>::default(),
last_coin_index: Default::default(),
last_message_index: Default::default(),
}
Expand Down Expand Up @@ -902,8 +912,8 @@ mod tests {
}
}

impl AsRef<DatabaseTemp> for TestDatabase {
fn as_ref(&self) -> &DatabaseTemp {
impl AsRef<Database> for TestDatabase {
fn as_ref(&self) -> &Database {
&self.database
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/fuel-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ pub struct Mutation(dap::DapMutation, tx::TxMutation, block::BlockMutation);
pub struct Subscription(tx::TxStatusSubscription);

pub type CoreSchema = Schema<Query, Mutation, Subscription>;
pub type CoreSchemaBuilder = SchemaBuilder<Query, Mutation, Subscription>;

pub fn build_schema() -> SchemaBuilder<Query, Mutation, Subscription> {
pub fn build_schema() -> CoreSchemaBuilder {
Schema::build_with_ignore_name_conflicts(
Query::default(),
Mutation::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/schema/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
database::{
transactional::DatabaseTransaction,
vm_database::VmDatabase,
Database,
},
fuel_core_graphql_api::service::Database,
schema::scalars::U64,
};
use async_graphql::{
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/schema/resource.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
fuel_core_graphql_api::{
service::DatabaseTemp,
service::Database,
Config as GraphQLConfig,
},
query::asset_query::AssetSpendTarget,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl ResourceQuery {

let spend_query = SpendQuery::new(owner, &query_per_asset, excluded_ids)?;

let db = ctx.data_unchecked::<DatabaseTemp>();
let db = ctx.data_unchecked::<Database>();

let resources = random_improve(db, &spend_query)?
.into_iter()
Expand Down
13 changes: 0 additions & 13 deletions crates/fuel-core/src/schema/scalars.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::database::transactions::OwnedTransactionIndexCursor;
use async_graphql::{
connection::CursorType,
InputValueError,
Expand Down Expand Up @@ -151,18 +150,6 @@ impl CursorType for HexString {
}
}

impl From<HexString> for OwnedTransactionIndexCursor {
fn from(string: HexString) -> Self {
string.0.into()
}
}

impl From<OwnedTransactionIndexCursor> for HexString {
fn from(cursor: OwnedTransactionIndexCursor) -> Self {
HexString(cursor.into())
}
}

impl FromStr for HexString {
type Err = String;

Expand Down
Loading

0 comments on commit ade0a04

Please sign in to comment.