Boot the mut (#7926)

This commit is contained in:
Jack May
2020-01-22 17:54:06 -08:00
committed by GitHub
parent e54bf563b5
commit c95e5346a4
37 changed files with 772 additions and 812 deletions

View File

@ -247,8 +247,8 @@ impl<'a> From<(&'a Pubkey, bool, &'a RefCell<Account>)> for KeyedAccount<'a> {
}
}
impl<'a> From<&'a mut (&'a Pubkey, &'a RefCell<Account>)> for KeyedAccount<'a> {
fn from((key, account): &'a mut (&'a Pubkey, &'a RefCell<Account>)) -> Self {
impl<'a> From<&'a (&'a Pubkey, &'a RefCell<Account>)> for KeyedAccount<'a> {
fn from((key, account): &'a (&'a Pubkey, &'a RefCell<Account>)) -> Self {
Self {
is_signer: false,
is_writable: true,
@ -259,16 +259,16 @@ impl<'a> From<&'a mut (&'a Pubkey, &'a RefCell<Account>)> for KeyedAccount<'a> {
}
pub fn create_keyed_accounts<'a>(
accounts: &'a mut [(&'a Pubkey, &'a RefCell<Account>)],
accounts: &'a [(&'a Pubkey, &'a RefCell<Account>)],
) -> Vec<KeyedAccount<'a>> {
accounts.iter_mut().map(Into::into).collect()
accounts.iter().map(Into::into).collect()
}
pub fn create_keyed_is_signer_accounts<'a>(
accounts: &'a mut [(&'a Pubkey, bool, &'a mut RefCell<Account>)],
accounts: &'a [(&'a Pubkey, bool, &'a RefCell<Account>)],
) -> Vec<KeyedAccount<'a>> {
accounts
.iter_mut()
.iter()
.map(|(key, is_signer, account)| KeyedAccount {
is_signer: *is_signer,
is_writable: false,
@ -279,10 +279,10 @@ pub fn create_keyed_is_signer_accounts<'a>(
}
pub fn create_keyed_readonly_accounts(
accounts: &mut [(Pubkey, RefCell<Account>)],
accounts: &[(Pubkey, RefCell<Account>)],
) -> Vec<KeyedAccount> {
accounts
.iter_mut()
.iter()
.map(|(key, account)| KeyedAccount {
is_signer: false,
is_writable: false,

View File

@ -6,12 +6,16 @@ use crate::{
use bincode::ErrorKind;
/// Convenience trait to covert bincode errors to instruction errors.
pub trait State<T> {
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> State<T> for Account
impl<T> StateMut<T> for Account
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
@ -34,7 +38,7 @@ where
fn state(&self) -> Result<T, InstructionError> {
self.try_account_ref()?.state()
}
fn set_state(&mut self, state: &T) -> Result<(), InstructionError> {
fn set_state(&self, state: &T) -> Result<(), InstructionError> {
self.try_account_ref_mut()?.set_state(state)
}
}

View File

@ -4,7 +4,7 @@ use num_traits::{FromPrimitive, ToPrimitive};
// Native program ENTRYPOINT prototype
pub type Entrypoint = unsafe extern "C" fn(
program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<(), InstructionError>;
@ -28,7 +28,7 @@ pub type Entrypoint = unsafe extern "C" fn(
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// keyed_accounts: &mut [KeyedAccount],
/// keyed_accounts: &[KeyedAccount],
/// instruction_data: &[u8],
/// ) -> Result<(), InstructionError> {
/// // Process an instruction
@ -59,7 +59,7 @@ pub type Entrypoint = unsafe extern "C" fn(
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// keyed_accounts: &mut [KeyedAccount],
/// keyed_accounts: &[KeyedAccount],
/// instruction_data: &[u8],
/// ) -> Result<(), InstructionError> {
/// // Process an instruction
@ -91,7 +91,7 @@ macro_rules! declare_program(
#[no_mangle]
pub extern "C" fn $name(
program_id: &$crate::pubkey::Pubkey,
keyed_accounts: &mut [$crate::account::KeyedAccount],
keyed_accounts: &[$crate::account::KeyedAccount],
instruction_data: &[u8],
) -> Result<(), $crate::instruction::InstructionError> {
$entrypoint(program_id, keyed_accounts, instruction_data)

View File

@ -46,26 +46,26 @@ impl NonceState {
pub trait NonceAccount {
fn advance_nonce_account(
&mut self,
&self,
recent_blockhashes: &RecentBlockhashes,
signers: &HashSet<Pubkey>,
) -> Result<(), InstructionError>;
fn withdraw_nonce_account(
&mut self,
&self,
lamports: u64,
to: &mut KeyedAccount,
to: &KeyedAccount,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
signers: &HashSet<Pubkey>,
) -> Result<(), InstructionError>;
fn initialize_nonce_account(
&mut self,
&self,
nonce_authority: &Pubkey,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
) -> Result<(), InstructionError>;
fn authorize_nonce_account(
&mut self,
&self,
nonce_authority: &Pubkey,
signers: &HashSet<Pubkey>,
) -> Result<(), InstructionError>;
@ -73,7 +73,7 @@ pub trait NonceAccount {
impl<'a> NonceAccount for KeyedAccount<'a> {
fn advance_nonce_account(
&mut self,
&self,
recent_blockhashes: &RecentBlockhashes,
signers: &HashSet<Pubkey>,
) -> Result<(), InstructionError> {
@ -98,9 +98,9 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
}
fn withdraw_nonce_account(
&mut self,
&self,
lamports: u64,
to: &mut KeyedAccount,
to: &KeyedAccount,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
signers: &HashSet<Pubkey>,
@ -138,7 +138,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
}
fn initialize_nonce_account(
&mut self,
&self,
nonce_authority: &Pubkey,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
@ -162,7 +162,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
}
fn authorize_nonce_account(
&mut self,
&self,
nonce_authority: &Pubkey,
signers: &HashSet<Pubkey>,
) -> Result<(), InstructionError> {
@ -192,14 +192,14 @@ pub fn create_account(lamports: u64) -> RefCell<Account> {
/// Convenience function for working with keyed accounts in tests
#[cfg(not(feature = "program"))]
pub fn with_test_keyed_account<F>(lamports: u64, signer: bool, mut f: F)
pub fn with_test_keyed_account<F>(lamports: u64, signer: bool, f: F)
where
F: FnMut(&mut KeyedAccount),
F: Fn(&KeyedAccount),
{
let pubkey = Pubkey::new_rand();
let account = create_account(lamports);
let mut keyed_account = KeyedAccount::new(&pubkey, signer, &account);
f(&mut keyed_account)
let keyed_account = KeyedAccount::new(&pubkey, signer, &account);
f(&keyed_account)
}
#[cfg(test)]
@ -262,7 +262,7 @@ mod test {
let stored = recent_blockhashes[0];
// Third nonce instruction for fun and profit
assert_eq!(state, NonceState::Initialized(meta, stored));
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let recent_blockhashes = create_test_recent_blockhashes(0);
let withdraw_lamports = keyed_account.account.borrow().lamports;
let expect_nonce_lamports =
@ -271,7 +271,7 @@ mod test {
keyed_account
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -304,7 +304,7 @@ mod test {
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
.unwrap();
let pubkey = nonce_account.account.borrow().owner.clone();
let mut nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
let nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
let state: NonceState = nonce_account.state().unwrap();
assert_eq!(state, NonceState::Initialized(meta, stored));
let signers = HashSet::new();
@ -428,7 +428,7 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
let state: NonceState = nonce_keyed.state().unwrap();
assert_eq!(state, NonceState::Uninitialized);
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let recent_blockhashes = create_test_recent_blockhashes(0);
@ -439,7 +439,7 @@ mod test {
nonce_keyed
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -467,13 +467,13 @@ mod test {
with_test_keyed_account(min_lamports + 42, false, |nonce_keyed| {
let state: NonceState = nonce_keyed.state().unwrap();
assert_eq!(state, NonceState::Uninitialized);
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let signers = HashSet::new();
let recent_blockhashes = create_test_recent_blockhashes(0);
let lamports = nonce_keyed.account.borrow().lamports;
let result = nonce_keyed.withdraw_nonce_account(
lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -493,14 +493,14 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
let state: NonceState = nonce_keyed.state().unwrap();
assert_eq!(state, NonceState::Uninitialized);
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let recent_blockhashes = create_test_recent_blockhashes(0);
let lamports = nonce_keyed.account.borrow().lamports + 1;
let result = nonce_keyed.withdraw_nonce_account(
lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -518,7 +518,7 @@ mod test {
};
let min_lamports = rent.minimum_balance(NonceState::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let recent_blockhashes = create_test_recent_blockhashes(0);
@ -529,7 +529,7 @@ mod test {
nonce_keyed
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -546,7 +546,7 @@ mod test {
nonce_keyed
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -579,7 +579,7 @@ mod test {
let state: NonceState = nonce_keyed.state().unwrap();
let stored = recent_blockhashes[0];
assert_eq!(state, NonceState::Initialized(meta, stored));
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let withdraw_lamports = nonce_keyed.account.borrow().lamports - min_lamports;
let nonce_expect_lamports =
nonce_keyed.account.borrow().lamports - withdraw_lamports;
@ -587,7 +587,7 @@ mod test {
nonce_keyed
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -606,7 +606,7 @@ mod test {
nonce_keyed
.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -631,13 +631,13 @@ mod test {
nonce_keyed
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
.unwrap();
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -660,14 +660,14 @@ mod test {
nonce_keyed
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
.unwrap();
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let recent_blockhashes = create_test_recent_blockhashes(63);
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let withdraw_lamports = nonce_keyed.account.borrow().lamports + 1;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,
@ -690,14 +690,14 @@ mod test {
nonce_keyed
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
.unwrap();
with_test_keyed_account(42, false, |mut to_keyed| {
with_test_keyed_account(42, false, |to_keyed| {
let recent_blockhashes = create_test_recent_blockhashes(63);
let mut signers = HashSet::new();
signers.insert(nonce_keyed.signer_key().unwrap().clone());
let withdraw_lamports = nonce_keyed.account.borrow().lamports - min_lamports + 1;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
&mut to_keyed,
&to_keyed,
&recent_blockhashes,
&rent,
&signers,