solana_program_stubs/
common.rs

1/// A macro providing the common types for both a Solana program and a loader of it.
2#[macro_export]
3macro_rules! common_stub_types {
4    () => {
5        pub const PUBKEY_BYTES: usize = 32;
6        #[repr(C)]
7        pub struct CPubkey(pub [u8; PUBKEY_BYTES]);
8
9        impl CPubkey {
10            pub const fn as_array(&self) -> &[u8; PUBKEY_BYTES] {
11                &self.0
12            }
13        }
14
15        impl AsRef<[u8]> for CPubkey {
16            fn as_ref(&self) -> &[u8] {
17                &self.0[..]
18            }
19        }
20
21        impl AsMut<[u8]> for CPubkey {
22            fn as_mut(&mut self) -> &mut [u8] {
23                &mut self.0[..]
24            }
25        }
26
27        impl From<[u8; 32]> for CPubkey {
28            #[inline]
29            fn from(from: [u8; 32]) -> Self {
30                Self(from)
31            }
32        }
33
34        impl From<&[u8; 32]> for CPubkey {
35            #[inline]
36            fn from(from: &[u8; 32]) -> Self {
37                Self(*from)
38            }
39        }
40
41        impl PartialEq<[u8; 32]> for CPubkey {
42            fn eq(&self, other: &[u8; 32]) -> bool {
43                &self.0 == other
44            }
45        }
46
47        #[repr(C)]
48        pub struct CProcessedSiblingInstruction {
49            pub data_len: u64,
50            pub accounts_len: u64,
51        }
52
53        #[repr(C)]
54        #[derive(Clone)]
55        pub struct CAccountMeta {
56            pub pubkey: *const CPubkey,
57            pub is_writable: bool,
58            pub is_signer: bool,
59        }
60
61        impl Default for CAccountMeta {
62            fn default() -> Self {
63                Self {
64                    pubkey: &CPubkey::from([0u8; 32]),
65                    is_writable: false,
66                    is_signer: false,
67                }
68            }
69        }
70
71        #[repr(C)]
72        #[derive(Debug)]
73        pub struct CAccountInfo {
74            // Public key of the account.
75            pub key: *const CPubkey,
76
77            // Number of lamports owned by this account.
78            pub lamports: *const u64,
79
80            // Length of data in bytes.
81            pub data_len: u64,
82
83            // On-chain data within this account.
84            pub data: *const u8,
85
86            // Program that owns this account.
87            pub owner: *const CPubkey,
88
89            // The epoch at which this account will next owe rent.
90            pub rent_epoch: u64,
91
92            // Transaction was signed by this account's key?
93            pub is_signer: bool,
94
95            // Is the account writable?
96            pub is_writable: bool,
97
98            // This account's data contains a loaded program (and is now read-only).
99            pub executable: bool,
100        }
101
102        #[repr(C)]
103        #[derive(Debug)]
104        pub struct CInstruction {
105            /// Public key of the program.
106            pub program_id: *const CPubkey,
107
108            /// Accounts expected by the program instruction.
109            pub accounts: *const CAccountMeta,
110
111            /// Number of accounts expected by the program instruction.
112            pub accounts_len: u64,
113
114            /// Data expected by the program instruction.
115            pub data: *const u8,
116
117            /// Length of the data expected by the program instruction.
118            pub data_len: u64,
119        }
120
121        #[repr(C)]
122        pub struct SyscallStubsApi {
123            pub sol_log_: extern "C" fn(message: *const u8, len: u64),
124            pub sol_log_compute_units_: extern "C" fn(),
125            pub sol_remaining_compute_units: extern "C" fn() -> u64,
126            pub sol_invoke_signed_c: extern "C" fn(
127                instruction_addr: *const u8,
128                account_infos_addr: *const u8,
129                account_infos_len: u64,
130                signers_seeds_addr: *const u8,
131                signers_seeds_len: u64,
132            ) -> u64,
133            pub sol_get_clock_sysvar: extern "C" fn(addr: *mut u8) -> u64,
134            pub sol_get_epoch_schedule_sysvar: extern "C" fn(addr: *mut u8) -> u64,
135            pub sol_get_fees_sysvar: extern "C" fn(addr: *mut u8) -> u64,
136            pub sol_get_rent_sysvar: extern "C" fn(addr: *mut u8) -> u64,
137            pub sol_get_last_restart_slot: extern "C" fn(addr: *mut u8) -> u64,
138            pub sol_get_sysvar: extern "C" fn(
139                sysvar_id_addr: *const u8,
140                result: *mut u8,
141                offset: u64,
142                length: u64,
143            ) -> u64,
144            pub sol_memcpy_: extern "C" fn(dst: *mut u8, src: *const u8, n: u64),
145            pub sol_memmove_: extern "C" fn(dst: *mut u8, src: *const u8, n: u64),
146            pub sol_memcmp_: extern "C" fn(s1: *const u8, s2: *const u8, n: u64, result: *mut i32),
147            pub sol_memset_: extern "C" fn(s: *mut u8, c: u8, n: u64),
148            pub sol_get_return_data:
149                extern "C" fn(data: *mut u8, length: u64, program_id: *mut CPubkey) -> u64,
150            pub sol_set_return_data: extern "C" fn(data: *const u8, length: u64),
151            pub sol_log_data: extern "C" fn(data: *const u8, data_len: u64),
152            pub sol_get_processed_sibling_instruction: extern "C" fn(
153                index: u64,
154                meta: *mut CProcessedSiblingInstruction,
155                program_id: *mut CPubkey,
156                data: *mut u8,
157                accounts: *mut CAccountMeta,
158            ) -> u64,
159            pub sol_get_stack_height: extern "C" fn() -> u64,
160            pub sol_get_epoch_rewards_sysvar: extern "C" fn(addr: *mut u8) -> u64,
161            pub sol_get_epoch_stake: extern "C" fn(vote_address: *const u8) -> u64,
162        }
163    };
164}