Skip to content

Commit

Permalink
cleanup and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Apr 12, 2022
1 parent 94671d0 commit 14289dc
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 143 deletions.
3 changes: 1 addition & 2 deletions actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,7 @@ where
illegal_argument,
"deal id {} present multiple times",
deal_id
)
.into());
));
}
let proposal = proposals
.get(*deal_id)?
Expand Down
4 changes: 2 additions & 2 deletions actors/market/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ where
lock_reason: Reason,
) -> Result<(), ActorError> {
if amount.is_negative() {
return Err(actor_error!(illegal_state, "unlock negative amount: {}", amount).into());
return Err(actor_error!(illegal_state, "unlock negative amount: {}", amount));
}
self.locked_table.as_mut().unwrap().must_subtract(addr, amount)?;

Expand Down Expand Up @@ -618,7 +618,7 @@ where
lock_reason: Reason,
) -> Result<(), ActorError> {
if amount.is_negative() {
return Err(actor_error!(illegal_state, "negative amount to slash: {}", amount).into());
return Err(actor_error!(illegal_state, "negative amount to slash: {}", amount));
}

// Subtract from locked and escrow tables
Expand Down
6 changes: 3 additions & 3 deletions actors/miner/src/bitfield_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::convert::TryInto;

use cid::Cid;
use fil_actors_runtime::{ActorContext, ActorDowncast, ActorError, Array};
use fil_actors_runtime::{ActorContext, ActorError, Array};
use fvm_ipld_amt::Error as AmtError;
use fvm_ipld_bitfield::BitField;
use fvm_ipld_blockstore::Blockstore;
Expand Down Expand Up @@ -39,13 +39,13 @@ impl<'db, BS: Blockstore> BitFieldQueue<'db, BS> {
let bitfield = self
.amt
.get(epoch)
.map_err(|e| e.downcast_wrap(format!("failed to lookup queue epoch {}", epoch)))?
.with_context(|| format!("failed to lookup queue epoch {}", epoch))?
.cloned()
.unwrap_or_default();

self.amt
.set(epoch, &bitfield | values)
.map_err(|e| e.downcast_wrap(format!("failed to set queue epoch {}", epoch)))?;
.with_context(|| format!("failed to set queue epoch {}", epoch))?;

Ok(())
}
Expand Down
7 changes: 3 additions & 4 deletions actors/miner/src/deadline_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ impl Deadline {
if let Some(&max_partition) = to_remove_set.iter().max() {
if max_partition > partition_count {
return Err(
actor_error!(illegal_argument; "partition index {} out of range [0, {})", max_partition, partition_count).into()
actor_error!(illegal_argument; "partition index {} out of range [0, {})", max_partition, partition_count),
);
}
} else {
Expand All @@ -625,7 +625,7 @@ impl Deadline {
// Should already be checked earlier, but we might as well check again.
if !self.early_terminations.is_empty() {
return Err(
actor_error!(illegal_argument; "cannot remove partitions from deadline with early terminations").into(),
actor_error!(illegal_argument; "cannot remove partitions from deadline with early terminations"),
);
}

Expand Down Expand Up @@ -663,8 +663,7 @@ impl Deadline {
illegal_argument,
"cannot remove partition {}: has unproven sectors",
partition_idx
)
.into());
));
}

// Get the live sectors.
Expand Down
5 changes: 2 additions & 3 deletions actors/miner/src/partition_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl Partition {
})?;

if !live_sectors.contains_all(sector_numbers) {
return Err(actor_error!(illegal_argument, "can only terminate live sectors").into());
return Err(actor_error!(illegal_argument, "can only terminate live sectors"));
}

let sector_infos = sectors.load_sector(sector_numbers)?;
Expand Down Expand Up @@ -733,8 +733,7 @@ impl Partition {
return Err(actor_error!(
illegal_argument,
"skipped faults contains sectors outside partition"
)
.into());
));
}

// Find all skipped faults that have been labeled recovered
Expand Down
18 changes: 6 additions & 12 deletions actors/miner/src/sectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
use std::collections::BTreeSet;

use cid::Cid;
use fil_actors_runtime::{actor_error, ActorDowncast, ActorError, Array};
use fil_actors_runtime::{actor_error, ActorContext, ActorError, Array};
use fvm_ipld_amt::Error as AmtError;
use fvm_ipld_bitfield::BitField;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::error::ExitCode;
use fvm_shared::sector::{SectorNumber, MAX_SECTOR_NUMBER};

use super::SectorOnChainInfo;
Expand Down Expand Up @@ -36,12 +35,7 @@ impl<'db, BS: Blockstore> Sectors<'db, BS> {
let sector_on_chain = self
.amt
.get(sector_number)
.map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to load sector {}", sector_number),
)
})?
.with_context(|| format!("failed to load sector {}", sector_number))?
.cloned()
.ok_or_else(|| actor_error!(not_found; "sector not found: {}", sector_number))?;
sector_infos.push(sector_on_chain);
Expand All @@ -56,7 +50,7 @@ impl<'db, BS: Blockstore> Sectors<'db, BS> {
Ok(self
.amt
.get(sector_number)
.map_err(|e| e.downcast_wrap(format!("failed to get sector {}", sector_number)))?
.with_context(|| format!("failed to get sector {}", sector_number))?
.cloned())
}

Expand All @@ -72,9 +66,9 @@ impl<'db, BS: Blockstore> Sectors<'db, BS> {
));
}

self.amt.set(sector_number, info).map_err(|e| {
e.downcast_wrap(format!("failed to store sector {}", sector_number))
})?;
self.amt
.set(sector_number, info)
.with_context(|| format!("failed to store sector {}", sector_number))?;
}

Ok(())
Expand Down
25 changes: 9 additions & 16 deletions actors/miner/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ impl State {
not_found;
"sector {} not a member of partition {}, deadline {}",
sector_number, partition_idx, deadline_idx
)
.into());
));
}

let faulty = partition.faults.get(sector_number);
Expand Down Expand Up @@ -660,26 +659,23 @@ impl State {
not_found;
"sector {} not a member of partition {}, deadline {}",
sector_number, partition_idx, deadline_idx
)
.into());
));
}

if partition.faults.get(sector_number) {
return Err(actor_error!(
forbidden;
"sector {} not a member of partition {}, deadline {}",
sector_number, partition_idx, deadline_idx
)
.into());
));
}

if partition.terminated.get(sector_number) {
return Err(actor_error!(
not_found;
"sector {} not of partition {}, deadline {} is terminated",
sector_number, partition_idx, deadline_idx
)
.into());
));
}

Ok(())
Expand All @@ -691,7 +687,7 @@ impl State {
store: &BS,
sectors: &BitField,
) -> Result<Vec<SectorOnChainInfo>, ActorError> {
Ok(Sectors::load(store, &self.sectors)?.load_sector(sectors)?)
Sectors::load(store, &self.sectors)?.load_sector(sectors)
}

pub fn load_deadlines<BS: Blockstore>(&self, store: &BS) -> Result<Deadlines, ActorError> {
Expand All @@ -717,12 +713,12 @@ impl State {
&self,
store: &BS,
) -> Result<VestingFunds, ActorError> {
Ok(store
store
.get_cbor(&self.vesting_funds)
.with_context(|| format!("failed to load vesting funds {}", self.vesting_funds))?
.ok_or_else(
|| actor_error!(not_found; "failed to load vesting funds {:?}", self.vesting_funds),
)?)
)
}

/// Saves the vesting table to the store.
Expand Down Expand Up @@ -868,8 +864,7 @@ impl State {
"unlocked balance can not repay fee debt ({} < {})",
unlocked_balance,
self.fee_debt
)
.into());
));
}

Ok(std::mem::take(&mut self.fee_debt))
Expand Down Expand Up @@ -1175,9 +1170,7 @@ impl State {
make_map_with_root_and_bitwidth(&self.pre_committed_sectors, store, HAMT_BIT_WIDTH)?;
for sector_no in sector_nos.iter() {
if sector_no as u64 > MAX_SECTOR_NUMBER {
return Err(
actor_error!(illegal_argument; "sector number greater than maximum").into()
);
return Err(actor_error!(illegal_argument; "sector number greater than maximum"));
}
let info: &SectorPreCommitOnChainInfo =
precommitted
Expand Down
11 changes: 7 additions & 4 deletions actors/runtime/src/actor_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::{fmt::Display, num::TryFromIntError};
use fvm_shared::error::ExitCode;
use thiserror::Error;

use crate::ActorDowncast;

/// The error type returned by actor method calls.
#[derive(Error, Debug, Clone, PartialEq)]
#[error("ActorError(exit_code: {exit_code:?}, msg: {msg})")]
Expand Down Expand Up @@ -232,7 +230,12 @@ impl<T, E: Into<ActorError>> ActorContext<T> for Result<T, E> {
// TODO: remove once the runtime doesn't use anyhow::Result anymore
impl From<anyhow::Error> for ActorError {
fn from(e: anyhow::Error) -> Self {
// THIS DEFAULT IS WRONG, it is just a placeholder
e.downcast_default(ExitCode::USR_ILLEGAL_ARGUMENT, "runtime error")
match e.downcast::<ActorError>() {
Ok(actor_err) => actor_err,
Err(other) => ActorError::unchecked(
ExitCode::USR_ILLEGAL_ARGUMENT,
format!("runtime error: {}", other),
),
}
}
}
8 changes: 4 additions & 4 deletions actors/runtime/src/runtime/actor_blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ impl fvm_ipld_blockstore::Blockstore for ActorBlockstore {

fn get(&self, cid: &Cid) -> Result<Option<Vec<u8>>, Self::Error> {
// If this fails, the _CID_ is invalid. I.e., we have a bug.
fvm::ipld::get(cid).map(Some).map_err(|c| {
actor_error!(illegal_state; "get failed with {:?} on CID '{}'", c, cid).into()
})
fvm::ipld::get(cid)
.map(Some)
.map_err(|c| actor_error!(illegal_state; "get failed with {:?} on CID '{}'", c, cid))
}

fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error> {
let code = Code::try_from(k.hash().code())
.map_err(|e| actor_error!(serialization, e.to_string()))?;
let k2 = self.put(code, &Block::new(k.codec(), block))?;
if k != &k2 {
Err(actor_error!(serialization; "put block with cid {} but has cid {}", k, k2).into())
Err(actor_error!(serialization; "put block with cid {} but has cid {}", k, k2))
} else {
Ok(())
}
Expand Down
88 changes: 0 additions & 88 deletions actors/runtime/src/util/downcast.rs

This file was deleted.

2 changes: 0 additions & 2 deletions actors/runtime/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

pub use self::downcast::*;
pub use self::multimap::{EitherError as MultiMapEitherError, Error as MultiMapError, Multimap};
pub use self::set::Set;
pub use self::set_multimap::SetMultimap;

pub mod cbor;
pub mod chaos;
mod downcast;
mod multimap;
mod set;
mod set_multimap;
Expand Down
2 changes: 1 addition & 1 deletion actors/runtime/src/util/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
// Calls the for each function on the hamt with ignoring the value
self.0.try_for_each(|s, _: &()| f(s)).map_err(|err| match err {
fvm_ipld_hamt::EitherError::User(e) => e,
fvm_ipld_hamt::EitherError::Hamt(e) => e.into(),
fvm_ipld_hamt::EitherError::Hamt(e) => e,
})
}

Expand Down
Loading

0 comments on commit 14289dc

Please sign in to comment.