Skip to content
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

Fungible Tokens (spike) #1

Merged
merged 22 commits into from
Jul 18, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'main' into token/spike
alexytsu committed Jul 14, 2022
commit dc97abf3bb16aa957076c9c11121d544aca67317
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -9,4 +9,5 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

.vscode
# IDE specific user-config
.vscode/
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[workspace]

members = [
"fvm_dispatch",
"fil_token",
"testing/fil_token_integration",
"testing/fil_token_integration/actors/wfil_token_actor",
"fvm_dispatch",
"fil_token",
"testing/fil_token_integration",
"testing/fil_token_integration/actors/wfil_token_actor",
]
33 changes: 18 additions & 15 deletions fvm_dispatch/src/hash.rs
Original file line number Diff line number Diff line change
@@ -41,22 +41,24 @@ pub enum MethodNameErr {
impl<T: Hasher> MethodResolver<T> {
const CONSTRUCTOR_METHOD_NAME: &'static str = "Constructor";
const CONSTRUCTOR_METHOD_NUMBER: u64 = 1_u64;
const RESERVED_METHOD_NUMBER: u64 = 0_u64;

/// Create a new MethodHasher using the given hash algorithm
/// Creates a MethodResolver with an instance of a hasher (blake2b by convention)
pub fn new(hasher: T) -> Self {
Self { hasher }
}

/// Generate the conventional method number based off an exported name
pub fn method_number(&self, method_name: &str) -> u64 {
if method_name == Self::CONSTRUCTOR_METHOD_NAME {
Self::CONSTRUCTOR_METHOD_NUMBER
} else {
let digest = self.hasher.hash(method_name.as_bytes());
if digest.len() < 4 {
panic!("Invalid hasher used: digest must be at least 4 bytes long");
}
as_u32(digest.as_slice()) as u64
/// Generates a standard FRC-XXX compliant method number
///
/// The method number is calculated as the first four bytes of `hash(method-name)`.
/// The name `Constructor` is always hashed to 1 and other method names that hash to
/// 0 or 1 are avoided via rejection sampling.
///
///
pub fn method_number(&self, method_name: &str) -> Result<u64, MethodNameErr> {
// TODO: sanitise method_name before checking (or reject invalid whitespace)
if method_name.contains('|') {
return Err(MethodNameErr::IllegalSymbol);
}

if method_name.is_empty() {
@@ -86,10 +88,11 @@ impl<T: Hasher> MethodResolver<T> {
/// Using big-endian order interperets the first four bytes to an int.
/// The slice passed to this must be at least length 4
fn as_u32(bytes: &[u8]) -> u32 {
((bytes[0] as u32) << (8 * 3)) +
((bytes[1] as u32) << (8 * 2)) +
((bytes[2] as u32) << 8) +
(bytes[3] as u32)
u32::from_be_bytes(
bytes[0..4]
.try_into()
.expect("bytes was not at least length 4"),
)
}

#[cfg(test)]
27 changes: 24 additions & 3 deletions fvm_dispatch/src/message.rs
Original file line number Diff line number Diff line change
@@ -12,15 +12,36 @@ pub struct MethodMessenger<T: Hasher> {
method_resolver: MethodResolver<T>,
}

impl<T: Hasher> MethodDispatcher<T> {
/// Create a new MethodDispatcher with a given hasher
#[derive(Error, PartialEq, Debug)]
pub enum MethodMessengerError {
#[error("error when calculating method name: `{0}`")]
MethodName(MethodNameErr),
#[error("error sending message: `{0}`")]
Syscall(ErrorNumber),
}

impl From<ErrorNumber> for MethodMessengerError {
fn from(e: ErrorNumber) -> Self {
Self::Syscall(e)
}
}

impl From<MethodNameErr> for MethodMessengerError {
fn from(e: MethodNameErr) -> Self {
Self::MethodName(e)
}
}

impl<T: Hasher> MethodMessenger<T> {
/// Creates a new method messenger using a specified hashing function (blake2b by default)
pub fn new(hasher: T) -> Self {
Self {
method_resolver: MethodResolver::new(hasher),
}
}

/// Call a method on another actor by conventional name
/// Calls a method (by name) on a specified actor by constructing and publishing the underlying
/// on-chain Message
pub fn call_method(
&self,
to: &Address,
You are viewing a condensed version of this merge commit. You can view the full changes here.