Skip to content

Commit

Permalink
feat: Add .blob query in fuel-core-client and use it in the test
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed Aug 19, 2024
1 parent 263cb20 commit 74a2317
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
11 changes: 10 additions & 1 deletion crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ use fuel_core_types::{
Word,
},
fuel_tx::{
BlobId,
Bytes32,
Receipt,
Transaction,
TxId,
},
fuel_types,
fuel_types::{
self,
canonical::Serialize,
BlockHeight,
Nonce,
Expand All @@ -69,6 +70,7 @@ use pagination::{
};
use schema::{
balance::BalanceArgs,
blob::BlobByIdArgs,
block::BlockByIdArgs,
coins::CoinByIdArgs,
contract::ContractByIdArgs,
Expand Down Expand Up @@ -778,6 +780,13 @@ impl FuelClient {
Ok(block)
}

/// Retrieve a blob by its ID
pub async fn blob(&self, id: BlobId) -> io::Result<Option<types::Blob>> {
let query = schema::blob::BlobByIdQuery::build(BlobByIdArgs { id: id.into() });
let blob = self.query(query).await?.blob.map(Into::into);
Ok(blob)
}

/// Retrieve multiple blocks
pub async fn blocks(
&self,
Expand Down
1 change: 1 addition & 0 deletions crates/client/src/client/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::client::pagination::{
pub use primitives::*;

pub mod balance;
pub mod blob;
pub mod block;
pub mod chain;
pub mod coins;
Expand Down
28 changes: 28 additions & 0 deletions crates/client/src/client/schema/blob.rs
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,
}
3 changes: 3 additions & 0 deletions crates/client/src/client/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod balance;
pub mod blob;
pub mod block;
pub mod chain_info;
pub mod coins;
Expand All @@ -11,6 +12,7 @@ pub mod message;
pub mod node_info;

pub use balance::Balance;
pub use blob::Blob;
pub use block::{
Block,
Consensus,
Expand Down Expand Up @@ -67,6 +69,7 @@ pub mod primitives {
fuel_types::{
Address,
AssetId,
BlobId,
Bytes32,
Bytes64,
ChainId,
Expand Down
24 changes: 24 additions & 0 deletions crates/client/src/client/types/blob.rs
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(),
}
}
}
35 changes: 13 additions & 22 deletions tests/tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use fuel_core::{
database_description::on_chain::OnChain,
Database,
},
schema::scalars,
service::Config,
};
use fuel_core_bin::FuelService;
Expand All @@ -31,7 +30,7 @@ use fuel_core_types::{
use tokio::io;

struct TestContext {
node: FuelService,
_node: FuelService,
client: FuelClient,
}
impl TestContext {
Expand All @@ -47,7 +46,10 @@ impl TestContext {
.unwrap();
let client = FuelClient::from(node.bound_address);

Self { node, client }
Self {
_node: node,
client,
}
}

async fn new_blob(
Expand All @@ -71,22 +73,6 @@ impl TestContext {
.await?;
Ok((status, blob_id))
}

async fn query_blob(&self, blob_id: BlobId) -> String {
let blob_id: scalars::BlobId = blob_id.into();
let query = r#"
query {
blob(id: "BLOB_ID" ) {
id,
bytecode
}
}
"#
.replace("BLOB_ID", &blob_id.to_string());

let url = format!("http://{}/v1/graphql", self.node.bound_address);
test_helpers::send_graph_ql_query(&url, &query).await
}
}

#[tokio::test]
Expand Down Expand Up @@ -178,9 +164,14 @@ async fn blob__can_be_queried_if_uploaded() {
assert!(matches!(status, TransactionStatus::Success { .. }));

// When
let result = ctx.query_blob(blob_id).await;
let queried_blob = ctx
.client
.blob(blob_id)
.await
.expect("blob query failed")
.expect("no block returned");

// Then
assert!(result.contains(&format!(r#""id":"0x{}"#, blob_id)));
assert!(result.contains(&format!(r#""bytecode":"0x{}""#, hex::encode(bytecode))));
assert_eq!(queried_blob.id, blob_id);
assert_eq!(queried_blob.bytecode, bytecode);
}

0 comments on commit 74a2317

Please sign in to comment.