Skip to content

Commit

Permalink
Add example showing patching actor state
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 committed Mar 20, 2024
1 parent ae750fe commit 5f31ca5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions fendermint/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true }
literally = { workspace = true }

lazy_static = { workspace = true }
fendermint_actor_chainmetadata = { path = "../actors/chainmetadata" }

fendermint_abci = { path = "../abci" }
fendermint_app_options = { path = "./options" }
fendermint_app_settings = { path = "./settings" }
Expand Down
1 change: 1 addition & 0 deletions fendermint/app/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod key;
pub mod materializer;
pub mod rpc;
pub mod run;
pub mod upgrades;

#[async_trait]
pub trait Cmd {
Expand Down
8 changes: 7 additions & 1 deletion fendermint/app/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use tokio::sync::broadcast::error::RecvError;
use tracing::info;

use crate::cmd::key::read_secret_key;
use crate::cmd::upgrades::patch_actor_state::UPGRADE_EXAMPLE_PATCH_STATE;
use crate::{cmd, options::run::RunArgs, settings::Settings};

cmd! {
Expand Down Expand Up @@ -107,14 +108,19 @@ async fn run(settings: Settings) -> anyhow::Result<()> {
ValidatorContext::new(sk, broadcaster)
});

let mut upgrade_scheduler = UpgradeScheduler::new();
upgrade_scheduler
.add(UPGRADE_EXAMPLE_PATCH_STATE.clone())
.unwrap();

let interpreter = FvmMessageInterpreter::<NamespaceBlockstore, _>::new(
tendermint_client.clone(),
validator_ctx,
settings.contracts_dir(),
settings.fvm.gas_overestimation_rate,
settings.fvm.gas_search_step,
settings.fvm.exec_in_check,
UpgradeScheduler::new(),
upgrade_scheduler,
);
let interpreter = SignedMessageInterpreter::new(interpreter);
let interpreter = ChainMessageInterpreter::<_, NamespaceBlockstore>::new(interpreter);
Expand Down
11 changes: 11 additions & 0 deletions fendermint/app/src/cmd/upgrades/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use fvm_shared::chainid::ChainID;
use lazy_static::lazy_static;

pub mod patch_actor_state;

lazy_static! {
pub static ref EXAMPLE_CHAIN_ID: ChainID = ChainID::from(1942764459484029);
}
71 changes: 71 additions & 0 deletions fendermint/app/src/cmd/upgrades/patch_actor_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use anyhow::anyhow;
use cid::multihash::Code;
use fendermint_actor_chainmetadata::State;
use fendermint_rocksdb::blockstore::NamespaceBlockstore;
use fendermint_vm_actor_interface::chainmetadata::CHAINMETADATA_ACTOR_ID;
use fendermint_vm_interpreter::fvm::upgrades::Upgrade;
use fvm::state_tree::ActorState;
use fvm_ipld_encoding::CborStore;
use lazy_static::lazy_static;

use crate::cmd::upgrades::EXAMPLE_CHAIN_ID;

lazy_static! {
// This example upgrade shows how we can patch the state of an actor
//
pub static ref UPGRADE_EXAMPLE_PATCH_STATE: Upgrade<NamespaceBlockstore> = Upgrade::new_by_id(
*EXAMPLE_CHAIN_ID,
100, // the upgrade block height
None, // this upgrade does not change the application version as its not consensus breaking
|state| {
let state_tree = state.state_tree_mut();

// get the ActorState from the state tree
//
let actor_state = match state_tree.get_actor(CHAINMETADATA_ACTOR_ID)? {
Some(actor) => actor,
None => {
return Err(anyhow!("chainmetadata actor not found"));
}
};
println!("chainmetadata code_cid: {:?}, state_cid: {:?}", actor_state.code, actor_state.state);

// retrieve the chainmetadata actor state from the blockstore
//
let mut chainmetadata_state: State = match state_tree.store().get_cbor(&actor_state.state)? {
Some(v) => v,
None => return Err(anyhow!("chain metadata actor state not found")),
};
println!("chainmetadata lookback length: {}", chainmetadata_state.lookback_len);

// lets patch the state, here we update the lookback_len from the default (256) to 1024
//
chainmetadata_state.lookback_len = 1024;

// store the updated state back to the blockstore and get the new state cid
//
let new_state_cid = state_tree
.store()
.put_cbor(&chainmetadata_state, Code::Blake2b256)
.map_err(|e| anyhow!("failed to put chain metadata actor state: {}", e))?;
println!("new chainmetadata state_cid: {:?}", new_state_cid);

// next we update the actor state in the state tree
//
state_tree.set_actor(CHAINMETADATA_ACTOR_ID, ActorState {
code: actor_state.code,
state: new_state_cid,
sequence: actor_state.sequence,
balance: actor_state.balance,
delegated_address: actor_state.delegated_address,
});

Ok(())
},
);


}

0 comments on commit 5f31ca5

Please sign in to comment.