move Account to solana-sdk (#13198)

This commit is contained in:
Jack May
2020-10-28 22:01:07 -07:00
committed by GitHub
parent d5e439037b
commit c458d4b213
38 changed files with 460 additions and 459 deletions

170
sdk/src/account.rs Normal file
View File

@@ -0,0 +1,170 @@
use crate::{clock::Epoch, pubkey::Pubkey};
use solana_program::{account_info::AccountInfo, sysvar::Sysvar};
use std::{cell::RefCell, cmp, fmt, rc::Rc};
/// An Account with data that is stored on chain
#[repr(C)]
#[frozen_abi(digest = "727tKRcoDvPiAsXxfvfsUauvZ4tLSw2WSw4HQfRQJ9Xx")]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Default, AbiExample)]
#[serde(rename_all = "camelCase")]
pub struct Account {
/// lamports in the account
pub lamports: u64,
/// 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,
"Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} }}",
self.lamports,
self.data.len(),
self.owner,
self.executable,
self.rent_epoch,
data_str,
)
}
}
impl Account {
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
Self {
lamports,
data: vec![0u8; space],
owner: *owner,
..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,
) -> Result<Self, bincode::Error> {
let data = bincode::serialize(state)?;
Ok(Self {
lamports,
data,
owner: *owner,
..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,
) -> 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> {
if bincode::serialized_size(state)? > self.data.len() as u64 {
return Err(Box::new(bincode::ErrorKind::SizeLimit));
}
bincode::serialize_into(&mut self.data[..], state)
}
}
/// Create an `Account` from a `Sysvar`.
pub fn create_account<S: Sysvar>(sysvar: &S, lamports: u64) -> Account {
let data_len = S::size_of().max(bincode::serialized_size(sysvar).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &solana_program::sysvar::id());
to_account::<S>(sysvar, &mut account).unwrap();
account
}
/// Create a `Sysvar` from an `Account`'s data.
pub fn from_account<S: Sysvar>(account: &Account) -> Option<S> {
bincode::deserialize(&account.data).ok()
}
/// Serialize a `Sysvar` into an `Account`'s data.
pub fn to_account<S: Sysvar>(sysvar: &S, account: &mut Account) -> Option<()> {
bincode::serialize_into(&mut account.data[..], sysvar).ok()
}
/// Return the information required to construct an `AccountInfo`. Used by the
/// `AccountInfo` conversion implementations.
impl solana_program::account_info::Account for Account {
fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool, Epoch) {
(
&mut self.lamports,
&mut self.data,
&self.owner,
self.executable,
self.rent_epoch,
)
}
}
/// Create `AccountInfo`s
pub fn create_account_infos(accounts: &mut [(Pubkey, Account)]) -> Vec<AccountInfo> {
accounts.iter_mut().map(Into::into).collect()
}
/// Create `AccountInfo`s
pub fn create_is_signer_account_infos<'a>(
accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)],
) -> Vec<AccountInfo<'a>> {
accounts
.iter_mut()
.map(|(key, is_signer, account)| {
AccountInfo::new(
key,
*is_signer,
false,
&mut account.lamports,
&mut account.data,
&account.owner,
account.executable,
account.rent_epoch,
)
})
.collect()
}

50
sdk/src/account_utils.rs Normal file
View File

@@ -0,0 +1,50 @@
//! useful extras for Account state
use crate::{account::Account, instruction::InstructionError};
use bincode::ErrorKind;
/// Convenience trait to covert bincode errors to instruction errors.
pub trait StateMut<T> {
fn state(&self) -> Result<T, InstructionError>;
fn set_state(&mut self, state: &T) -> Result<(), InstructionError>;
}
pub trait State<T> {
fn state(&self) -> Result<T, InstructionError>;
fn set_state(&self, state: &T) -> Result<(), InstructionError>;
}
impl<T> StateMut<T> for Account
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
fn state(&self) -> Result<T, InstructionError> {
self.deserialize_data()
.map_err(|_| InstructionError::InvalidAccountData)
}
fn set_state(&mut self, state: &T) -> Result<(), InstructionError> {
self.serialize_data(state).map_err(|err| match *err {
ErrorKind::SizeLimit => InstructionError::AccountDataTooSmall,
_ => InstructionError::GenericError,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{account::Account, pubkey::Pubkey};
#[test]
fn test_account_state() {
let state = 42u64;
assert!(Account::default().set_state(&state).is_err());
let res = Account::default().state() as Result<u64, InstructionError>;
assert!(res.is_err());
let mut account = Account::new(0, std::mem::size_of::<u64>(), &Pubkey::default());
assert!(account.set_state(&state).is_ok());
let stored_state: u64 = account.state().unwrap();
assert_eq!(stored_state, state);
}
}

View File

@@ -61,7 +61,7 @@ impl FromStr for ClusterType {
}
}
#[frozen_abi(digest = "BAfWTFBwzed5FYCGAyMDq4HLsoZNTp8dZx2bqtYTCmGZ")]
#[frozen_abi(digest = "VxfEg5DXq5czYouMdcCbqDzUE8jGi3iSDSjzrrWp5iG")]
#[derive(Serialize, Deserialize, Debug, Clone, AbiExample)]
pub struct GenesisConfig {
/// when the network (bootstrap validator) was started relative to the UNIX Epoch

View File

@@ -1,11 +1,8 @@
use solana_program::{
account::Account,
use crate::{
account::{from_account, Account},
account_utils::{State, StateMut},
clock::Epoch,
instruction::InstructionError,
pubkey::Pubkey,
sysvar::Sysvar,
};
use solana_program::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey, sysvar::Sysvar};
use std::{
cell::{Ref, RefCell, RefMut},
iter::FromIterator,
@@ -215,13 +212,16 @@ pub fn from_keyed_account<S: Sysvar>(
if !S::check_id(keyed_account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
S::from_account(&*keyed_account.try_account_ref()?).ok_or(InstructionError::InvalidArgument)
from_account::<S>(&*keyed_account.try_account_ref()?).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pubkey::Pubkey;
use crate::{
account::{create_account, to_account},
pubkey::Pubkey,
};
use std::cell::RefCell;
#[repr(C)]
@@ -243,13 +243,13 @@ mod tests {
let key = crate::keyed_account::tests::id();
let wrong_key = Pubkey::new_unique();
let account = test_sysvar.create_account(42);
let test_sysvar = TestSysvar::from_account(&account).unwrap();
let account = create_account(&test_sysvar, 42);
let test_sysvar = from_account::<TestSysvar>(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let mut account = Account::new(42, TestSysvar::size_of(), &key);
test_sysvar.to_account(&mut account).unwrap();
let test_sysvar = TestSysvar::from_account(&account).unwrap();
to_account(&test_sysvar, &mut account).unwrap();
let test_sysvar = from_account::<TestSysvar>(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let account = RefCell::new(account);

View File

@@ -6,6 +6,8 @@ extern crate self as solana_sdk;
pub use solana_program::*;
pub mod account;
pub mod account_utils;
pub mod builtins;
pub mod client;
pub mod commitment_config;
@@ -21,11 +23,13 @@ pub mod inflation;
pub mod keyed_account;
pub mod log;
pub mod native_loader;
pub mod nonce_account;
pub mod nonce_keyed_account;
pub mod packet;
pub mod poh_config;
pub mod program_utils;
pub mod pubkey;
pub mod recent_blockhashes_account;
pub mod rpc_port;
pub mod secp256k1;
pub mod shred_version;

37
sdk/src/nonce_account.rs Normal file
View File

@@ -0,0 +1,37 @@
use crate::{
account::Account,
account_utils::StateMut,
fee_calculator::FeeCalculator,
hash::Hash,
nonce::{state::Versions, State},
};
use std::cell::RefCell;
pub fn create_account(lamports: u64) -> RefCell<Account> {
RefCell::new(
Account::new_data_with_space(
lamports,
&Versions::new_current(State::Uninitialized),
State::size(),
&crate::system_program::id(),
)
.expect("nonce_account"),
)
}
pub fn verify_nonce_account(acc: &Account, hash: &Hash) -> bool {
match StateMut::<Versions>::state(acc).map(|v| v.convert_to_current()) {
Ok(State::Initialized(ref data)) => *hash == data.blockhash,
_ => false,
}
}
pub fn fee_calculator_of(account: &Account) -> Option<FeeCalculator> {
let state = StateMut::<Versions>::state(account)
.ok()?
.convert_to_current();
match state {
State::Initialized(data) => Some(data.fee_calculator),
_ => None,
}
}

View File

@@ -1,6 +1,8 @@
use crate::keyed_account::KeyedAccount;
use crate::{
account_utils::State as AccountUtilsState, keyed_account::KeyedAccount,
nonce_account::create_account,
};
use solana_program::{
account_utils::State as AccountUtilsState,
instruction::InstructionError,
nonce::{self, state::Versions, State},
pubkey::Pubkey,
@@ -162,7 +164,7 @@ where
F: Fn(&KeyedAccount),
{
let pubkey = Pubkey::new_unique();
let account = solana_program::nonce::create_account(lamports);
let account = create_account(lamports);
let keyed_account = KeyedAccount::new(&pubkey, signer, &account);
f(&keyed_account)
}
@@ -174,10 +176,11 @@ mod test {
account_utils::State as AccountUtilsState,
keyed_account::KeyedAccount,
nonce::{self, State},
nonce_account::verify_nonce_account,
system_instruction::NonceError,
sysvar::recent_blockhashes::{create_test_recent_blockhashes, RecentBlockhashes},
};
use solana_program::{hash::Hash, nonce::utils::verify_nonce_account};
use solana_program::hash::Hash;
use std::iter::FromIterator;
#[test]

View File

@@ -0,0 +1,105 @@
use crate::account::{create_account, to_account, Account};
use solana_program::sysvar::recent_blockhashes::{
IntoIterSorted, IterItem, RecentBlockhashes, MAX_ENTRIES,
};
use std::{collections::BinaryHeap, iter::FromIterator};
pub fn update_account<'a, I>(account: &mut Account, recent_blockhash_iter: I) -> Option<()>
where
I: IntoIterator<Item = IterItem<'a>>,
{
let sorted = BinaryHeap::from_iter(recent_blockhash_iter);
let sorted_iter = IntoIterSorted::new(sorted);
let recent_blockhash_iter = sorted_iter.take(MAX_ENTRIES);
let recent_blockhashes = RecentBlockhashes::from_iter(recent_blockhash_iter);
to_account(&recent_blockhashes, account)
}
pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> Account
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account = create_account::<RecentBlockhashes>(&RecentBlockhashes::default(), lamports);
update_account(&mut account, recent_blockhash_iter).unwrap();
account
}
#[cfg(test)]
mod tests {
use super::*;
use crate::account::from_account;
use rand::{seq::SliceRandom, thread_rng};
use solana_program::{
fee_calculator::FeeCalculator,
hash::{Hash, HASH_BYTES},
sysvar::recent_blockhashes::Entry,
};
#[test]
fn test_create_account_empty() {
let account = create_account_with_data(42, vec![].into_iter());
let recent_blockhashes = from_account::<RecentBlockhashes>(&account).unwrap();
assert_eq!(recent_blockhashes, RecentBlockhashes::default());
}
#[test]
fn test_create_account_full() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES].into_iter(),
);
let recent_blockhashes = from_account::<RecentBlockhashes>(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
}
#[test]
fn test_create_account_truncate() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES + 1].into_iter(),
);
let recent_blockhashes = from_account::<RecentBlockhashes>(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
}
#[test]
fn test_create_account_unsorted() {
let def_fees = FeeCalculator::default();
let mut unsorted_blocks: Vec<_> = (0..MAX_ENTRIES)
.map(|i| {
(i as u64, {
// create hash with visibly recognizable ordering
let mut h = [0; HASH_BYTES];
h[HASH_BYTES - 1] = i as u8;
Hash::new(&h)
})
})
.collect();
unsorted_blocks.shuffle(&mut thread_rng());
let account = create_account_with_data(
42,
unsorted_blocks
.iter()
.map(|(i, hash)| IterItem(*i, hash, &def_fees)),
);
let recent_blockhashes = from_account::<RecentBlockhashes>(&account).unwrap();
let mut unsorted_recent_blockhashes: Vec<_> = unsorted_blocks
.iter()
.map(|(i, hash)| IterItem(*i, hash, &def_fees))
.collect();
unsorted_recent_blockhashes.sort();
unsorted_recent_blockhashes.reverse();
let expected_recent_blockhashes: Vec<_> = (unsorted_recent_blockhashes
.into_iter()
.map(|IterItem(_, b, f)| Entry::new(b, f)))
.collect();
assert_eq!(*recent_blockhashes, expected_recent_blockhashes);
}
}