Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some initialization helpers to the sdk #896

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
)
}));
}