Skip to content

Commit

Permalink
feat: allow unrestricted method numbers on accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jan 20, 2023
1 parent ed54692 commit fc736c3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
24 changes: 14 additions & 10 deletions actors/account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use fvm_actor_utils::receiver::UniversalReceiverParams;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::{Address, Protocol};
use fvm_shared::crypto::signature::SignatureType::{Secp256k1, BLS};
use fvm_shared::crypto::signature::{Signature, SignatureType};
use fvm_shared::error::ExitCode;
use fvm_shared::METHOD_CONSTRUCTOR;
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
use num_derive::FromPrimitive;

use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_dispatch, ActorDowncast};
use fil_actors_runtime::{actor_dispatch, ActorDowncast, FIRST_EXPORTED_METHOD_NUMBER};
use fil_actors_runtime::{actor_error, ActorError};

use crate::types::AuthenticateMessageParams;
Expand All @@ -34,7 +34,6 @@ pub enum Method {
// Deprecated in v10
// AuthenticateMessage = 3,
AuthenticateMessageExported = frc42_dispatch::method_hash!("AuthenticateMessage"),
UniversalReceiverHook = frc42_dispatch::method_hash!("Receive"),
}

/// Account Actor
Expand Down Expand Up @@ -91,13 +90,18 @@ impl Actor {
Ok(())
}

// Always succeeds, accepting any transfers.
pub fn universal_receiver_hook(
/// Fallback method for unimplemented method numbers.
pub fn fallback(
rt: &mut impl Runtime,
_params: UniversalReceiverParams,
) -> Result<(), ActorError> {
method: MethodNum,
_: Option<IpldBlock>,
) -> Result<Option<IpldBlock>, ActorError> {
rt.validate_immediate_caller_accept_any()?;
Ok(())
if method >= FIRST_EXPORTED_METHOD_NUMBER {
Ok(None)
} else {
Err(actor_error!(unhandled_message; "invalid method: {}", method))
}
}
}

Expand All @@ -107,6 +111,6 @@ impl ActorCode for Actor {
Constructor => constructor,
PubkeyAddress => pubkey_address,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
_ => fallback [raw],
}
}
2 changes: 1 addition & 1 deletion actors/account/tests/account_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn token_receiver() {
rt.expect_validate_caller_any();
let ret = rt
.call::<AccountActor>(
Method::UniversalReceiverHook as MethodNum,
frc42_dispatch::method_hash!("Receive"),
IpldBlock::serialize_cbor(&UniversalReceiverParams {
type_: 0,
payload: RawBytes::new(vec![1, 2, 3]),
Expand Down
22 changes: 13 additions & 9 deletions actors/ethaccount/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
pub mod types;

use fvm_actor_utils::receiver::UniversalReceiverParams;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::{Payload, Protocol};
use fvm_shared::crypto::hash::SupportedHashes::Keccak256;
use fvm_shared::error::ExitCode;
use fvm_shared::METHOD_CONSTRUCTOR;
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
use num_derive::FromPrimitive;

use crate::types::AuthenticateMessageParams;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, ActorDowncast, ActorError, AsActorError, EAM_ACTOR_ID,
SYSTEM_ACTOR_ADDR,
FIRST_EXPORTED_METHOD_NUMBER, SYSTEM_ACTOR_ADDR,
};

#[cfg(feature = "fil-actor")]
Expand All @@ -23,7 +23,6 @@ fil_actors_runtime::wasm_trampoline!(EthAccountActor);
pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
AuthenticateMessageExported = frc42_dispatch::method_hash!("AuthenticateMessage"),
UniversalReceiverHook = frc42_dispatch::method_hash!("Receive"),
}

/// Ethereum Account actor.
Expand Down Expand Up @@ -127,12 +126,17 @@ impl EthAccountActor {
}

// Always succeeds, accepting any transfers.
pub fn universal_receiver_hook(
pub fn fallback(
rt: &mut impl Runtime,
_params: UniversalReceiverParams,
) -> Result<(), ActorError> {
method: MethodNum,
_: Option<IpldBlock>,
) -> Result<Option<IpldBlock>, ActorError> {
rt.validate_immediate_caller_accept_any()?;
Ok(())
if method >= FIRST_EXPORTED_METHOD_NUMBER {
Ok(None)
} else {
Err(actor_error!(unhandled_message; "invalid method: {}", method))
}
}
}

Expand All @@ -141,6 +145,6 @@ impl ActorCode for EthAccountActor {
actor_dispatch! {
Constructor => constructor,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
_ => fallback [raw],
}
}
2 changes: 1 addition & 1 deletion actors/ethaccount/tests/ethaccount_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn token_receiver() {
rt.expect_validate_caller_any();
let ret = rt
.call::<EthAccountActor>(
Method::UniversalReceiverHook as MethodNum,
frc42_dispatch::method_hash!("Receive"),
IpldBlock::serialize_cbor(&UniversalReceiverParams {
type_: 0,
payload: RawBytes::new(vec![1, 2, 3]),
Expand Down

0 comments on commit fc736c3

Please sign in to comment.