Skip to content

Commit

Permalink
Add feature to disable code upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 committed Oct 4, 2023
1 parent 22d00c5 commit 3b2accf
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions fvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ testing = []
arb = ["arbitrary", "quickcheck", "fvm_shared/arb", "cid/arb"]
m2-native = []
gas_calibration = []
upgrade-actor = []
8 changes: 5 additions & 3 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::convert::{TryFrom, TryInto};
use std::panic::{self, UnwindSafe};
use std::path::PathBuf;

use crate::syscalls::error::Abort;
use anyhow::{anyhow, Context as _};
use cid::Cid;
use filecoin_proofs_api::{self as proofs, ProverId, PublicReplicaInfo, SectorId};
Expand All @@ -15,12 +14,11 @@ use fvm_shared::address::Payload;
use fvm_shared::consensus::ConsensusFault;
use fvm_shared::crypto::signature;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::{ErrorNumber, ExitCode};
use fvm_shared::error::ErrorNumber;
use fvm_shared::event::{ActorEvent, Entry, Flags};
use fvm_shared::piece::{zero_piece_commitment, PaddedPieceSize};
use fvm_shared::sector::{RegisteredPoStProof, SectorInfo};
use fvm_shared::sys::out::vm::ContextFlags;
use fvm_shared::upgrade::UpgradeInfo;
use fvm_shared::{commcid, ActorID};
use lazy_static::lazy_static;
use multihash::MultihashDigest;
Expand Down Expand Up @@ -874,7 +872,11 @@ where
.create_actor(code_id, actor_id, delegated_address)
}

#[cfg(feature = "upgrade-actor")]
fn upgrade_actor<K: Kernel>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<u32> {
use crate::syscalls::error::Abort;
use fvm_shared::upgrade::UpgradeInfo;

if self.read_only {
return Err(
syscall_error!(ReadOnly, "upgrade_actor cannot be called while read-only").into(),
Expand Down
1 change: 1 addition & 0 deletions fvm/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub trait ActorOps {
delegated_address: Option<Address>,
) -> Result<()>;

#[cfg(feature = "upgrade-actor")]
fn upgrade_actor<K: Kernel>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<u32>;

/// Installs actor code pointed by cid
Expand Down
1 change: 1 addition & 0 deletions fvm/src/syscalls/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub fn create_actor(
context.kernel.create_actor(typ, actor_id, addr)
}

#[cfg(feature = "upgrade-actor")]
pub fn upgrade_actor<K: Kernel>(
context: Context<'_, K>,
new_code_cid_off: u32,
Expand Down
1 change: 1 addition & 0 deletions fvm/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ pub fn bind_syscalls(
linker.bind("actor", "get_actor_code_cid", actor::get_actor_code_cid)?;
linker.bind("actor", "next_actor_address", actor::next_actor_address)?;
linker.bind("actor", "create_actor", actor::create_actor)?;
#[cfg(feature = "upgrade-actor")]
linker.bind("actor", "upgrade_actor", actor::upgrade_actor)?;
linker.bind(
"actor",
Expand Down
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ byteorder = "1.4.3"
[features]
default = []
m2-native = []
upgrade-actor = []
11 changes: 7 additions & 4 deletions sdk/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use core::option::Option;
use std::ptr; // no_std

use cid::Cid;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::{Address, Payload, MAX_ADDRESS_LEN};
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ErrorNumber;
use fvm_shared::{ActorID, MAX_CID_LEN};
use log::error;

use crate::{sys, SyscallResult, NO_DATA_BLOCK_ID};
use crate::{sys, SyscallResult};

/// Resolves the ID address of an actor. Returns `None` if the address cannot be resolved.
/// Successfully resolving an address doesn't necessarily mean the actor exists (e.g., if the
Expand Down Expand Up @@ -109,13 +108,17 @@ pub fn create_actor(
}

/// Upgrades an actor using the given block which includes the old code cid and the upgrade params
pub fn upgrade_actor(new_code_cid: Cid, params: Option<IpldBlock>) -> SyscallResult<u32> {
#[cfg(feature = "upgrade-actor")]
pub fn upgrade_actor(
new_code_cid: Cid,
params: Option<fvm_ipld_encoding::ipld_block::IpldBlock>,
) -> SyscallResult<u32> {
unsafe {
let cid = new_code_cid.to_bytes();

let params_id = match params {
Some(p) => sys::ipld::block_create(p.codec, p.data.as_ptr(), p.data.len() as u32)?,
None => NO_DATA_BLOCK_ID,
None => crate::NO_DATA_BLOCK_ID,
};

sys::actor::upgrade_actor(cid.as_ptr(), params_id)
Expand Down
1 change: 1 addition & 0 deletions sdk/src/sys/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ super::fvm_syscalls! {
/// | [`LimitExceeded`] | recursion limit reached. |
/// | [`IllegalArgument`] | invalid code cid buffer. |
/// | [`Forbidden`] | target actor doesn't have an upgrade endpoint. |
#[cfg(feature = "upgrade-actor")]
pub fn upgrade_actor(
new_code_cid_off: *const u8,
params: u32,
Expand Down
1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ blst = ["bls-signatures/blst"]
pairing = ["bls-signatures/pairing"]
testing = []
arb = ["arbitrary", "dep:quickcheck", "num-bigint/quickcheck"]
upgrade-actor = []
15 changes: 11 additions & 4 deletions shared/src/upgrade/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use cid::Cid;
use fvm_ipld_encoding::tuple::*;

#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
#[cfg(feature = "upgrade-actor")]
#[derive(
Clone,
Debug,
Copy,
PartialEq,
Eq,
fvm_ipld_encoding::tuple::Serialize_tuple,
fvm_ipld_encoding::tuple::Deserialize_tuple,
)]
pub struct UpgradeInfo {
// the old code cid we are upgrading from
pub old_code_cid: Cid,
pub old_code_cid: cid::Cid,
}
1 change: 1 addition & 0 deletions testing/conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ features = ["testing"]
[features]
vtune = ["wasmtime/vtune", "ittapi-rs"]
m2-native = []
upgrade-actor = []

[dev-dependencies]
env_logger = "0.10.0"
Expand Down
1 change: 1 addition & 0 deletions testing/conformance/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ where
self.0.lookup_delegated_address(actor_id)
}

#[cfg(feature = "upgrade-actor")]
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
1 change: 1 addition & 0 deletions testing/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ minstant = "0.1.3"
default = []
m2-native = []
calibration = ["fvm/gas_calibration"]
upgrade-actor = []
26 changes: 14 additions & 12 deletions testing/integration/tests/upgrade_test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
mod bundles;
use bundles::*;
use fvm::executor::{ApplyKind, Executor};
use fvm_integration_tests::dummy::DummyExterns;
use fvm_integration_tests::tester::Account;
use fvm_ipld_blockstore::MemoryBlockstore;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::message::Message;
use fvm_shared::state::StateTreeVersion;
use fvm_shared::version::NetworkVersion;
use fvm_test_actors::wasm_bin::UPGRADE_ACTOR_BINARY;
use num_traits::Zero;

#[test]
#[cfg(feature = "upgrade-actor")]
fn upgrade_actor_test() {
use bundles::*;
use fvm::executor::{ApplyKind, Executor};
use fvm_integration_tests::dummy::DummyExterns;
use fvm_integration_tests::tester::Account;
use fvm_ipld_blockstore::MemoryBlockstore;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::message::Message;
use fvm_shared::state::StateTreeVersion;
use fvm_shared::version::NetworkVersion;
use fvm_test_actors::wasm_bin::UPGRADE_ACTOR_BINARY;
use num_traits::Zero;

let mut tester = new_tester(
NetworkVersion::V21,
StateTreeVersion::V5,
Expand Down
2 changes: 2 additions & 0 deletions testing/test_actors/actors/fil-syscall-actor/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn invoke(_: u32) -> u32 {
test_message_context();
test_balance();
test_unaligned();
#[cfg(feature = "upgrade-actor")]
test_upgrade();

#[cfg(coverage)]
Expand Down Expand Up @@ -379,6 +380,7 @@ fn test_unaligned() {
}
}

#[cfg(feature = "upgrade-actor")]
fn test_upgrade() {
// test that calling `upgrade_actor` on ourselves results in a Forbidden error
// since we don't have a upgrade endpoint
Expand Down

0 comments on commit 3b2accf

Please sign in to comment.