#cid #ipld #bitswap

blockstore

An IPLD blockstore capable of holding arbitrary data indexed by CID

6 releases (breaking)

0.5.0 Apr 15, 2024
0.4.0 Apr 3, 2024
0.3.0 Apr 2, 2024
0.2.0 Mar 26, 2024
0.1.1 Jan 15, 2024

#162 in Asynchronous

Download history 48/week @ 2024-01-16 33/week @ 2024-01-23 158/week @ 2024-01-30 361/week @ 2024-02-06 463/week @ 2024-02-13 365/week @ 2024-02-20 378/week @ 2024-02-27 443/week @ 2024-03-05 545/week @ 2024-03-12 569/week @ 2024-03-19 1029/week @ 2024-03-26 1774/week @ 2024-04-02 1614/week @ 2024-04-09 1268/week @ 2024-04-16 1022/week @ 2024-04-23 591/week @ 2024-04-30

4,726 downloads per month
Used in 6 crates (3 directly)

Apache-2.0

43KB
867 lines

Blockstore

An IPLD blockstore capable of holding arbitrary data indexed by CID.

use blockstore::{Blockstore, InMemoryBlockstore};
use blockstore::block::{Block, CidError};
use cid::Cid;
use multihash_codetable::{Code, MultihashDigest};

const RAW_CODEC: u64 = 0x55;

struct RawBlock(Vec<u8>);

impl Block<64> for RawBlock {
    fn cid(&self) -> Result<Cid, CidError> {
        let hash = Code::Sha2_256.digest(&self.0);
        Ok(Cid::new_v1(RAW_CODEC, hash))
    }

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

async fn put_and_get() {
    let block = RawBlock(vec![0xde, 0xad]);
    let cid = block.cid().unwrap();

    let blockstore = InMemoryBlockstore::<64>::new();
    blockstore.put(block).await.unwrap();

    let block = blockstore.get(&cid).await.unwrap();

    assert_eq!(block, Some(vec![0xde, 0xad]));
}

tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()
    .unwrap()
    .block_on(put_and_get());

Dependencies

~2–11MB
~98K SLoC