diff --git a/src/rpc/methods/chain.rs b/src/rpc/methods/chain.rs index 6e53baecbd6..8393d190a52 100644 --- a/src/rpc/methods/chain.rs +++ b/src/rpc/methods/chain.rs @@ -17,6 +17,7 @@ use crate::shim::clock::ChainEpoch; use crate::shim::error::ExitCode; use crate::shim::executor::Receipt; use crate::shim::message::Message; +use crate::utils::db::CborStoreExt as _; use crate::utils::io::VoidAsyncWriter; use anyhow::{Context as _, Result}; use cid::Cid; @@ -306,11 +307,7 @@ impl RpcMethod<1> for ChainGetBlockMessages { ctx: Ctx, (cid,): Self::Params, ) -> Result { - let blk: CachingBlockHeader = ctx - .state_manager - .blockstore() - .get_cbor(&cid)? - .context("can't find block with that cid")?; + let blk: CachingBlockHeader = ctx.state_manager.blockstore().get_cbor_required(&cid)?; let blk_msgs = &blk.messages; let (unsigned_cids, signed_cids) = crate::chain::read_msg_cids(ctx.state_manager.blockstore(), blk_msgs)?; @@ -507,11 +504,7 @@ impl RpcMethod<1> for ChainGetBlock { ctx: Ctx, (cid,): Self::Params, ) -> Result { - let blk: CachingBlockHeader = ctx - .state_manager - .blockstore() - .get_cbor(&cid)? - .context("can't find BlockHeader with that cid")?; + let blk: CachingBlockHeader = ctx.state_manager.blockstore().get_cbor_required(&cid)?; Ok(blk) } } diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index e1e76ad53e1..049eaa888bd 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -20,7 +20,7 @@ use crate::shim::fvm_shared_latest::MethodNum; use crate::shim::message::Message; use crate::shim::{clock::ChainEpoch, state_tree::StateTree}; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use bytes::{Buf, BytesMut}; use cbor4ii::core::{dec::Decode, utils::SliceReader, Value}; use cid::{ @@ -782,9 +782,7 @@ impl RpcMethod<2> for EthGetBalance { let ts = tipset_by_block_number_or_hash(&ctx.chain_store, block_param)?; let state = StateTree::new_from_root(ctx.state_manager.blockstore_owned(), ts.parent_state())?; - let actor = state - .get_actor(&fil_addr)? - .context("Failed to retrieve actor")?; + let actor = state.get_required_actor(&fil_addr)?; Ok(BigInt(actor.balance.atto().clone())) } } diff --git a/src/rpc/methods/state.rs b/src/rpc/methods/state.rs index 609d05d6926..60ddce44bc3 100644 --- a/src/rpc/methods/state.rs +++ b/src/rpc/methods/state.rs @@ -18,7 +18,10 @@ use crate::shim::{ use crate::state_manager::chain_rand::ChainRand; use crate::state_manager::circulating_supply::GenesisInfo; use crate::state_manager::MarketBalance; -use crate::utils::db::car_stream::{CarBlock, CarWriter}; +use crate::utils::db::{ + car_stream::{CarBlock, CarWriter}, + BlockstoreExt as _, +}; use crate::{ beacon::BeaconEntry, rpc::{types::*, ApiVersion, Ctx, Permission, RpcMethod, ServerError}, @@ -729,8 +732,7 @@ impl RpcMethod<3> for StateMinerInitialPledgeCollateral { let actor = ctx .state_manager - .get_actor(&Address::MARKET_ACTOR, state)? - .context("Market actor address could not be resolved")?; + .get_required_actor(&Address::MARKET_ACTOR, state)?; let market_state = market::State::load(ctx.store(), actor.code, actor.state)?; let (w, vw) = market_state.verify_deals_for_activation( ctx.store(), @@ -744,16 +746,14 @@ impl RpcMethod<3> for StateMinerInitialPledgeCollateral { let actor = ctx .state_manager - .get_actor(&Address::POWER_ACTOR, state)? - .context("Power actor address could not be resolved")?; + .get_required_actor(&Address::POWER_ACTOR, state)?; let power_state = power::State::load(ctx.store(), actor.code, actor.state)?; let power_smoothed = power_state.total_power_smoothed(); let pledge_collateral = power_state.total_locked(); let actor = ctx .state_manager - .get_actor(&Address::REWARD_ACTOR, state)? - .context("Reward actor address could not be resolved")?; + .get_required_actor(&Address::REWARD_ACTOR, state)?; let reward_state = reward::State::load(ctx.store(), actor.code, actor.state)?; let genesis_info = GenesisInfo::from_chain_config(ctx.state_manager.chain_config()); let circ_supply = genesis_info.get_vm_circulating_supply_detailed( @@ -1240,11 +1240,7 @@ impl RpcMethod<2> for StateReadState { let actor = ctx .state_manager .get_required_actor(&address, *ts.parent_state())?; - let blk = ctx - .state_manager - .blockstore() - .get(&actor.state)? - .context("Failed to get block from blockstore")?; + let blk = ctx.state_manager.blockstore().get_required(&actor.state)?; let state = *fvm_ipld_encoding::from_slice::>(&blk)?.first(); Ok(ApiActorState { balance: actor.balance.clone().into(), diff --git a/src/shim/machine/manifest.rs b/src/shim/machine/manifest.rs index 3321b49905e..d470f370e91 100644 --- a/src/shim/machine/manifest.rs +++ b/src/shim/machine/manifest.rs @@ -5,10 +5,10 @@ use std::collections::BTreeMap; +use crate::utils::db::CborStoreExt as _; use anyhow::{ensure, Context as _}; use cid::Cid; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore as _; use itertools::Itertools as _; /// This should be the latest enumeration of all builtin actors @@ -29,9 +29,7 @@ static_assertions::assert_not_impl_all!(BuiltinActor: std::hash::Hash); impl BuiltinActorManifest { const MANDATORY_BUILTINS: &'static [BuiltinActor] = &[BuiltinActor::Init, BuiltinActor::System]; pub fn load_manifest(b: impl Blockstore, manifest_cid: &Cid) -> anyhow::Result { - let (manifest_version, actor_list_cid) = b - .get_cbor::<(u32, Cid)>(manifest_cid)? - .context("failed to load manifest")?; + let (manifest_version, actor_list_cid) = b.get_cbor_required::<(u32, Cid)>(manifest_cid)?; ensure!( manifest_version == 1, "unsupported manifest version {}", @@ -40,9 +38,7 @@ impl BuiltinActorManifest { Self::load_v1_actor_list(b, &actor_list_cid) } pub fn load_v1_actor_list(b: impl Blockstore, actor_list_cid: &Cid) -> anyhow::Result { - let mut actor_list = b - .get_cbor::>(actor_list_cid)? - .context("failed to load actor list")?; + let mut actor_list = b.get_cbor_required::>(actor_list_cid)?; actor_list.sort(); ensure!( actor_list.iter().map(|(name, _cid)| name).all_unique(), diff --git a/src/state_manager/circulating_supply.rs b/src/state_manager/circulating_supply.rs index 47f9d3be003..4b275ac8bb9 100644 --- a/src/state_manager/circulating_supply.rs +++ b/src/state_manager/circulating_supply.rs @@ -267,9 +267,7 @@ fn get_fil_vested(genesis_info: &GenesisInfo, height: ChainEpoch) -> TokenAmount } fn get_fil_mined(state_tree: &StateTree) -> Result { - let actor = state_tree - .get_actor(&Address::REWARD_ACTOR)? - .context("Reward actor address could not be resolved")?; + let actor = state_tree.get_required_actor(&Address::REWARD_ACTOR)?; let state = reward::State::load(state_tree.store(), actor.code, actor.state)?; Ok(state.into_total_storage_power_reward().into()) diff --git a/src/state_manager/mod.rs b/src/state_manager/mod.rs index cc9954ac9d1..4773dac0860 100644 --- a/src/state_manager/mod.rs +++ b/src/state_manager/mod.rs @@ -777,14 +777,9 @@ where let message_from_address = message.from(); let message_sequence = message.sequence(); let mut current_actor_state = self - .get_actor(&message_from_address, *current.parent_state()) - .map_err(|e| Error::State(e.to_string()))? - .context("Failed to load actor state") - .map_err(|e| Error::State(e.to_string()))?; - let message_from_id = self - .lookup_id(&message_from_address, current.as_ref())? - .context("Failed to lookup id") + .get_required_actor(&message_from_address, *current.parent_state()) .map_err(|e| Error::State(e.to_string()))?; + let message_from_id = self.lookup_required_id(&message_from_address, current.as_ref())?; while current.epoch() > look_back_limit.unwrap_or_default() { let parent_tipset = self .cs @@ -1185,9 +1180,7 @@ where epoch, )?; - let actor = self - .get_actor(&addr, *tipset.parent_state())? - .context("miner actor does not exist")?; + let actor = self.get_required_actor(&addr, *tipset.parent_state())?; let miner_state = miner::State::load(self.blockstore(), actor.code, actor.state)?; @@ -1324,7 +1317,7 @@ where addr: &Address, ts: &Arc, ) -> anyhow::Result> { - let id = self.lookup_id(addr, ts)?.context("actor not found")?; + let id = self.lookup_required_id(addr, ts)?; let network_version = self.get_network_version(ts.epoch()); // This is a copy of Lotus code, we need to treat all the actors below version 9 diff --git a/src/state_migration/common/macros/verifier.rs b/src/state_migration/common/macros/verifier.rs index 9c7896aa25c..5af84346e88 100644 --- a/src/state_migration/common/macros/verifier.rs +++ b/src/state_migration/common/macros/verifier.rs @@ -10,11 +10,9 @@ macro_rules! impl_verifier { pub(super) mod verifier { use $crate::cid_collections::CidHashMap; use $crate::shim::{address::Address, machine::BuiltinActorManifest, state_tree::StateTree}; - use ::fvm_ipld_blockstore::Blockstore; - use ::fvm_ipld_encoding::CborStore as _; use $crate::state_migration::common::{verifier::ActorMigrationVerifier, Migrator}; - use ::anyhow::Context as _; - + use $crate::utils::db::CborStoreExt as _; + use ::fvm_ipld_blockstore::Blockstore; use super::*; #[derive(Default)] @@ -28,12 +26,9 @@ macro_rules! impl_verifier { actors_in: &StateTree, ) -> anyhow::Result<()> { let system_actor = actors_in - .get_actor(&Address::SYSTEM_ACTOR)? - .context("system actor not found")?; - + .get_required_actor(&Address::SYSTEM_ACTOR)?; let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + .get_cbor_required::(&system_actor.state)?; let manifest = BuiltinActorManifest::load_v1_actor_list(&store, &system_actor_state.builtin_actors)?; let manifest_actors_count = manifest.builtin_actors().len(); diff --git a/src/state_migration/nv17/migration.rs b/src/state_migration/nv17/migration.rs index 9ce811eb4d1..e4f77e85ab2 100644 --- a/src/state_migration/nv17/migration.rs +++ b/src/state_migration/nv17/migration.rs @@ -10,7 +10,8 @@ use crate::shim::{ machine::{BuiltinActor, BuiltinActorManifest}, state_tree::{StateTree, StateTreeVersion}, }; -use anyhow::{anyhow, Context as _}; +use crate::utils::db::CborStoreExt as _; +use anyhow::anyhow; use cid::Cid; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::CborStore as _; @@ -46,24 +47,19 @@ impl StateMigration { BuiltinActorManifest::load_v1_actor_list(store, ¤t_manifest_data)?; let verifreg_actor_v8 = actors_in - .get_actor(&fil_actors_shared::v8::VERIFIED_REGISTRY_ACTOR_ADDR.into())? - .context("Failed to load verifreg actor v8")?; + .get_required_actor(&fil_actors_shared::v8::VERIFIED_REGISTRY_ACTOR_ADDR.into())?; let market_actor_v8 = actors_in - .get_actor(&fil_actors_shared::v8::STORAGE_MARKET_ACTOR_ADDR.into())? - .context("Failed to load market actor v8")?; + .get_required_actor(&fil_actors_shared::v8::STORAGE_MARKET_ACTOR_ADDR.into())?; - let market_state_v8: fil_actor_market_state::v8::State = store - .get_cbor(&market_actor_v8.state)? - .context("Failed to load market state v8")?; + let market_state_v8: fil_actor_market_state::v8::State = + store.get_cbor_required(&market_actor_v8.state)?; - let init_actor_v8 = actors_in - .get_actor(&fil_actors_shared::v8::INIT_ACTOR_ADDR.into())? - .context("Failed to load init actor v8")?; + let init_actor_v8 = + actors_in.get_required_actor(&fil_actors_shared::v8::INIT_ACTOR_ADDR.into())?; - let init_state_v8: fil_actor_init_state::v8::State = store - .get_cbor(&init_actor_v8.state)? - .context("Failed to load init state v8")?; + let init_state_v8: fil_actor_init_state::v8::State = + store.get_cbor_required(&init_actor_v8.state)?; let (pending_verified_deals, pending_verified_deal_size) = get_pending_verified_deals_and_total_size(&store, &market_state_v8)?; @@ -95,9 +91,8 @@ impl StateMigration { ); let verifreg_state_v8_cid = verifreg_actor_v8.state; - let verifreg_state_v8: fil_actor_verifreg_state::v8::State = store - .get_cbor(&verifreg_state_v8_cid)? - .context("Failed to load verifreg state v8")?; + let verifreg_state_v8: fil_actor_verifreg_state::v8::State = + store.get_cbor_required(&verifreg_state_v8_cid)?; let verifreg_code = new_manifest.get(BuiltinActor::VerifiedRegistry)?; let market_code = new_manifest.get(BuiltinActor::Market)?; @@ -118,9 +113,7 @@ impl StateMigration { // while forest uses a post migrator to simplify the logic. self.add_post_migrator(Arc::new(datacap::DataCapPostMigrator { new_code_cid: new_manifest.get(BuiltinActor::DataCap)?, - verifreg_state: store - .get_cbor(&verifreg_state_v8_cid)? - .context("Failed to load verifreg state v8")?, + verifreg_state: store.get_cbor_required(&verifreg_state_v8_cid)?, pending_verified_deal_size, })); diff --git a/src/state_migration/nv17/miner.rs b/src/state_migration/nv17/miner.rs index fc398be2834..a2e06ef1aef 100644 --- a/src/state_migration/nv17/miner.rs +++ b/src/state_migration/nv17/miner.rs @@ -19,7 +19,6 @@ use fil_actor_miner_state::{ use fil_actors_shared::abi::commp::compute_unsealed_sector_cid_v2; use fil_actors_shared::fvm_ipld_amt; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use super::super::common::{ ActorMigration, ActorMigrationInput, ActorMigrationOutput, TypeMigration, TypeMigrator, @@ -93,9 +92,7 @@ where ) -> anyhow::Result> { let mut cache: HashMap = Default::default(); - let in_state: MinerStateOld = store - .get_cbor(&input.head)? - .context("Init actor: could not read v9 state")?; + let in_state: MinerStateOld = store.get_cbor_required(&input.head)?; let new_pre_committed_sectors = self.migrate_pre_committed_sectors(&store, &in_state.pre_committed_sectors)?; let new_sectors = @@ -252,9 +249,8 @@ impl MinerMigrator { if deadlines == &self.empty_deadlines_v8_cid { Ok(self.empty_deadlines_v9_cid) } else { - let in_deadlines: fil_actor_miner_state::v8::Deadlines = store - .get_cbor(deadlines)? - .context("Failed to get in_deadlines")?; + let in_deadlines: fil_actor_miner_state::v8::Deadlines = + store.get_cbor_required(deadlines)?; let policy = match &self.chain { NetworkChain::Mainnet => fil_actors_shared::v9::runtime::Policy::mainnet(), @@ -273,7 +269,7 @@ impl MinerMigrator { } } else { let in_deadline: fil_actor_miner_state::v8::Deadline = - store.get_cbor(c)?.context("Failed to get in_deadline")?; + store.get_cbor_required(c)?; let out_sectors_snapshot_cid_cache_key = sectors_amt_key(&in_deadline.sectors_snapshot)?; @@ -394,7 +390,7 @@ mod tests { .unwrap() .unwrap(); let system_state_old: fil_actor_system_state::v9::State = - store.get_cbor(&system_actor_old.state).unwrap().unwrap(); + store.get_cbor_required(&system_actor_old.state).unwrap(); let manifest_data_cid_old = system_state_old.builtin_actors; assert_eq!(manifest_data_cid_old, manifest_old.source_cid()); assert_eq!( @@ -412,7 +408,7 @@ mod tests { .unwrap() .unwrap(); let mut market_state_old: fil_actor_market_state::v8::State = - store.get_cbor(&market_actor_old.state).unwrap().unwrap(); + store.get_cbor_required(&market_actor_old.state).unwrap(); let mut proposals = fil_actors_shared::v8::Array::< fil_actor_market_state::v8::DealProposal, _, @@ -621,7 +617,7 @@ mod tests { } let new_state_cid = super::super::run_migration(&chain_config, &store, &tree_root, 200).unwrap(); - let actors_out_state_root: StateRoot = store.get_cbor(&new_state_cid).unwrap().unwrap(); + let actors_out_state_root: StateRoot = store.get_cbor_required(&new_state_cid).unwrap(); assert_eq!( actors_out_state_root.actors.to_string(), "bafy2bzacedgtk3lnnyfxnzc32etqaj3zvi7ar7nxq2jtxd2qr36ftbsjoycqu" @@ -654,7 +650,7 @@ mod tests { } let new_state_cid = super::super::run_migration(&chain_config, &store, &state_tree_old_root, 200).unwrap(); - let actors_out_state_root: StateRoot = store.get_cbor(&new_state_cid).unwrap().unwrap(); + let actors_out_state_root: StateRoot = store.get_cbor_required(&new_state_cid).unwrap(); assert_eq!( actors_out_state_root.actors.to_string(), "bafy2bzacebdpnjjyspbyj7al7d6234kdhkmdygkfdkp6zyao5o3egsfmribty" diff --git a/src/state_migration/nv18/eam.rs b/src/state_migration/nv18/eam.rs index 20751e21e6b..00c5ea1fd9e 100644 --- a/src/state_migration/nv18/eam.rs +++ b/src/state_migration/nv18/eam.rs @@ -6,9 +6,8 @@ use crate::shim::{ machine::{BuiltinActor, BuiltinActorManifest}, state_tree::{ActorState, StateTree}, }; -use anyhow::Context as _; +use crate::utils::db::CborStoreExt as _; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use crate::state_migration::common::PostMigrator; @@ -19,12 +18,8 @@ pub struct EamPostMigrator; impl PostMigrator for EamPostMigrator { /// Creates the Ethereum Account Manager actor in the state tree. fn post_migrate_state(&self, store: &BS, actors_out: &mut StateTree) -> anyhow::Result<()> { - let sys_actor = actors_out - .get_actor(&Address::SYSTEM_ACTOR)? - .context("Couldn't get sys actor state")?; - let sys_state: SystemStateNew = store - .get_cbor(&sys_actor.state)? - .context("Couldn't get statev10")?; + let sys_actor = actors_out.get_required_actor(&Address::SYSTEM_ACTOR)?; + let sys_state: SystemStateNew = store.get_cbor_required(&sys_actor.state)?; let manifest = BuiltinActorManifest::load_v1_actor_list(store, &sys_state.builtin_actors)?; diff --git a/src/state_migration/nv18/eth_account.rs b/src/state_migration/nv18/eth_account.rs index 2090489da8c..6dde962499c 100644 --- a/src/state_migration/nv18/eth_account.rs +++ b/src/state_migration/nv18/eth_account.rs @@ -1,31 +1,26 @@ // Copyright 2019-2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +use super::SystemStateNew; use crate::shim::{ address::Address, machine::{BuiltinActor, BuiltinActorManifest}, state_tree::{ActorState, StateTree}, }; +use crate::state_migration::common::PostMigrator; +use crate::utils::db::CborStoreExt as _; use anyhow::anyhow; -use anyhow::Context as _; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::CborStore; -use crate::state_migration::common::PostMigrator; - -use super::SystemStateNew; - pub struct EthAccountPostMigrator; impl PostMigrator for EthAccountPostMigrator { /// Creates the Ethereum Account actor in the state tree. fn post_migrate_state(&self, store: &BS, actors_out: &mut StateTree) -> anyhow::Result<()> { - let init_actor = actors_out - .get_actor(&Address::INIT_ACTOR)? - .context("Couldn't get init actor state")?; - let init_state: fil_actor_init_state::v10::State = store - .get_cbor(&init_actor.state)? - .context("Couldn't get statev10")?; + let init_actor = actors_out.get_required_actor(&Address::INIT_ACTOR)?; + let init_state: fil_actor_init_state::v10::State = + store.get_cbor_required(&init_actor.state)?; let eth_zero_addr = Address::new_delegated(Address::ETHEREUM_ACCOUNT_MANAGER_ACTOR.id()?, &[0; 20])?; diff --git a/src/state_migration/nv18/init.rs b/src/state_migration/nv18/init.rs index 97e0792e28b..9e5916cd45a 100644 --- a/src/state_migration/nv18/init.rs +++ b/src/state_migration/nv18/init.rs @@ -10,11 +10,9 @@ use crate::state_migration::common::{ ActorMigration, ActorMigrationInput, ActorMigrationOutput, TypeMigration, TypeMigrator, }; use crate::utils::db::CborStoreExt; -use anyhow::Context as _; use cid::Cid; use fil_actor_init_state::{v10::State as InitStateNew, v9::State as InitStateOld}; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; pub struct InitMigrator(Cid); @@ -30,14 +28,9 @@ impl ActorMigration for InitMigrator { store: &BS, input: ActorMigrationInput, ) -> anyhow::Result> { - let in_state: InitStateOld = store - .get_cbor(&input.head)? - .context("Init actor: could not read v9 state")?; - + let in_state: InitStateOld = store.get_cbor_required(&input.head)?; let out_state: InitStateNew = TypeMigrator::migrate_type(in_state, &store)?; - let new_head = store.put_cbor_default(&out_state)?; - Ok(Some(ActorMigrationOutput { new_code_cid: self.0, new_head, diff --git a/src/state_migration/nv19/miner.rs b/src/state_migration/nv19/miner.rs index 5b894595014..4b717ae8af8 100644 --- a/src/state_migration/nv19/miner.rs +++ b/src/state_migration/nv19/miner.rs @@ -7,12 +7,10 @@ use crate::state_migration::common::{ ActorMigration, ActorMigrationInput, ActorMigrationOutput, TypeMigration, TypeMigrator, }; -use crate::utils::db::CborStoreExt; -use anyhow::Context as _; +use crate::utils::db::CborStoreExt as _; use cid::Cid; use fil_actor_miner_state::{v10::State as MinerStateOld, v11::State as MinerStateNew}; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore as _; use std::sync::Arc; pub struct MinerMigrator(Cid); @@ -29,14 +27,9 @@ impl ActorMigration for MinerMigrator { store: &BS, input: ActorMigrationInput, ) -> anyhow::Result> { - let in_state: MinerStateOld = store - .get_cbor(&input.head)? - .context("Miner actor: could not read v10 state")?; - + let in_state: MinerStateOld = store.get_cbor_required(&input.head)?; let out_state: MinerStateNew = TypeMigrator::migrate_type(in_state, &store)?; - let new_head = store.put_cbor_default(&out_state)?; - Ok(Some(ActorMigrationOutput { new_code_cid: self.0, new_head, diff --git a/src/state_migration/nv19/power.rs b/src/state_migration/nv19/power.rs index 25904d505fe..1cca6993625 100644 --- a/src/state_migration/nv19/power.rs +++ b/src/state_migration/nv19/power.rs @@ -6,8 +6,7 @@ use crate::shim::sector::convert_window_post_proof_v1_to_v1p1; use crate::state_migration::common::{ActorMigration, ActorMigrationInput, ActorMigrationOutput}; -use crate::utils::db::CborStoreExt; -use anyhow::Context as _; +use crate::utils::db::CborStoreExt as _; use cid::Cid; use fil_actor_power_state::{ v10::{Claim as ClaimV10, State as StateV10}, @@ -17,7 +16,6 @@ use fil_actors_shared::v11::{ builtin::HAMT_BIT_WIDTH, make_empty_map, make_map_with_root_and_bitwidth, }; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use std::sync::Arc; pub struct PowerMigrator(Cid); @@ -35,9 +33,7 @@ impl ActorMigration for PowerMigrator { store: &BS, input: ActorMigrationInput, ) -> anyhow::Result> { - let in_state: StateV10 = store - .get_cbor(&input.head)? - .context("Power actor: could not read v10 state")?; + let in_state: StateV10 = store.get_cbor_required(&input.head)?; let in_claims = make_map_with_root_and_bitwidth(&in_state.claims, &store, HAMT_BIT_WIDTH)?; diff --git a/src/state_migration/nv21/migration.rs b/src/state_migration/nv21/migration.rs index 12e7e64b22a..9ce03189864 100644 --- a/src/state_migration/nv21/migration.rs +++ b/src/state_migration/nv21/migration.rs @@ -3,6 +3,7 @@ use std::sync::Arc; +use super::{miner, system, verifier::Verifier, SystemStateOld}; use crate::make_butterfly_policy; use crate::networks::{ChainConfig, Height, NetworkChain}; use crate::shim::{ @@ -12,14 +13,12 @@ use crate::shim::{ sector::{RegisteredPoStProofV3, RegisteredSealProofV3}, state_tree::{StateTree, StateTreeVersion}, }; +use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; +use crate::utils::db::CborStoreExt as _; use anyhow::Context; use cid::Cid; use fil_actors_shared::v11::runtime::ProofSet; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; - -use super::{miner, system, verifier::Verifier, SystemStateOld}; -use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; impl StateMigration { pub fn add_nv21_migrations( @@ -30,13 +29,8 @@ impl StateMigration { chain_config: &ChainConfig, ) -> anyhow::Result<()> { let state_tree = StateTree::new_from_root(store.clone(), state)?; - let system_actor = state_tree - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; - - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor = state_tree.get_required_actor(&Address::new_id(0))?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; diff --git a/src/state_migration/nv21/miner.rs b/src/state_migration/nv21/miner.rs index c0c62989cd8..dbe5b44e51c 100644 --- a/src/state_migration/nv21/miner.rs +++ b/src/state_migration/nv21/miner.rs @@ -7,6 +7,9 @@ use std::sync::Arc; use crate::shim::econ::TokenAmount; +use crate::state_migration::common::{ + ActorMigration, ActorMigrationInput, ActorMigrationOutput, TypeMigration, TypeMigrator, +}; use crate::{ shim::address::Address, state_migration::common::MigrationCache, utils::db::CborStoreExt, }; @@ -20,11 +23,6 @@ use fil_actors_shared::fvm_ipld_amt; use fil_actors_shared::v11::{runtime::Policy as PolicyOld, Array as ArrayOld}; use fil_actors_shared::v12::{runtime::Policy as PolicyNew, Array as ArrayNew}; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; - -use crate::state_migration::common::{ - ActorMigration, ActorMigrationInput, ActorMigrationOutput, TypeMigration, TypeMigrator, -}; pub struct MinerMigrator { empty_deadline_v11: Cid, @@ -69,9 +67,7 @@ impl ActorMigration for MinerMigrator { store: &BS, input: ActorMigrationInput, ) -> anyhow::Result> { - let in_state: MinerStateOld = store - .get_cbor(&input.head)? - .context("Miner actor: could not read v11 state")?; + let in_state: MinerStateOld = store.get_cbor_required(&input.head)?; let new_sectors = self.migrate_sectors_with_cache( store, @@ -204,9 +200,7 @@ impl MinerMigrator { return Ok(self.empty_deadlines_v12); } - let in_deadlines = store - .get_cbor::(deadlines)? - .context("failed to get in_deadlines")?; + let in_deadlines = store.get_cbor_required::(deadlines)?; let mut out_deadlines = DeadlinesNew::new(&self.policy_new, self.empty_deadline_v12); for (i, deadline) in in_deadlines.due.iter().enumerate() { @@ -217,9 +211,7 @@ impl MinerMigrator { out_deadlines.due.push(self.empty_deadline_v12); } } else { - let in_deadline = store - .get_cbor::(deadline)? - .context("failed to get in_deadline")?; + let in_deadline = store.get_cbor_required::(deadline)?; let out_sectors_snapshot_cid_cache_key = sectors_amt_key(&in_deadline.sectors_snapshot)?; @@ -309,11 +301,10 @@ mod tests { let store = Arc::new(crate::db::MemoryDB::default()); let (mut state_tree_old, manifest_old) = make_input_tree(&store); let system_actor_old = state_tree_old - .get_actor(&fil_actor_interface::system::ADDRESS.into()) - .unwrap() + .get_required_actor(&fil_actor_interface::system::ADDRESS.into()) .unwrap(); let system_state_old: fil_actor_system_state::v11::State = - store.get_cbor(&system_actor_old.state).unwrap().unwrap(); + store.get_cbor_required(&system_actor_old.state).unwrap(); let manifest_data_cid_old = system_state_old.builtin_actors; assert_eq!(manifest_data_cid_old, manifest_old.source_cid()); @@ -376,11 +367,11 @@ mod tests { assert_eq!(new_state_cid, new_state_cid2); let new_state_tree = StateTree::new_from_root(store.clone(), &new_state_cid).unwrap(); - let new_miner_state_cid = new_state_tree.get_actor(&addr).unwrap().unwrap().state; + let new_miner_state_cid = new_state_tree.get_required_actor(&addr).unwrap().state; let new_miner_state: fil_actor_miner_state::v12::State = - store.get_cbor(&new_miner_state_cid).unwrap().unwrap(); + store.get_cbor_required(&new_miner_state_cid).unwrap(); let deadlines: fil_actor_miner_state::v12::Deadlines = - store.get_cbor(&new_miner_state.deadlines).unwrap().unwrap(); + store.get_cbor_required(&new_miner_state.deadlines).unwrap(); deadlines .for_each(&store, |_, deadline| { let sectors_snapshots = diff --git a/src/state_migration/nv21fix/migration.rs b/src/state_migration/nv21fix/migration.rs index 53de85ce162..99797be6759 100644 --- a/src/state_migration/nv21fix/migration.rs +++ b/src/state_migration/nv21fix/migration.rs @@ -11,10 +11,10 @@ use crate::shim::{ state_tree::{StateTree, StateTreeVersion}, }; use crate::state_migration::common::PostMigrationCheck; +use crate::utils::db::CborStoreExt as _; use anyhow::{bail, Context}; use cid::Cid; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use super::{system, verifier::Verifier, SystemStateOld}; use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; @@ -27,13 +27,9 @@ impl StateMigration { new_manifest: &BuiltinActorManifest, ) -> anyhow::Result<()> { let state_tree = StateTree::new_from_root(store.clone(), state)?; - let system_actor = state_tree - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = state_tree.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -61,13 +57,9 @@ struct PostMigrationVerifier { impl PostMigrationCheck for PostMigrationVerifier { fn post_migrate_check(&self, store: &BS, actors_out: &StateTree) -> anyhow::Result<()> { let actors_in = StateTree::new_from_root(Arc::new(store), &self.state_pre)?; - let system_actor = actors_in - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = actors_in.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -75,9 +67,7 @@ impl PostMigrationCheck for PostMigrationVerifier { BuiltinActorManifest::load_v1_actor_list(store, ¤t_manifest_data)?; actors_in.for_each(|address, actor_in| { - let actor_out = actors_out - .get_actor(&address)? - .context("failed to get actor from state tree")?; + let actor_out = actors_out.get_required_actor(&address)?; if actor_in.sequence != actor_out.sequence { bail!( diff --git a/src/state_migration/nv21fix2/migration.rs b/src/state_migration/nv21fix2/migration.rs index f434dd0088f..ecc7ae4a420 100644 --- a/src/state_migration/nv21fix2/migration.rs +++ b/src/state_migration/nv21fix2/migration.rs @@ -11,10 +11,10 @@ use crate::shim::{ state_tree::{StateTree, StateTreeVersion}, }; use crate::state_migration::common::PostMigrationCheck; +use crate::utils::db::CborStoreExt as _; use anyhow::{bail, Context}; use cid::Cid; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use super::{system, verifier::Verifier, SystemStateOld}; use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; @@ -27,13 +27,9 @@ impl StateMigration { new_manifest: &BuiltinActorManifest, ) -> anyhow::Result<()> { let state_tree = StateTree::new_from_root(store.clone(), state)?; - let system_actor = state_tree - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = state_tree.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -61,13 +57,9 @@ struct PostMigrationVerifier { impl PostMigrationCheck for PostMigrationVerifier { fn post_migrate_check(&self, store: &BS, actors_out: &StateTree) -> anyhow::Result<()> { let actors_in = StateTree::new_from_root(Arc::new(store), &self.state_pre)?; - let system_actor = actors_in - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = actors_in.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -75,9 +67,7 @@ impl PostMigrationCheck for PostMigrationVerifier { BuiltinActorManifest::load_v1_actor_list(store, ¤t_manifest_data)?; actors_in.for_each(|address, actor_in| { - let actor_out = actors_out - .get_actor(&address)? - .context("failed to get actor from state tree")?; + let actor_out = actors_out.get_required_actor(&address)?; if actor_in.sequence != actor_out.sequence { bail!( diff --git a/src/state_migration/nv22/market.rs b/src/state_migration/nv22/market.rs index ea3dcb80a0d..50187b7bd41 100644 --- a/src/state_migration/nv22/market.rs +++ b/src/state_migration/nv22/market.rs @@ -21,7 +21,6 @@ use fil_actor_market_state::v13::{ use fil_actors_shared::v12::Array as ArrayOld; use fil_actors_shared::v13::Array as ArrayNew; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use fvm_shared4::clock::ChainEpoch; use crate::state_migration::common::{ActorMigration, ActorMigrationInput, ActorMigrationOutput}; @@ -51,9 +50,7 @@ impl ActorMigration for MarketMigrator { store: &BS, input: ActorMigrationInput, ) -> anyhow::Result> { - let in_state: MarketStateOld = store - .get_cbor(&input.head)? - .context("failed to load state")?; + let in_state: MarketStateOld = store.get_cbor_required(&input.head)?; let (provider_sectors, new_states) = self.migrate_provider_sectors_and_states(store, &in_state.states, &in_state.proposals)?; diff --git a/src/state_migration/nv22/migration.rs b/src/state_migration/nv22/migration.rs index 7a659133681..81e757b2e4e 100644 --- a/src/state_migration/nv22/migration.rs +++ b/src/state_migration/nv22/migration.rs @@ -14,11 +14,11 @@ use crate::shim::{ machine::{BuiltinActor, BuiltinActorManifest}, state_tree::{StateTree, StateTreeVersion}, }; +use crate::utils::db::CborStoreExt as _; use anyhow::Context; use cid::Cid; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use super::{market, miner, system, verifier::Verifier, SystemStateOld}; use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; @@ -38,13 +38,8 @@ impl StateMigration { .epoch; let state_tree = StateTree::new_from_root(store.clone(), state)?; - let system_actor = state_tree - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; - - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor = state_tree.get_required_actor(&Address::new_id(0))?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; diff --git a/src/state_migration/nv22/miner.rs b/src/state_migration/nv22/miner.rs index 0214fc47c5e..0fdf35ae4ad 100644 --- a/src/state_migration/nv22/miner.rs +++ b/src/state_migration/nv22/miner.rs @@ -12,13 +12,12 @@ //! > requires reading all sector metadata from the miner actor. use crate::state_migration::common::{ActorMigration, ActorMigrationInput, ActorMigrationOutput}; +use crate::utils::db::CborStoreExt as _; use ahash::HashMap; -use anyhow::Context; use cid::Cid; use fil_actor_miner_state::v12::State as MinerStateOld; use fil_actors_shared::v12::Array as ArrayOld; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use fvm_shared4::clock::ChainEpoch; use fvm_shared4::deal::DealID; use fvm_shared4::sector::{SectorID, SectorNumber}; @@ -57,9 +56,7 @@ impl ActorMigration for MinerMigrator { input: ActorMigrationInput, ) -> anyhow::Result> { let miner_id = input.address.id()?; - let in_state: MinerStateOld = store - .get_cbor(&input.head)? - .context("Miner actor: could not read v12 state")?; + let in_state: MinerStateOld = store.get_cbor_required(&input.head)?; let in_sectors = ArrayOld::::load( &in_state.sectors, diff --git a/src/state_migration/nv22fix/migration.rs b/src/state_migration/nv22fix/migration.rs index 67c99f60dac..e3ac73ada84 100644 --- a/src/state_migration/nv22fix/migration.rs +++ b/src/state_migration/nv22fix/migration.rs @@ -5,6 +5,7 @@ //! Corresponding use std::sync::Arc; +use super::{system, verifier::Verifier, SystemStateOld}; use crate::networks::{ChainConfig, Height, NetworkChain}; use crate::shim::{ address::Address, @@ -13,14 +14,11 @@ use crate::shim::{ state_tree::{StateTree, StateTreeVersion}, }; use crate::state_migration::common::PostMigrationCheck; +use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; +use crate::utils::db::CborStoreExt as _; use anyhow::{bail, Context}; use cid::Cid; - use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; - -use super::{system, verifier::Verifier, SystemStateOld}; -use crate::state_migration::common::{migrators::nil_migrator, StateMigration}; impl StateMigration { pub fn add_nv22fix_migrations( @@ -30,13 +28,9 @@ impl StateMigration { new_manifest: &BuiltinActorManifest, ) -> anyhow::Result<()> { let state_tree = StateTree::new_from_root(store.clone(), state)?; - let system_actor = state_tree - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = state_tree.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -64,13 +58,9 @@ struct PostMigrationVerifier { impl PostMigrationCheck for PostMigrationVerifier { fn post_migrate_check(&self, store: &BS, actors_out: &StateTree) -> anyhow::Result<()> { let actors_in = StateTree::new_from_root(Arc::new(store), &self.state_pre)?; - let system_actor = actors_in - .get_actor(&Address::new_id(0))? - .context("failed to get system actor")?; + let system_actor = actors_in.get_required_actor(&Address::new_id(0))?; - let system_actor_state = store - .get_cbor::(&system_actor.state)? - .context("system actor state not found")?; + let system_actor_state = store.get_cbor_required::(&system_actor.state)?; let current_manifest_data = system_actor_state.builtin_actors; @@ -78,9 +68,7 @@ impl PostMigrationCheck for PostMigrationVerifier { BuiltinActorManifest::load_v1_actor_list(store, ¤t_manifest_data)?; actors_in.for_each(|address, actor_in| { - let actor_out = actors_out - .get_actor(&address)? - .context("failed to get actor from state tree")?; + let actor_out = actors_out.get_required_actor(&address)?; if actor_in.sequence != actor_out.sequence { bail!( diff --git a/src/state_migration/type_migrations/miner/state_v10_to_v11.rs b/src/state_migration/type_migrations/miner/state_v10_to_v11.rs index 12dd097a6e0..c7bcc716b13 100644 --- a/src/state_migration/type_migrations/miner/state_v10_to_v11.rs +++ b/src/state_migration/type_migrations/miner/state_v10_to_v11.rs @@ -2,22 +2,17 @@ // SPDX-License-Identifier: Apache-2.0, MIT use crate::shim::sector::convert_window_post_proof_v1_to_v1p1; -use crate::utils::db::CborStoreExt; -use anyhow::Context as _; +use crate::state_migration::common::{TypeMigration, TypeMigrator}; +use crate::utils::db::CborStoreExt as _; use fil_actor_miner_state::{ v10::{MinerInfo as MinerInfoV10, State as MinerStateV10}, v11::{MinerInfo as MinerInfoV11, State as MinerStateV11}, }; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; - -use crate::state_migration::common::{TypeMigration, TypeMigrator}; impl TypeMigration for TypeMigrator { fn migrate_type(from: MinerStateV10, store: &impl Blockstore) -> anyhow::Result { - let in_info: MinerInfoV10 = store - .get_cbor(&from.info)? - .context("Miner info: could not read v10 state")?; + let in_info: MinerInfoV10 = store.get_cbor_required(&from.info)?; let out_proof_type = convert_window_post_proof_v1_to_v1p1(in_info.window_post_proof_type) .map_err(|e| anyhow::anyhow!(e))?; diff --git a/src/state_migration/type_migrations/miner/state_v8_to_v9.rs b/src/state_migration/type_migrations/miner/state_v8_to_v9.rs index 7ad808dd8f4..eaf71d880ab 100644 --- a/src/state_migration/type_migrations/miner/state_v8_to_v9.rs +++ b/src/state_migration/type_migrations/miner/state_v8_to_v9.rs @@ -2,21 +2,17 @@ // SPDX-License-Identifier: Apache-2.0, MIT use crate::utils::db::CborStoreExt; -use anyhow::Context as _; use fil_actor_miner_state::{ v8::{MinerInfo as MinerInfoV8, State as MinerStateV8}, v9::{MinerInfo as MinerInfoV9, State as MinerStateV9}, }; use fvm_ipld_blockstore::Blockstore; -use fvm_ipld_encoding::CborStore; use super::super::super::common::{TypeMigration, TypeMigrator}; impl TypeMigration for TypeMigrator { fn migrate_type(from: MinerStateV8, store: &impl Blockstore) -> anyhow::Result { - let in_info: MinerInfoV8 = store - .get_cbor(&from.info)? - .context("Miner info: could not read v8 state")?; + let in_info: MinerInfoV8 = store.get_cbor_required(&from.info)?; let out_info: MinerInfoV9 = TypeMigrator::migrate_type(in_info, store)?; diff --git a/src/statediff/resolve.rs b/src/statediff/resolve.rs index d0129ddd7d5..9acbe71bf23 100644 --- a/src/statediff/resolve.rs +++ b/src/statediff/resolve.rs @@ -1,7 +1,7 @@ // Copyright 2019-2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -use anyhow::Context as _; +use crate::utils::db::CborStoreExt as _; use cid::Cid; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::CborStore; @@ -17,12 +17,8 @@ pub fn resolve_cids_recursive( where BS: Blockstore, { - let mut ipld = bs - .get_cbor(cid)? - .context("Cid does not exist in blockstore")?; - + let mut ipld = bs.get_cbor_required(cid)?; resolve_ipld(bs, &mut ipld, depth)?; - Ok(ipld) } diff --git a/src/utils/db/mod.rs b/src/utils/db/mod.rs index e869bf5cb8c..8967bb0234f 100644 --- a/src/utils/db/mod.rs +++ b/src/utils/db/mod.rs @@ -4,6 +4,7 @@ pub mod car_stream; pub mod car_util; +use anyhow::Context as _; use cid::{ multihash::{Code, MultihashDigest}, Cid, @@ -41,6 +42,12 @@ pub trait BlockstoreExt: Blockstore { Ok(cids) } + + /// Gets the block from the blockstore. Return an error when not found. + fn get_required(&self, cid: &Cid) -> anyhow::Result> { + self.get(cid)? + .with_context(|| format!("Entry not found in block store: cid={cid}")) + } } impl BlockstoreExt for T {} @@ -61,6 +68,19 @@ pub trait CborStoreExt: CborStore { fn put_cbor_default(&self, obj: &S) -> anyhow::Result { self.put_cbor(obj, Self::default_code()) } + + /// Get typed object from block store by `CID`. Return an error when not found. + fn get_cbor_required(&self, cid: &Cid) -> anyhow::Result + where + T: serde::de::DeserializeOwned, + { + self.get_cbor(cid)?.with_context(|| { + format!( + "Entry not found in cbor store: cid={cid}, type={}", + std::any::type_name::() + ) + }) + } } impl CborStoreExt for T {}