Skip to content

Commit

Permalink
Refactor EntrypointParams into Entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 committed Oct 3, 2023
1 parent 0c5ed7b commit 22318f9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 3 additions & 54 deletions fvm/src/call_manager/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fvm_shared::econ::TokenAmount;
use fvm_shared::error::{ErrorNumber, ExitCode};
use fvm_shared::event::StampedEvent;
use fvm_shared::sys::BlockId;
use fvm_shared::{ActorID, MethodNum, METHOD_SEND};
use fvm_shared::{ActorID, METHOD_SEND};
use num_traits::Zero;

use super::state_access_tracker::{ActorAccessState, StateAccessTracker};
Expand Down Expand Up @@ -688,8 +688,7 @@ where

// additional_params takes care of adding entrypoint specific params to the block
// registry and passing them to wasmtime
let mut additional_params = EntrypointParams::new(entrypoint);
additional_params.maybe_put_registry(&mut block_registry)?;
let additional_params = entrypoint.into_params(&mut block_registry)?;

// Increment invocation count
self.invocation_count += 1;
Expand Down Expand Up @@ -746,7 +745,7 @@ where
};

let mut params = vec![wasmtime::Val::I32(params_id as i32)];
params.extend_from_slice(additional_params.params().as_slice());
params.extend_from_slice(additional_params.as_slice());

// Set the available gas.
update_gas_available(&mut store)?;
Expand Down Expand Up @@ -998,53 +997,3 @@ impl EventsAccumulator {
})
}
}

impl Entrypoint {
fn method_num(&self) -> MethodNum {
match self {
Entrypoint::Invoke(num) => *num,
Entrypoint::Upgrade(_) => fvm_shared::METHOD_UPGRADE,
}
}

fn func_name(&self) -> &'static str {
match self {
Entrypoint::Invoke(_) => "invoke",
Entrypoint::Upgrade(_) => "upgrade",
}
}
}

// EntrypointParams is a helper struct to init the registry with the entrypoint specific
// parameters and then forward them to wasmtime
struct EntrypointParams {
entrypoint: Entrypoint,
params: Vec<wasmtime::Val>,
}

impl EntrypointParams {
fn new(entrypoint: Entrypoint) -> Self {
Self {
entrypoint,
params: Vec::new(),
}
}

fn maybe_put_registry(&mut self, br: &mut BlockRegistry) -> Result<()> {
match self.entrypoint {
Entrypoint::Invoke(_) => Ok(()),
Entrypoint::Upgrade(ui) => {
let ui_params = to_vec(&ui).map_err(
|e| syscall_error!(IllegalArgument; "failed to serialize upgrade params: {}", e),
)?;
let block_id = br.put_reachable(Block::new(CBOR, ui_params, Vec::new()))?;
self.params.push(wasmtime::Val::I32(block_id as i32));
Ok(())
}
}
}

fn params(&self) -> &Vec<wasmtime::Val> {
&self.params
}
}
32 changes: 31 additions & 1 deletion fvm/src/call_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use cid::Cid;
use fvm_ipld_encoding::{to_vec, CBOR};
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
Expand All @@ -9,7 +10,7 @@ use fvm_shared::{ActorID, MethodNum};

use crate::engine::Engine;
use crate::gas::{Gas, GasCharge, GasTimer, GasTracker, PriceList};
use crate::kernel::{self, Result};
use crate::kernel::{self, BlockRegistry, Result};
use crate::machine::{Machine, MachineContext};
use crate::state_tree::ActorState;
use crate::Kernel;
Expand Down Expand Up @@ -213,3 +214,32 @@ impl std::fmt::Display for Entrypoint {
}
}
}

impl Entrypoint {
fn method_num(&self) -> MethodNum {
match self {
Entrypoint::Invoke(num) => *num,
Entrypoint::Upgrade(_) => fvm_shared::METHOD_UPGRADE,
}
}

fn func_name(&self) -> &'static str {
match self {
Entrypoint::Invoke(_) => "invoke",
Entrypoint::Upgrade(_) => "upgrade",
}
}

fn into_params(self, br: &mut BlockRegistry) -> Result<Vec<wasmtime::Val>> {
match self {
Entrypoint::Invoke(_) => Ok(Vec::new()),
Entrypoint::Upgrade(ui) => {
let ui_params = to_vec(&ui).map_err(
|e| crate::syscall_error!(IllegalArgument; "failed to serialize upgrade params: {}", e),
)?;
let block_id = br.put_reachable(kernel::Block::new(CBOR, ui_params, Vec::new()))?;
Ok(vec![wasmtime::Val::I32(block_id as i32)])
}
}
}
}
4 changes: 2 additions & 2 deletions testing/conformance/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ where
self.0.lookup_delegated_address(actor_id)
}

fn upgrade_actor(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<BlockId> {
self.0.upgrade_actor(new_code_cid, params_id)
fn upgrade_actor<KK>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<BlockId> {
self.0.upgrade_actor::<Self>(new_code_cid, params_id)
}
}

Expand Down
4 changes: 2 additions & 2 deletions testing/test_actors/actors/fil-upgrade-actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"
publish = false

[target.'cfg(target_arch = "wasm32")'.dependencies]
fvm_sdk = { version = "4.0.0-alpha.3", path = "../../../../sdk" }
fvm_shared = { version = "4.0.0-alpha.3", path = "../../../../shared" }
fvm_sdk = { version = "4.0.0-alpha.4", path = "../../../../sdk" }
fvm_shared = { version = "4.0.0-alpha.4", path = "../../../../shared" }
fvm_ipld_encoding = { version = "0.4.0", path = "../../../../ipld/encoding" }
cid = { workspace = true }
serde = { version = "1.0.164", features = ["derive"] }
Expand Down

0 comments on commit 22318f9

Please sign in to comment.