-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
379 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::convert::TryFrom; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use cid::multihash::Code; | ||
use cid::Cid; | ||
use fvm_ipld_blockstore::Block; | ||
use fvm_sdk::ipld; | ||
|
||
/// A blockstore that delegates to IPLD syscalls. | ||
#[derive(Default, Debug, Copy, Clone)] | ||
pub struct Blockstore; | ||
|
||
impl fvm_ipld_blockstore::Blockstore for Blockstore { | ||
fn get(&self, cid: &Cid) -> Result<Option<Vec<u8>>> { | ||
// If this fails, the _CID_ is invalid. I.e., we have a bug. | ||
ipld::get(cid) | ||
.map(Some) | ||
.map_err(|e| anyhow!("get failed with {:?} on CID '{}'", e, cid)) | ||
} | ||
|
||
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> { | ||
let code = Code::try_from(k.hash().code()).map_err(|e| anyhow!(e.to_string()))?; | ||
let k2 = self.put(code, &Block::new(k.codec(), block))?; | ||
if k != &k2 { | ||
return Err(anyhow!("put block with cid {} but has cid {}", k, k2)); | ||
} | ||
Ok(()) | ||
} | ||
fn put<D>(&self, code: Code, block: &Block<D>) -> Result<Cid> | ||
where | ||
D: AsRef<[u8]>, | ||
{ | ||
// TODO: Don't hard-code the size. Unfortunately, there's no good way to get it from the | ||
// codec at the moment. | ||
const SIZE: u32 = 32; | ||
let k = ipld::put(code.into(), SIZE, block.codec, block.data.as_ref()) | ||
.map_err(|e| anyhow!("put failed with {:?}", e))?; | ||
Ok(k) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod blockstore; | ||
pub mod token; | ||
|
||
#[cfg(test)] | ||
|
Oops, something went wrong.