Skip to content

Commit

Permalink
refactor to use apply exit codes in the actor directly
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Apr 28, 2022
1 parent 0e32a83 commit 4642af3
Show file tree
Hide file tree
Showing 29 changed files with 1,112 additions and 754 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions actors/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::{Address, Protocol};
use fvm_shared::error::ExitCode;
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
use fil_actors_runtime::cbor;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_error, ActorError};
use fil_actors_runtime::{cbor, ActorContext2};

pub use self::state::State;

Expand Down Expand Up @@ -80,7 +81,7 @@ impl ActorCode for Actor {
}
Some(Method::PubkeyAddress) => {
let addr = Self::pubkey_address(rt)?;
Ok(RawBytes::serialize(addr)?)
Ok(RawBytes::serialize(addr).exit_code(ExitCode::USR_ILLEGAL_STATE)?)
}
None => Err(actor_error!(unhandled_message; "Invalid method")),
}
Expand Down
10 changes: 5 additions & 5 deletions actors/init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

use cid::Cid;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_error, cbor, ActorContext, ActorError, SYSTEM_ACTOR_ADDR};
use fil_actors_runtime::{actor_error, cbor, ActorContext2, ActorError, SYSTEM_ACTOR_ADDR};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::actor::builtin::Type;
use fvm_shared::address::Address;
use fvm_shared::error::ExitCode;
use fvm_shared::{ActorID, MethodNum, METHOD_CONSTRUCTOR};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -42,8 +43,7 @@ impl Actor {
{
let sys_ref: &Address = &SYSTEM_ACTOR_ADDR;
rt.validate_immediate_caller_is(std::iter::once(sys_ref))?;
let state = State::new(rt.store(), params.network_name)
.context("failed to construct init actor state")?;
let state = State::new(rt.store(), params.network_name)?;

rt.create(&state)?;

Expand Down Expand Up @@ -85,7 +85,7 @@ impl Actor {
// Store mapping of pubkey or actor address to actor ID
let id_address: ActorID = rt.transaction(|s: &mut State, rt| {
s.map_address_to_new_id(rt.store(), &robust_address)
.context("failed to allocate ID address")
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to allocate ID address")
})?;

// Create an empty actor
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ActorCode for Actor {
}
Some(Method::Exec) => {
let res = Self::exec(rt, cbor::deserialize_params(params)?)?;
Ok(RawBytes::serialize(res)?)
Ok(RawBytes::serialize(res).exit_code(ExitCode::USR_ILLEGAL_STATE)?)
}
None => Err(actor_error!(unhandled_message; "Invalid method")),
}
Expand Down
16 changes: 11 additions & 5 deletions actors/init/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::Cid;
use fil_actors_runtime::ActorError;
use fil_actors_runtime::{
make_empty_map, make_map_with_root_and_bitwidth, FIRST_NON_SINGLETON_ADDR,
make_empty_map, make_map_with_root_and_bitwidth, ActorContext2, FIRST_NON_SINGLETON_ADDR,
};
use fil_actors_runtime::{ActorContext, ActorError};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_ipld_encoding::Cbor;
use fvm_ipld_hamt::Error as HamtError;
use fvm_shared::address::{Address, Protocol};
use fvm_shared::error::ExitCode;
use fvm_shared::{ActorID, HAMT_BIT_WIDTH};

/// State is reponsible for creating
Expand All @@ -25,7 +26,7 @@ impl State {
pub fn new<BS: Blockstore>(store: &BS, network_name: String) -> Result<Self, ActorError> {
let empty_map = make_empty_map::<_, ()>(store, HAMT_BIT_WIDTH)
.flush()
.context("failed to create empty map")?;
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to create empty map")?;

Ok(Self { address_map: empty_map, next_id: FIRST_NON_SINGLETON_ADDR, network_name })
}
Expand Down Expand Up @@ -66,9 +67,14 @@ impl State {
return Ok(Some(*addr));
}

let map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)?;
let map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)
.exit_code(ExitCode::USR_ILLEGAL_STATE)?;

Ok(map.get(&addr.to_bytes())?.copied().map(Address::new_id))
Ok(map
.get(&addr.to_bytes())
.exit_code(ExitCode::USR_ILLEGAL_STATE)?
.copied()
.map(Address::new_id))
}
}

Expand Down
16 changes: 9 additions & 7 deletions actors/market/src/balance_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_hamt::Error as HamtError;
use fvm_shared::address::Address;
use fvm_shared::bigint::bigint_ser::BigIntDe;
use fvm_shared::econ::TokenAmount;
use fvm_shared::{address::Address, error::ExitCode};
use num_traits::{Signed, Zero};

use fil_actors_runtime::{
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError, Map,
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorContext2, ActorError, Map,
};

pub const BALANCE_TABLE_BITWIDTH: u32 = 6;
Expand Down Expand Up @@ -47,7 +47,7 @@ where

/// Adds token amount to previously initialized account.
pub fn add(&mut self, key: &Address, value: &TokenAmount) -> Result<(), ActorError> {
let prev = self.get(key)?;
let prev = self.get(key).exit_code(ExitCode::USR_SERIALIZATION)?;
let sum = &prev + value;
if sum.is_negative() {
return Err(actor_error!(
Expand All @@ -57,10 +57,12 @@ where
));
}
if sum.is_zero() && !prev.is_zero() {
self.0.delete(&key.to_bytes())?;
self.0.delete(&key.to_bytes()).exit_code(ExitCode::USR_SERIALIZATION)?;
Ok(())
} else {
self.0.set(key.to_bytes().into(), BigIntDe(sum))?;
self.0
.set(key.to_bytes().into(), BigIntDe(sum))
.exit_code(ExitCode::USR_SERIALIZATION)?;
Ok(())
}
}
Expand All @@ -74,7 +76,7 @@ where
req: &TokenAmount,
floor: &TokenAmount,
) -> Result<TokenAmount, ActorError> {
let prev = self.get(key)?;
let prev = self.get(key).exit_code(ExitCode::USR_SERIALIZATION)?;
let available = std::cmp::max(TokenAmount::zero(), prev - floor);
let sub: TokenAmount = std::cmp::min(&available, req).clone();

Expand All @@ -87,7 +89,7 @@ where

/// Subtracts value from a balance, and errors if full amount was not substracted.
pub fn must_subtract(&mut self, key: &Address, req: &TokenAmount) -> Result<(), ActorError> {
let prev = self.get(key)?;
let prev = self.get(key).exit_code(ExitCode::USR_SERIALIZATION)?;

if req > &prev {
return Err(actor_error!(illegal_argument, "couldn't subtract the requested amount"));
Expand Down
Loading

0 comments on commit 4642af3

Please sign in to comment.