Skip to content

Commit

Permalink
feat: allow calling InvokeEVM on accounts
Browse files Browse the repository at this point in the history
Additionally, use the same dispatch mechanism for the universal receiver
hook. Given #1076, CBOR encoding should no longer be a compatibility
hazard for us.
  • Loading branch information
Stebalien committed Jan 19, 2023
1 parent b749285 commit b6aae0c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
14 changes: 3 additions & 11 deletions actors/account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use fvm_actor_utils::receiver::UniversalReceiverParams;
use fvm_shared::address::{Address, Protocol};
use fvm_shared::crypto::signature::SignatureType::{Secp256k1, BLS};
use fvm_shared::crypto::signature::{Signature, SignatureType};
Expand Down Expand Up @@ -36,6 +35,7 @@ pub enum Method {
// AuthenticateMessage = 3,
AuthenticateMessageExported = frc42_dispatch::method_hash!("AuthenticateMessage"),
UniversalReceiverHook = frc42_dispatch::method_hash!("Receive"),
InvokeEVM = frc42_dispatch::method_hash!("InvokeEVM"),
}

/// Account Actor
Expand Down Expand Up @@ -91,15 +91,6 @@ impl Actor {

Ok(())
}

// Always succeeds, accepting any transfers.
pub fn universal_receiver_hook(
rt: &mut impl Runtime,
_params: UniversalReceiverParams,
) -> Result<(), ActorError> {
rt.validate_immediate_caller_accept_any()?;
Ok(())
}
}

impl ActorCode for Actor {
Expand All @@ -108,6 +99,7 @@ impl ActorCode for Actor {
Constructor => constructor,
PubkeyAddress => pubkey_address,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
UniversalReceiverHook => (),
InvokeEVM => (),
}
}
14 changes: 3 additions & 11 deletions actors/ethaccount/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod types;

use fvm_actor_utils::receiver::UniversalReceiverParams;
use fvm_shared::address::{Payload, Protocol};
use fvm_shared::crypto::hash::SupportedHashes::Keccak256;
use fvm_shared::error::ExitCode;
Expand All @@ -25,6 +24,7 @@ pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
AuthenticateMessageExported = frc42_dispatch::method_hash!("AuthenticateMessage"),
UniversalReceiverHook = frc42_dispatch::method_hash!("Receive"),
InvokeEVM = frc42_dispatch::method_hash!("InvokeEVM"),
}

/// Ethereum Account actor.
Expand Down Expand Up @@ -126,22 +126,14 @@ impl EthAccountActor {

Ok(())
}

// Always succeeds, accepting any transfers.
pub fn universal_receiver_hook(
rt: &mut impl Runtime,
_params: UniversalReceiverParams,
) -> Result<(), ActorError> {
rt.validate_immediate_caller_accept_any()?;
Ok(())
}
}

impl ActorCode for EthAccountActor {
type Methods = Method;
actor_dispatch! {
Constructor => constructor,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
UniversalReceiverHook => (),
InvokeEVM => (),
}
}
26 changes: 22 additions & 4 deletions runtime/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::ActorError;
/// ```
#[macro_export]
macro_rules! actor_dispatch {
($($(#[$m:meta])* $method:ident => $func:ident,)*) => {
($($(#[$m:meta])* $method:ident => $func:tt,)*) => {
fn invoke_method<RT>(
rt: &mut RT,
method: MethodNum,
Expand All @@ -36,16 +36,25 @@ macro_rules! actor_dispatch {
{
restrict_internal_api(rt, method)?;
match FromPrimitive::from_u64(method) {
$($(#[$m])* Some(Self::Methods::$method) => $crate::dispatch(rt, Self::$func, &args),)*
$($(#[$m])* Some(Self::Methods::$method) => {
$crate::actor_dispatch!(@dispatch (rt args) $func)
}),*
None => Err(actor_error!(unhandled_message; "invalid method: {}", method)),
}
}
};
(@dispatch ($rt:ident $args:ident) ()) => {{
$rt.validate_immediate_caller_accept_any()?;
Ok(None)
}};
(@dispatch ($rt:ident $args:ident) $func:ident) => {
$crate::dispatch($rt, Self::$func, &$args)
};
}

#[macro_export]
macro_rules! actor_dispatch_unrestricted {
($($(#[$m:meta])* $method:ident => $func:ident,)*) => {
($($(#[$m:meta])* $method:ident => $func:tt,)*) => {
fn invoke_method<RT>(
rt: &mut RT,
method: MethodNum,
Expand All @@ -56,11 +65,20 @@ macro_rules! actor_dispatch_unrestricted {
RT::Blockstore: Clone,
{
match FromPrimitive::from_u64(method) {
$($(#[$m])* Some(Self::Methods::$method) => $crate::dispatch(rt, Self::$func, &args),)*
$($(#[$m])* Some(Self::Methods::$method) => {
$crate::actor_dispatch!(@dispatch (rt args) $func)
}),*
None => Err(actor_error!(unhandled_message; "invalid method: {}", method)),
}
}
};
(@dispatch ($rt:ident $args:ident) ()) => {{
$rt.validate_immediate_caller_accept_any()?;
Ok(None)
}};
(@dispatch ($rt:ident $args:ident) $func:ident) => {
$crate::dispatch($rt, Self::$func, &$args)
};
}

pub trait Dispatch<'de, RT> {
Expand Down

0 comments on commit b6aae0c

Please sign in to comment.