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 aab0e3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
13 changes: 3 additions & 10 deletions actors/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 +92,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 +100,7 @@ impl ActorCode for Actor {
Constructor => constructor,
PubkeyAddress => pubkey_address,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
UniversalReceiverHook => (),
InvokeEVM => (),
}
}
13 changes: 3 additions & 10 deletions actors/ethaccount/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 +127,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 aab0e3c

Please sign in to comment.