1use crate::account_view::AccountView;
8use crate::address::Address;
9use crate::error::ProgramError;
10use crate::raw_account::RuntimeAccount;
11use crate::{ProgramResult, NOT_BORROWED};
12use core::marker::PhantomData;
13
14#[repr(C)]
18#[derive(Debug, Clone)]
19pub struct InstructionAccount<'a> {
20 pub address: &'a Address,
22 pub is_writable: bool,
24 pub is_signer: bool,
26}
27
28impl<'a> InstructionAccount<'a> {
29 #[inline(always)]
31 pub const fn new(address: &'a Address, is_writable: bool, is_signer: bool) -> Self {
32 Self {
33 address,
34 is_writable,
35 is_signer,
36 }
37 }
38
39 #[inline(always)]
41 pub const fn readonly(address: &'a Address) -> Self {
42 Self {
43 address,
44 is_writable: false,
45 is_signer: false,
46 }
47 }
48
49 #[inline(always)]
51 pub const fn writable(address: &'a Address) -> Self {
52 Self {
53 address,
54 is_writable: true,
55 is_signer: false,
56 }
57 }
58
59 #[inline(always)]
61 pub const fn readonly_signer(address: &'a Address) -> Self {
62 Self {
63 address,
64 is_writable: false,
65 is_signer: true,
66 }
67 }
68
69 #[inline(always)]
71 pub const fn writable_signer(address: &'a Address) -> Self {
72 Self {
73 address,
74 is_writable: true,
75 is_signer: true,
76 }
77 }
78}
79
80impl<'a> From<&'a AccountView<'a>> for InstructionAccount<'a> {
81 #[inline(always)]
82 fn from(view: &'a AccountView<'a>) -> Self {
83 Self {
84 address: view.address(),
85 is_writable: view.is_writable(),
86 is_signer: view.is_signer(),
87 }
88 }
89}
90
91#[derive(Debug, Clone)]
95pub struct InstructionView<'a, 'b, 'c, 'd>
96where
97 'a: 'b,
98{
99 pub program_id: &'c Address,
101 pub data: &'d [u8],
103 pub accounts: &'b [InstructionAccount<'a>],
105}
106
107#[repr(C)]
113#[derive(Clone, Copy, Debug)]
114pub struct CpiAccount<'a> {
115 address: *const Address,
116 lamports: *const u64,
117 data_len: u64,
118 data: *const u8,
119 owner: *const Address,
120 rent_epoch: u64,
121 is_signer: bool,
122 is_writable: bool,
123 executable: bool,
124 _account_view: PhantomData<&'a AccountView<'a>>,
125}
126
127impl<'a> From<&'a AccountView<'a>> for CpiAccount<'a> {
128 #[inline(always)]
129 fn from(view: &'a AccountView<'a>) -> Self {
130 let raw = view.account_ptr();
131 let header = unsafe { core::ptr::read_unaligned(raw as *const u32) };
138 Self {
139 address: unsafe { &(*raw).address as *const Address },
140 lamports: unsafe { &(*raw).lamports as *const u64 },
142 data_len: view.data_len() as u64,
143 data: view.data_ptr_unchecked(),
144 owner: unsafe { &(*raw).owner as *const Address },
146 rent_epoch: 0,
147 is_signer: header & 0x0000_FF00 != 0,
148 is_writable: header & 0x00FF_0000 != 0,
149 executable: header & 0xFF00_0000 != 0,
150 _account_view: PhantomData,
151 }
152 }
153}
154
155impl<'a> CpiAccount<'a> {
156 #[inline(always)]
162 pub(crate) fn instruction_account(
163 &self,
164 is_writable: bool,
165 is_signer: bool,
166 ) -> InstructionAccount<'a> {
167 let address = unsafe { &*self.address };
170 InstructionAccount::new(address, is_writable, is_signer)
171 }
172}
173
174#[inline(always)]
182pub(crate) fn preflight_cpi_accounts(
183 accounts: &[CpiAccount<'_>],
184 writable_mask: usize,
185 signer_mask: usize,
186 has_pda_signers: bool,
187) -> ProgramResult {
188 let mut index = 0usize;
189 while index < accounts.len() {
190 let account = &accounts[index];
191 if signer_mask & (1usize << index) != 0 && !account.is_signer && !has_pda_signers {
196 return Err(ProgramError::MissingRequiredSignature);
197 }
198 let is_writable_meta = writable_mask & (1usize << index) != 0;
199 if is_writable_meta && !account.is_writable {
200 return Err(ProgramError::Immutable);
201 }
202
203 let raw = unsafe { account.data.sub(RuntimeAccount::SIZE) as *const RuntimeAccount };
207 let borrow_state = unsafe { (*raw).borrow_state };
210 let compatible = if is_writable_meta {
211 borrow_state == NOT_BORROWED
212 } else {
213 borrow_state != 0
214 };
215 if !compatible {
216 return Err(ProgramError::AccountBorrowFailed);
217 }
218
219 index += 1;
220 }
221 Ok(())
222}
223
224const _: () = {
228 assert!(core::mem::size_of::<InstructionAccount<'static>>() == 16);
229 assert!(core::mem::align_of::<InstructionAccount<'static>>() == 8);
230 assert!(core::mem::offset_of!(InstructionAccount<'static>, address) == 0);
231 assert!(core::mem::offset_of!(InstructionAccount<'static>, is_writable) == 8);
232 assert!(core::mem::offset_of!(InstructionAccount<'static>, is_signer) == 9);
233
234 assert!(core::mem::size_of::<CpiAccount<'static>>() == 56);
235 assert!(core::mem::align_of::<CpiAccount<'static>>() == 8);
236 assert!(core::mem::offset_of!(CpiAccount<'static>, address) == 0);
237 assert!(core::mem::offset_of!(CpiAccount<'static>, lamports) == 8);
238 assert!(core::mem::offset_of!(CpiAccount<'static>, data_len) == 16);
239 assert!(core::mem::offset_of!(CpiAccount<'static>, data) == 24);
240 assert!(core::mem::offset_of!(CpiAccount<'static>, owner) == 32);
241 assert!(core::mem::offset_of!(CpiAccount<'static>, rent_epoch) == 40);
242 assert!(core::mem::offset_of!(CpiAccount<'static>, is_signer) == 48);
243 assert!(core::mem::offset_of!(CpiAccount<'static>, is_writable) == 49);
244 assert!(core::mem::offset_of!(CpiAccount<'static>, executable) == 50);
245};
246
247#[repr(C)]
251#[derive(Debug, Clone)]
252pub struct Seed<'a> {
253 pub(crate) seed: *const u8,
254 pub(crate) len: u64,
255 _bytes: PhantomData<&'a [u8]>,
256}
257
258impl<'a> From<&'a [u8]> for Seed<'a> {
259 #[inline(always)]
260 fn from(bytes: &'a [u8]) -> Self {
261 Self {
262 seed: bytes.as_ptr(),
263 len: bytes.len() as u64,
264 _bytes: PhantomData,
265 }
266 }
267}
268
269impl<'a, const N: usize> From<&'a [u8; N]> for Seed<'a> {
270 #[inline(always)]
271 fn from(bytes: &'a [u8; N]) -> Self {
272 Self {
273 seed: bytes.as_ptr(),
274 len: N as u64,
275 _bytes: PhantomData,
276 }
277 }
278}
279
280impl core::ops::Deref for Seed<'_> {
281 type Target = [u8];
282
283 #[inline(always)]
284 fn deref(&self) -> &[u8] {
285 unsafe { core::slice::from_raw_parts(self.seed, self.len as usize) }
287 }
288}
289
290#[repr(C)]
294#[derive(Debug, Clone)]
295pub struct Signer<'a, 'b> {
296 pub(crate) seeds: *const Seed<'a>,
297 pub(crate) len: u64,
298 _seeds: PhantomData<&'b [Seed<'a>]>,
299}
300
301impl<'a, 'b> From<&'b [Seed<'a>]> for Signer<'a, 'b> {
302 #[inline(always)]
303 fn from(seeds: &'b [Seed<'a>]) -> Self {
304 Self {
305 seeds: seeds.as_ptr(),
306 len: seeds.len() as u64,
307 _seeds: PhantomData,
308 }
309 }
310}
311
312impl<'a, 'b, const N: usize> From<&'b [Seed<'a>; N]> for Signer<'a, 'b> {
313 #[inline(always)]
314 fn from(seeds: &'b [Seed<'a>; N]) -> Self {
315 Self {
316 seeds: seeds.as_ptr(),
317 len: N as u64,
318 _seeds: PhantomData,
319 }
320 }
321}
322
323#[macro_export]
327macro_rules! seeds {
328 ( $($seed:expr),* $(,)? ) => {
329 [$(
330 $crate::instruction::Seed::from($seed),
331 )*]
332 };
333}