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

fault_proving(global_roots): GraphQL API for serving state roots #2742

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/added/2724.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Explicit error type for merkleized storage.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
"crates/fuel-core",
"crates/fuel-gas-price-algorithm",
"crates/keygen",
"crates/metrics",
"crates/metrics", "crates/proof_system/global_merkle_root/api",
"crates/proof_system/global_merkle_root/service",
"crates/proof_system/global_merkle_root/storage",
"crates/services",
Expand Down Expand Up @@ -106,6 +106,10 @@ fuel-vm-private = { version = "0.59.2", package = "fuel-vm", default-features =

# Common dependencies
anyhow = "1.0"
async-graphql = { version = "7.0.11", features = [
"graphiql",
"tracing",
], default-features = false }
async-trait = "0.1"
aws-sdk-kms = "1.37"
cynic = { version = "3.1.0", features = ["http-reqwest"] }
Expand Down
25 changes: 24 additions & 1 deletion crates/client/src/client/schema/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,30 @@ macro_rules! number_scalar {

number_scalar!(U128, u128);
number_scalar!(U64, u64);
number_scalar!(U32, u32);
#[derive(
Debug, Clone, derive_more::Into, derive_more::From, PartialOrd, Eq, PartialEq,
)]
pub struct U32(pub u32);

impl_scalar!(U32, schema::U32);
impl Serialize for U32 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = self.0.to_string();
serializer.serialize_str(s.as_str())
}
}
impl<'de> Deserialize<'de> for U32 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
Ok(Self(s.parse().map_err(D::Error::custom)?))
}
}
number_scalar!(U16, u16);

impl TryFrom<U64> for PanicInstruction {
Expand Down
5 changes: 1 addition & 4 deletions crates/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ version = { workspace = true }

[dependencies]
anyhow = { workspace = true }
async-graphql = { version = "7.0.11", features = [
"graphiql",
"tracing",
], default-features = false }
async-graphql = { workspace = true }
async-graphql-value = "7.0.11"
async-trait = { workspace = true }
axum = { workspace = true }
Expand Down
48 changes: 48 additions & 0 deletions crates/proof_system/global_merkle_root/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[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-trait = { workspace = true }
async-graphql = { workspace = true }
axum = { workspace = true }
derive_more = { workspace = true }
fuel-core-global-merkle-root-storage = { 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 }
tracing = { workspace = true }

[dev-dependencies]
fuel-core-global-merkle-root-storage = { workspace = true, features = [
"test-helpers",
] }
fuel-core-storage = { workspace = true, features = ["alloc", "test-helpers"] }
fuel-core-types = { workspace = true, default-features = false, features = [
"serde",
"random",
"test-helpers",
] }
rand = { workspace = true }
tokio = { workspace = true }

[features]
fault-proving = [
"fuel-core-types/fault-proving",
"fuel-core-storage/fault-proving",
"fuel-core-global-merkle-root-storage/fault-proving",
]
20 changes: 20 additions & 0 deletions crates/proof_system/global_merkle_root/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! The state root API

//#![deny(clippy::arithmetic_side_effects)]
//#![deny(clippy::cast_possible_truncation)]
//#![deny(unused_crate_dependencies)]
//#![deny(missing_docs)]
//#![deny(warnings)]
// TODO: Uncomment

/// Port definitions
pub mod ports;

/// API service definition
pub mod service;

/// GraphQL schema definition
pub mod schema;

#[cfg(test)]
pub mod tests;
10 changes: 10 additions & 0 deletions crates/proof_system/global_merkle_root/api/src/ports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use fuel_core_storage::Error as StorageError;
use fuel_core_types::{
fuel_tx::Bytes32,
fuel_types::BlockHeight,
};

pub trait GetStateRoot {
fn state_root_at(&self, height: BlockHeight)
-> Result<Option<Bytes32>, StorageError>;
}
104 changes: 104 additions & 0 deletions crates/proof_system/global_merkle_root/api/src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
array::TryFromSliceError,
fmt::Display,
str::FromStr,
};

use async_graphql::{
EmptyMutation,
EmptySubscription,
InputValueError,
ScalarType,
};
use hex::FromHexError;

use crate::ports;

pub type Schema<Storage> =
async_graphql::Schema<Query<Storage>, EmptyMutation, EmptySubscription>;

pub struct Query<Storage> {
storage: Storage,
}

impl<Storage> Query<Storage> {
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<Bytes32>> {
let state_root = self.storage.state_root_at(height.0.into())?;

Ok(state_root.map(|root| Bytes32(*root)))
}
}

#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
derive_more::FromStr,
derive_more::Display,
)]
pub struct BlockHeight(u32);

#[derive(Clone, Copy, Debug)]
pub struct Bytes32([u8; 32]);

impl Display for Bytes32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{}", hex::encode(&self.0))
}
}

impl FromStr for Bytes32 {
type Err = Bytes32ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(s.trim_start_matches("0x"))?;
Ok(Bytes32(bytes.as_slice().try_into()?))
}
}

#[derive(Clone, Debug, derive_more::Display, derive_more::From, derive_more::Error)]
pub enum Bytes32ParseError {
DecodeFailure(FromHexError),
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!(Bytes32);
impl_scalar_type!(BlockHeight);
Loading
Loading