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

Foreign Blockstore Support #16

Merged
merged 8 commits into from
May 17, 2022
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ Its goal is to be as dependency-less as possible in order to be easily compiled
cargo install wasm-pack
```

On ARM-based (M1 family) macOS, you might need to explicitly install the following:

- Install wasm-bindgen

```bash
cargo install -f wasm-bindgen-cli
```

- Install wasm-opt

```bash
brew install binaryen
```

</details>

- **The _wnfs_ Helper Script**
Expand Down
1 change: 1 addition & 0 deletions crates/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ field_names = "0.2.0"
futures = "0.3.21"
async-stream = "0.3.3"


[lib]
path = "lib.rs"
crate-type = ["cdylib" , "rlib"]
Expand Down
9 changes: 1 addition & 8 deletions crates/fs/common/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ pub trait BlockStoreLookup {
async fn get_block<'a>(&'a self, cid: &Cid) -> Result<Cow<'a, Vec<u8>>>;
}

// /// For types that implement loading decodable object from a blockstore using a CID.
// #[async_trait(?Send)]
// pub trait BlockStoreCidLoad {
// /// Loads a decodable object from the store with provided CID.
// async fn load<T: Decode<C>, C: Codec>(&self, cid: &Cid, decoder: C) -> Result<T>;
// }

/// For types that implement block store operations like adding, getting content from the store.
#[async_trait(?Send)]
pub trait BlockStore: BlockStoreLookup {
Expand All @@ -35,7 +28,7 @@ pub trait BlockStore: BlockStoreLookup {

/// An in-memory block store to simulate IPFS.
///
/// IPFS is basically an glorified HashMap.
/// IPFS is basically a glorified HashMap.
#[derive(Debug, Default)]
pub struct MemoryBlockStore(HashMap<String, Vec<u8>>);

Expand Down
14 changes: 8 additions & 6 deletions crates/fs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ pub use utils::*;
// Re-exports
//--------------------------------------------------------------------------------------------------

pub use libipld::{
cbor::DagCborCodec,
codec::Codec,
codec::{Decode, Encode},
Cid, IpldCodec,
};
pub mod ipld {
pub use libipld::{
cbor::DagCborCodec,
codec::Codec,
codec::{Decode, Encode},
Cid, IpldCodec,
};
}

//--------------------------------------------------------------------------------------------------
// Utilities
Expand Down
1 change: 1 addition & 0 deletions crates/wasm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pkg/
node_modules/
yarn-error.log
dist/
tests/dist/
build/
2 changes: 1 addition & 1 deletion crates/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories = [
"web-programming",
"wasm",
]
license-file = "../../LICENSE"
license-file = "LICENSE"
readme = "README.md"
edition = "2021"
repository = "https://github.com/fission-suite/rs-wnfs"
Expand Down
16 changes: 0 additions & 16 deletions crates/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,3 @@
```bash
yarn playwright test
```

## Trying the Examples

- Run server

```bash
npx http-server -p 8080
```

- Visit the following pages:

```bash
open http://localhost:8080/examples/graph/
```

NOTE: Examples will be moved into separate projects in the future.
21 changes: 21 additions & 0 deletions crates/wasm/examples/graph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## The WNFS Graph Example

## Usage

- Build project

```bash
npx webpack
```

- Run project

```bash
npx http-server -p 8080 dist
```

- Open browser

```bash
open http://localhost:8080
```
1 change: 0 additions & 1 deletion crates/wasm/examples/graph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WNFS Graph</title>
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div id="graph-canvas"></div>
Expand Down
29 changes: 29 additions & 0 deletions crates/wasm/examples/graph/src/blockstore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CID } from "multiformats/cid";
import { sha256 } from "multiformats/hashes/sha2";

/**
* An in-memory block store to simulate IPFS.
*
* IPFS is basically a glorified HashMap.
*/
export class MemoryBlockStore {
private store: Map<string, Uint8Array>;

/** Creates a new in-memory block store. */
constructor() {
this.store = new Map();
}

/** Stores an array of bytes in the block store. */
async getBlock(cid: Uint8Array): Promise<Uint8Array | undefined> {
const decoded_cid = CID.decode(cid);
return this.store.get(decoded_cid.toString());
}

/** Retrieves an array of bytes from the block store with given CID. */
async putBlock(bytes: Uint8Array, code: number): Promise<void> {
const hash = await sha256.digest(bytes);
const cid = CID.create(1, code, hash);
this.store.set(cid.toString(), bytes);
}
}
23 changes: 23 additions & 0 deletions crates/wasm/examples/graph/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MemoryBlockStore } from "./blockstore";
import { Nullable } from "./types";

declare global {
var store: MemoryBlockStore;
class LeaderLine {
constructor(
start: Nullable<HTMLElement>,
end: Nullable<HTMLElement>,
options?: Options
);
}
}

type Options = {
color?: string;
size?: number;
dash?: {
animation: boolean;
};
};

export {};
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import init, { MemoryBlockStore, PublicDirectory } from "./wasm_wnfs.js";
import "./styles/index.css";

import { draw } from "./render.js";
import { MemoryBlockStore } from "./blockstore";
import { draw } from "./render";

//------------------------------------------------------------------------------
// Init
//------------------------------------------------------------------------------

await init();
const { PublicDirectory } = await import("../../../pkg/index");

const time = new Date();
const store = new MemoryBlockStore();
Expand Down
Loading