2020-10-26 09:14:57 -07:00
|
|
|
use crate::{clock::Epoch, pubkey::Pubkey};
|
|
|
|
use std::{cell::RefCell, cmp, fmt, 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
|
|
|
}
|