Skip to content

Fungible Tokens (spike) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.vscode
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]

members = [
"fvm_dispatch",
"fil_token",
"testing/fil_token_integration",
"testing/fil_token_integration/actors/wfil_token_actor",
]
16 changes: 16 additions & 0 deletions fil_token/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[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 = "2.0.0-alpha.1", git = "https://github.com/filecoin-project/ref-fvm" }
fvm_shared = { version = "0.8.0", git = "https://github.com/filecoin-project/ref-fvm" }
serde = { version = "1.0.136", features = ["derive"] }
serde_tuple = { version = "0.5.0" }
41 changes: 41 additions & 0 deletions fil_token/src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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)
}
}
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 {}
18 changes: 18 additions & 0 deletions fil_token/src/runtime/fvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::Runtime;

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

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"))
}
}
11 changes: 11 additions & 0 deletions fil_token/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod fvm;
pub use fvm::*;

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

pub trait Runtime {
fn caller(&self) -> u64;

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