solana/sdk/src/account.rs

231 lines
6.1 KiB
Rust
Raw Normal View History

2019-09-20 13:21:12 -07:00
use crate::hash::Hash;
2019-09-06 15:33:58 -07:00
use crate::{clock::Epoch, pubkey::Pubkey};
use std::{cmp, fmt, iter::FromIterator};
/// An Account with data that is stored on chain
#[repr(C)]
2019-09-20 13:21:12 -07:00
#[derive(Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Account {
2019-03-05 16:28:14 -08:00
/// lamports in the account
pub lamports: u64,
2018-11-12 09:55:28 -08:00
/// data held in this account
#[serde(with = "serde_bytes")]
pub data: Vec<u8>,
/// the program that owns this account. If executable, the program that loads this account.
pub owner: Pubkey,
/// this account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// the epoch at which this account will next owe rent
pub rent_epoch: Epoch,
2019-09-20 13:21:12 -07:00
/// Hash of this account's state, skip serializing as to not expose to external api
/// Used for keeping the accounts state hash updated.
#[serde(skip)]
2019-09-20 13:21:12 -07:00
pub hash: Hash,
}
2019-09-20 13:21:12 -07:00
/// skip comparison of account.hash, since it is only meaningful when the account is loaded in a
/// given fork and some tests do not have that.
impl PartialEq for Account {
fn eq(&self, other: &Self) -> bool {
self.lamports == other.lamports
&& self.data == other.data
&& self.owner == other.owner
&& self.executable == other.executable
&& self.rent_epoch == other.rent_epoch
}
}
impl Eq for Account {}
impl fmt::Debug for Account {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data_len = cmp::min(64, self.data.len());
let data_str = if data_len > 0 {
format!(" data: {}", hex::encode(self.data[..data_len].to_vec()))
} else {
"".to_string()
};
write!(
f,
2019-09-20 13:21:12 -07:00
"Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} hash: {} }}",
2019-03-05 16:28:14 -08:00
self.lamports,
self.data.len(),
self.owner,
self.executable,
self.rent_epoch,
data_str,
2019-09-20 13:21:12 -07:00
self.hash,
)
}
}
impl Account {
2020-01-20 15:27:36 -08:00
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
Self {
2019-03-05 16:28:14 -08:00
lamports,
data: vec![0u8; space],
owner: *owner,
2020-01-20 15:27:36 -08:00
..Self::default()
}
}
pub fn new_data<T: serde::Serialize>(
lamports: u64,
state: &T,
owner: &Pubkey,
2020-01-20 15:27:36 -08:00
) -> Result<Self, bincode::Error> {
let data = bincode::serialize(state)?;
2020-01-20 15:27:36 -08:00
Ok(Self {
lamports,
data,
owner: *owner,
2020-01-20 15:27:36 -08:00
..Self::default()
})
}
pub fn new_data_with_space<T: serde::Serialize>(
lamports: u64,
state: &T,
space: usize,
owner: &Pubkey,
2020-01-20 15:27:36 -08:00
) -> Result<Self, bincode::Error> {
let mut account = Self::new(lamports, space, owner);
account.serialize_data(state)?;
Ok(account)
}
pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
bincode::deserialize(&self.data)
}
pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
2019-06-19 21:29:36 -07:00
if bincode::serialized_size(state)? > self.data.len() as u64 {
return Err(Box::new(bincode::ErrorKind::SizeLimit));
}
bincode::serialize_into(&mut self.data[..], state)
}
}
#[repr(C)]
#[derive(Debug)]
pub struct KeyedAccount<'a> {
is_signer: bool, // Transaction was signed by this account's key
is_writable: bool,
key: &'a Pubkey,
pub account: &'a mut Account,
}
impl<'a> KeyedAccount<'a> {
pub fn signer_key(&self) -> Option<&Pubkey> {
if self.is_signer {
Some(self.key)
} else {
None
}
}
pub fn unsigned_key(&self) -> &Pubkey {
self.key
}
pub fn is_writable(&self) -> bool {
self.is_writable
}
2020-01-03 09:14:51 -08:00
pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> Self {
Self {
is_signer,
is_writable: true,
key,
account,
}
}
2020-01-03 09:14:51 -08:00
pub fn new_readonly(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> Self {
Self {
is_signer,
is_writable: false,
key,
account,
}
}
}
impl<'a> From<(&'a Pubkey, &'a mut Account)> for KeyedAccount<'a> {
fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self {
2020-01-03 09:14:51 -08:00
Self {
is_signer: false,
is_writable: true,
key,
account,
}
}
}
impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for KeyedAccount<'a> {
fn from((key, is_signer, account): (&'a Pubkey, bool, &'a mut Account)) -> Self {
2020-01-03 09:14:51 -08:00
Self {
is_signer,
is_writable: true,
key,
account,
}
}
}
impl<'a> From<&'a mut (Pubkey, Account)> for KeyedAccount<'a> {
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
2020-01-03 09:14:51 -08:00
Self {
is_signer: false,
is_writable: true,
key,
account,
}
}
}
pub fn create_keyed_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
accounts.iter_mut().map(Into::into).collect()
}
pub fn create_keyed_is_signer_accounts<'a>(
accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)],
) -> Vec<KeyedAccount<'a>> {
accounts
.iter_mut()
.map(|(key, is_signer, account)| KeyedAccount {
is_signer: *is_signer,
is_writable: false,
key,
account,
})
.collect()
}
pub fn create_keyed_readonly_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
accounts
.iter_mut()
.map(|(key, account)| KeyedAccount {
is_signer: false,
is_writable: false,
key,
account,
})
.collect()
}
/// Return all the signers from a set of KeyedAccounts
pub fn get_signers<A>(keyed_accounts: &[KeyedAccount]) -> A
where
A: FromIterator<Pubkey>,
{
keyed_accounts
.iter()
.filter_map(|keyed_account| keyed_account.signer_key())
.cloned()
.collect::<A>()
}