2020-01-03 09:14:51 -08:00
|
|
|
use crate::{account::Account, pubkey::Pubkey};
|
2020-01-29 21:15:04 -08:00
|
|
|
use std::{cell::RefCell, cmp, fmt, rc::Rc};
|
2019-09-06 17:32:14 -07:00
|
|
|
|
2020-01-24 10:54:26 -08:00
|
|
|
/// Account information
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AccountInfo<'a> {
|
|
|
|
/// Public key of the account
|
|
|
|
pub key: &'a Pubkey,
|
|
|
|
// Was the transaction signed by this account's public key?
|
|
|
|
pub is_signer: bool,
|
|
|
|
/// Account members that are mutable by the program
|
2020-01-29 21:15:04 -08:00
|
|
|
pub lamports: Rc<RefCell<&'a mut u64>>,
|
|
|
|
/// Account members that are mutable by the program
|
|
|
|
pub data: Rc<RefCell<&'a mut [u8]>>,
|
2019-09-06 17:32:14 -07:00
|
|
|
/// Program that owns this account
|
|
|
|
pub owner: &'a Pubkey,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fmt::Debug for AccountInfo<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-01-24 14:34:59 -08:00
|
|
|
let data_len = cmp::min(64, self.data_len());
|
2019-09-06 17:32:14 -07:00
|
|
|
let data_str = if data_len > 0 {
|
2020-01-24 10:54:26 -08:00
|
|
|
format!(
|
|
|
|
" data: {}",
|
2020-01-29 21:15:04 -08:00
|
|
|
hex::encode(self.data.borrow()[..data_len].to_vec())
|
2020-01-24 10:54:26 -08:00
|
|
|
)
|
2019-09-06 17:32:14 -07:00
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}",
|
2020-01-24 14:34:59 -08:00
|
|
|
self.lamports(),
|
|
|
|
self.data_len(),
|
2019-09-06 17:32:14 -07:00
|
|
|
self.owner,
|
|
|
|
data_str,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AccountInfo<'a> {
|
2020-01-03 09:14:51 -08:00
|
|
|
pub fn signer_key(&self) -> Option<&Pubkey> {
|
|
|
|
if self.is_signer {
|
|
|
|
Some(self.key)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsigned_key(&self) -> &Pubkey {
|
|
|
|
self.key
|
|
|
|
}
|
|
|
|
|
2020-01-24 14:34:59 -08:00
|
|
|
pub fn lamports(&self) -> u64 {
|
2020-01-29 21:15:04 -08:00
|
|
|
**self.lamports.borrow()
|
2020-01-24 10:54:26 -08:00
|
|
|
}
|
|
|
|
|
2020-01-24 14:34:59 -08:00
|
|
|
pub fn data_len(&self) -> usize {
|
2020-01-29 21:15:04 -08:00
|
|
|
self.data.borrow().len()
|
2020-01-24 10:54:26 -08:00
|
|
|
}
|
|
|
|
|
2020-01-24 14:34:59 -08:00
|
|
|
pub fn data_is_empty(&self) -> bool {
|
2020-01-29 21:15:04 -08:00
|
|
|
self.data.borrow().is_empty()
|
2020-01-24 10:54:26 -08:00
|
|
|
}
|
|
|
|
|
2020-01-03 09:14:51 -08:00
|
|
|
pub fn new(
|
|
|
|
key: &'a Pubkey,
|
|
|
|
is_signer: bool,
|
|
|
|
lamports: &'a mut u64,
|
|
|
|
data: &'a mut [u8],
|
|
|
|
owner: &'a Pubkey,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
key,
|
|
|
|
is_signer,
|
2020-01-29 21:15:04 -08:00
|
|
|
lamports: Rc::new(RefCell::new(lamports)),
|
|
|
|
data: Rc::new(RefCell::new(data)),
|
2020-01-03 09:14:51 -08:00
|
|
|
owner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:32:14 -07:00
|
|
|
pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
|
2020-01-29 21:15:04 -08:00
|
|
|
bincode::deserialize(&self.data.borrow())
|
2019-09-06 17:32:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
|
2020-01-24 14:34:59 -08:00
|
|
|
if bincode::serialized_size(state)? > self.data_len() as u64 {
|
2019-09-06 17:32:14 -07:00
|
|
|
return Err(Box::new(bincode::ErrorKind::SizeLimit));
|
|
|
|
}
|
2020-01-29 21:15:04 -08:00
|
|
|
bincode::serialize_into(&mut self.data.borrow_mut()[..], state)
|
2019-09-06 17:32:14 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 09:14:51 -08:00
|
|
|
|
|
|
|
impl<'a> From<(&'a Pubkey, &'a mut Account)> for AccountInfo<'a> {
|
|
|
|
fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self {
|
2020-01-24 10:54:26 -08:00
|
|
|
Self::new(
|
2020-01-03 09:14:51 -08:00
|
|
|
key,
|
2020-01-24 10:54:26 -08:00
|
|
|
false,
|
|
|
|
&mut account.lamports,
|
|
|
|
&mut account.data,
|
|
|
|
&account.owner,
|
|
|
|
)
|
2020-01-03 09:14:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for AccountInfo<'a> {
|
|
|
|
fn from((key, is_signer, account): (&'a Pubkey, bool, &'a mut Account)) -> Self {
|
2020-01-24 10:54:26 -08:00
|
|
|
Self::new(
|
2020-01-03 09:14:51 -08:00
|
|
|
key,
|
|
|
|
is_signer,
|
2020-01-24 10:54:26 -08:00
|
|
|
&mut account.lamports,
|
|
|
|
&mut account.data,
|
|
|
|
&account.owner,
|
|
|
|
)
|
2020-01-03 09:14:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a mut (Pubkey, Account)> for AccountInfo<'a> {
|
|
|
|
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
|
2020-01-24 10:54:26 -08:00
|
|
|
Self::new(
|
2020-01-03 09:14:51 -08:00
|
|
|
key,
|
2020-01-24 10:54:26 -08:00
|
|
|
false,
|
|
|
|
&mut account.lamports,
|
|
|
|
&mut account.data,
|
|
|
|
&account.owner,
|
|
|
|
)
|
2020-01-03 09:14:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_account_infos(accounts: &mut [(Pubkey, Account)]) -> Vec<AccountInfo> {
|
|
|
|
accounts.iter_mut().map(Into::into).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_is_signer_account_infos<'a>(
|
|
|
|
accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)],
|
|
|
|
) -> Vec<AccountInfo<'a>> {
|
|
|
|
accounts
|
|
|
|
.iter_mut()
|
2020-01-24 10:54:26 -08:00
|
|
|
.map(|(key, is_signer, account)| {
|
|
|
|
AccountInfo::new(
|
|
|
|
key,
|
|
|
|
*is_signer,
|
|
|
|
&mut account.lamports,
|
|
|
|
&mut account.data,
|
|
|
|
&account.owner,
|
|
|
|
)
|
2020-01-03 09:14:51 -08:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|