Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexytsu committed Jul 5, 2022
1 parent c17b76d commit b391ee5
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
members = [
"fvm_dispatch",
"fil_token",
"testing/actors/fil_token_actor",
"testing/integration",
"testing/integration/actors/fil_token_actor",
]
2 changes: 2 additions & 0 deletions fil_token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ edition = "2021"
[dependencies]
anyhow = "1.0.56"
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }
fvm_dispatch = { version = "0.1.0", path = "../fvm_dispatch" }
fvm_ipld_blockstore = "0.1.1"
fvm_ipld_hamt = "0.5.1"
fvm_ipld_amt = { version = "0.4.2", features = ["go-interop"] }
fvm_ipld_encoding = "0.2.2"
fvm_sdk = { version = "1.0.0" }
fvm_shared = { version = "0.8.0" }
serde = { version = "1.0.136", features = ["derive"] }
40 changes: 40 additions & 0 deletions fil_token/src/blockstore.rs
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)
}
}
1 change: 1 addition & 0 deletions fil_token/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod blockstore;
pub mod token;

#[cfg(test)]
Expand Down
Loading

0 comments on commit b391ee5

Please sign in to comment.