Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
fix: Recover from Kubo pin check (#193)
Browse files Browse the repository at this point in the history
Previously, if a Kubo instance was missing a pin, we would get an
unexpected response and the client method would return an error result.
Now we recover and just mark the block as not pinned (it is acceptable
to resend a block that is actually pinned, just a wasteful).
  • Loading branch information
cdata authored Dec 14, 2022
1 parent 402a8dc commit b0e0851
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions rust/noosphere-cli/src/native/commands/serve/ipfs/kubo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ impl IpfsClient for KuboClient {
let response = self.client.request(request).await?;

let body_bytes = hyper::body::to_bytes(response.into_body()).await?;
let pin_ls_response: PinLsResponse = serde_json::from_slice(body_bytes.as_ref())?;

Ok(pin_ls_response.keys.contains_key(&cid_base64))
match serde_json::from_slice(body_bytes.as_ref()) {
Ok(PinLsResponse { keys }) => Ok(keys.contains_key(&cid_base64)),
Err(_) => Ok(false),
}
}

async fn server_identity(&self) -> Result<String> {
Expand All @@ -54,9 +55,10 @@ impl IpfsClient for KuboClient {
let response = self.client.request(request).await?;

let body_bytes = hyper::body::to_bytes(response.into_body()).await?;
let id_response: IdResponse = serde_json::from_slice(body_bytes.as_ref())?;

Ok(id_response.public_key)
match serde_json::from_slice(body_bytes.as_ref())? {
IdResponse { public_key, .. } => Ok(public_key),
}
}

async fn syndicate_blocks<R>(&self, car: R) -> Result<()>
Expand Down Expand Up @@ -152,4 +154,14 @@ mod tests {
assert!(kubo_client.block_is_pinned(&foo_cid).await.unwrap());
assert!(kubo_client.block_is_pinned(&bar_cid).await.unwrap());
}

#[tokio::test]
pub async fn it_gives_a_useful_result_when_a_block_is_not_pinned() {
initialize_tracing();

let (cid, _) = block_serialize::<DagCborCodec, _>(vec![1, 2, 3]).unwrap();

let kubo_client = KuboClient::new(&Url::parse("http://127.0.0.1:5001").unwrap()).unwrap();
assert!(!kubo_client.block_is_pinned(&cid).await.unwrap());
}
}

0 comments on commit b0e0851

Please sign in to comment.