2018-12-14 20:39:10 -08:00
|
|
|
use crate::pubkey::Pubkey;
|
2019-02-21 10:55:29 -08:00
|
|
|
use std::{cmp, fmt};
|
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)]
|
2019-02-21 10:55:29 -08:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Default, Eq, PartialEq)]
|
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-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,
|
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,
|
2019-03-14 10:48:27 -06:00
|
|
|
"Account {{ lamports: {} data.len: {} owner: {} executable: {}{} }}",
|
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-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 {
|
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?
|
2019-03-09 19:28:43 -08:00
|
|
|
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Account {
|
2018-09-26 17:33:18 -06:00
|
|
|
Account {
|
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,
|
2018-10-16 09:43:49 -07:00
|
|
|
executable: false,
|
2018-09-26 17:33:18 -06:00
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
) -> Result<Account, bincode::Error> {
|
|
|
|
let data = bincode::serialize(state)?;
|
|
|
|
Ok(Account {
|
|
|
|
lamports,
|
|
|
|
data,
|
|
|
|
owner: *owner,
|
|
|
|
executable: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-10 20:50:02 -06:00
|
|
|
pub type LamportCredit = u64;
|
|
|
|
|
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-06-27 17:25:10 -04:00
|
|
|
is_debitable: bool,
|
2018-11-29 12:32:16 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-27 17:25:10 -04:00
|
|
|
pub fn is_debitable(&self) -> bool {
|
|
|
|
self.is_debitable
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:32:16 -08:00
|
|
|
pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> KeyedAccount<'a> {
|
|
|
|
KeyedAccount {
|
2019-06-27 17:25:10 -04:00
|
|
|
is_signer,
|
|
|
|
is_debitable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
key,
|
2019-06-27 17:25:10 -04:00
|
|
|
account,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_credit_only(
|
|
|
|
key: &'a Pubkey,
|
|
|
|
is_signer: bool,
|
|
|
|
account: &'a mut Account,
|
|
|
|
) -> KeyedAccount<'a> {
|
|
|
|
KeyedAccount {
|
2018-11-29 12:32:16 -08:00
|
|
|
is_signer,
|
2019-06-27 17:25:10 -04:00
|
|
|
is_debitable: false,
|
|
|
|
key,
|
2018-11-29 12:32:16 -08:00
|
|
|
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,
|
2019-06-27 17:25:10 -04:00
|
|
|
is_debitable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
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,
|
2019-06-27 17:25:10 -04:00
|
|
|
is_debitable: true,
|
2018-11-29 12:32:16 -08:00
|
|
|
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()
|
|
|
|
}
|
2019-06-27 17:25:10 -04:00
|
|
|
|
|
|
|
pub fn create_keyed_credit_only_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
|
|
|
|
accounts
|
|
|
|
.iter_mut()
|
|
|
|
.map(|(key, account)| KeyedAccount {
|
|
|
|
is_signer: false,
|
|
|
|
is_debitable: false,
|
|
|
|
key,
|
|
|
|
account,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|