Skip to content
Draft
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
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ To use the `entrypoint!` macro, use the following in your entrypoint definition:
use pinocchio::{
account::AccountView,
entrypoint,
msg,
ProgramResult,
Address
};
Expand All @@ -82,7 +81,6 @@ pub fn process_instruction(
accounts: &[AccountView],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Hello from my program!");
Ok(())
}
```
Expand Down Expand Up @@ -114,7 +112,6 @@ use pinocchio::{
default_panic_handler,
entrypoint::InstructionContext,
lazy_program_entrypoint,
msg,
ProgramResult
};

Expand All @@ -125,7 +122,6 @@ default_panic_handler!();
pub fn process_instruction(
mut context: InstructionContext
) -> ProgramResult {
msg!("Hello from my lazy program!");
Ok(())
}
```
Expand All @@ -149,7 +145,6 @@ To use the `no_allocator!` macro, use the following in your entrypoint definitio
use pinocchio::{
account::AccountView,
default_panic_handler,
msg,
no_allocator,
program_entrypoint,
ProgramResult,
Expand All @@ -165,7 +160,6 @@ pub fn process_instruction(
accounts: &[AccountView],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Hello from `no_std` program!");
Ok(())
}
```
Expand All @@ -174,13 +168,15 @@ pub fn process_instruction(

## Crate feature: `std`

By default, `pinocchio` is a `no_std` crate. This means that it does not use any code from the standard (`std`) library. While this does not affect how `pinocchio` is used, there is one particular apparent difference. In a `no_std` environment, the `msg!` macro does not provide any formatting options since the `format!` macro requires the `std` library. In order to use `msg!` with formatting, the `std` feature should be enabled when adding `pinocchio` as a dependency:
By default, Pinocchio is a `no_std` crate. This means that it does not use any code from the
standard (`std`) library. While this does not affect how Pinocchio is used, there is a one
particular apparent difference. Helpers that need to allocate memory, such as fetching `SlotHashes`
sysvar data, are not available. To enable these helpers, the `std` feature must be enabled when adding
Pinocchio as a dependency:
```
pinocchio = { version = "0.7.0", features = ["std"] }
pinocchio = { version = "0.10.0", features = ["std"] }
```

Instead of enabling the `std` feature to be able to format log messages with `msg!`, it is recommended to use the [`pinocchio-log`](https://crates.io/crates/pinocchio-log) crate. This crate provides a lightweight `log!` macro with better compute units consumption than the standard `format!` macro without requiring the `std` library.

## Advance entrypoint configuration

The symbols emitted by the entrypoint macros — program entrypoint, global allocator and default panic handler — can only be defined once globally. If the program crate is also intended to be used as a library, it is common practice to define a Cargo [feature](https://doc.rust-lang.org/cargo/reference/features.html) in your program crate to conditionally enable the module that includes the `entrypoint!` macro invocation. The convention is to name the feature `bpf-entrypoint`.
Expand All @@ -191,7 +187,6 @@ mod entrypoint {
use pinocchio::{
account::AccountView,
entrypoint,
msg,
ProgramResult,
Address
};
Expand All @@ -203,7 +198,6 @@ mod entrypoint {
accounts: &[AccountView],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Hello from my program!");
Ok(())
}
}
Expand Down
2 changes: 0 additions & 2 deletions sdk/pinocchio/src/entrypoint/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ macro_rules! lazy_entrypoint {
/// default_panic_handler,
/// entrypoint::InstructionContext,
/// lazy_program_entrypoint,
/// msg,
/// ProgramResult
/// };
///
Expand All @@ -73,7 +72,6 @@ macro_rules! lazy_entrypoint {
/// pub fn process_instruction(
/// mut context: InstructionContext,
/// ) -> ProgramResult {
/// msg!("Hello from my `lazy` program!");
/// Ok(())
/// }
///
Expand Down
40 changes: 9 additions & 31 deletions sdk/pinocchio/src/entrypoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const STATIC_ACCOUNT_DATA: usize = size_of::<Account>() + MAX_PERMITTED_DATA_INC
/// use pinocchio::{
/// AccountView,
/// entrypoint,
/// msg,
/// Address,
/// ProgramResult
/// };
Expand All @@ -106,7 +105,6 @@ const STATIC_ACCOUNT_DATA: usize = size_of::<Account>() + MAX_PERMITTED_DATA_INC
/// accounts: &[AccountView],
/// instruction_data: &[u8],
/// ) -> ProgramResult {
/// msg!("Hello from my program!");
/// Ok(())
/// }
///
Expand Down Expand Up @@ -434,35 +432,10 @@ pub unsafe fn deserialize<const MAX_ACCOUNTS: usize>(
(program_id, processed, instruction_data)
}

/// Default panic hook.
///
/// This macro sets up a default panic hook that logs the panic message and the file where the panic
/// occurred. It acts as a hook after Rust runtime panics; syscall `abort()` will be called after it
/// returns.
///
/// Note that this requires the `"std"` feature to be enabled.
#[cfg(feature = "std")]
#[macro_export]
macro_rules! default_panic_handler {
() => {
/// Default panic handler.
#[cfg(target_os = "solana")]
#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
// Panic reporting.
$crate::msg!("{}", info);
}
};
}

/// Default panic hook.
///
/// This macro sets up a default panic hook that logs the file where the panic occurred. It acts as
/// a hook after Rust runtime panics; syscall `abort()` will be called after it returns.
///
/// This is used when the `"std"` feature is disabled, while either the program or any of its
/// dependencies are not `no_std`.
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! default_panic_handler {
() => {
Expand All @@ -471,10 +444,12 @@ macro_rules! default_panic_handler {
#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
if let Some(location) = info.location() {
$crate::log::sol_log(location.file());
let location = location.file();
unsafe { $crate::syscalls::sol_log_(location.as_ptr(), location.len() as u64) };
}
// Panic reporting.
$crate::log::sol_log("** PANICKED **");
const PANICKED: &str = "** PANICKED **";
unsafe { $crate::syscalls::sol_log_(PANICKED.as_ptr(), PANICKED.len() as u64) };
}
};
}
Expand Down Expand Up @@ -505,8 +480,11 @@ macro_rules! nostd_panic_handler {
}
} else {
// Panic reporting.
$crate::log::sol_log("** PANICKED **");
unsafe { $crate::syscalls::abort() }
const PANICKED: &str = "** PANICKED **";
unsafe {
$crate::syscalls::sol_log_(PANICKED.as_ptr(), PANICKED.len() as u64);
$crate::syscalls::abort();
}
}
}

Expand Down
24 changes: 5 additions & 19 deletions sdk/pinocchio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
//! AccountView,
//! Address,
//! entrypoint,
//! msg,
//! ProgramResult
//! };
//!
Expand All @@ -47,7 +46,6 @@
//! accounts: &[AccountView],
//! instruction_data: &[u8],
//! ) -> ProgramResult {
//! msg!("Hello from my program!");
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -97,7 +95,6 @@
//! default_panic_handler,
//! entrypoint::InstructionContext,
//! lazy_program_entrypoint,
//! msg,
//! ProgramResult
//! };
//!
Expand All @@ -108,7 +105,6 @@
//! pub fn process_instruction(
//! mut context: InstructionContext
//! ) -> ProgramResult {
//! msg!("Hello from my lazy program!");
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -142,7 +138,6 @@
//! AccountView,
//! Address,
//! default_panic_handler,
//! msg,
//! no_allocator,
//! program_entrypoint,
//! ProgramResult
Expand All @@ -157,7 +152,6 @@
//! accounts: &[AccountView],
//! instruction_data: &[u8],
//! ) -> ProgramResult {
//! msg!("Hello from `no_std` program!");
//! Ok(())
//! }
//! ```
Expand All @@ -170,19 +164,14 @@
//!
//! By default, Pinocchio is a `no_std` crate. This means that it does not use any
//! code from the standard (`std`) library. While this does not affect how Pinocchio
//! is used, there is a one particular apparent difference. In a `no_std` environment,
//! the [`msg!`] macro does not provide any formatting options since the `format!` macro
//! requires the `std` library. In order to use [`msg!`] with formatting, the `std`
//! feature should be enable when adding Pinocchio as a dependency:
//! is used, there is a one particular apparent difference. Helpers that need to
//! allocate memory, such as fetching `SlotHashes` sysvar, are not available. To
//! enable these helpers, the `std` feature must be enabled when adding Pinocchio
//! as a dependency:
//! ```ignore
//! pinocchio = { version = "0.7.0", features = ["std"] }
//! pinocchio = { version = "0.10.0", features = ["std"] }
//! ```
//!
//! Instead of enabling the `std` feature to be able to format log messages with [`msg!`],
//! it is recommended to use the [`pinocchio-log`](https://crates.io/crates/pinocchio-log)
//! crate. This crate provides a lightweight `log!` macro with better compute units
//! consumption than the standard `format!` macro without requiring the `std` library.
//!
//! ## Advanced entrypoint configuration
//!
//! The symbols emitted by the entrypoint macros - program entrypoint, global
Expand All @@ -199,7 +188,6 @@
//! AccountView,
//! Address,
//! entrypoint,
//! msg,
//! ProgramResult
//! };
//!
Expand All @@ -210,7 +198,6 @@
//! accounts: &[AccountView],
//! instruction_data: &[u8],
//! ) -> ProgramResult {
//! msg!("Hello from my program!");
//! Ok(())
//! }
//! }
Expand All @@ -227,7 +214,6 @@
extern crate std;

pub mod entrypoint;
pub mod log;
pub mod syscalls;
pub mod sysvars;

Expand Down
Loading