Skip to content

Commit e5f42d0

Browse files
committed
feat: middleware entrypoint
1 parent dd52b4a commit e5f42d0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Defines the middleware entrypoint, enabling a hot path to bypass
2+
//! entrypoint deserialization, ejecting to the cold path on failure.
3+
#[macro_export]
4+
macro_rules! middleware_entrypoint {
5+
($hot:expr, $cold:expr) => {
6+
$crate::middleware_entrypoint!($hot, $cold, { $crate::MAX_TX_ACCOUNTS });
7+
};
8+
($hot:expr, $cold:expr, $maximum:expr ) => {
9+
10+
#[no_mangle]
11+
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
12+
if $hot(input) == 0 {
13+
return $crate::SUCCESS
14+
}
15+
16+
const UNINIT: core::mem::MaybeUninit<$crate::account_info::AccountInfo> = core::mem::MaybeUninit::<$crate::account_info::AccountInfo>::uninit();
17+
// Create an array of uninitialized account infos.
18+
let mut accounts = [UNINIT; $maximum];
19+
20+
let (program_id, count, instruction_data) = unsafe {
21+
$crate::entrypoint::deserialize::<$maximum>(input, &mut accounts) };
22+
23+
// Call the program's entrypoint passing `count` account infos; we know that
24+
// they are initialized so we cast the pointer to a slice of `[AccountInfo]`.
25+
match $cold(
26+
&program_id,
27+
unsafe { core::slice::from_raw_parts(accounts.as_ptr() as _, count) },
28+
&instruction_data,
29+
) {
30+
Ok(()) => $crate::SUCCESS,
31+
Err(error) => error.into(),
32+
}
33+
}
34+
};
35+
}

sdk/pinocchio/src/entrypoint/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub mod lazy;
55

66
pub use lazy::{InstructionContext, MaybeAccount};
77

8+
pub mod middleware;
9+
810
#[cfg(not(feature = "std"))]
911
use core::alloc::{GlobalAlloc, Layout};
1012

0 commit comments

Comments
 (0)