2020-05-12 23:39:46 +08:00
|
|
|
use crate::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey};
|
2020-01-22 09:11:56 -08:00
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefCell, RefMut},
|
|
|
|
cmp, fmt,
|
|
|
|
iter::FromIterator,
|
|
|
|
rc::Rc,
|
|
|
|
};
|
2018-09-26 17:33:18 -06:00
|
|
|
|
2019-03-14 10:48:27 -06:00
|
|
|
/// An Account with data that is stored on chain
|
2018-10-04 09:44:44 -07:00
|
|
|
#[repr(C)]
|
2020-10-22 10:32:35 -07:00
|
|
|
#[frozen_abi(digest = "Upy4zg4EXZTnY371b4JPrGTh2kLcYpRno2K2pvjbN4e")]
|
2020-07-06 20:22:23 +09:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Default, AbiExample)]
|
2020-01-15 00:25:45 -07:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2018-09-26 17:33:18 -06:00
|
|
|
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
|
2019-10-18 17:18:06 -07:00
|
|
|
#[serde(with = "serde_bytes")]
|
2019-03-14 10:48:27 -06:00
|
|
|
pub data: Vec<u8>,
|
2019-02-14 10:57:54 -07:00
|
|
|
/// the program that owns this account. If executable, the program that loads this account.
|
2018-11-12 09:29:17 -08:00
|
|
|
pub owner: Pubkey,
|
2019-03-14 10:48:27 -06:00
|
|
|
/// this account's data contains a loaded program (and is now read-only)
|
2018-10-16 09:43:49 -07:00
|
|
|
pub executable: bool,
|
2019-08-23 14:04:53 -07:00
|
|
|
/// the epoch at which this account will next owe rent
|
|
|
|
pub rent_epoch: Epoch,
|
2018-09-26 17:33:18 -06:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:55:29 -08:00
|
|
|
impl fmt::Debug for Account {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-14 10:48:27 -06:00
|
|
|
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()))
|
2019-02-21 10:55:29 -08:00
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
write!(
|
|
|
|
f,
|
2020-05-12 23:39:46 +08:00
|
|
|
"Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} }}",
|
2019-03-05 16:28:14 -08:00
|
|
|
self.lamports,
|
2019-03-14 10:48:27 -06:00
|
|
|
self.data.len(),
|
2019-02-21 10:55:29 -08:00
|
|
|
self.owner,
|
|
|
|
self.executable,
|
2019-08-23 14:04:53 -07:00
|
|
|
self.rent_epoch,
|
2019-03-14 10:48:27 -06:00
|
|
|
data_str,
|
2019-02-21 10:55:29 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:18 -06:00
|
|
|
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,
|
2019-03-14 10:48:27 -06:00
|
|
|
data: vec![0u8; space],
|
2019-03-09 19:28:43 -08:00
|
|
|
owner: *owner,
|
2020-01-20 15:27:36 -08:00
|
|
|
..Self::default()
|
2018-09-26 17:33:18 -06:00
|
|
|
}
|
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::new(RefCell::new(Self::new(lamports, space, owner)))
|
|
|
|
}
|
2019-04-04 12:01:09 -07:00
|
|
|
|
2019-06-17 15:58:05 -07:00
|
|
|
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> {
|
2019-06-17 15:58:05 -07:00
|
|
|
let data = bincode::serialize(state)?;
|
2020-01-20 15:27:36 -08:00
|
|
|
Ok(Self {
|
2019-06-17 15:58:05 -07:00
|
|
|
lamports,
|
|
|
|
data,
|
|
|
|
owner: *owner,
|
2020-01-20 15:27:36 -08:00
|
|
|
..Self::default()
|
2019-06-17 15:58:05 -07:00
|
|
|
})
|
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn new_ref_data<T: serde::Serialize>(
|
|
|
|
lamports: u64,
|
|
|
|
state: &T,
|
|
|
|
owner: &Pubkey,
|
|
|
|
) -> Result<RefCell<Self>, bincode::Error> {
|
|
|
|
Ok(RefCell::new(Self::new_data(lamports, state, owner)?))
|
|
|
|
}
|
2019-06-17 15:58:05 -07:00
|
|
|
|
2019-09-04 13:34:09 -07:00
|
|
|
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> {
|
2019-09-04 13:34:09 -07:00
|
|
|
let mut account = Self::new(lamports, space, owner);
|
|
|
|
|
|
|
|
account.serialize_data(state)?;
|
|
|
|
|
|
|
|
Ok(account)
|
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn new_ref_data_with_space<T: serde::Serialize>(
|
|
|
|
lamports: u64,
|
|
|
|
state: &T,
|
|
|
|
space: usize,
|
|
|
|
owner: &Pubkey,
|
|
|
|
) -> Result<RefCell<Self>, bincode::Error> {
|
|
|
|
Ok(RefCell::new(Self::new_data_with_space(
|
|
|
|
lamports, state, space, owner,
|
|
|
|
)?))
|
|
|
|
}
|
2019-09-04 13:34:09 -07:00
|
|
|
|
2019-04-04 12:01:09 -07:00
|
|
|
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));
|
|
|
|
}
|
2019-04-04 12:01:09 -07:00
|
|
|
bincode::serialize_into(&mut self.data[..], state)
|
|
|
|
}
|
2018-09-26 17:33:18 -06:00
|
|
|
}
|
2018-09-26 20:23:59 -06:00
|
|
|
|
2018-10-04 09:44:44 -07:00
|
|
|
#[repr(C)]
|
2018-09-26 20:23:59 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct KeyedAccount<'a> {
|
2018-11-29 12:32:16 -08:00
|
|
|
is_signer: bool, // Transaction was signed by this account's key
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: bool,
|
2018-11-29 12:32:16 -08:00
|
|
|
key: &'a Pubkey,
|
2020-01-22 09:11:56 -08:00
|
|
|
pub account: &'a RefCell<Account>,
|
2018-09-26 20:23:59 -06:00
|
|
|
}
|
2018-10-09 06:29:09 -06:00
|
|
|
|
2018-11-29 12:32:16 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:38:35 -07:00
|
|
|
pub fn is_writable(&self) -> bool {
|
|
|
|
self.is_writable
|
2019-06-27 17:25:10 -04:00
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn lamports(&self) -> Result<u64, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.lamports)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data_len(&self) -> Result<usize, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.data.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data_is_empty(&self) -> Result<bool, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.data.is_empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn owner(&self) -> Result<Pubkey, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn executable(&self) -> Result<bool, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.executable)
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:52:09 -08:00
|
|
|
pub fn rent_epoch(&self) -> Result<Epoch, InstructionError> {
|
|
|
|
Ok(self.try_borrow()?.rent_epoch)
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn try_account_ref(&'a self) -> Result<Ref<Account>, InstructionError> {
|
|
|
|
self.try_borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_account_ref_mut(&'a self) -> Result<RefMut<Account>, InstructionError> {
|
|
|
|
self.try_borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_borrow(&self) -> Result<Ref<Account>, InstructionError> {
|
|
|
|
self.account
|
|
|
|
.try_borrow()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowFailed)
|
|
|
|
}
|
|
|
|
fn try_borrow_mut(&self) -> Result<RefMut<Account>, InstructionError> {
|
|
|
|
self.account
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a RefCell<Account>) -> Self {
|
2020-01-03 09:14:51 -08:00
|
|
|
Self {
|
2019-06-27 17:25:10 -04:00
|
|
|
is_signer,
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
key,
|
2019-06-27 17:25:10 -04:00
|
|
|
account,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn new_readonly(key: &'a Pubkey, is_signer: bool, account: &'a RefCell<Account>) -> Self {
|
2020-01-03 09:14:51 -08:00
|
|
|
Self {
|
2018-11-29 12:32:16 -08:00
|
|
|
is_signer,
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: false,
|
2019-06-27 17:25:10 -04:00
|
|
|
key,
|
2018-11-29 12:32:16 -08:00
|
|
|
account,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
impl<'a> PartialEq for KeyedAccount<'a> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.key == other.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<(&'a Pubkey, &'a RefCell<Account>)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, account): (&'a Pubkey, &'a RefCell<Account>)) -> Self {
|
2020-01-03 09:14:51 -08:00
|
|
|
Self {
|
2018-11-29 12:32:16 -08:00
|
|
|
is_signer: false,
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
key,
|
|
|
|
account,
|
|
|
|
}
|
2018-10-09 06:29:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
impl<'a> From<(&'a Pubkey, bool, &'a RefCell<Account>)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, is_signer, account): (&'a Pubkey, bool, &'a RefCell<Account>)) -> Self {
|
2020-01-03 09:14:51 -08:00
|
|
|
Self {
|
2019-12-04 19:24:12 -08:00
|
|
|
is_signer,
|
|
|
|
is_writable: true,
|
|
|
|
key,
|
|
|
|
account,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 17:54:06 -08:00
|
|
|
impl<'a> From<&'a (&'a Pubkey, &'a RefCell<Account>)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, account): &'a (&'a Pubkey, &'a RefCell<Account>)) -> Self {
|
2020-01-03 09:14:51 -08:00
|
|
|
Self {
|
2018-11-29 12:32:16 -08:00
|
|
|
is_signer: false,
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
key,
|
|
|
|
account,
|
|
|
|
}
|
2018-10-09 06:29:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn create_keyed_accounts<'a>(
|
2020-01-22 17:54:06 -08:00
|
|
|
accounts: &'a [(&'a Pubkey, &'a RefCell<Account>)],
|
2020-01-22 09:11:56 -08:00
|
|
|
) -> Vec<KeyedAccount<'a>> {
|
2020-01-22 17:54:06 -08:00
|
|
|
accounts.iter().map(Into::into).collect()
|
2018-10-09 06:29:09 -06:00
|
|
|
}
|
2019-06-27 17:25:10 -04:00
|
|
|
|
2019-12-04 19:24:12 -08:00
|
|
|
pub fn create_keyed_is_signer_accounts<'a>(
|
2020-01-22 17:54:06 -08:00
|
|
|
accounts: &'a [(&'a Pubkey, bool, &'a RefCell<Account>)],
|
2019-12-04 19:24:12 -08:00
|
|
|
) -> Vec<KeyedAccount<'a>> {
|
|
|
|
accounts
|
2020-01-22 17:54:06 -08:00
|
|
|
.iter()
|
2019-12-04 19:24:12 -08:00
|
|
|
.map(|(key, is_signer, account)| KeyedAccount {
|
|
|
|
is_signer: *is_signer,
|
|
|
|
is_writable: false,
|
|
|
|
key,
|
|
|
|
account,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
pub fn create_keyed_readonly_accounts(
|
2020-01-22 17:54:06 -08:00
|
|
|
accounts: &[(Pubkey, RefCell<Account>)],
|
2020-01-22 09:11:56 -08:00
|
|
|
) -> Vec<KeyedAccount> {
|
2019-06-27 17:25:10 -04:00
|
|
|
accounts
|
2020-01-22 17:54:06 -08:00
|
|
|
.iter()
|
2019-06-27 17:25:10 -04:00
|
|
|
.map(|(key, account)| KeyedAccount {
|
|
|
|
is_signer: false,
|
2019-11-05 09:38:35 -07:00
|
|
|
is_writable: false,
|
2019-06-27 17:25:10 -04:00
|
|
|
key,
|
|
|
|
account,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2019-10-14 15:02:24 -07:00
|
|
|
|
|
|
|
/// 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>()
|
|
|
|
}
|
2020-06-17 10:39:14 -07:00
|
|
|
|
|
|
|
/// Return the next KeyedAccount or a NotEnoughAccountKeys error
|
|
|
|
pub fn next_keyed_account<'a, 'b, I: Iterator<Item = &'a KeyedAccount<'b>>>(
|
|
|
|
iter: &mut I,
|
|
|
|
) -> Result<I::Item, InstructionError> {
|
|
|
|
iter.next().ok_or(InstructionError::NotEnoughAccountKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the first keyed_account is executable, used to determine if
|
|
|
|
/// the loader should call a program's 'main'
|
|
|
|
pub fn is_executable(keyed_accounts: &[KeyedAccount]) -> Result<bool, InstructionError> {
|
|
|
|
Ok(!keyed_accounts.is_empty() && keyed_accounts[0].executable()?)
|
|
|
|
}
|