Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for actor upgrades #1866

Merged
merged 40 commits into from
Oct 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ccb9c62
wip
fridrik01 Aug 30, 2023
3d5a07d
Add UpgradeInfo as ipld block to upgrade endpoint
fridrik01 Aug 31, 2023
d26bafb
Cleanup unused Abort enum
fridrik01 Aug 31, 2023
078f902
exit now returns to upgrade caller, still issue with block registry
fridrik01 Aug 31, 2023
4847d04
fix issue with block registry + initial handle success/failure
fridrik01 Sep 1, 2023
ab25ab3
several fixes
fridrik01 Sep 4, 2023
b26f589
simplify match arm in CM
fridrik01 Sep 4, 2023
85a9129
Improve error checking + more tests
fridrik01 Sep 5, 2023
aead419
Detect re-entrant upgrade
fridrik01 Sep 5, 2023
60ef934
Recursively calling upgrade returns correct block
fridrik01 Sep 5, 2023
b7a0cb0
fix lints
fridrik01 Sep 5, 2023
d219597
Add Entrypoint to send and refactor upgrade_actor to call send
fridrik01 Sep 6, 2023
6d28737
Support entrypoints with different number of params
fridrik01 Sep 8, 2023
5bff8a4
Update cargo.lock
fridrik01 Sep 8, 2023
cbea1d0
Wasm programs within infinite loop should result in SYS_ILLEGAL_INSTR…
fridrik01 Sep 8, 2023
ed2187d
Fix check for sys_out_of_gas
fridrik01 Sep 8, 2023
237b9ba
Use match where more appropriate
fridrik01 Sep 9, 2023
a782844
Preserve caller
fridrik01 Sep 28, 2023
f15f9b0
fixes after rebasing in ipld reachability
fridrik01 Sep 28, 2023
cd67d6d
Move upgrade logic to kernel
fridrik01 Oct 3, 2023
31f11f2
Refactor EntrypointParams into Entrypoint
fridrik01 Oct 3, 2023
22d00c5
Address review comments
fridrik01 Oct 3, 2023
e8ddd7d
error refactor
Stebalien Oct 4, 2023
9a7583c
fix tests after merging in stebs error refactor
fridrik01 Oct 5, 2023
8839e13
dedup processing send response
fridrik01 Oct 5, 2023
9807fef
put_reachable can never fail in kernel:upgrade_actor
fridrik01 Oct 5, 2023
c1ef6e3
reduce disk space by having fewer integration test files which broke CI
fridrik01 Oct 6, 2023
53aa0d5
minor fixes
fridrik01 Oct 17, 2023
1913868
Reject upgrades if actor already on call stack
fridrik01 Oct 18, 2023
c7e3498
Add test for calling upgrade on an actor alread on call stack
fridrik01 Oct 18, 2023
0f65803
Add actor call stack to call manager + detect reentrant upgrades in s…
fridrik01 Oct 19, 2023
816ed06
simplify call_manager interface for actor call stack
fridrik01 Oct 20, 2023
79653b6
Address review comments
fridrik01 Oct 24, 2023
caf8200
Rename SendResult to CallResult
fridrik01 Oct 24, 2023
f55904d
fix: take cid as ref in upgrade_actor
fridrik01 Oct 24, 2023
dc66e33
Refactor tests + add testcase for upgrading after self_destruct
fridrik01 Oct 24, 2023
700837c
fix: used wrong id when updating the code_cid in upgrade_actor
fridrik01 Oct 26, 2023
7d00218
Refactor and improve test cases for testing upgrade_actor
fridrik01 Oct 26, 2023
0a98555
fixup the error conditions
Stebalien Oct 26, 2023
84a47d1
disable the actor-upgrade feature by default
Stebalien Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support entrypoints with different number of params
fridrik01 committed Oct 3, 2023

Verified

This commit was signed with the committer’s verified signature.
commit 6d287376dd079da10a46c2315bd8ebaf080cbca8
120 changes: 80 additions & 40 deletions fvm/src/call_manager/default.rs
Original file line number Diff line number Diff line change
@@ -12,6 +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::upgrade::UpgradeInfo;
use fvm_shared::{ActorID, MethodNum, METHOD_SEND};
use num_traits::Zero;

@@ -417,8 +418,8 @@ where
.get_actor(actor_id)?
.ok_or_else(|| syscall_error!(NotFound; "actor not found: {}", actor_id))?;

// store the code cid of the calling actor before running the upgrade endpoint
// in case it was changed (which could happen if the target upgrade endpoint
// store the code cid of the calling actor before running the upgrade entrypoint
// in case it was changed (which could happen if the target upgrade entrypoint
// sent a message to this actor which in turn called upgrade)
let code = state.code;

@@ -434,11 +435,11 @@ where
),
);

// run the upgrade endpoint
// run the upgrade entrypoint
let result = self.send::<K>(
actor_id,
Address::new_id(actor_id),
Entrypoint::Upgrade,
Entrypoint::Upgrade(UpgradeInfo { old_code_cid: code }),
params,
&TokenAmount::zero(),
None,
@@ -742,6 +743,11 @@ where
NO_DATA_BLOCK_ID
};

// 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)?;

// Increment invocation count
self.invocation_count += 1;

@@ -789,20 +795,21 @@ where

store.data_mut().memory = memory;

// Lookup the invoke method.
let invoke: wasmtime::TypedFunc<(u32,), u32> = instance
.get_typed_func(&mut store, entrypoint.func_name())
// All actors will have an invoke method.
.map_err(Abort::Fatal)?;
let func = match instance.get_func(&mut store, entrypoint.func_name()) {
Some(func) => func,
None => {
return Err(Abort::EntrypointNotFound);
}
};

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

// Set the available gas.
update_gas_available(&mut store)?;

// Invoke it.
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
invoke.call(&mut store, (params_id,))
}))
.map_err(|panic| Abort::Fatal(anyhow!("panic within actor: {:?}", panic)))?;
let mut out = [wasmtime::Val::I32(0)];
func.call(&mut store, params.as_slice(), &mut out)?;

// Charge for any remaining uncharged execution gas, returning an error if we run
// out.
@@ -812,35 +819,26 @@ where
// detected it and returned OutOfGas above. Any other invocation failure is returned
// here as an Abort

Ok(res?)
Ok(out[0].unwrap_i32() as u32)
})();

let invocation_data = store.into_data();
let last_error = invocation_data.last_error;
let (mut cm, block_registry) = invocation_data.kernel.into_inner();

// Resolve the return block's ID into an actual block, converting to an abort if it
// doesn't exist.
let result = result.and_then(|ret_id| {
Ok(if ret_id == NO_DATA_BLOCK_ID {
None
} else {
Some(block_registry.get(ret_id).map_err(|_| {
Abort::Exit(
ExitCode::SYS_MISSING_RETURN,
String::from("returned block does not exist"),
NO_DATA_BLOCK_ID,
)
})?)
})
});

// Process the result, updating the backtrace if necessary.
let mut ret = match result {
Ok(ret) => Ok(InvocationResult {
Ok(NO_DATA_BLOCK_ID) => Ok(InvocationResult {
exit_code: ExitCode::OK,
value: ret.cloned(),
value: None,
}),
Ok(block_id) => match block_registry.get(block_id) {
Ok(blk) => Ok(InvocationResult {
exit_code: ExitCode::OK,
value: Some(blk.clone()),
}),
Err(e) => Err(ExecutionError::Fatal(anyhow!(e))),
},
Err(abort) => {
let (code, message, res) = match abort {
Abort::Exit(code, message, NO_DATA_BLOCK_ID) => (
@@ -852,11 +850,6 @@ where
}),
),
Abort::Exit(code, message, blk_id) => match block_registry.get(blk_id) {
Err(e) => (
ExitCode::SYS_MISSING_RETURN,
"error getting exit data block".to_owned(),
Err(ExecutionError::Fatal(anyhow!(e))),
),
Ok(blk) => (
code,
message,
@@ -865,7 +858,20 @@ where
value: Some(blk.clone()),
}),
),
Err(e) => (
ExitCode::SYS_MISSING_RETURN,
"error getting exit data block".to_owned(),
Err(ExecutionError::Fatal(anyhow!(e))),
),
},
Abort::EntrypointNotFound => (
ExitCode::USR_FORBIDDEN,
"entrypoint not found".to_owned(),
Err(ExecutionError::Syscall(SyscallError::new(
ErrorNumber::Forbidden,
"entrypoint not found",
))),
),
Abort::OutOfGas => (
ExitCode::SYS_OUT_OF_GAS,
"out of gas".to_owned(),
@@ -1048,14 +1054,48 @@ impl Entrypoint {
fn method_num(&self) -> MethodNum {
match self {
Entrypoint::Invoke(num) => *num,
Entrypoint::Upgrade => 191919,
Entrypoint::Upgrade(_) => fvm_shared::METHOD_UPGRADE,
}
}

fn func_name(&self) -> &'static str {
match self {
Entrypoint::Invoke(_) => "invoke",
Entrypoint::Upgrade => "upgrade",
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(()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we should treat invoke params the same way (if possible).

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(Block::new(CBOR, ui_params))?;
self.params.push(wasmtime::Val::I32(block_id as i32));
Ok(())
}
}
}

fn params(&self) -> &Vec<wasmtime::Val> {
&self.params
}
}
5 changes: 3 additions & 2 deletions fvm/src/call_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use cid::Cid;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::upgrade::UpgradeInfo;
use fvm_shared::{ActorID, MethodNum};

use crate::engine::Engine;
@@ -210,14 +211,14 @@ pub struct FinishRet {
#[derive(Clone, Debug, Copy)]
pub enum Entrypoint {
Invoke(MethodNum),
Upgrade,
Upgrade(UpgradeInfo),
}

impl std::fmt::Display for Entrypoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Entrypoint::Invoke(method) => write!(f, "invoke({})", method),
Entrypoint::Upgrade => write!(f, "upgrade"),
Entrypoint::Upgrade(_) => write!(f, "upgrade"),
}
}
}
3 changes: 3 additions & 0 deletions fvm/src/syscalls/error.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,9 @@ pub enum Abort {
/// The actor ran out of gas.
#[error("out of gas")]
OutOfGas,
/// The actor did not export the endpoint that was called.
#[error("entrypoint not found")]
EntrypointNotFound,
/// The system failed with a fatal error.
#[error("fatal error: {0}")]
Fatal(anyhow::Error),
2 changes: 2 additions & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -109,6 +109,8 @@ pub type MethodNum = u64;
pub const METHOD_SEND: MethodNum = 0;
/// Base actor constructor method.
pub const METHOD_CONSTRUCTOR: MethodNum = 1;
/// Upgrade actor method.
pub const METHOD_UPGRADE: MethodNum = 932083;

/// The outcome of a `Send`, covering its ExitCode and optional return data
#[derive(Debug, PartialEq, Eq, Clone)]
2 changes: 1 addition & 1 deletion shared/src/upgrade/mod.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
use cid::Cid;
use fvm_ipld_encoding::tuple::*;

#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct UpgradeInfo {
// the old code cid we are upgrading from
pub old_code_cid: Cid,
16 changes: 14 additions & 2 deletions testing/test_actors/actors/fil-upgrade-actor/src/actor.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_ipld_encoding::{to_vec, CBOR};
use fvm_sdk as sdk;
use fvm_shared::address::Address;
use fvm_shared::upgrade::UpgradeInfo;
use serde_tuple::*;
#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)]
struct SomeStruct {
@@ -13,13 +14,24 @@ struct SomeStruct {
const UPGRADE_FAILED_EXIT_CODE: u32 = 19;

#[no_mangle]
pub fn upgrade(params_id: u32) -> u32 {
pub fn upgrade(params_id: u32, upgrade_info_id: u32) -> u32 {
sdk::initialize();

let params = sdk::message::params_raw(params_id).unwrap().unwrap();
let ui_params = sdk::message::params_raw(upgrade_info_id).unwrap().unwrap();

assert_eq!(params.codec, fvm_ipld_encoding::CBOR);
assert_eq!(ui_params.codec, fvm_ipld_encoding::CBOR);

let p = params.deserialize::<SomeStruct>().unwrap();
let ui = ui_params.deserialize::<UpgradeInfo>().unwrap();

sdk::debug::log(format!(
"[upgrade] value: {}, old_code_cid: {}",
p.value, ui.old_code_cid
));

match params.deserialize::<SomeStruct>().unwrap().value {
match p.value {
1 => {
let block_id = sdk::ipld::put_block(CBOR, &to_vec(&666).unwrap()).unwrap();
sdk::debug::log(format!(