Skip to content

Commit e5b4a0c

Browse files
authored
dispatch: improve dispatch macro and allow specifying codecs (#1412)
* dispatch: make it possible to specify the param/return codec And specify it for two EVM methods that need to pass "reachable" CIDs: 1. The `bytecode` return value. 2. The `invoke_contract_delegate` parameters. * dispatch: auto detect "raw" dispatch Instead of forcing the user to specify `[raw]`, just auto detect it based on the method parameters. * dispatch: test changes * address feedback
1 parent 9de9a98 commit e5b4a0c

File tree

6 files changed

+233
-44
lines changed

6 files changed

+233
-44
lines changed

actors/account/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ impl ActorCode for Actor {
118118
Constructor => constructor,
119119
PubkeyAddress => pubkey_address,
120120
AuthenticateMessageExported => authenticate_message,
121-
_ => fallback [raw],
121+
_ => fallback,
122122
}
123123
}

actors/ethaccount/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ impl ActorCode for EthAccountActor {
7474

7575
actor_dispatch! {
7676
Constructor => constructor,
77-
_ => fallback [raw],
77+
_ => fallback,
7878
}
7979
}

actors/evm/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use fil_actors_evm_shared::address::EthAddress;
22
use fil_actors_runtime::{
3-
actor_dispatch_unrestricted, actor_error, ActorError, AsActorError, EAM_ACTOR_ADDR,
3+
actor_dispatch_unrestricted, actor_error, ActorError, AsActorError, WithCodec, EAM_ACTOR_ADDR,
44
INIT_ACTOR_ADDR,
55
};
66
use fvm_ipld_blockstore::Blockstore;
77
use fvm_ipld_encoding::ipld_block::IpldBlock;
8-
use fvm_ipld_encoding::BytesSer;
8+
use fvm_ipld_encoding::{BytesSer, DAG_CBOR};
99
use fvm_shared::address::Address;
1010
use fvm_shared::econ::TokenAmount;
1111
use fvm_shared::error::ExitCode;
@@ -206,14 +206,20 @@ impl EvmContractActor {
206206
initialize_evm_contract(&mut System::resurrect(rt)?, params.creator, params.initcode.into())
207207
}
208208

209+
/// Invoke the contract with some _alternative_ bytecode. This can only be called by the
210+
/// contract itself and is used to implement the EVM's DELEGATECALL opcode.
211+
///
212+
/// This method expects DAG_CBOR encoded parameters (the linked `params.code` needs to be
213+
/// reachable).
209214
pub fn invoke_contract_delegate<RT>(
210215
rt: &RT,
211-
params: DelegateCallParams,
216+
params: WithCodec<DelegateCallParams, DAG_CBOR>,
212217
) -> Result<DelegateCallReturn, ActorError>
213218
where
214219
RT: Runtime,
215220
RT::Blockstore: Clone,
216221
{
222+
let params = params.0;
217223
rt.validate_immediate_caller_is(&[rt.message().receiver()])?;
218224

219225
let mut system = System::load(rt).map_err(|e| {
@@ -281,15 +287,17 @@ impl EvmContractActor {
281287

282288
/// Returns the contract's EVM bytecode, or `None` if the contract has been deleted (has called
283289
/// SELFDESTRUCT).
284-
pub fn bytecode(rt: &impl Runtime) -> Result<BytecodeReturn, ActorError> {
290+
///
291+
/// Return value is "dag cbor" as we need the linked bytecode (if present) to be reachable.
292+
pub fn bytecode(rt: &impl Runtime) -> Result<WithCodec<BytecodeReturn, DAG_CBOR>, ActorError> {
285293
// Any caller can fetch the bytecode of a contract; this is now EXT* opcodes work.
286294
rt.validate_immediate_caller_accept_any()?;
287295

288296
let state: State = rt.state()?;
289297
if is_dead(rt, &state) {
290-
Ok(BytecodeReturn { code: None })
298+
Ok(BytecodeReturn { code: None }.into())
291299
} else {
292-
Ok(BytecodeReturn { code: Some(state.bytecode) })
300+
Ok(BytecodeReturn { code: Some(state.bytecode) }.into())
293301
}
294302
}
295303

@@ -417,6 +425,6 @@ impl ActorCode for EvmContractActor {
417425
GetStorageAt => storage_at,
418426
InvokeContractDelegate => invoke_contract_delegate,
419427
Resurrect => resurrect,
420-
_ => handle_filecoin_method [raw],
428+
_ => handle_filecoin_method,
421429
}
422430
}

actors/multisig/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,6 @@ impl ActorCode for Actor {
558558
ChangeNumApprovalsThreshold => change_num_approvals_threshold,
559559
LockBalance => lock_balance,
560560
UniversalReceiverHook => universal_receiver_hook,
561-
_ => fallback [raw],
561+
_ => fallback,
562562
}
563563
}

0 commit comments

Comments
 (0)