Skip to content

Commit

Permalink
refactor: Switch to ipld-core and eigerco/blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 22, 2024
1 parent 85f3e6c commit e3cf6e1
Show file tree
Hide file tree
Showing 63 changed files with 2,186 additions and 1,179 deletions.
50 changes: 44 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"wnfs-unixfs-file",
"wnfs-wasm",
]
resolver = "2"

# Speedup build on macOS
# See https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#splitting-debug-information
Expand Down
23 changes: 13 additions & 10 deletions wnfs-bench/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use criterion::{
use proptest::{arbitrary::any, collection::vec, test_runner::TestRunner};
use std::cmp;
use wnfs_common::{
blockstore::{block::Block as _, Blockstore, InMemoryBlockstore},
utils::{Arc, Sampleable},
BlockStore, Link, MemoryBlockStore, Storable, StoreIpld,
Blake3Block, Link, Storable, StoreIpld,
};
use wnfs_hamt::{
diff, merge,
Expand All @@ -17,7 +18,7 @@ use wnfs_hamt::{

fn node_set(c: &mut Criterion) {
let mut runner = TestRunner::deterministic();
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let operations = operations(any::<[u8; 32]>(), any::<u64>(), 1_000_000).sample(&mut runner);
let node =
&async_std::task::block_on(async { node_from_operations(&operations, &store).await })
Expand Down Expand Up @@ -50,7 +51,7 @@ fn node_set_consecutive(c: &mut Criterion) {
c.bench_function("node set 1000 consecutive", |b| {
b.to_async(AsyncStdExecutor).iter_batched(
|| {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let operations =
operations(any::<[u8; 32]>(), any::<u64>(), 1000).sample(&mut runner);
let node = async_std::task::block_on(async {
Expand All @@ -73,7 +74,7 @@ fn node_set_consecutive(c: &mut Criterion) {
}

fn node_load_get(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let cid = async_std::task::block_on(async {
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
Expand All @@ -100,7 +101,7 @@ fn node_load_get(c: &mut Criterion) {
}

fn node_load_remove(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let cid = async_std::task::block_on(async {
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
Expand All @@ -123,7 +124,7 @@ fn node_load_remove(c: &mut Criterion) {
}

fn hamt_load_decode(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let (cid, bytes) = async_std::task::block_on(async {
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
Expand All @@ -137,7 +138,9 @@ fn hamt_load_decode(c: &mut Criterion) {
.encode_ipld()
.unwrap();

let cid = store.put_block(encoded_hamt.clone(), codec).await.unwrap();
let block = Blake3Block::new(codec, encoded_hamt.clone());
let cid = block.cid().unwrap();
store.put(block).await.unwrap();

(cid, encoded_hamt)
});
Expand All @@ -157,7 +160,7 @@ fn hamt_set_encode(c: &mut Criterion) {
b.to_async(AsyncStdExecutor).iter_batched(
|| {
(
MemoryBlockStore::default(),
InMemoryBlockstore::<64>::new(),
Arc::new(<Node<_, _>>::default()),
)
},
Expand Down Expand Up @@ -187,7 +190,7 @@ fn hamt_diff(c: &mut Criterion) {
c.bench_function("hamt diff", |b| {
b.to_async(AsyncStdExecutor).iter_batched(
|| {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let kvs1 = generate_kvs("[a-z0-9]{1,3}", 0u64..1000, 0..100).sample(&mut runner);
let kvs2 = generate_kvs("[a-z0-9]{1,3}", 0u64..1000, 0..100).sample(&mut runner);
let (node1, node2) = task::block_on(async {
Expand Down Expand Up @@ -216,7 +219,7 @@ fn hamt_merge(c: &mut Criterion) {
c.bench_function("hamt merge", |b| {
b.to_async(AsyncStdExecutor).iter_batched(
|| {
let store = MemoryBlockStore::default();
let store = InMemoryBlockstore::<64>::new();
let kvs1 = generate_kvs("[a-z0-9]{1,3}", 0u64..1000, 0..100).sample(&mut runner);
let kvs2 = generate_kvs("[a-z0-9]{1,3}", 0u64..1000, 0..100).sample(&mut runner);
let (node1, node2) = task::block_on(async {
Expand Down
23 changes: 18 additions & 5 deletions wnfs-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,42 @@ anyhow = "1.0"
async-once-cell = "0.5"
base64 = { version = "0.21", optional = true }
base64-serde = { version = "0.7", optional = true }
blake3 = "1.5.1"
blockstore = "0.5"
bytes = { version = "1.4", features = ["serde"] }
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
cid = "0.10"
dashmap = "5.5.3"
cid = "0.11"
futures = "0.3"
libipld = { version = "0.16", features = ["dag-cbor", "derive", "serde-codec"] }
ipld-core = "0.4"
ipld-dagpb = { version = "0.2", optional = true }
multihash = "0.18"
once_cell = "1.16"
parking_lot = "0.12"
proptest = { version = "1.1", optional = true }
rand_core = "0.6"
serde = { version = "1.0", features = ["rc"] }
serde_ipld_dagcbor = "0.4.2"
serde_ipld_dagcbor = "0.4"
serde_ipld_dagjson = { version = "0.2", optional = true }
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"

[dev-dependencies]
async-std = { version = "1.11", features = ["attributes"] }
base64 = "0.21"
base64-serde = "0.7"
ipld-dagpb = "0.2"
proptest = "1.1"
rand = "0.8"
serde_ipld_dagjson = "0.2"
serde_json = "1.0"
testresult = "0.4.0"

[features]
test_utils = ["dep:proptest", "dep:base64-serde", "dep:base64", "dep:serde_json"]
test_utils = [
"dep:proptest",
"dep:base64-serde",
"dep:base64",
"dep:serde_json",
"dep:serde_ipld_dagjson",
"dep:ipld-dagpb"
]
72 changes: 72 additions & 0 deletions wnfs-common/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use bytes::Bytes;
use cid::{multihash::Multihash, Cid};

//--------------------------------------------------------------------------------------------------
// 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;

const MULTICODEC_BLAKE3: u64 = 0x1e;

//--------------------------------------------------------------------------------------------------
// Traits
//--------------------------------------------------------------------------------------------------

pub struct Blake3Block {
cid: Cid,
bytes: Bytes,
}

//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

impl Blake3Block {
pub fn new(codec: u64, bytes: impl Into<Bytes>) -> Self {
let bytes: Bytes = bytes.into();

// Compute the Blake3 hash of the bytes
let hash = blake3::hash(&bytes);

let multihash =
Multihash::wrap(MULTICODEC_BLAKE3, hash.as_bytes()).expect("constant hash size");

// Represent the hash as a V1 CID
let cid = Cid::new_v1(codec, multihash);

Self { cid, bytes }
}
}

impl blockstore::block::Block<64> for Blake3Block {
fn cid(&self) -> Result<Cid, blockstore::block::CidError> {
Ok(self.cid)
}

fn data(&self) -> &[u8] {
self.bytes.as_ref()
}
}
Loading

0 comments on commit e3cf6e1

Please sign in to comment.