|
1 | 1 | //! Defines the middleware program entrypoint, enabling a hot path to bypass |
2 | 2 | //! entrypoint deserialization, ejecting to the cold path on failure. |
| 3 | +
|
| 4 | +/// Declare the middleware program entrypoint. |
| 5 | +/// |
| 6 | +/// The macro expect a `hot` and `cold` path expressions. The `hot` is a function with |
| 7 | +/// the following type signature: |
| 8 | +/// |
| 9 | +/// ```ignore |
| 10 | +/// fn hot(input: mut *u8) -> u64; |
| 11 | +/// ``` |
| 12 | +/// The `input` argument represents the program input parameters serialized by the SVM |
| 13 | +/// loader. The `cold` is a function with the following type signature: |
| 14 | +/// |
| 15 | +/// ```ignore |
| 16 | +/// fn cold( |
| 17 | +/// program_id: &Pubkey, // Public key of the account the program was loaded into |
| 18 | +/// accounts: &[AccountInfo], // All accounts required to process the instruction |
| 19 | +/// instruction_data: &[u8], // Serialized instruction-specific data |
| 20 | +/// ) -> ProgramResult; |
| 21 | +/// ``` |
| 22 | +/// # Example |
| 23 | +/// |
| 24 | +/// A middleware program entrypoint where an invocation with zero accounts will lead to |
| 25 | +/// the "hot" path, and anything else will fallback to the "cold" path: |
| 26 | +/// |
| 27 | +/// ```norun |
| 28 | +/// #![cfg_attr(target_os = "solana", no_std)] |
| 29 | +/// use pinocchio::{ |
| 30 | +/// ProgramResult, |
| 31 | +/// account_info::AccountInfo, |
| 32 | +/// middleware_entrypoint, |
| 33 | +/// msg, |
| 34 | +/// no_allocator, |
| 35 | +/// nostd_panic_handler, |
| 36 | +/// pubkey::Pubkey |
| 37 | +/// }; |
| 38 | +/// |
| 39 | +/// nostd_panic_handler!(); |
| 40 | +/// no_allocator!(); |
| 41 | +/// |
| 42 | +/// middleware_entrypoint!(hot,cold); |
| 43 | +/// |
| 44 | +/// // This uses 4 CUs |
| 45 | +/// #[inline(always)] |
| 46 | +/// pub fn hot(input: *mut u8) -> u64 { |
| 47 | +/// unsafe { *input as u64 } |
| 48 | +/// } |
| 49 | +/// |
| 50 | +/// // This uses 113 CUs |
| 51 | +/// #[cold] |
| 52 | +/// #[inline(always)] |
| 53 | +/// pub fn cold( |
| 54 | +/// _program_id: &Pubkey, |
| 55 | +/// _accounts: &[AccountInfo], |
| 56 | +/// _instruction_data: &[u8], |
| 57 | +/// ) -> ProgramResult { |
| 58 | +/// msg!("Hello from my Pinocchio!"); |
| 59 | +/// Ok(()) |
| 60 | +/// } |
| 61 | +/// ``` |
3 | 62 | #[macro_export] |
4 | 63 | macro_rules! middleware_program_entrypoint { |
5 | 64 | ($hot:expr, $cold:expr) => { |
|
0 commit comments