waffles_solana_program/
account_info.rs

1//! Account information.
2
3use {
4    crate::{
5        clock::Epoch, debug_account_data::*, entrypoint::MAX_PERMITTED_DATA_INCREASE,
6        program_error::ProgramError, program_memory::sol_memset, pubkey::Pubkey,
7    },
8    std::{
9        cell::{Ref, RefCell, RefMut},
10        fmt,
11        rc::Rc,
12        slice::from_raw_parts_mut,
13    },
14};
15
16/// Account information
17#[derive(Clone)]
18#[repr(C)]
19pub struct AccountInfo<'a> {
20    /// Public key of the account
21    pub key: &'a Pubkey,
22    /// The lamports in the account.  Modifiable by programs.
23    pub lamports: Rc<RefCell<&'a mut u64>>,
24    /// The data held in this account.  Modifiable by programs.
25    pub data: Rc<RefCell<&'a mut [u8]>>,
26    /// Program that owns this account
27    pub owner: &'a Pubkey,
28    /// The epoch at which this account will next owe rent
29    pub rent_epoch: Epoch,
30    /// Was the transaction signed by this account's public key?
31    pub is_signer: bool,
32    /// Is the account writable?
33    pub is_writable: bool,
34    /// This account's data contains a loaded program (and is now read-only)
35    pub executable: bool,
36}
37
38impl<'a> fmt::Debug for AccountInfo<'a> {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        let mut f = f.debug_struct("AccountInfo");
41
42        f.field("key", &self.key)
43            .field("owner", &self.owner)
44            .field("is_signer", &self.is_signer)
45            .field("is_writable", &self.is_writable)
46            .field("executable", &self.executable)
47            .field("rent_epoch", &self.rent_epoch)
48            .field("lamports", &self.lamports())
49            .field("data.len", &self.data_len());
50        debug_account_data(&self.data.borrow(), &mut f);
51
52        f.finish_non_exhaustive()
53    }
54}
55
56impl<'a> AccountInfo<'a> {
57    pub fn signer_key(&self) -> Option<&Pubkey> {
58        if self.is_signer {
59            Some(self.key)
60        } else {
61            None
62        }
63    }
64
65    pub fn unsigned_key(&self) -> &Pubkey {
66        self.key
67    }
68
69    pub fn lamports(&self) -> u64 {
70        **self.lamports.borrow()
71    }
72
73    pub fn try_lamports(&self) -> Result<u64, ProgramError> {
74        Ok(**self.try_borrow_lamports()?)
75    }
76
77    /// Return the account's original data length when it was serialized for the
78    /// current program invocation.
79    ///
80    /// # Safety
81    ///
82    /// This method assumes that the original data length was serialized as a u32
83    /// integer in the 4 bytes immediately preceding the serialized account key.
84    pub unsafe fn original_data_len(&self) -> usize {
85        let key_ptr = self.key as *const _ as *const u8;
86        let original_data_len_ptr = key_ptr.offset(-4) as *const u32;
87        *original_data_len_ptr as usize
88    }
89
90    pub fn data_len(&self) -> usize {
91        self.data.borrow().len()
92    }
93
94    pub fn try_data_len(&self) -> Result<usize, ProgramError> {
95        Ok(self.try_borrow_data()?.len())
96    }
97
98    pub fn data_is_empty(&self) -> bool {
99        self.data.borrow().is_empty()
100    }
101
102    pub fn try_data_is_empty(&self) -> Result<bool, ProgramError> {
103        Ok(self.try_borrow_data()?.is_empty())
104    }
105
106    pub fn try_borrow_lamports(&self) -> Result<Ref<&mut u64>, ProgramError> {
107        self.lamports
108            .try_borrow()
109            .map_err(|_| ProgramError::AccountBorrowFailed)
110    }
111
112    pub fn try_borrow_mut_lamports(&self) -> Result<RefMut<&'a mut u64>, ProgramError> {
113        self.lamports
114            .try_borrow_mut()
115            .map_err(|_| ProgramError::AccountBorrowFailed)
116    }
117
118    pub fn try_borrow_data(&self) -> Result<Ref<&mut [u8]>, ProgramError> {
119        self.data
120            .try_borrow()
121            .map_err(|_| ProgramError::AccountBorrowFailed)
122    }
123
124    pub fn try_borrow_mut_data(&self) -> Result<RefMut<&'a mut [u8]>, ProgramError> {
125        self.data
126            .try_borrow_mut()
127            .map_err(|_| ProgramError::AccountBorrowFailed)
128    }
129
130    /// Realloc the account's data and optionally zero-initialize the new
131    /// memory.
132    ///
133    /// Note:  Account data can be increased within a single call by up to
134    /// `solana_program::entrypoint::MAX_PERMITTED_DATA_INCREASE` bytes.
135    ///
136    /// Note: Memory used to grow is already zero-initialized upon program
137    /// entrypoint and re-zeroing it wastes compute units.  If within the same
138    /// call a program reallocs from larger to smaller and back to larger again
139    /// the new space could contain stale data.  Pass `true` for `zero_init` in
140    /// this case, otherwise compute units will be wasted re-zero-initializing.
141    ///
142    /// # Safety
143    ///
144    /// This method makes assumptions about the layout and location of memory
145    /// referenced by `AccountInfo` fields. It should only be called for
146    /// instances of `AccountInfo` that were created by the runtime and received
147    /// in the `process_instruction` entrypoint of a program.
148    pub fn realloc(&self, new_len: usize, zero_init: bool) -> Result<(), ProgramError> {
149        let mut data = self.try_borrow_mut_data()?;
150        let old_len = data.len();
151
152        // Return early if length hasn't changed
153        if new_len == old_len {
154            return Ok(());
155        }
156
157        // Return early if the length increase from the original serialized data
158        // length is too large and would result in an out of bounds allocation.
159        let original_data_len = unsafe { self.original_data_len() };
160        if new_len.saturating_sub(original_data_len) > MAX_PERMITTED_DATA_INCREASE {
161            return Err(ProgramError::InvalidRealloc);
162        }
163
164        // realloc
165        unsafe {
166            let data_ptr = data.as_mut_ptr();
167
168            // First set new length in the serialized data
169            *(data_ptr.offset(-8) as *mut u64) = new_len as u64;
170
171            // Then recreate the local slice with the new length
172            *data = from_raw_parts_mut(data_ptr, new_len)
173        }
174
175        if zero_init {
176            let len_increase = new_len.saturating_sub(old_len);
177            if len_increase > 0 {
178                sol_memset(&mut data[old_len..], 0, len_increase);
179            }
180        }
181
182        Ok(())
183    }
184
185    pub fn assign(&self, new_owner: &Pubkey) {
186        // Set the non-mut owner field
187        unsafe {
188            std::ptr::write_volatile(
189                self.owner as *const Pubkey as *mut [u8; 32],
190                new_owner.to_bytes(),
191            );
192        }
193    }
194
195    pub fn new(
196        key: &'a Pubkey,
197        is_signer: bool,
198        is_writable: bool,
199        lamports: &'a mut u64,
200        data: &'a mut [u8],
201        owner: &'a Pubkey,
202        executable: bool,
203        rent_epoch: Epoch,
204    ) -> Self {
205        Self {
206            key,
207            is_signer,
208            is_writable,
209            lamports: Rc::new(RefCell::new(lamports)),
210            data: Rc::new(RefCell::new(data)),
211            owner,
212            executable,
213            rent_epoch,
214        }
215    }
216
217    pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
218        bincode::deserialize(&self.data.borrow())
219    }
220
221    pub fn serialize_data<T: serde::Serialize>(&self, state: &T) -> Result<(), bincode::Error> {
222        if bincode::serialized_size(state)? > self.data_len() as u64 {
223            return Err(Box::new(bincode::ErrorKind::SizeLimit));
224        }
225        bincode::serialize_into(&mut self.data.borrow_mut()[..], state)
226    }
227}
228
229/// Constructs an `AccountInfo` from self, used in conversion implementations.
230pub trait IntoAccountInfo<'a> {
231    fn into_account_info(self) -> AccountInfo<'a>;
232}
233impl<'a, T: IntoAccountInfo<'a>> From<T> for AccountInfo<'a> {
234    fn from(src: T) -> Self {
235        src.into_account_info()
236    }
237}
238
239/// Provides information required to construct an `AccountInfo`, used in
240/// conversion implementations.
241pub trait Account {
242    fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool, Epoch);
243}
244
245/// Convert (&'a Pubkey, &'a mut T) where T: Account into an `AccountInfo`
246impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Pubkey, &'a mut T) {
247    fn into_account_info(self) -> AccountInfo<'a> {
248        let (key, account) = self;
249        let (lamports, data, owner, executable, rent_epoch) = account.get();
250        AccountInfo::new(
251            key, false, false, lamports, data, owner, executable, rent_epoch,
252        )
253    }
254}
255
256/// Convert (&'a Pubkey, bool, &'a mut T)  where T: Account into an
257/// `AccountInfo`.
258impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Pubkey, bool, &'a mut T) {
259    fn into_account_info(self) -> AccountInfo<'a> {
260        let (key, is_signer, account) = self;
261        let (lamports, data, owner, executable, rent_epoch) = account.get();
262        AccountInfo::new(
263            key, is_signer, false, lamports, data, owner, executable, rent_epoch,
264        )
265    }
266}
267
268/// Convert &'a mut (Pubkey, T) where T: Account into an `AccountInfo`.
269impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) {
270    fn into_account_info(self) -> AccountInfo<'a> {
271        let (ref key, account) = self;
272        let (lamports, data, owner, executable, rent_epoch) = account.get();
273        AccountInfo::new(
274            key, false, false, lamports, data, owner, executable, rent_epoch,
275        )
276    }
277}
278
279/// Convenience function for accessing the next item in an [`AccountInfo`]
280/// iterator.
281///
282/// This is simply a wrapper around [`Iterator::next`] that returns a
283/// [`ProgramError`] instead of an option.
284///
285/// # Errors
286///
287/// Returns [`ProgramError::NotEnoughAccountKeys`] if there are no more items in
288/// the iterator.
289///
290/// # Examples
291///
292/// ```
293/// use solana_program::{
294///    account_info::{AccountInfo, next_account_info},
295///    entrypoint::ProgramResult,
296///    pubkey::Pubkey,
297/// };
298/// # use solana_program::program_error::ProgramError;
299///
300/// pub fn process_instruction(
301///     program_id: &Pubkey,
302///     accounts: &[AccountInfo],
303///     instruction_data: &[u8],
304/// ) -> ProgramResult {
305///     let accounts_iter = &mut accounts.iter();
306///     let signer = next_account_info(accounts_iter)?;
307///     let payer = next_account_info(accounts_iter)?;
308///
309///     // do stuff ...
310///
311///     Ok(())
312/// }
313/// # let p = Pubkey::new_unique();
314/// # let l = &mut 0;
315/// # let d = &mut [0u8];
316/// # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
317/// # let accounts = &[a.clone(), a];
318/// # process_instruction(
319/// #    &Pubkey::new_unique(),
320/// #    accounts,
321/// #    &[],
322/// # )?;
323/// # Ok::<(), ProgramError>(())
324/// ```
325pub fn next_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
326    iter: &mut I,
327) -> Result<I::Item, ProgramError> {
328    iter.next().ok_or(ProgramError::NotEnoughAccountKeys)
329}
330
331/// Convenience function for accessing multiple next items in an [`AccountInfo`]
332/// iterator.
333///
334/// Returns a slice containing the next `count` [`AccountInfo`]s.
335///
336/// # Errors
337///
338/// Returns [`ProgramError::NotEnoughAccountKeys`] if there are not enough items
339/// in the iterator to satisfy the request.
340///
341/// # Examples
342///
343/// ```
344/// use solana_program::{
345///    account_info::{AccountInfo, next_account_info, next_account_infos},
346///    entrypoint::ProgramResult,
347///    pubkey::Pubkey,
348/// };
349/// # use solana_program::program_error::ProgramError;
350///
351/// pub fn process_instruction(
352///     program_id: &Pubkey,
353///     accounts: &[AccountInfo],
354///     instruction_data: &[u8],
355/// ) -> ProgramResult {
356///     let accounts_iter = &mut accounts.iter();
357///     let signer = next_account_info(accounts_iter)?;
358///     let payer = next_account_info(accounts_iter)?;
359///     let outputs = next_account_infos(accounts_iter, 3)?;
360///
361///     // do stuff ...
362///
363///     Ok(())
364/// }
365/// # let p = Pubkey::new_unique();
366/// # let l = &mut 0;
367/// # let d = &mut [0u8];
368/// # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
369/// # let accounts = &[a.clone(), a.clone(), a.clone(), a.clone(), a];
370/// # process_instruction(
371/// #    &Pubkey::new_unique(),
372/// #    accounts,
373/// #    &[],
374/// # )?;
375/// # Ok::<(), ProgramError>(())
376/// ```
377pub fn next_account_infos<'a, 'b: 'a>(
378    iter: &mut std::slice::Iter<'a, AccountInfo<'b>>,
379    count: usize,
380) -> Result<&'a [AccountInfo<'b>], ProgramError> {
381    let accounts = iter.as_slice();
382    if accounts.len() < count {
383        return Err(ProgramError::NotEnoughAccountKeys);
384    }
385    let (accounts, remaining) = accounts.split_at(count);
386    *iter = remaining.iter();
387    Ok(accounts)
388}
389
390impl<'a> AsRef<AccountInfo<'a>> for AccountInfo<'a> {
391    fn as_ref(&self) -> &AccountInfo<'a> {
392        self
393    }
394}
395
396#[cfg(test)]
397mod tests {
398    use super::*;
399
400    #[test]
401    fn test_next_account_infos() {
402        let k1 = Pubkey::new_unique();
403        let k2 = Pubkey::new_unique();
404        let k3 = Pubkey::new_unique();
405        let k4 = Pubkey::new_unique();
406        let k5 = Pubkey::new_unique();
407        let l1 = &mut 0;
408        let l2 = &mut 0;
409        let l3 = &mut 0;
410        let l4 = &mut 0;
411        let l5 = &mut 0;
412        let d1 = &mut [0u8];
413        let d2 = &mut [0u8];
414        let d3 = &mut [0u8];
415        let d4 = &mut [0u8];
416        let d5 = &mut [0u8];
417
418        let infos = &[
419            AccountInfo::new(&k1, false, false, l1, d1, &k1, false, 0),
420            AccountInfo::new(&k2, false, false, l2, d2, &k2, false, 0),
421            AccountInfo::new(&k3, false, false, l3, d3, &k3, false, 0),
422            AccountInfo::new(&k4, false, false, l4, d4, &k4, false, 0),
423            AccountInfo::new(&k5, false, false, l5, d5, &k5, false, 0),
424        ];
425        let infos_iter = &mut infos.iter();
426        let info1 = next_account_info(infos_iter).unwrap();
427        let info2_3_4 = next_account_infos(infos_iter, 3).unwrap();
428        let info5 = next_account_info(infos_iter).unwrap();
429
430        assert_eq!(k1, *info1.key);
431        assert_eq!(k2, *info2_3_4[0].key);
432        assert_eq!(k3, *info2_3_4[1].key);
433        assert_eq!(k4, *info2_3_4[2].key);
434        assert_eq!(k5, *info5.key);
435    }
436
437    #[test]
438    fn test_account_info_as_ref() {
439        let k = Pubkey::new_unique();
440        let l = &mut 0;
441        let d = &mut [0u8];
442        let info = AccountInfo::new(&k, false, false, l, d, &k, false, 0);
443        assert_eq!(info.key, info.as_ref().key);
444    }
445
446    #[test]
447    fn test_account_info_debug_data() {
448        let key = Pubkey::new_unique();
449        let mut lamports = 42;
450        let mut data = vec![5; 80];
451        let data_str = format!("{:?}", Hex(&data[..MAX_DEBUG_ACCOUNT_DATA]));
452        let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0);
453        assert_eq!(
454            format!("{info:?}"),
455            format!(
456                "AccountInfo {{ \
457                key: {}, \
458                owner: {}, \
459                is_signer: {}, \
460                is_writable: {}, \
461                executable: {}, \
462                rent_epoch: {}, \
463                lamports: {}, \
464                data.len: {}, \
465                data: {}, .. }}",
466                key,
467                key,
468                false,
469                false,
470                false,
471                0,
472                lamports,
473                data.len(),
474                data_str,
475            )
476        );
477
478        let mut data = vec![5; 40];
479        let data_str = format!("{:?}", Hex(&data));
480        let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0);
481        assert_eq!(
482            format!("{info:?}"),
483            format!(
484                "AccountInfo {{ \
485                key: {}, \
486                owner: {}, \
487                is_signer: {}, \
488                is_writable: {}, \
489                executable: {}, \
490                rent_epoch: {}, \
491                lamports: {}, \
492                data.len: {}, \
493                data: {}, .. }}",
494                key,
495                key,
496                false,
497                false,
498                false,
499                0,
500                lamports,
501                data.len(),
502                data_str,
503            )
504        );
505
506        let mut data = vec![];
507        let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0);
508        assert_eq!(
509            format!("{info:?}"),
510            format!(
511                "AccountInfo {{ \
512                key: {}, \
513                owner: {}, \
514                is_signer: {}, \
515                is_writable: {}, \
516                executable: {}, \
517                rent_epoch: {}, \
518                lamports: {}, \
519                data.len: {}, .. }}",
520                key,
521                key,
522                false,
523                false,
524                false,
525                0,
526                lamports,
527                data.len(),
528            )
529        );
530    }
531}