Skip to content

Commit

Permalink
feat: add some initialization helpers to the sdk
Browse files Browse the repository at this point in the history
This replaces the custom assert macros. Instead of overriding the assert
macros, the user would:

1. Call `fvm_sdk::initialize()` at the top of their actor/tests.
2. Use the default `assert` macros.

replaces #660
  • Loading branch information
Stebalien committed Sep 19, 2022
1 parent 2705679 commit 9c7aa10
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 76 deletions.
1 change: 0 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ fvm_ipld_encoding = { version = "0.2", path = "../ipld/encoding" }
[features]
default = ["debug"]
debug = ["lazy_static"]
testing = []
m2-native = []
1 change: 1 addition & 0 deletions sdk/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod inner {
}
}
/// Initialize logging if debugging is enabled.
#[inline(always)]
pub fn init_logging() {
if enabled() {
log::set_logger(&Logger).expect("failed to enable logging");
Expand Down
17 changes: 14 additions & 3 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub mod sself;
pub mod sys;
pub mod vm;

#[cfg(feature = "testing")]
pub mod testing;

/// The maximum actor address length (class 2 addresses).
pub const MAX_ACTOR_ADDR_LEN: usize = 21;

Expand All @@ -35,3 +32,17 @@ pub(crate) fn status_code_to_bool(code: i32) -> bool {
/// Error messages don't make it across the boundary, but are logged at the FVM
/// level for debugging and informational purposes.
pub type SyscallResult<T> = core::result::Result<T, fvm_shared::error::ErrorNumber>;

/// Initialize the FVM SDK. Calling this function optional but encouraged.
///
/// At the moment, this will:
///
/// 1. Initialize logging (if "debug mode" is enabled).
/// 2. Setup a panic handler for easier debugging.
///
/// In the future, this may perform additional setup operations, but will never incure more than a
/// minimal runtime cost.
pub fn initialize() {
debug::init_logging();
vm::set_panic_handler();
}
72 changes: 0 additions & 72 deletions sdk/src/testing.rs

This file was deleted.

14 changes: 14 additions & 0 deletions sdk/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ptr;

use fvm_shared::error::ExitCode;
use fvm_shared::sys::out::vm::InvocationContext;

use crate::sys;
Expand Down Expand Up @@ -27,3 +28,16 @@ pub fn abort(code: u32, message: Option<&str>) -> ! {
sys::vm::abort(code, message, message_len as u32);
}
}

/// Sets a panic handler to turn all panics into aborts with `USR_ASSERTION_FAILED`. This should be
/// called early in the actor to improve debuggability.
///
/// NOTE: This will incure a small cost on failure (to format an error message).
pub fn set_panic_handler() {
std::panic::set_hook(Box::new(|info| {
abort(
ExitCode::USR_ASSERTION_FAILED.value(),
Some(&format!("{}", info)),
)
}));
}

0 comments on commit 9c7aa10

Please sign in to comment.