2018-12-14 20:39:10 -08:00
|
|
|
use crate::pubkey::Pubkey;
|
2018-09-26 17:33:18 -06:00
|
|
|
|
|
|
|
/// An Account with userdata that is stored on chain
|
2018-10-04 09:44:44 -07:00
|
|
|
#[repr(C)]
|
2019-01-08 09:20:25 -08:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
|
2018-09-26 17:33:18 -06:00
|
|
|
pub struct Account {
|
|
|
|
/// tokens in the account
|
2018-11-05 09:36:22 -07:00
|
|
|
pub tokens: u64,
|
2018-11-12 09:55:28 -08:00
|
|
|
/// data held in this account
|
2018-09-26 17:33:18 -06:00
|
|
|
pub userdata: Vec<u8>,
|
2018-11-12 09:55:28 -08:00
|
|
|
/// the program that owns this account
|
2018-11-12 09:29:17 -08:00
|
|
|
pub owner: Pubkey,
|
2018-11-12 09:55:28 -08:00
|
|
|
/// this account's userdata contains a loaded program (and is now read-only)
|
2018-10-16 09:43:49 -07:00
|
|
|
pub executable: bool,
|
2018-11-12 09:55:28 -08:00
|
|
|
/// the loader for this account
|
|
|
|
/// (Pubkey::default() if the account is not executable and thus was never 'loaded')
|
2018-11-12 09:11:24 -08:00
|
|
|
pub loader: Pubkey,
|
2018-09-26 17:33:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Account {
|
2018-11-12 09:29:17 -08:00
|
|
|
// TODO do we want to add executable and leader_owner even though they should always be false/default?
|
|
|
|
pub fn new(tokens: u64, space: usize, owner: Pubkey) -> Account {
|
2018-09-26 17:33:18 -06:00
|
|
|
Account {
|
|
|
|
tokens,
|
|
|
|
userdata: vec![0u8; space],
|
2018-11-12 09:29:17 -08:00
|
|
|
owner,
|
2018-10-16 09:43:49 -07:00
|
|
|
executable: false,
|
2018-11-12 09:11:24 -08:00
|
|
|
loader: Pubkey::default(),
|
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
|
|
|
|
key: &'a Pubkey,
|
2018-09-26 20:23:59 -06:00
|
|
|
pub account: &'a mut Account,
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> KeyedAccount<'a> {
|
|
|
|
KeyedAccount {
|
|
|
|
key,
|
|
|
|
is_signer,
|
|
|
|
account,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:29:09 -06:00
|
|
|
impl<'a> From<(&'a Pubkey, &'a mut Account)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self {
|
2018-11-29 12:32:16 -08:00
|
|
|
KeyedAccount {
|
|
|
|
is_signer: false,
|
|
|
|
key,
|
|
|
|
account,
|
|
|
|
}
|
2018-10-09 06:29:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a mut (Pubkey, Account)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
|
2018-11-29 12:32:16 -08:00
|
|
|
KeyedAccount {
|
|
|
|
is_signer: false,
|
|
|
|
key,
|
|
|
|
account,
|
|
|
|
}
|
2018-10-09 06:29:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_keyed_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
|
|
|
|
accounts.iter_mut().map(Into::into).collect()
|
|
|
|
}
|