Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Cargo.lock
**/*.rs.bk

# IDE specific user-config
.vscode/
.vscode/
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

members = [
"fvm_dispatch",
"fil_token",
"testing/fil_token_integration",
"testing/fil_token_integration/actors/wfil_token_actor",
]
17 changes: 17 additions & 0 deletions fil_token/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "fil_token"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.56"
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }
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"] }
serde_tuple = { version = "0.5.0" }
thiserror = { version = "1.0.31" }
72 changes: 72 additions & 0 deletions fil_token/src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! Blockstore implementation is borrowed from https://github.com/filecoin-project/builtin-actors/blob/6df845dcdf9872beb6e871205eb34dcc8f7550b5/runtime/src/runtime/actor_blockstore.rs
//! This impl will likely be made redundant if low-level SDKs export blockstore implementations
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)
}
}

// TODO: put this somewhere more appropriate when a tests folder exists
/// An in-memory blockstore impl taken from filecoin-project/ref-fvm
#[derive(Debug, Default, Clone)]
pub struct MemoryBlockstore {
blocks: RefCell<HashMap<Cid, Vec<u8>>>,
}

use std::{cell::RefCell, collections::HashMap};
impl MemoryBlockstore {
pub fn new() -> Self {
Self::default()
}
}

impl fvm_ipld_blockstore::Blockstore for MemoryBlockstore {
fn has(&self, k: &Cid) -> Result<bool> {
Ok(self.blocks.borrow().contains_key(k))
}

fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>> {
Ok(self.blocks.borrow().get(k).cloned())
}

fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> {
self.blocks.borrow_mut().insert(*k, block.into());
Ok(())
}
}
6 changes: 6 additions & 0 deletions fil_token/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod blockstore;
pub mod runtime;
pub mod token;

#[cfg(test)]
mod tests {}
19 changes: 19 additions & 0 deletions fil_token/src/runtime/fvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::Runtime;

use anyhow::{anyhow, Result};
use fvm_sdk as sdk;
use sdk::actor;
use sdk::message;

/// Provides access to the FVM which acts as the runtime for actors deployed on-chain
pub struct FvmRuntime {}

impl Runtime for FvmRuntime {
fn caller(&self) -> u64 {
message::caller()
}

fn resolve_address(&self, addr: &fvm_shared::address::Address) -> Result<u64> {
actor::resolve_address(addr).ok_or_else(|| anyhow!("Failed to resolve address"))
}
}
15 changes: 15 additions & 0 deletions fil_token/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod fvm;
pub use fvm::*;

use anyhow::Result;
use fvm_shared::address::Address;

/// Abstraction of the runtime that an actor is executed in, providing access to syscalls and
/// features of the FVM
pub trait Runtime {
/// Get the direct-caller that invoked the current actor
fn caller(&self) -> u64;

/// Attempts to resolve an address to an ActorID
fn resolve_address(&self, addr: &Address) -> Result<u64>;
}
71 changes: 71 additions & 0 deletions fil_token/src/token/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::{error::Error, fmt::Display};

use fvm_ipld_hamt::Error as HamtError;
use fvm_shared::address::Address;

#[derive(Debug)]
pub enum RuntimeError {
AddrNotFound(Address),
}

impl Display for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RuntimeError::AddrNotFound(_) => write!(f, "Address not found"),
}
}
}

impl Error for RuntimeError {}

#[derive(Debug)]
pub enum StateError {}

impl Display for StateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "State error")
}
}

impl Error for StateError {}

#[derive(Debug)]
pub enum ActorError {
AddrNotFound(Address),
Arithmetic(String),
IpldState(StateError),
IpldHamt(HamtError),
RuntimeError(RuntimeError),
}

impl Display for ActorError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ActorError::AddrNotFound(e) => write!(f, "{}", e),
ActorError::Arithmetic(e) => write!(f, "{}", e),
ActorError::IpldState(e) => write!(f, "{}", e),
ActorError::IpldHamt(e) => write!(f, "{}", e),
ActorError::RuntimeError(e) => write!(f, "{}", e),
}
}
}

impl Error for ActorError {}

impl From<StateError> for ActorError {
fn from(e: StateError) -> Self {
Self::IpldState(e)
}
}

impl From<HamtError> for ActorError {
fn from(e: HamtError) -> Self {
Self::IpldHamt(e)
}
}

impl From<RuntimeError> for ActorError {
fn from(e: RuntimeError) -> Self {
ActorError::RuntimeError(e)
}
}
Loading