-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: graphql endpoint to fetch the blob byte code by its blob ID (#2096
) ## Linked Issues/PRs - Closes #2084 ## Description This PR adds a graphql query to fetch blob bytecode by their ID ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) --------- Co-authored-by: Mitchell Turner <james.mitchell.turner@gmail.com>
- Loading branch information
1 parent
b1f2bf4
commit 4c0cd47
Showing
25 changed files
with
239 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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,28 @@ | ||
use crate::client::schema::{ | ||
schema, | ||
BlobId, | ||
HexString, | ||
}; | ||
|
||
#[derive(cynic::QueryVariables, Debug)] | ||
pub struct BlobByIdArgs { | ||
pub id: BlobId, | ||
} | ||
|
||
#[derive(cynic::QueryFragment, Clone, Debug)] | ||
#[cynic( | ||
schema_path = "./assets/schema.sdl", | ||
graphql_type = "Query", | ||
variables = "BlobByIdArgs" | ||
)] | ||
pub struct BlobByIdQuery { | ||
#[arguments(id: $id)] | ||
pub blob: Option<Blob>, | ||
} | ||
|
||
#[derive(cynic::QueryFragment, Clone, Debug)] | ||
#[cynic(schema_path = "./assets/schema.sdl")] | ||
pub struct Blob { | ||
pub id: BlobId, | ||
pub bytecode: HexString, | ||
} |
This file contains 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 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,24 @@ | ||
use crate::client::{ | ||
schema, | ||
types::primitives::{ | ||
BlobId, | ||
Bytes, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct Blob { | ||
pub id: BlobId, | ||
pub bytecode: Bytes, | ||
} | ||
|
||
// GraphQL Translation | ||
|
||
impl From<schema::blob::Blob> for Blob { | ||
fn from(value: schema::blob::Blob) -> Self { | ||
Self { | ||
id: value.id.into(), | ||
bytecode: value.bytecode.into(), | ||
} | ||
} | ||
} |
This file contains 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 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 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 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,32 @@ | ||
use crate::graphql_api::ports::{ | ||
OffChainDatabase, | ||
OnChainDatabase, | ||
}; | ||
use fuel_core_storage::{ | ||
not_found, | ||
tables::BlobData, | ||
Result as StorageResult, | ||
StorageAsRef, | ||
}; | ||
use fuel_core_types::fuel_tx::BlobId; | ||
|
||
pub trait BlobQueryData: Send + Sync { | ||
fn blob_exists(&self, id: BlobId) -> StorageResult<bool>; | ||
fn blob_bytecode(&self, id: BlobId) -> StorageResult<Vec<u8>>; | ||
} | ||
|
||
impl<D: OnChainDatabase + OffChainDatabase + ?Sized> BlobQueryData for D { | ||
fn blob_exists(&self, id: BlobId) -> StorageResult<bool> { | ||
self.storage::<BlobData>().contains_key(&id) | ||
} | ||
|
||
fn blob_bytecode(&self, id: BlobId) -> StorageResult<Vec<u8>> { | ||
let blob = self | ||
.storage::<BlobData>() | ||
.get(&id)? | ||
.ok_or(not_found!(BlobData))? | ||
.into_owned(); | ||
|
||
Ok(blob.into()) | ||
} | ||
} |
This file contains 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 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 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,69 @@ | ||
use crate::{ | ||
fuel_core_graphql_api::QUERY_COSTS, | ||
graphql_api::IntoApiResult, | ||
query::BlobQueryData, | ||
schema::{ | ||
scalars::{ | ||
BlobId, | ||
HexString, | ||
}, | ||
ReadViewProvider, | ||
}, | ||
}; | ||
use async_graphql::{ | ||
Context, | ||
Object, | ||
}; | ||
use fuel_core_storage::{ | ||
not_found, | ||
tables::BlobData, | ||
}; | ||
use fuel_core_types::fuel_types; | ||
|
||
pub struct Blob(fuel_types::BlobId); | ||
|
||
#[Object] | ||
impl Blob { | ||
async fn id(&self) -> BlobId { | ||
self.0.into() | ||
} | ||
|
||
#[graphql(complexity = "QUERY_COSTS.bytecode_read")] | ||
async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString> { | ||
let query = ctx.read_view()?; | ||
query | ||
.blob_bytecode(self.0) | ||
.map(HexString) | ||
.map_err(async_graphql::Error::from) | ||
} | ||
} | ||
|
||
impl From<fuel_types::BlobId> for Blob { | ||
fn from(value: fuel_types::BlobId) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct BlobQuery; | ||
|
||
#[Object] | ||
impl BlobQuery { | ||
async fn blob( | ||
&self, | ||
ctx: &Context<'_>, | ||
#[graphql(desc = "ID of the Blob")] id: BlobId, | ||
) -> async_graphql::Result<Option<Blob>> { | ||
let query = ctx.read_view()?; | ||
query | ||
.blob_exists(id.0) | ||
.and_then(|blob_exists| { | ||
if blob_exists { | ||
Ok(id.0) | ||
} else { | ||
Err(not_found!(BlobData)) | ||
} | ||
}) | ||
.into_api_result() | ||
} | ||
} |
Oops, something went wrong.