2018-09-26 17:55:36 -06:00
|
|
|
use 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)]
|
2018-09-26 17:33:18 -06:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
|
|
|
|
pub struct Account {
|
|
|
|
/// tokens in the account
|
2018-11-05 09:36:22 -07:00
|
|
|
pub tokens: u64,
|
2018-09-26 17:33:18 -06:00
|
|
|
/// user data
|
|
|
|
/// A transaction can write to its userdata
|
|
|
|
pub userdata: Vec<u8>,
|
|
|
|
/// contract id this contract belongs to
|
2018-11-12 09:29:17 -08:00
|
|
|
pub owner: Pubkey,
|
2018-10-16 09:43:49 -07:00
|
|
|
|
|
|
|
/// this account contains a program (and is strictly read-only)
|
|
|
|
pub executable: bool,
|
|
|
|
|
|
|
|
/// the loader for this program (Pubkey::default() for no loader)
|
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> {
|
|
|
|
pub key: &'a Pubkey,
|
|
|
|
pub account: &'a mut 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 {
|
|
|
|
KeyedAccount { key, account }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a mut (Pubkey, Account)> for KeyedAccount<'a> {
|
|
|
|
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
|
|
|
|
KeyedAccount { key, account }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_keyed_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
|
|
|
|
accounts.iter_mut().map(Into::into).collect()
|
|
|
|
}
|