Skip to content

Commit 4642af3

Browse files
refactor to use apply exit codes in the actor directly
1 parent 0e32a83 commit 4642af3

File tree

29 files changed

+1112
-754
lines changed

29 files changed

+1112
-754
lines changed

Cargo.lock

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/account/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
use fvm_ipld_blockstore::Blockstore;
55
use fvm_ipld_encoding::RawBytes;
66
use fvm_shared::address::{Address, Protocol};
7+
use fvm_shared::error::ExitCode;
78
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
89
use num_derive::FromPrimitive;
910
use num_traits::FromPrimitive;
1011

1112
use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
12-
use fil_actors_runtime::cbor;
1313
use fil_actors_runtime::runtime::{ActorCode, Runtime};
1414
use fil_actors_runtime::{actor_error, ActorError};
15+
use fil_actors_runtime::{cbor, ActorContext2};
1516

1617
pub use self::state::State;
1718

@@ -80,7 +81,7 @@ impl ActorCode for Actor {
8081
}
8182
Some(Method::PubkeyAddress) => {
8283
let addr = Self::pubkey_address(rt)?;
83-
Ok(RawBytes::serialize(addr)?)
84+
Ok(RawBytes::serialize(addr).exit_code(ExitCode::USR_ILLEGAL_STATE)?)
8485
}
8586
None => Err(actor_error!(unhandled_message; "Invalid method")),
8687
}

actors/init/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
use cid::Cid;
55
use fil_actors_runtime::runtime::{ActorCode, Runtime};
6-
use fil_actors_runtime::{actor_error, cbor, ActorContext, ActorError, SYSTEM_ACTOR_ADDR};
6+
use fil_actors_runtime::{actor_error, cbor, ActorContext2, ActorError, SYSTEM_ACTOR_ADDR};
77
use fvm_ipld_blockstore::Blockstore;
88
use fvm_ipld_encoding::RawBytes;
99
use fvm_shared::actor::builtin::Type;
1010
use fvm_shared::address::Address;
11+
use fvm_shared::error::ExitCode;
1112
use fvm_shared::{ActorID, MethodNum, METHOD_CONSTRUCTOR};
1213
use num_derive::FromPrimitive;
1314
use num_traits::FromPrimitive;
@@ -42,8 +43,7 @@ impl Actor {
4243
{
4344
let sys_ref: &Address = &SYSTEM_ACTOR_ADDR;
4445
rt.validate_immediate_caller_is(std::iter::once(sys_ref))?;
45-
let state = State::new(rt.store(), params.network_name)
46-
.context("failed to construct init actor state")?;
46+
let state = State::new(rt.store(), params.network_name)?;
4747

4848
rt.create(&state)?;
4949

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

9191
// Create an empty actor
@@ -121,7 +121,7 @@ impl ActorCode for Actor {
121121
}
122122
Some(Method::Exec) => {
123123
let res = Self::exec(rt, cbor::deserialize_params(params)?)?;
124-
Ok(RawBytes::serialize(res)?)
124+
Ok(RawBytes::serialize(res).exit_code(ExitCode::USR_ILLEGAL_STATE)?)
125125
}
126126
None => Err(actor_error!(unhandled_message; "Invalid method")),
127127
}

actors/init/src/state.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use cid::Cid;
5+
use fil_actors_runtime::ActorError;
56
use fil_actors_runtime::{
6-
make_empty_map, make_map_with_root_and_bitwidth, FIRST_NON_SINGLETON_ADDR,
7+
make_empty_map, make_map_with_root_and_bitwidth, ActorContext2, FIRST_NON_SINGLETON_ADDR,
78
};
8-
use fil_actors_runtime::{ActorContext, ActorError};
99
use fvm_ipld_blockstore::Blockstore;
1010
use fvm_ipld_encoding::tuple::*;
1111
use fvm_ipld_encoding::Cbor;
1212
use fvm_ipld_hamt::Error as HamtError;
1313
use fvm_shared::address::{Address, Protocol};
14+
use fvm_shared::error::ExitCode;
1415
use fvm_shared::{ActorID, HAMT_BIT_WIDTH};
1516

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

3031
Ok(Self { address_map: empty_map, next_id: FIRST_NON_SINGLETON_ADDR, network_name })
3132
}
@@ -66,9 +67,14 @@ impl State {
6667
return Ok(Some(*addr));
6768
}
6869

69-
let map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)?;
70+
let map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)
71+
.exit_code(ExitCode::USR_ILLEGAL_STATE)?;
7072

71-
Ok(map.get(&addr.to_bytes())?.copied().map(Address::new_id))
73+
Ok(map
74+
.get(&addr.to_bytes())
75+
.exit_code(ExitCode::USR_ILLEGAL_STATE)?
76+
.copied()
77+
.map(Address::new_id))
7278
}
7379
}
7480

actors/market/src/balance_table.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
use cid::Cid;
55
use fvm_ipld_blockstore::Blockstore;
66
use fvm_ipld_hamt::Error as HamtError;
7-
use fvm_shared::address::Address;
87
use fvm_shared::bigint::bigint_ser::BigIntDe;
98
use fvm_shared::econ::TokenAmount;
9+
use fvm_shared::{address::Address, error::ExitCode};
1010
use num_traits::{Signed, Zero};
1111

1212
use fil_actors_runtime::{
13-
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError, Map,
13+
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorContext2, ActorError, Map,
1414
};
1515

1616
pub const BALANCE_TABLE_BITWIDTH: u32 = 6;
@@ -47,7 +47,7 @@ where
4747

4848
/// Adds token amount to previously initialized account.
4949
pub fn add(&mut self, key: &Address, value: &TokenAmount) -> Result<(), ActorError> {
50-
let prev = self.get(key)?;
50+
let prev = self.get(key).exit_code(ExitCode::USR_SERIALIZATION)?;
5151
let sum = &prev + value;
5252
if sum.is_negative() {
5353
return Err(actor_error!(
@@ -57,10 +57,12 @@ where
5757
));
5858
}
5959
if sum.is_zero() && !prev.is_zero() {
60-
self.0.delete(&key.to_bytes())?;
60+
self.0.delete(&key.to_bytes()).exit_code(ExitCode::USR_SERIALIZATION)?;
6161
Ok(())
6262
} else {
63-
self.0.set(key.to_bytes().into(), BigIntDe(sum))?;
63+
self.0
64+
.set(key.to_bytes().into(), BigIntDe(sum))
65+
.exit_code(ExitCode::USR_SERIALIZATION)?;
6466
Ok(())
6567
}
6668
}
@@ -74,7 +76,7 @@ where
7476
req: &TokenAmount,
7577
floor: &TokenAmount,
7678
) -> Result<TokenAmount, ActorError> {
77-
let prev = self.get(key)?;
79+
let prev = self.get(key).exit_code(ExitCode::USR_SERIALIZATION)?;
7880
let available = std::cmp::max(TokenAmount::zero(), prev - floor);
7981
let sub: TokenAmount = std::cmp::min(&available, req).clone();
8082

@@ -87,7 +89,7 @@ where
8789

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

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

0 commit comments

Comments
 (0)