Skip to content

Commit

Permalink
Merge branch 'main' into matheus23/name-accumulators
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 27, 2023
2 parents 3c66316 + 25c5215 commit ad378c6
Show file tree
Hide file tree
Showing 67 changed files with 1,008 additions and 1,307 deletions.
12 changes: 6 additions & 6 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"wnfs": "0.1.21",
"wnfs-bench": "0.1.21",
"wnfs-common": "0.1.21",
"wnfs-hamt": "0.1.21",
"wnfs-nameaccumulator": "0.1.21",
"wnfs-wasm": "0.1.21"
"wnfs": "0.1.22",
"wnfs-bench": "0.1.22",
"wnfs-common": "0.1.22",
"wnfs-hamt": "0.1.22",
"wnfs-nameaccumulator": "0.1.22",
"wnfs-wasm": "0.1.22"
}
125 changes: 64 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ This library is designed with WebAssembly in mind. You can follow instructions o
## Crates

- [wnfs](https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs)
- [wnfs-common](https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs-common)
- [wnfs-hamt](https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs-hamt)
- [wnfs-namefilter](https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs-namefilter)
- [wnfs-wasm](https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs-wasm)

## Building the Project
Expand Down Expand Up @@ -156,7 +159,7 @@ This library is designed with WebAssembly in mind. You can follow instructions o
Check [REQUIREMENTS](#requirements) on how to set up the `rs-wnfs` command.

```bash
scripts/rs-wnfs build --all
scripts/rs-wnfs build
```

- You can also build for specific crates
Expand All @@ -167,55 +170,62 @@ This library is designed with WebAssembly in mind. You can follow instructions o

## Usage

WNFS does not have an opinion on where you want to persist your content or the file tree. Instead, the API expects any object that implements the async [`BlockStore`][blockstore-trait] interface. This implementation also defers system-level operations to the user; requiring that operations like time and random number generation be passed in from the interface. This makes for a clean wasm interface that works everywhere.
WNFS does not have an opinion on where you want to persist your content or the file tree. Instead, the API takes any object that implements the asynchronous [`BlockStore`][blockstore-trait] trait. The library also avoids including system function calls that could possibly tie it to a set of platforms. Operations like time and random number generation have to be passed in via the API. This allows the library to be used in a wide variety of environments. It particularly makes virtualisation easier.

Let's see an example of working with a public directory. Here we are going to use the memory-based blockstore provided by the library.
Let's see an example of working with a public filesystem. We will use the in-memory block store provided by the library.
```rust
use wnfs::{MemoryBlockStore, PublicDirectory};
use anyhow::Result;
use chrono::Utc;
use std::rc::Rc;
use wnfs::public::PublicDirectory;
use wnfs_common::MemoryBlockStore;
#[async_std::main]
async fn main() {
async fn main() -> Result<()> {
// Create a new public directory.
let dir = &mut Rc::new(PublicDirectory::new(Utc::now()));
// Create a memory-based blockstore.
let store = &mut MemoryBlockStore::default();
// Create an in-memory block store.
let store = &MemoryBlockStore::default();
// Add a /pictures/cats subdirectory.
dir
.mkdir(&["pictures".into(), "cats".into()], Utc::now(), store)
.await
.unwrap();
dir.mkdir(&["pictures".into(), "cats".into()], Utc::now(), store)
.await?;
// Store the the file tree in the memory blockstore.
root_dir.store(store).await.unwrap();
// Store the the file tree in the in-memory block store.
dir.store(store).await?;
// Print root directory.
println!("{:#?}", root_dir);
// List all files in /pictures directory.
let result = dir.ls(&["pictures".into()], store).await?;
println!("Files in /pictures: {:#?}", result);
Ok(())
}
```
You may notice that we store the `root_dir` returned by the `mkdir` operation, not the `dir` we started with. That is because WNFS internal state is immutable and every operation potentially returns a new root directory. This allows us to track and rollback changes when needed. It also makes collaborative editing easier to implement and reason about. You can find more examples in the [`wnfs/examples/`][wnfs-examples] folder. And there is a basic demo of the filesystem immutability [here][wnfs-graph-demo].
Here we create a root directory `dir` and subsequently add a `/pictures/cats` subdirectory to it. As mentioned earlier, system-level operations like time are passed in from the API. In this case, we use the `Utc::now()` function from the [chrono][chrono-crate] crate to get the current time.
The private filesystem, on the other hand, is a bit more involved. [Hash Array Mapped Trie (HAMT)][hamt-wiki] is used as the intermediate format of private file tree before it is persisted to the blockstore. Our use of HAMTs obfuscate the file tree hierarchy.
`PublicDirectory` gets wrapped in `Rc` here because it lets us pass it around without worrying about ownership and lifetimes. Making the Rc `&mut` futher allows us to relinquish ownership to the interior `PublicDirectory` and point to a new one when needed (essentially for every write). This immutable way of handling changes has cool benefits like tracking and rolling back changes. It also makes collaborative editing easier to implement and reason about. You can find more examples in the [`wnfs/examples/`][wnfs-examples] folder.
That's the public filesystem, the private filesystem, on the other hand, is a bit more involved. The [Hash Array Mapped Trie (HAMT)][hamt-wiki] is where we store the private filesystem tree and some other information related to it. HAMT allows for effective storage and retrieval of encrypted and obfuscated filesystem trees and `PrivateForest` is basically a HAMT that can contain multiple file trees with hash for keys and CIDs for values.

```rust
use wnfs::{
private::PrivateForest, MemoryBlockStore, Namefilter, PrivateDirectory,
};
use anyhow::Result;
use chrono::Utc;
use rand::thread_rng;
use std::rc::Rc;
use wnfs::private::{PrivateDirectory, PrivateForest};
use wnfs_common::MemoryBlockStore;
use wnfs_namefilter::Namefilter;
#[async_std::main]
async fn main() {
// Create a memory-based blockstore.
let store = &mut MemoryBlockStore::default();
async fn main() -> Result<()> {
// Create an in-memory block store.
let store = &MemoryBlockStore::default();
// A random number generator the private filesystem can use.
// A random number generator.
let rng = &mut thread_rng();
// Create a private forest.
Expand All @@ -229,43 +239,42 @@ async fn main() {
));
// Add a file to /pictures/cats directory.
dir
.mkdir(
&["pictures".into(), "cats".into()],
true,
Utc::now(),
forest,
store,
rng,
)
.await
.unwrap();
dir.mkdir(
&["pictures".into(), "cats".into()],
true,
Utc::now(),
forest,
store,
rng,
)
.await?;
// Add a file to /pictures/dogs/billie.jpg file.
dir
.write(
&["pictures".into(), "dogs".into(), "billie.jpeg".into()],
true,
Utc::now(),
b"hello world".to_vec(),
forest,
store,
rng,
)
.await
.unwrap();
dir.write(
&["pictures".into(), "dogs".into(), "billie.jpg".into()],
true,
Utc::now(),
b"Hello! This is billie".to_vec(),
forest,
store,
rng,
)
.await?;
// List all files in /pictures directory.
let result = dir
.ls(&["pictures".into()], true, forest, store)
.await
.unwrap();
let result = dir.ls(&["pictures".into()], true, forest, store).await?;
println!("Files in /pictures: {:#?}", result);
Ok(())
}
```

Namefilters are currently how we identify private node blocks in the filesystem. They have nice properties, one of which is the ability to check if one node belongs to another. This is necessary in a filesystem where metadata like hierarchy needs to be hidden from observing agents. One notable caveat with namefilters is that they can only reliably store information of a file tree 47 levels deep or less so there is a plan to replace them with other cryptographic accumlators in the near future.
This example introduces a few new concepts. The first is the `PrivateForest` which is a HAMT that can contain multiple file trees.

The second is the `Namefilter` (a fixed-size bloomfilter) that lets us identify nodes in the filesystem, and are suitable for offspring checks. Namefilters currently have limitation on how deep the file tree can go but that is going to change in the near future.

Finally, we have the random number generator, `rng`, that the library uses for ridding predictability and avoiding collisions in the `PrivateForest`.

Check the [`wnfs/examples/`][wnfs-examples] folder for more examples.

Expand All @@ -274,13 +283,7 @@ Check the [`wnfs/examples/`][wnfs-examples] folder for more examples.
- Run all tests

```bash
scripts/rs-wnfs test --all
```
- Show code coverage
```bash
scripts/rs-wnfs coverage
scripts/rs-wnfs test
```

- Run benchmarks
Expand All @@ -303,7 +306,7 @@ This library recommends using [pre-commit][pre-commit-guide] for running pre-com
### Conventional Commits
This project *lightly* follows the [Conventional Commits convention][commit-spec-site]
This project _lightly_ follows the [Conventional Commits convention][commit-spec-site]
to help explain commit history and tie in with our release process. The full
specification can be found [here][commit-spec]. We recommend prefixing your
commits with a type of `fix`, `feat`, `docs`, `ci`, `refactor`, etc...,
Expand All @@ -327,7 +330,7 @@ We would be happy to try to answer your question or try opening a new issue on G
This project is licensed under the [Apache License 2.0](https://github.com/wnfs-wg/rs-wnfs/blob/main/LICENSE).
[benchmarks]: https://wnfs-wg.github.io/rs-wnfs/dev/bench/
[blockstore-trait]: https://github.com/wnfs-wg/rs-wnfs/blob/main/wnfs/src/common/blockstore.rs
[blockstore-trait]: https://github.com/wnfs-wg/rs-wnfs/blob/main/wnfs-common/src/blockstore.rs
[commit-spec]: https://www.conventionalcommits.org/en/v1.0.0/#specification
[commit-spec-site]: https://www.conventionalcommits.org/
[hamt-wiki]: https://en.wikipedia.org/wiki/Hash_array_mapped_trie
Expand Down
2 changes: 1 addition & 1 deletion wnfs-bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wnfs-bench"
version = "0.1.21"
version = "0.1.22"
description = "WNFS Benchmarks"
publish = false
edition = "2021"
Expand Down
7 changes: 7 additions & 0 deletions wnfs-common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [0.1.22](https://github.com/wnfs-wg/rs-wnfs/compare/wnfs-common-v0.1.21...wnfs-common-v0.1.22) (2023-06-23)


### Features

* make changes to BlockStore trait based on feedback ([#286](https://github.com/wnfs-wg/rs-wnfs/issues/286)) ([085242d](https://github.com/wnfs-wg/rs-wnfs/commit/085242d15aa48db17d77ed45e1c7717d13ed105f))

## [0.1.21](https://github.com/wnfs-wg/rs-wnfs/compare/wnfs-common-v0.1.20...wnfs-common-v0.1.21) (2023-05-22)


Expand Down
3 changes: 2 additions & 1 deletion wnfs-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wnfs-common"
version = "0.1.21"
version = "0.1.22"
description = "Common types for the Webnative Filesystem"
keywords = ["wnfs", "webnative", "ipfs", "decentralisation"]
categories = [
Expand All @@ -20,6 +20,7 @@ authors = ["The Fission Authors"]
anyhow = "1.0"
async-once-cell = "0.4"
async-trait = "0.1"
bytes = { version = "1.4.0", features = ["serde"] }
chrono = { version = "0.4.23", default-features = false, features = ["clock", "std"] }
futures = "0.3"
libipld = { version = "0.16", features = ["dag-cbor", "derive", "serde-codec"] }
Expand Down
75 changes: 56 additions & 19 deletions wnfs-common/src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
use crate::{dagcbor, AsyncSerialize, BlockStoreError, MAX_BLOCK_SIZE};
use anyhow::{bail, Result};
use async_trait::async_trait;
use bytes::Bytes;
use libipld::{
cid::Version,
multihash::{Code, MultihashDigest},
serde as ipld_serde, Cid, IpldCodec,
serde as ipld_serde, Cid,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{borrow::Cow, cell::RefCell, collections::HashMap};
use std::{cell::RefCell, collections::HashMap};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

/// The value representing the DAG-JSON codec.
///
/// - https://ipld.io/docs/codecs/#known-codecs
/// - https://github.com/multiformats/multicodec/blob/master/table.csv
pub const CODEC_DAG_JSON: u64 = 0x0129;

/// The value representing the DAG-CBOR codec.
///
/// - https://ipld.io/docs/codecs/#known-codecs
/// - https://github.com/multiformats/multicodec/blob/master/table.csv
pub const CODEC_DAG_CBOR: u64 = 0x71;

/// The value representing the DAG-Protobuf codec.
///
/// - https://ipld.io/docs/codecs/#known-codecs
/// - https://github.com/multiformats/multicodec/blob/master/table.csv
pub const CODEC_DAG_PB: u64 = 0x70;

/// The value representing the raw codec.
///
/// - https://ipld.io/docs/codecs/#known-codecs
/// - https://github.com/multiformats/multicodec/blob/master/table.csv
pub const CODEC_RAW: u64 = 0x55;

//--------------------------------------------------------------------------------------------------
// Type Definitions
Expand All @@ -16,8 +45,8 @@ use std::{borrow::Cow, cell::RefCell, collections::HashMap};
/// For types that implement block store operations like adding, getting content from the store.
#[async_trait(?Send)]
pub trait BlockStore: Sized {
async fn get_block(&self, cid: &Cid) -> Result<Cow<Vec<u8>>>;
async fn put_block(&self, bytes: Vec<u8>, codec: IpldCodec) -> Result<Cid>;
async fn get_block(&self, cid: &Cid) -> Result<Bytes>;
async fn put_block(&self, bytes: impl Into<Bytes>, codec: u64) -> Result<Cid>;

async fn get_deserializable<V: DeserializeOwned>(&self, cid: &Cid) -> Result<V> {
let bytes = self.get_block(cid).await?;
Expand All @@ -27,26 +56,28 @@ pub trait BlockStore: Sized {

async fn put_serializable<V: Serialize>(&self, value: &V) -> Result<Cid> {
let bytes = dagcbor::encode(&ipld_serde::to_ipld(value)?)?;
self.put_block(bytes, IpldCodec::DagCbor).await
self.put_block(bytes, CODEC_DAG_CBOR).await
}

async fn put_async_serializable<V: AsyncSerialize>(&self, value: &V) -> Result<Cid> {
let ipld = value.async_serialize_ipld(self).await?;
let bytes = dagcbor::encode(&ipld)?;
self.put_block(bytes, IpldCodec::DagCbor).await
self.put_block(bytes, CODEC_DAG_CBOR).await
}

// This should be the same in all implementations of BlockStore
fn create_cid(&self, bytes: &Vec<u8>, codec: IpldCodec) -> Result<Cid> {
fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid> {
// If there are too many bytes, abandon this task
if bytes.len() > MAX_BLOCK_SIZE {
bail!(BlockStoreError::MaximumBlockSizeExceeded(bytes.len()))
}

// Compute the SHA256 hash of the bytes
let hash = Code::Sha2_256.digest(bytes);

// Represent the hash as a V1 CID
let cid = Cid::new(Version::V1, codec.into(), hash)?;
// Return Ok with the CID
let cid = Cid::new(Version::V1, codec, hash)?;

Ok(cid)
}
}
Expand All @@ -59,7 +90,7 @@ pub trait BlockStore: Sized {
///
/// IPFS is basically a glorified HashMap.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct MemoryBlockStore(RefCell<HashMap<String, Vec<u8>>>);
pub struct MemoryBlockStore(RefCell<HashMap<String, Bytes>>);

impl MemoryBlockStore {
/// Creates a new in-memory block store.
Expand All @@ -71,22 +102,28 @@ impl MemoryBlockStore {
#[async_trait(?Send)]
impl BlockStore for MemoryBlockStore {
/// Retrieves an array of bytes from the block store with given CID.
async fn get_block(&self, cid: &Cid) -> Result<Cow<Vec<u8>>> {
Ok(Cow::Owned(
self.0
.borrow()
.get(&cid.to_string())
.ok_or(BlockStoreError::CIDNotFound(*cid))?
.clone(),
))
async fn get_block(&self, cid: &Cid) -> Result<Bytes> {
let bytes = self
.0
.borrow()
.get(&cid.to_string())
.ok_or(BlockStoreError::CIDNotFound(*cid))?
.clone();

Ok(bytes)
}

/// Stores an array of bytes in the block store.
async fn put_block(&self, bytes: Vec<u8>, codec: IpldCodec) -> Result<Cid> {
async fn put_block(&self, bytes: impl Into<Bytes>, codec: u64) -> Result<Cid> {
// Convert the bytes into a Bytes object
let bytes: Bytes = bytes.into();

// Try to build the CID from the bytes and codec
let cid = self.create_cid(&bytes, codec)?;

// Insert the bytes into the HashMap using the CID as the key
self.0.borrow_mut().insert(cid.to_string(), bytes);

// Return Ok status with the generated CID
Ok(cid)
}
Expand Down
Loading

0 comments on commit ad378c6

Please sign in to comment.