Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of the graphql-api service to use ports for Database #858

Merged
merged 26 commits into from
Jan 14, 2023

Conversation

ControlCplusControlV
Copy link
Contributor

@ControlCplusControlV ControlCplusControlV commented Dec 20, 2022

Refs #809
Closes: #907

It is the refactoring for the graphql-api service to use ports instead of direct work with Database. The service defines minimal database functionality and builds its functionality on top of it to process GrapQL requests.

Right now, this functionality is represented by {}QueryContext structures with the public methods. It is done to make the migration easier.
But later, we will need to move this functionality into corresponding traits and implement it directly for the DatabaseTemp type. Like:

impl {}QueryContext for DatabaseTemp {...}

or

impl<D: DatabasePort> {}QueryContext for D {...}

With traits, we can mock them and improve our tests.
The PR contains the following changes:

  • Renamed Transactional into Transaction.
  • Added Transactional trait with transaction method.
  • Used BlockId instead of Bytes32 in Database.
  • Created ports for block.rs, tx.rs, message.rs, balance.rs, coin.rs and updated corresponding schema.
  • Added and used in ports BoxedIter instead of associated types(In most cases they were Box<dyn> too).
  • Almost removed usage of fuel_core::database from schema. Only dap.rs uses it right now for VMDatabase.
  • Introduced CompressedCoin and Coin(contains UtxoId). CompressedCoin is used during work with the database, Coin in external API.
  • Moved resources into fuel-core-type and simplified it by removing generics.
  • Tried to re-work dap.rs but the change is big and also related to fuel-core-executor, so will do it in a separate PR.
  • Updated Transactional to support Transactional<dyn Database>

Before we can extract grapgql-api into its crate, we need:

  • Add ports for other services.
  • Add iteration abstraction into fuel-core-storage.
  • Solve the problem with DatabaseVM(The solution should also fit for fuel-core-executor).

@ControlCplusControlV ControlCplusControlV requested a review from a team December 20, 2022 20:05
@ControlCplusControlV ControlCplusControlV added the graphql-api Affects API of the GraphQL label Dec 20, 2022
@ControlCplusControlV ControlCplusControlV self-assigned this Dec 20, 2022
crates/fuel-core/src/query/block.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/query/block.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/query/chain.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/query/coin.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/schema/block.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/schema/block.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/schema/block.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/schema/block.rs Show resolved Hide resolved
crates/fuel-core/src/schema/chain.rs Outdated Show resolved Hide resolved
crates/fuel-core/src/schema/coin.rs Outdated Show resolved Hide resolved
Base automatically changed from teamwork/removing_channels to master December 21, 2022 03:25
@Voxelot Voxelot force-pushed the controlc/gql_refactor branch from 51311f3 to 1b0b321 Compare December 21, 2022 21:37
@ControlCplusControlV ControlCplusControlV requested review from xgreenx and a team December 21, 2022 22:41
.github/workflows/ci.yml Outdated Show resolved Hide resolved
@ControlCplusControlV ControlCplusControlV requested review from a team and Voxelot December 23, 2022 19:05
ControlCplusControlV and others added 4 commits December 23, 2022 15:53
- Renamed `Transactional` into `Transaction`.
- Added `Transactional` trait with `transaction` method.
- Used `BlockId` instead of `Bytes32` in `Database`. 
- Created ports for `block.rs`, `tx.rs`, `message.rs`, `balance.rs`,
`coin.rs` and updated corresponding schema.
- Added and used in ports `BoxedIter` instead of associated types(In
most cases they were `Box<dyn>` too).
- Almost removed usage of fuel_core::database from schema. Only `dap.rs`
uses it right now for `VMDatabase`.
- Introduced `CompressedCoin` and `Coin`(contains `UtxoId`).
`CompressedCoin` is used during work with the database, `Coin` in
external API.
- Moved resources into `fuel-core-type` and simplified it by removing
generics.
- Tried to re-work `dap.rs` but the change is big and also related to
`fuel-core-executor`, so will do it in a separate PR.
- Updated `Transactional` to support `Transactional<dyn Database>`

Co-authored-by: ControlCplusControlV <44706811+ControlCplusControlV@users.noreply.github.com>
@Voxelot
Copy link
Member

Voxelot commented Jan 11, 2023

This is getting to be a very large PR, what's left todo on it? It may be good to work on getting this merged before more refactoring.

@xgreenx
Copy link
Collaborator

xgreenx commented Jan 11, 2023

It is in the stage when almost all work with Database is replaced with ports(except VMDatabase). I also vote to merge it=)

.transaction_status(&transaction_id)
.into_api_result::<TransactionStatus, StorageError>()?
{
Some(TransactionStatus::Success { block_id, .. }) => block_id,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to highlight that here I changed the business logic, now we don't allow verification for failed transactions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably ok since messages are only set by the VM currently. However there's nothing specifically preventing failed scripts from having a valid message output in the future.

xgreenx
xgreenx previously approved these changes Jan 11, 2023
@xgreenx xgreenx changed the title Controlc/gql refactor Refactoring of the graphql-api service to use ports for Database Jan 11, 2023
@xgreenx xgreenx self-assigned this Jan 11, 2023
@xgreenx xgreenx requested a review from a team January 11, 2023 17:12
@ControlCplusControlV
Copy link
Contributor Author

I have changes fixing the TempDatabase vs Database issue, so I would prefer to wait until that is in before merging. Awaiting some work around the debugger before committing (since its a shared PR), but discussing with @Voxelot soon

Removed unused `OwnedTransactionIndexCursor` from the schema.
Extracted `dap.rs` into the `fuel-core` level.
Renamed `TempDatabase` into `Database`.
xgreenx
xgreenx previously approved these changes Jan 12, 2023
@xgreenx xgreenx requested a review from a team January 12, 2023 01:58
@ControlCplusControlV
Copy link
Contributor Author

PR should be good for review now that dap is separated out and instances of TempDatabase have been renamed


/// The database port expected by GraphQL API service.
#[cfg(test)]
pub trait DatabasePort:
Copy link
Member

@Voxelot Voxelot Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] why not just define a trait extension in tests?

let query = BlockQueryContext(ctx.data_unchecked());

let latest_block = query.latest_block()?.into();
Ok(latest_block)
}

async fn base_chain_height(&self) -> U64 {
Copy link
Member

@Voxelot Voxelot Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realized we forgot to update this api after finishing the relayer

Implemented base chain height.
Removed duplication of `DatabasePort` for tests.
@@ -21,7 +21,7 @@ use fuel_core_types::{

#[async_trait::async_trait]
impl RelayerDb for Database {
async fn insert_message(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

@Voxelot Voxelot merged commit 8575ee0 into master Jan 14, 2023
@Voxelot Voxelot deleted the controlc/gql_refactor branch January 14, 2023 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
graphql-api Affects API of the GraphQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make graphql api for base chain height use da_height from relayer
3 participants