Skip to content

Commit

Permalink
chore: use fn get_required_* to avoid uninformative `<Option as any…
Browse files Browse the repository at this point in the history
…how::Context>::context` calls (#4293)
  • Loading branch information
hanabi1224 authored May 8, 2024
1 parent 4d86953 commit bb4f5c7
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 249 deletions.
13 changes: 3 additions & 10 deletions src/rpc/methods/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -306,11 +307,7 @@ impl RpcMethod<1> for ChainGetBlockMessages {
ctx: Ctx<impl Blockstore>,
(cid,): Self::Params,
) -> Result<Self::Ok, ServerError> {
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)?;
Expand Down Expand Up @@ -507,11 +504,7 @@ impl RpcMethod<1> for ChainGetBlock {
ctx: Ctx<impl Blockstore>,
(cid,): Self::Params,
) -> Result<Self::Ok, ServerError> {
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)
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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()))
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/rpc/methods/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(),
Expand All @@ -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(
Expand Down Expand Up @@ -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::<NonEmpty<Cid>>(&blk)?.first();
Ok(ApiActorState {
balance: actor.balance.clone().into(),
Expand Down
10 changes: 3 additions & 7 deletions src/shim/machine/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Self> {
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 {}",
Expand All @@ -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<Self> {
let mut actor_list = b
.get_cbor::<Vec<(String, Cid)>>(actor_list_cid)?
.context("failed to load actor list")?;
let mut actor_list = b.get_cbor_required::<Vec<(String, Cid)>>(actor_list_cid)?;
actor_list.sort();
ensure!(
actor_list.iter().map(|(name, _cid)| name).all_unique(),
Expand Down
4 changes: 1 addition & 3 deletions src/state_manager/circulating_supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ fn get_fil_vested(genesis_info: &GenesisInfo, height: ChainEpoch) -> TokenAmount
}

fn get_fil_mined<DB: Blockstore>(state_tree: &StateTree<DB>) -> Result<TokenAmount, anyhow::Error> {
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())
Expand Down
15 changes: 4 additions & 11 deletions src/state_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -1324,7 +1317,7 @@ where
addr: &Address,
ts: &Arc<Tipset>,
) -> anyhow::Result<Option<DataCap>> {
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
Expand Down
13 changes: 4 additions & 9 deletions src/state_migration/common/macros/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -28,12 +26,9 @@ macro_rules! impl_verifier {
actors_in: &StateTree<BS>,
) -> 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::<SystemStateOld>(&system_actor.state)?
.context("system actor state not found")?;
.get_cbor_required::<SystemStateOld>(&system_actor.state)?;
let manifest =
BuiltinActorManifest::load_v1_actor_list(&store, &system_actor_state.builtin_actors)?;
let manifest_actors_count = manifest.builtin_actors().len();
Expand Down
33 changes: 13 additions & 20 deletions src/state_migration/nv17/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -46,24 +47,19 @@ impl<BS: Blockstore + Send + Sync> StateMigration<BS> {
BuiltinActorManifest::load_v1_actor_list(store, &current_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)?;
Expand Down Expand Up @@ -95,9 +91,8 @@ impl<BS: Blockstore + Send + Sync> StateMigration<BS> {
);

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

Expand All @@ -118,9 +113,7 @@ impl<BS: Blockstore + Send + Sync> StateMigration<BS> {
// 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,
}));

Expand Down
20 changes: 8 additions & 12 deletions src/state_migration/nv17/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -93,9 +92,7 @@ where
) -> anyhow::Result<Option<ActorMigrationOutput>> {
let mut cache: HashMap<String, Cid> = 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 =
Expand Down Expand Up @@ -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(),
Expand All @@ -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)?;
Expand Down Expand Up @@ -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!(
Expand All @@ -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,
_,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 3 additions & 8 deletions src/state_migration/nv18/eam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,12 +18,8 @@ pub struct EamPostMigrator;
impl<BS: Blockstore> PostMigrator<BS> for EamPostMigrator {
/// Creates the Ethereum Account Manager actor in the state tree.
fn post_migrate_state(&self, store: &BS, actors_out: &mut StateTree<BS>) -> 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)?;

Expand Down
Loading

0 comments on commit bb4f5c7

Please sign in to comment.