Skip to content

Commit 09c204e

Browse files
prototype abort error
1 parent 5a92754 commit 09c204e

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

actors/account/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use num_derive::FromPrimitive;
99
use num_traits::FromPrimitive;
1010

1111
use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
12-
use fil_actors_runtime::cbor;
1312
use fil_actors_runtime::runtime::{ActorCode, Runtime};
14-
use fil_actors_runtime::{actor_error, ensure_args, ActorError};
13+
use fil_actors_runtime::{actor_error, ensure_args};
14+
use fil_actors_runtime::{cbor, Abort};
1515

1616
pub use self::state::State;
1717

@@ -34,7 +34,7 @@ pub enum Method {
3434
pub struct Actor;
3535
impl Actor {
3636
/// Constructor for Account actor
37-
pub fn constructor<BS, RT>(rt: &mut RT, address: Address) -> Result<(), ActorError>
37+
pub fn constructor<BS, RT>(rt: &mut RT, address: Address) -> Result<(), Abort>
3838
where
3939
BS: Blockstore,
4040
RT: Runtime<BS>,
@@ -50,7 +50,7 @@ impl Actor {
5050
}
5151

5252
// Fetches the pubkey-type address from this actor.
53-
pub fn pubkey_address<BS, RT>(rt: &mut RT) -> Result<Address, ActorError>
53+
pub fn pubkey_address<BS, RT>(rt: &mut RT) -> Result<Address, Abort>
5454
where
5555
BS: Blockstore,
5656
RT: Runtime<BS>,
@@ -66,7 +66,7 @@ impl ActorCode for Actor {
6666
rt: &mut RT,
6767
method: MethodNum,
6868
params: &RawBytes,
69-
) -> Result<RawBytes, ActorError>
69+
) -> Result<RawBytes, Abort>
7070
where
7171
BS: Blockstore,
7272
RT: Runtime<BS>,
@@ -80,7 +80,7 @@ impl ActorCode for Actor {
8080
let addr = Self::pubkey_address(rt)?;
8181
Ok(RawBytes::serialize(addr)?)
8282
}
83-
None => Err(actor_error!(SysErrInvalidMethod; "Invalid method")),
83+
None => Err(actor_error!(SysErrInvalidMethod; "Invalid method").into()),
8484
}
8585
}
8686
}

actors/runtime/src/actor_error.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
use fvm_shared::error::ExitCode;
22
use thiserror::Error;
33

4+
mod abort {
5+
use crate::ActorError;
6+
use fvm_shared::error::ExitCode;
7+
8+
#[derive(thiserror::Error, Debug)]
9+
#[error("abort error")]
10+
pub struct Abort {
11+
/// This ensures that this error can not be crated outside.
12+
_private: (),
13+
}
14+
15+
#[cfg(feature = "fil-actor")]
16+
fn maybe_abort(exit_code: ExitCode, msg: Option<&str>) -> ! {
17+
fvm_sdk::vm::abort(exit_code as u32, msg);
18+
}
19+
#[cfg(not(feature = "fil-actor"))]
20+
fn maybe_abort(exit_code: ExitCode, msg: Option<&str>) -> ! {
21+
// TODO: maybe not panic, needs discussion what we want here
22+
panic!("Abort: {}: {:?}", exit_code, msg);
23+
}
24+
25+
impl From<ActorError> for Abort {
26+
fn from(err: ActorError) -> Self {
27+
let ActorError { exit_code, msg } = err;
28+
maybe_abort(exit_code, Some(&msg));
29+
}
30+
}
31+
32+
/// Converts a raw encoding error into an ErrSerialization.
33+
impl From<fvm_ipld_encoding::Error> for Abort {
34+
fn from(e: fvm_ipld_encoding::Error) -> Self {
35+
maybe_abort(ExitCode::ErrSerialization, Some(&e.to_string()));
36+
}
37+
}
38+
}
39+
40+
pub use abort::Abort;
41+
442
/// TODO fix error system; actor errors should be transparent to the VM.
543
/// The error type that gets returned by actor method calls.
644
#[derive(Error, Debug, Clone, PartialEq)]
@@ -114,17 +152,17 @@ macro_rules! actor_error {
114152
macro_rules! ensure {
115153
($cond:expr, $code:ident, $msg:literal $(,)?) => {
116154
if !$cond {
117-
return Err($crate::actor_error!($code, $msg));
155+
return Err($crate::actor_error!($code, $msg).into());
118156
}
119157
};
120158
($cond:expr, $code:ident, $err:expr $(,)?) => {
121159
if !$cond {
122-
return Err($crate::actor_error!($code, $err));
160+
return Err($crate::actor_error!($code, $err).into());
123161
}
124162
};
125163
($cond:expr, $code:ident, $fmt:expr, $($arg:tt)*) => {
126164
if !$cond {
127-
return Err($crate::actor_error!($code, $fmt, $($arg)*));
165+
return Err($crate::actor_error!($code, $fmt, $($arg)*).into());
128166
}
129167
};
130168
}

actors/runtime/src/runtime/actor_code.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use fvm_ipld_blockstore::Blockstore;
55
use fvm_ipld_encoding::RawBytes;
66
use fvm_shared::MethodNum;
77

8-
use crate::{ActorError, Runtime};
8+
use crate::{Abort, Runtime};
99

1010
/// Interface for invoking methods on an Actor
1111
pub trait ActorCode {
@@ -15,7 +15,7 @@ pub trait ActorCode {
1515
rt: &mut RT,
1616
method: MethodNum,
1717
params: &RawBytes,
18-
) -> Result<RawBytes, ActorError>
18+
) -> Result<RawBytes, Abort>
1919
where
2020
// TODO: remove the clone requirement on the blockstore when we fix "replica update" to not
2121
// hold onto state between transactions.

actors/runtime/src/runtime/fvm.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ pub fn trampoline<C: ActorCode>(params: u32) -> u32 {
430430
// Construct a new runtime.
431431
let mut rt = FvmRuntime::default();
432432
// Invoke the method, aborting if the actor returns an errored exit code.
433-
let ret = C::invoke_method(&mut rt, method, &params)
434-
.unwrap_or_else(|err| fvm::vm::abort(err.exit_code() as u32, Some(err.msg())));
433+
434+
// can unwrap because `Abort` will already have called rt::abort on an error.
435+
let ret = C::invoke_method(&mut rt, method, &params).unwrap();
435436

436437
// Abort with "illegal actor" if the actor failed to validate the caller somewhere.
437438
// We do this after handling the error, because the actor may have encountered an error before

actors/runtime/src/test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ impl MockRuntime {
450450
self.state = prev_state;
451451
}
452452
self.in_call = false;
453-
res
453+
// res
454+
todo!()
454455
}
455456

456457
pub fn verify(&mut self) {

actors/runtime/src/util/chaos/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use state::*;
1414
pub use types::*;
1515

1616
use crate::runtime::{ActorCode, Runtime};
17-
use crate::{actor_error, cbor, ActorError};
17+
use crate::{actor_error, cbor, Abort, ActorError};
1818

1919
mod state;
2020
mod types;
@@ -197,7 +197,7 @@ impl ActorCode for Actor {
197197
rt: &mut RT,
198198
method: MethodNum,
199199
params: &RawBytes,
200-
) -> Result<RawBytes, ActorError>
200+
) -> Result<RawBytes, Abort>
201201
where
202202
BS: Blockstore,
203203
RT: Runtime<BS>,
@@ -246,7 +246,7 @@ impl ActorCode for Actor {
246246
Ok(RawBytes::serialize(inspect)?)
247247
}
248248

249-
None => Err(actor_error!(SysErrInvalidMethod; "Invalid method")),
249+
None => Err(actor_error!(SysErrInvalidMethod; "Invalid method").into()),
250250
}
251251
}
252252
}

0 commit comments

Comments
 (0)