-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fault_proving(global_roots): GraphQL API for serving state roots #2742
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
Merged
netrome
merged 4 commits into
master
from
2728-fault_provingglobal_roots-api-for-serving-state-roots
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added API crate for merkle root service. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| [package] | ||
| authors = { workspace = true } | ||
| categories = ["cryptography::cryptocurrencies"] | ||
| description = "GraphQL API for the global merkle root service." | ||
| edition = { workspace = true } | ||
| homepage = { workspace = true } | ||
| keywords = ["blockchain", "cryptocurrencies", "fuel-client", "fuel-core"] | ||
| license = { workspace = true } | ||
| name = "fuel-core-global-merkle-root-api" | ||
| repository = { workspace = true } | ||
| version = { workspace = true } | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| async-graphql = { workspace = true } | ||
| async-trait = { workspace = true } | ||
| axum = { workspace = true } | ||
| derive_more = { workspace = true } | ||
| fuel-core-services = { workspace = true } | ||
| fuel-core-storage = { workspace = true, features = ["alloc"] } | ||
| fuel-core-types = { workspace = true, default-features = false, features = [ | ||
| "serde", | ||
| "alloc", | ||
| ] } | ||
| hex = { workspace = true } | ||
| hyper = { workspace = true } | ||
| tokio = { workspace = true } | ||
| tracing = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| fuel-core-storage = { workspace = true, features = ["std", "test-helpers"] } | ||
| fuel-core-types = { workspace = true, default-features = false, features = [ | ||
| "serde", | ||
| "random", | ||
| "test-helpers", | ||
| ] } | ||
| reqwest = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //! The GraphQL API of the state root service | ||
|
|
||
| #![deny(clippy::arithmetic_side_effects)] | ||
| #![deny(clippy::cast_possible_truncation)] | ||
| #![deny(unused_crate_dependencies)] | ||
| #![deny(missing_docs)] | ||
| #![deny(warnings)] | ||
|
|
||
| /// Port definitions | ||
| pub mod ports; | ||
|
|
||
| /// API service definition | ||
| pub mod service; | ||
|
|
||
| /// GraphQL schema definition | ||
| pub mod schema; | ||
|
|
||
| #[cfg(test)] | ||
| pub mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| use fuel_core_storage::Error as StorageError; | ||
| use fuel_core_types::{ | ||
| fuel_tx::Bytes32, | ||
| fuel_types::BlockHeight, | ||
| }; | ||
|
|
||
| /// Represents the ability to retrieve a state root at a given block height | ||
| pub trait GetStateRoot { | ||
| /// Get the state root at the given height | ||
| fn state_root_at(&self, height: BlockHeight) | ||
| -> Result<Option<Bytes32>, StorageError>; | ||
| } |
112 changes: 112 additions & 0 deletions
112
crates/proof_system/global_merkle_root/api/src/schema.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use std::{ | ||
| array::TryFromSliceError, | ||
| fmt::Display, | ||
| str::FromStr, | ||
| }; | ||
|
|
||
| use async_graphql::{ | ||
| EmptyMutation, | ||
| EmptySubscription, | ||
| InputValueError, | ||
| ScalarType, | ||
| }; | ||
| use hex::FromHexError; | ||
|
|
||
| use crate::ports; | ||
|
|
||
| /// The schema of the state root service. | ||
| pub type Schema<Storage> = | ||
| async_graphql::Schema<Query<Storage>, EmptyMutation, EmptySubscription>; | ||
|
|
||
| /// The query type of the schema. | ||
| pub struct Query<Storage> { | ||
| storage: Storage, | ||
| } | ||
|
|
||
| impl<Storage> Query<Storage> { | ||
| /// Create a new query object. | ||
| pub fn new(storage: Storage) -> Self { | ||
| Self { storage } | ||
| } | ||
| } | ||
|
|
||
| #[async_graphql::Object] | ||
| impl<Storage> Query<Storage> | ||
| where | ||
| Storage: ports::GetStateRoot + Send + Sync, | ||
| { | ||
| async fn state_root( | ||
| &self, | ||
| height: BlockHeight, | ||
| ) -> async_graphql::Result<Option<MerkleRoot>> { | ||
| let state_root = self.storage.state_root_at(height.0.into())?; | ||
|
|
||
| Ok(state_root.map(|root| MerkleRoot(*root))) | ||
| } | ||
| } | ||
|
|
||
| /// GraphQL scalar type for block height. | ||
| #[derive( | ||
| Clone, | ||
| Copy, | ||
| Debug, | ||
| PartialEq, | ||
| Eq, | ||
| PartialOrd, | ||
| Ord, | ||
| derive_more::FromStr, | ||
| derive_more::Display, | ||
| )] | ||
| pub struct BlockHeight(u32); | ||
|
|
||
| /// GraphQL scalar type for the merkle root. | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub struct MerkleRoot([u8; 32]); | ||
|
|
||
| impl Display for MerkleRoot { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "0x{}", hex::encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for MerkleRoot { | ||
| type Err = MerkleRootParseError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| let bytes = hex::decode(s.trim_start_matches("0x"))?; | ||
| Ok(MerkleRoot(bytes.as_slice().try_into()?)) | ||
| } | ||
| } | ||
|
|
||
| /// Error type for the merkle root parsing. | ||
| #[derive(Clone, Debug, derive_more::Display, derive_more::From, derive_more::Error)] | ||
| pub enum MerkleRootParseError { | ||
| /// The merkle root isn't valid hex. | ||
| DecodeFailure(FromHexError), | ||
| /// The merkle root has the wrong number of bytes. | ||
| WrongLength(TryFromSliceError), | ||
| } | ||
|
|
||
| macro_rules! impl_scalar_type { | ||
| ($type:ty) => { | ||
| #[async_graphql::Scalar] | ||
| impl ScalarType for $type { | ||
| fn parse( | ||
| value: async_graphql::Value, | ||
| ) -> async_graphql::InputValueResult<Self> { | ||
| let async_graphql::Value::String(text) = value else { | ||
| return Err(InputValueError::expected_type(value)) | ||
| }; | ||
|
|
||
| text.parse().map_err(InputValueError::custom) | ||
| } | ||
|
|
||
| fn to_value(&self) -> async_graphql::Value { | ||
| async_graphql::Value::String(self.to_string()) | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| impl_scalar_type!(MerkleRoot); | ||
| impl_scalar_type!(BlockHeight); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.