Files
solana/sdk/program/src/account.rs

112 lines
3.4 KiB
Rust
Raw Normal View History

use crate::{clock::Epoch, pubkey::Pubkey};
use std::{cell::RefCell, cmp, fmt, rc::Rc};
/// An Account with data that is stored on chain
#[repr(C)]
Separate the "program" feature of `solana-sdk` into a new crate called `solana-program` (bp #12989) (#13131) * Add solana-program-sdk boilerplate (cherry picked from commit 3718771ffb9caad0bc1c180e488f699ce895a10e) # Conflicts: # sdk/Cargo.toml * Initial population of solana-program-sdk (cherry picked from commit 63db3242043602c7b9765a066b6f8d01ef20454e) # Conflicts: # Cargo.lock * Port programs to solana-program-sdk (cherry picked from commit fe68f7f786acf13e5b1f1fe4c54eb7af7b549391) # Conflicts: # programs/bpf/Cargo.lock # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Only activate legacy program feature for the solana-sdk crate (cherry picked from commit 85c51f5787895c97220d3a2f8ce9be6b9cbdd6c6) * Run serum-dex unit tests (cherry picked from commit 92ce381d60d65a73973fc20cdd4772334f973b96) * Rename solana-program-sdk to solana-program (cherry picked from commit dd711ab5fbf96c1efcfdf9488397e8af2568182a) # Conflicts: # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Update frozen_abi hashes The movement of files in sdk/ caused ABI hashes to change (cherry picked from commit a4956844bdd081e7b90508066c579f29be306ce7) * Resolve merge conflicts Co-authored-by: Michael Vines <mvines@gmail.com>
2020-10-24 17:25:22 +00:00
#[frozen_abi(digest = "Upy4zg4EXZTnY371b4JPrGTh2kLcYpRno2K2pvjbN4e")]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Default, AbiExample)]
#[serde(rename_all = "camelCase")]
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
#[serde(with = "serde_bytes")]
pub data: Vec<u8>,
/// the program that owns this account. If executable, the program that loads this account.
pub owner: Pubkey,
/// this account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// the epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}
impl fmt::Debug for Account {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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()))
} 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,
self.data.len(),
self.owner,
self.executable,
self.rent_epoch,
data_str,
)
}
}
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,
data: vec![0u8; space],
owner: *owner,
2020-01-20 15:27:36 -08:00
..Self::default()
}
}
pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::new(lamports, space, owner)))
}
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> {
let data = bincode::serialize(state)?;
2020-01-20 15:27:36 -08:00
Ok(Self {
lamports,
data,
owner: *owner,
2020-01-20 15:27:36 -08:00
..Self::default()
})
}
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)?))
}
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> {
let mut account = Self::new(lamports, space, owner);
account.serialize_data(state)?;
Ok(account)
}
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,
)?))
}
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));
}
bincode::serialize_into(&mut self.data[..], state)
}
}