Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Refs local endpoint #150

Merged
merged 1 commit into from Apr 14, 2020
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
4 changes: 3 additions & 1 deletion http/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod block;
pub mod dag;
pub mod id;
pub mod pubsub;
pub mod refs;
pub mod swarm;
pub mod version;

Expand Down Expand Up @@ -53,7 +54,8 @@ where
.or(warp::path!("pin" / ..).and_then(not_implemented))
.or(warp::path!("ping" / ..).and_then(not_implemented))
.or(pubsub::routes(ipfs))
.or(warp::path!("refs" / ..).and_then(not_implemented))
.or(refs::local(ipfs))
// .or(warp::path!("refs").and_then(refs::of_path))
.or(warp::path!("repo" / ..).and_then(not_implemented))
.or(warp::path!("stats" / ..).and_then(not_implemented))
.or(swarm::connect(ipfs))
Expand Down
92 changes: 92 additions & 0 deletions http/src/v0/refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use futures::stream;
use ipfs::error::Error;
use ipfs::{Ipfs, IpfsTypes};
use serde::Serialize;
use warp::hyper::Body;
use warp::{path, Filter, Rejection, Reply};

use super::support::{with_ipfs, StringError};

#[derive(Serialize, Debug)]
struct RefsResponseItem {
#[serde(rename = "Err")]
err: String,

#[serde(rename = "Ref")]
refs: String,
}

/// Handling of https://docs-beta.ipfs.io/reference/http/api/#api-v0-refs-local
pub fn local<T: IpfsTypes>(
ipfs: &Ipfs<T>,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
path!("refs" / "local")
.and(with_ipfs(ipfs))
.and_then(inner_local)
}

async fn inner_local<T: IpfsTypes>(ipfs: Ipfs<T>) -> Result<impl Reply, Rejection> {
let refs: Vec<Result<String, Error>> = ipfs
.refs_local()
.await
.map_err(StringError::from)?
.into_iter()
.map(|cid| cid.to_string())
.map(|refs| RefsResponseItem {
refs,
err: "".to_string(),
koivunej marked this conversation as resolved.
Show resolved Hide resolved
})
.map(|response| {
serde_json::to_string(&response).map_err(|e| {
eprintln!("error from serde_json: {}", e);
HandledErr
}).unwrap()
})
.map(|ref_json| Ok(format!("{}{}", ref_json, "\n")))
.collect();

let stream = stream::iter(refs);
Ok(warp::reply::Response::new(Body::wrap_stream(stream)))
}

#[derive(Debug)]
struct HandledErr;

impl std::error::Error for HandledErr {}

use std::fmt;

impl fmt::Display for HandledErr {
fn fmt(&self, _fmt: &mut fmt::Formatter) -> fmt::Result {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::inner_local;
use ipfs::Block;
use libipld::cid::Cid;
use libipld::cid::Codec;
use multihash::Sha2_256;

#[tokio::test]
async fn test_inner_local() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks incomplete...?

use ipfs::{IpfsOptions, UninitializedIpfs};

let options = IpfsOptions::inmemory_with_generated_keys(false);

let (mut ipfs, fut) = UninitializedIpfs::new(options).await.start().await.unwrap();
drop(fut);

for data in &[b"1", b"2", b"3"] {
let data_slice = data.to_vec().into_boxed_slice();
let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data_slice));
let block = Block::new(data_slice, cid);
ipfs.put_block(block.clone()).await.unwrap();
}

let _result = inner_local(ipfs).await;
// println!("{:?}", result.unwrap());
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ impl<Types: IpfsTypes> Ipfs<Types> {
Ok(rx.await?)
}

pub async fn refs_local(&self) -> Result<Vec<Cid>, Error> {
Ok(self.repo.list_blocks().await?)
}

pub async fn bitswap_stats(&self) -> Result<BitswapStats, Error> {
let (tx, rx) = oneshot_channel();

Expand Down
30 changes: 30 additions & 0 deletions src/repo/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl BlockStore for FsBlockStore {
}
Ok(())
}

async fn list(&self) -> Result<Vec<Cid>, Error> {
// unwrapping as we want to panic on poisoned lock
let guard = self.cids.lock().unwrap();
Ok(guard.iter().cloned().collect())
}
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -290,6 +296,30 @@ mod tests {
std::fs::remove_dir_all(&tmp).ok();
}

#[async_std::test]
async fn test_fs_blockstore_list() {
let mut tmp = temp_dir();
tmp.push("blockstore_list");
std::fs::remove_dir_all(&tmp).ok();

let block_store = FsBlockStore::new(tmp.clone().into());
block_store.init().await.unwrap();
block_store.open().await.unwrap();

for data in &[b"1", b"2", b"3"] {
let data_slice = data.to_vec().into_boxed_slice();
let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data_slice));
let block = Block::new(data_slice, cid);
block_store.put(block.clone()).await.unwrap();
}

let cids = block_store.list().await.unwrap();
assert_eq!(cids.len(), 3);
for cid in cids.iter() {
assert!(block_store.contains(cid).await.unwrap());
}
}

#[async_std::test]
#[cfg(feature = "rocksdb")]
fn test_rocks_datastore() {
Expand Down
30 changes: 30 additions & 0 deletions src/repo/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use async_std::sync::{Arc, Mutex};
use async_trait::async_trait;
use bitswap::Block;
use libipld::cid::Cid;

// FIXME: Transition to Persistent Map to make iterating more consistent
use std::collections::HashMap;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -61,6 +63,11 @@ impl BlockStore for MemBlockStore {
self.blocks.lock().await.remove(cid);
Ok(())
}

async fn list(&self) -> Result<Vec<Cid>, Error> {
let guard = self.blocks.lock().await;
Ok(guard.iter().map(|(cid, _block)| cid).cloned().collect())
}
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -164,6 +171,29 @@ mod tests {
assert_eq!(get.await.unwrap(), None);
}

#[async_std::test]
async fn test_mem_blockstore_list() {
let tmp = temp_dir();
let mem_store = MemBlockStore::new(tmp.into());

mem_store.init().await.unwrap();
mem_store.open().await.unwrap();

for data in &[b"1", b"2", b"3"] {
let data_slice = data.to_vec().into_boxed_slice();
let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data_slice));
let block = Block::new(data_slice, cid);
mem_store.put(block.clone()).await.unwrap();
assert!(mem_store.contains(block.cid()).await.unwrap());
}

let cids = mem_store.list().await.unwrap();
assert_eq!(cids.len(), 3);
for cid in cids.iter() {
assert!(mem_store.contains(cid).await.unwrap());
}
}

#[async_std::test]
async fn test_mem_datastore() {
let tmp = temp_dir();
Expand Down
5 changes: 5 additions & 0 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub trait BlockStore: Debug + Clone + Send + Sync + Unpin + 'static {
async fn get(&self, cid: &Cid) -> Result<Option<Block>, Error>;
async fn put(&self, block: Block) -> Result<(Cid, BlockPut), Error>;
async fn remove(&self, cid: &Cid) -> Result<(), Error>;
async fn list(&self) -> Result<Vec<Cid>, Error>;
}

#[async_trait]
Expand Down Expand Up @@ -183,6 +184,10 @@ impl<TRepoTypes: RepoTypes> Repo<TRepoTypes> {
}
}

pub async fn list_blocks(&self) -> Result<Vec<Cid>, Error> {
Ok(self.block_store.list().await?)
}

/// Remove block from the block store.
pub async fn remove_block(&self, cid: &Cid) -> Result<(), Error> {
if self.is_pinned(cid).await? {
Expand Down