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

feat: blob exists query #2300

Merged
merged 17 commits into from
Oct 5, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2265](https://github.com/FuelLabs/fuel-core/pull/2265): Integrate Block Committer API for DA Block Costs.
- [2280](https://github.com/FuelLabs/fuel-core/pull/2280): Allow comma separated relayer addresses in cli
- [2299](https://github.com/FuelLabs/fuel-core/pull/2299): Support blobs in the predicates.
- [2300](https://github.com/FuelLabs/fuel-core/pull/2300): Added new function to `fuel-core-client` for checking whether a blob exists.

### Changed

Expand Down
6 changes: 6 additions & 0 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,12 @@ impl FuelClient {
Ok(blob)
}

/// Check whether a blob with ID exists
pub async fn blob_exists(&self, id: BlobId) -> io::Result<bool> {
let query = schema::blob::BlobExistsQuery::build(BlobByIdArgs { id: id.into() });
Ok(self.query(query).await?.blob.is_some())
}

/// Retrieve multiple blocks
pub async fn blocks(
&self,
Expand Down
17 changes: 17 additions & 0 deletions crates/client/src/client/schema/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ pub struct Blob {
pub id: BlobId,
pub bytecode: HexString,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Blob")]
pub struct BlobIdFragment {
pub id: BlobId,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
variables = "BlobByIdArgs"
)]
pub struct BlobExistsQuery {
#[arguments(id: $id)]
pub blob: Option<BlobIdFragment>,
rymnc marked this conversation as resolved.
Show resolved Hide resolved
}
35 changes: 35 additions & 0 deletions tests/tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,41 @@ async fn blob__can_be_queried_if_uploaded() {
assert_eq!(queried_blob.bytecode, bytecode);
}

#[tokio::test]
async fn blob__exists_if_uploaded() {
// Given
let mut ctx = TestContext::new().await;
let bytecode: Vec<u8> = [op::ret(RegId::ONE)].into_iter().collect();
let (status, blob_id) = ctx.new_blob(bytecode.clone()).await.unwrap();
assert!(matches!(status, TransactionStatus::Success { .. }));

// When
let blob_exists = ctx
.client
.blob_exists(blob_id)
.await
.expect("blob query failed");

// Then
assert!(blob_exists);
}

#[tokio::test]
async fn blob__ask_whether_a_nonexisting_blob_exists() {
// Given
let ctx = TestContext::new().await;

// When
let blob_exists = ctx
.client
.blob_exists(Default::default())
.await
.expect("blob query failed");

// Then
assert!(!blob_exists);
}

#[tokio::test]
async fn predicate_can_load_blob() {
let blob_predicate = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();
Expand Down
Loading