Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another attempt at error cleanup that preserves more exit codes #1338

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actors/datacap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use num_derive::FromPrimitive;

use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, extract_send_result, ActorContext, ActorError, AsActorError,
actor_dispatch, actor_error, extract_send_result, ActorResult, ActorError, AsActorError,
SYSTEM_ACTOR_ADDR,
};
use fvm_ipld_encoding::ipld_block::IpldBlock;
Expand Down
4 changes: 3 additions & 1 deletion actors/datacap/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use frc46_token::token;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_shared::ActorID;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::ActorID;

use fil_actors_runtime::{ActorError, AsActorError};

Expand All @@ -17,6 +17,7 @@ pub struct State {
impl State {
pub fn new<BS: Blockstore>(store: &BS, governor: Address) -> Result<State, ActorError> {
let token_state = token::state::TokenState::new(store)
.map_err(|e| e.to_string())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to create token state")?;
Ok(State { governor, token: token_state })
}
Expand All @@ -29,6 +30,7 @@ impl State {
) -> Result<TokenAmount, ActorError> {
self.token
.get_balance(bs, owner)
.map_err(|e| e.to_string())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get balance")
}
}
39 changes: 17 additions & 22 deletions actors/eam/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
use std::iter;

use fil_actors_evm_shared::address::EthAddress;
use fvm_ipld_encoding::{RawBytes, strict_bytes, tuple::*};
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::{ActorID, error::ExitCode, METHOD_CONSTRUCTOR, sys::SendFlags};
use fvm_shared::address::{Address, Payload};
use fvm_shared::crypto::hash::SupportedHashes;
use num_derive::FromPrimitive;
use num_traits::Zero;
use serde::{Deserialize, Serialize};

use ext::{
account::PUBKEY_ADDRESS_METHOD,
evm::RESURRECT_METHOD,
init::{Exec4Params, Exec4Return},
};
use fil_actors_runtime::{
actor_dispatch_unrestricted, actor_error, deserialize_block, extract_send_result, ActorError,
AsActorError, EAM_ACTOR_ID, INIT_ACTOR_ADDR, SYSTEM_ACTOR_ADDR,
};

use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::{error::ExitCode, sys::SendFlags, ActorID, METHOD_CONSTRUCTOR};
use serde::{Deserialize, Serialize};

pub mod ext;

use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_evm_shared::address::EthAddress;
use fil_actors_runtime::{actor_dispatch_unrestricted, actor_error, ActorError, ActorResult, AsActorError, deserialize_block, EAM_ACTOR_ID, extract_send_result, INIT_ACTOR_ADDR, SYSTEM_ACTOR_ADDR};
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::runtime::builtins::Type;

use fvm_ipld_encoding::{strict_bytes, tuple::*, RawBytes};
use fvm_shared::address::{Address, Payload};
use fvm_shared::crypto::hash::SupportedHashes;
use num_derive::FromPrimitive;
pub mod ext;

#[cfg(feature = "fil-actor")]
fil_actors_runtime::wasm_trampoline!(EamActor);
Expand Down Expand Up @@ -175,8 +169,8 @@ fn create_actor(
fn resolve_eth_address(rt: &impl Runtime, actor_id: ActorID) -> Result<EthAddress, ActorError> {
match rt.lookup_delegated_address(actor_id).map(|a| *a.payload()) {
Some(Payload::Delegated(addr)) if addr.namespace() == EAM_ACTOR_ID => Ok(EthAddress(
addr.subaddress()
.try_into()
std::convert::TryInto::<[u8; 20]>::try_into(addr.subaddress())
.map_err(|e| e.to_string())
.context_code(ExitCode::USR_FORBIDDEN, "caller's eth address isn't valid")?,
)),
_ => Err(actor_error!(forbidden; "caller doesn't have an eth address")),
Expand All @@ -198,8 +192,8 @@ fn resolve_caller_external(rt: &impl Runtime) -> Result<(EthAddress, EthAddress)
None,
SendFlags::READ_ONLY,
)
.context_code(
ExitCode::USR_ASSERTION_FAILED,
.map_err(ActorError::from)
.context(
"account failed to return its key address",
)?;

Expand Down Expand Up @@ -305,9 +299,10 @@ impl ActorCode for EamActor {

#[cfg(test)]
mod test {
use fil_actors_runtime::test_utils::MockRuntime;
use fvm_shared::error::ExitCode;

use fil_actors_runtime::test_utils::MockRuntime;

use crate::compute_address_create2;

use super::{compute_address_create, create_actor, EthAddress};
Expand Down
13 changes: 8 additions & 5 deletions actors/evm/src/interpreter/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub fn returndatacopy(
) -> Result<(), ActorError> {
let region = get_memory_region(&mut state.memory, mem_index, size)?;

let src: usize = input_index
.try_into()
let src: usize = <U256 as TryInto<usize>>::try_into(input_index)
.map_err(|e| e.to_string())
.context_code(EVM_CONTRACT_ILLEGAL_MEMORY_ACCESS, "returndatacopy index exceeds max u32")?;
if src > state.return_data.len() {
return Err(ActorError::unchecked(
Expand Down Expand Up @@ -127,7 +127,9 @@ pub fn returndatacopy(

#[inline]
pub fn jump(bytecode: &Bytecode, _pc: usize, dest: U256) -> Result<usize, ActorError> {
let dst = dest.try_into().context_code(EVM_CONTRACT_BAD_JUMPDEST, "jumpdest exceeds u32")?;
let dst: usize = <U256 as TryInto<usize>>::try_into(dest)
.map_err(|e| e.to_string())
.context_code(EVM_CONTRACT_BAD_JUMPDEST, "jumpdest exceeds u32")?;
if !bytecode.valid_jump_destination(dst) {
return Err(ActorError::unchecked(
EVM_CONTRACT_BAD_JUMPDEST,
Expand All @@ -141,8 +143,9 @@ pub fn jump(bytecode: &Bytecode, _pc: usize, dest: U256) -> Result<usize, ActorE
#[inline]
pub fn jumpi(bytecode: &Bytecode, pc: usize, dest: U256, test: U256) -> Result<usize, ActorError> {
if !test.is_zero() {
let dst =
dest.try_into().context_code(EVM_CONTRACT_BAD_JUMPDEST, "jumpdest exceeds u32")?;
let dst = <U256 as TryInto<usize>>::try_into(dest)
.map_err(|e| e.to_string())
.context_code(EVM_CONTRACT_BAD_JUMPDEST, "jumpdest exceeds u32")?;
if !bytecode.valid_jump_destination(dst) {
return Err(ActorError::unchecked(
EVM_CONTRACT_BAD_JUMPDEST,
Expand Down
2 changes: 1 addition & 1 deletion actors/init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::runtime::{ActorCode, Runtime};

use fil_actors_runtime::{
actor_dispatch, actor_error, extract_send_result, ActorContext, ActorError, AsActorError,
actor_dispatch, actor_error, extract_send_result, ActorResult, ActorError, AsActorError,
EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR,
};
use fvm_shared::address::Address;
Expand Down
15 changes: 7 additions & 8 deletions actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use fil_actors_runtime::cbor::{deserialize, serialize};
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::runtime::{ActorCode, Policy, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, deserialize_block, ActorContext, ActorDowncast, ActorError,
actor_dispatch, actor_error, deserialize_block, ActorDowncast, ActorError, ActorResult,
AsActorError, BURNT_FUNDS_ACTOR_ADDR, CRON_ACTOR_ADDR, DATACAP_TOKEN_ACTOR_ADDR,
REWARD_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, VERIFIED_REGISTRY_ACTOR_ADDR,
};
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Actor {
deal.proposal.client = Address::new_id(client_id);

let serialized_proposal = serialize(&deal.proposal, "normalized deal proposal")
.context_code(ExitCode::USR_SERIALIZATION, "failed to serialize")?;
.context("failed to serialize")?;
let pcid = rt_serialized_deal_cid(rt, &serialized_proposal).map_err(
|e| actor_error!(illegal_argument; "failed to take cid of proposal {}: {}", di, e),
)?;
Expand All @@ -359,7 +359,7 @@ impl Actor {
if deal.proposal.verified_deal {
let remaining_datacap = match client_datacap_remaining.get(&client_id).cloned() {
None => balance_of(rt, &Address::new_id(client_id))
.with_context_code(ExitCode::USR_NOT_FOUND, || {
.with_override_code(ExitCode::USR_NOT_FOUND, || {
format!("failed to get datacap balance for client {}", client_id)
})?,
Some(client_data) => client_data,
Expand Down Expand Up @@ -394,10 +394,9 @@ impl Actor {
let params = datacap_transfer_request(&Address::new_id(*client_id), reqs)?;
// A datacap transfer is all-or-nothing.
// We expect it to succeed because we checked the client's balance earlier.
let alloc_ids = transfer_from(rt, params)
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to transfer datacap from client {}", *client_id)
})?;
let alloc_ids = transfer_from(rt, params).with_context(|| {
format!("failed to transfer datacap from client {}", *client_id)
})?;
if alloc_ids.len() != cids_and_reqs.len() {
return Err(
actor_error!(illegal_state; "datacap transfer returned {} allocation IDs for {} requests",
Expand Down Expand Up @@ -481,7 +480,7 @@ impl Actor {
})?,
TokenAmount::zero(),
))
.with_context_code(ExitCode::USR_ILLEGAL_ARGUMENT, || {
.with_context(|| {
format!("failed to notify deal with proposal cid {}", valid_deal.cid)
})?;
}
Expand Down
31 changes: 14 additions & 17 deletions actors/market/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::balance_table::BalanceTable;
use crate::ext::verifreg::AllocationID;
use cid::Cid;
use fil_actors_runtime::{
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError, Array, AsActorError,
Set, SetMultimap,
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorResult, ActorError, Array,
AsActorError, Set, SetMultimap,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
Expand Down Expand Up @@ -670,10 +670,7 @@ impl State {

// Unlock remaining storage fee
self.unlock_balance(store, &deal.client, &payment_remaining, Reason::ClientStorageFee)
.context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to unlock remaining client storage fee",
)?;
.context("failed to unlock remaining client storage fee")?;

// Unlock client collateral
self.unlock_balance(
Expand All @@ -682,12 +679,12 @@ impl State {
&deal.client_collateral,
Reason::ClientCollateral,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to unlock client collateral")?;
.context("failed to unlock client collateral")?;

// slash provider collateral
let slashed = deal.provider_collateral.clone();
self.slash_balance(store, &deal.provider, &slashed, Reason::ProviderCollateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "slashing balance")?;
.context("slashing balance")?;

return Ok((slashed, true));
}
Expand Down Expand Up @@ -716,20 +713,20 @@ impl State {
&deal.total_storage_fee(),
Reason::ClientStorageFee,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failure unlocking client storage fee")?;
.context("failure unlocking client storage fee")?;

self.unlock_balance(store, &deal.client, &deal.client_collateral, Reason::ClientCollateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failure unlocking client collateral")?;
.context("failure unlocking client collateral")?;

let amount_slashed =
collateral_penalty_for_deal_activation_missed(deal.provider_collateral.clone());
let amount_remaining = deal.provider_balance_requirement() - &amount_slashed;

self.slash_balance(store, &deal.provider, &amount_slashed, Reason::ProviderCollateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to slash balance")?;
.context("failed to slash balance")?;

self.unlock_balance(store, &deal.provider, &amount_remaining, Reason::ProviderCollateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to unlock deal provider balance")?;
.context("failed to unlock deal provider balance")?;

Ok(amount_slashed)
}
Expand All @@ -754,10 +751,10 @@ impl State {
&deal.provider_collateral,
Reason::ProviderCollateral,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed unlocking deal provider balance")?;
.context("failed unlocking deal provider balance")?;

self.unlock_balance(store, &deal.client, &deal.client_collateral, Reason::ClientCollateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed unlocking deal client balance")?;
.context("failed unlocking deal client balance")?;

Ok(())
}
Expand Down Expand Up @@ -849,10 +846,10 @@ impl State {
BS: Blockstore,
{
self.maybe_lock_balance(store, &proposal.client, &proposal.client_balance_requirement())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to lock client funds")?;
.context("failed to lock client funds")?;

self.maybe_lock_balance(store, &proposal.provider, &proposal.provider_collateral)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to lock provider funds")?;
.context("failed to lock provider funds")?;

self.total_client_locked_collateral += &proposal.client_collateral;

Expand Down Expand Up @@ -927,7 +924,7 @@ impl State {
.context_code(ExitCode::USR_ILLEGAL_STATE, "subtract from escrow")?;

self.unlock_balance(store, from_addr, amount, Reason::ClientStorageFee)
.context_code(ExitCode::USR_ILLEGAL_STATE, "subtract from locked")?;
.context("subtract from locked")?;

// Add subtracted amount to the recipient
escrow_table
Expand Down
2 changes: 1 addition & 1 deletion actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use fil_actors_runtime::cbor::{serialize, serialize_vec};
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::runtime::{ActorCode, DomainSeparationTag, Policy, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, deserialize_block, extract_send_result, ActorContext,
actor_dispatch, actor_error, deserialize_block, extract_send_result, ActorResult,
ActorDowncast, ActorError, AsActorError, BatchReturn, BURNT_FUNDS_ACTOR_ADDR, INIT_ACTOR_ADDR,
REWARD_ACTOR_ADDR, STORAGE_MARKET_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR,
VERIFIED_REGISTRY_ACTOR_ADDR,
Expand Down
2 changes: 1 addition & 1 deletion actors/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use fil_actors_runtime::cbor::serialize_vec;
use fil_actors_runtime::runtime::{ActorCode, Primitives, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, extract_send_result, make_empty_map, make_map_with_root,
resolve_to_actor_id, ActorContext, ActorError, AsActorError, Map, INIT_ACTOR_ADDR,
resolve_to_actor_id, ActorResult, ActorError, AsActorError, Map, INIT_ACTOR_ADDR,
};

pub use self::state::*;
Expand Down
2 changes: 1 addition & 1 deletion actors/paych/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, deserialize_block, extract_send_result, resolve_to_actor_id,
ActorContext, ActorDowncast, ActorError, Array,
ActorResult, ActorDowncast, ActorError, Array,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::CBOR;
Expand Down
2 changes: 1 addition & 1 deletion actors/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use num_derive::FromPrimitive;

use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, ActorContext, ActorError, AsActorError, SYSTEM_ACTOR_ADDR,
actor_dispatch, actor_error, ActorResult, ActorError, AsActorError, SYSTEM_ACTOR_ADDR,
};

#[cfg(feature = "fil-actor")]
Expand Down
2 changes: 1 addition & 1 deletion actors/verifreg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use fil_actors_runtime::{
Map, DATACAP_TOKEN_ACTOR_ADDR, STORAGE_MARKET_ACTOR_ADDR, SYSTEM_ACTOR_ADDR,
VERIFIED_REGISTRY_ACTOR_ADDR,
};
use fil_actors_runtime::{ActorContext, AsActorError, BatchReturnGen};
use fil_actors_runtime::{ActorResult, AsActorError, BatchReturnGen};
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::sys::SendFlags;

Expand Down
Loading