Skip to content

Commit 6faa394

Browse files
anorthdignifiedquire
authored andcommitted
Traits and impls for adding context and exit codes to results. (filecoin-project#589)
Co-authored-by: dignifiedquire <[email protected]>
1 parent ad6f4f6 commit 6faa394

File tree

2 files changed

+50
-43
lines changed

2 files changed

+50
-43
lines changed

actors/init/src/lib.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ impl Actor {
137137
// Allocate an ID for this actor.
138138
// Store mapping of actor addresses to the actor ID.
139139
let id_address: ActorID = rt.transaction(|s: &mut State, rt| {
140-
s.map_address_to_f4(rt.store(), &robust_address, &delegated_address).map_err(|e| {
141-
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to allocate ID address")
142-
})
140+
s.map_address_to_f4(rt.store(), &robust_address, &delegated_address)
141+
.context("constructor failed")
143142
})?;
144143

145144
// Create an empty actor
@@ -164,42 +163,35 @@ impl Actor {
164163
RT: Runtime<BS>,
165164
{
166165
use cid::multihash::Code;
166+
use fil_actors_runtime::AsActorError;
167167
use fvm_ipld_blockstore::Block;
168+
use fvm_shared::error::ExitCode;
168169

169170
rt.validate_immediate_caller_accept_any()?;
170171

171172
let (code_cid, installed) = rt.transaction(|st: &mut State, rt| {
172173
let code = params.code.bytes();
173-
let code_cid =
174-
rt.store().put(Code::Blake2b256, &Block::new(0x55, code)).map_err(|e| {
175-
e.downcast_default(
176-
ExitCode::USR_SERIALIZATION,
177-
"failed to put code into the bockstore",
178-
)
179-
})?;
180-
181-
if st.is_installed_actor(rt.store(), &code_cid).map_err(|e| {
182-
e.downcast_default(
183-
ExitCode::USR_ILLEGAL_STATE,
184-
"failed to check state for installed actor",
185-
)
186-
})? {
174+
let code_cid = rt.store().put(Code::Blake2b256, &Block::new(0x55, code)).context_code(
175+
ExitCode::USR_SERIALIZATION,
176+
"failed to put code into the bockstore",
177+
)?;
178+
179+
if st.is_installed_actor(rt.store(), &code_cid).context_code(
180+
ExitCode::USR_ILLEGAL_STATE,
181+
"failed to check state for installed actor",
182+
)? {
187183
return Ok((code_cid, false));
188184
}
189185

190-
rt.install_actor(&code_cid).map_err(|e| {
191-
e.downcast_default(
192-
ExitCode::USR_ILLEGAL_ARGUMENT,
193-
"failed to check state for installed actor",
194-
)
195-
})?;
186+
rt.install_actor(&code_cid).context_code(
187+
ExitCode::USR_ILLEGAL_ARGUMENT,
188+
"failed to check state for installed actor",
189+
)?;
196190

197-
st.add_installed_actor(rt.store(), code_cid).map_err(|e| {
198-
e.downcast_default(
199-
ExitCode::USR_ILLEGAL_STATE,
200-
"failed to add installed actor to state",
201-
)
202-
})?;
191+
st.add_installed_actor(rt.store(), code_cid).context_code(
192+
ExitCode::USR_ILLEGAL_STATE,
193+
"failed to add installed actor to state",
194+
)?;
203195
Ok((code_cid, true))
204196
})?;
205197

actors/init/src/state.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use cid::Cid;
5-
use fil_actors_runtime::actor_error;
65
use fil_actors_runtime::{
76
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError, AsActorError,
87
FIRST_NON_SINGLETON_ADDR,
@@ -72,37 +71,45 @@ impl State {
7271
store: &BS,
7372
addr: &Address,
7473
f4addr: &Address,
75-
) -> anyhow::Result<ActorID>
74+
) -> Result<ActorID, ActorError>
7675
where
7776
BS: Blockstore,
7877
{
79-
let mut map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)?;
78+
let mut map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)
79+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load address map")?;
8080

8181
// Assign a new ID address, or use the one currently mapped to the f4 address. We don't
8282
// bother checking if the target actor is an embryo here, the FVM will check that when we go to create the actor.
8383
let f4addr_key = f4addr.to_bytes().into();
84-
let id: u64 = match map.get(&f4addr_key)? {
84+
let id: u64 = match map
85+
.get(&f4addr_key)
86+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to lookup f4 address in map")?
87+
{
8588
Some(id) => *id,
8689
None => {
8790
let id = self.next_id;
8891
self.next_id += 1;
89-
map.set(f4addr_key, id)?;
92+
map.set(f4addr_key, id)
93+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set f4 address in map")?;
9094
id
9195
}
9296
};
9397

9498
// Then go ahead and assign the f2 address.
95-
let is_new = map.set_if_absent(addr.to_bytes().into(), id)?;
99+
let is_new = map
100+
.set_if_absent(addr.to_bytes().into(), id)
101+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set map key")?;
96102
if !is_new {
97103
// this is impossible today as the robust address is a hash of unique inputs
98104
// but in close future predictable address generation will make this possible
99-
return Err(anyhow!(actor_error!(
105+
return Err(actor_error!(
100106
forbidden,
101107
"robust address {} is already allocated in the address map",
102108
addr
103-
)));
109+
));
104110
}
105-
self.address_map = map.flush()?;
111+
self.address_map =
112+
map.flush().context_code(ExitCode::USR_ILLEGAL_STATE, "failed to store address map")?;
106113

107114
Ok(id)
108115
}
@@ -141,8 +148,11 @@ impl State {
141148
&self,
142149
store: &BS,
143150
cid: &Cid,
144-
) -> anyhow::Result<bool> {
145-
let installed: Vec<Cid> = match store.get_cbor(&self.installed_actors)? {
151+
) -> Result<bool, ActorError> {
152+
let installed: Vec<Cid> = match store
153+
.get_cbor(&self.installed_actors)
154+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load installed actor list")?
155+
{
146156
Some(v) => v,
147157
None => Vec::new(),
148158
};
@@ -155,13 +165,18 @@ impl State {
155165
&mut self,
156166
store: &BS,
157167
cid: Cid,
158-
) -> anyhow::Result<()> {
159-
let mut installed: Vec<Cid> = match store.get_cbor(&self.installed_actors)? {
168+
) -> Result<(), ActorError> {
169+
let mut installed: Vec<Cid> = match store
170+
.get_cbor(&self.installed_actors)
171+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load installed actor list")?
172+
{
160173
Some(v) => v,
161174
None => Vec::new(),
162175
};
163176
installed.push(cid);
164-
self.installed_actors = store.put_cbor(&installed, Code::Blake2b256)?;
177+
self.installed_actors = store
178+
.put_cbor(&installed, Code::Blake2b256)
179+
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to save installed actor list")?;
165180
Ok(())
166181
}
167182
}

0 commit comments

Comments
 (0)