Allow the same account to be passed multiple times to a single instruction (#7795)

This commit is contained in:
Jack May
2020-01-22 09:11:56 -08:00
committed by GitHub
parent d854e90c23
commit 023074650f
33 changed files with 1392 additions and 765 deletions

View File

@ -421,13 +421,13 @@ fn check_redeemable(
owner: &mut StorageAccount,
) -> Result<(), InstructionError> {
let rewards = (credits.redeemable as f64 * storage_point_value) as u64;
if rewards_pool.account.lamports < rewards {
if rewards_pool.lamports()? < rewards {
Err(InstructionError::CustomError(
StorageError::RewardPoolDepleted as u32,
))
} else {
if rewards >= 1 {
rewards_pool.account.lamports -= rewards;
rewards_pool.try_account_ref_mut()?.lamports -= rewards;
owner.account.lamports += rewards;
//clear credits
credits.redeemable = 0;
@ -503,7 +503,7 @@ fn count_valid_proofs(
mod tests {
use super::*;
use crate::{id, rewards_pools};
use std::collections::BTreeMap;
use std::{cell::RefCell, collections::BTreeMap};
#[test]
fn test_account_data() {
@ -617,7 +617,7 @@ mod tests {
lamports: 1,
..Account::default()
};
let mut rewards_pool = create_rewards_pool();
let mut rewards_pool = RefCell::new(create_rewards_pool());
let pool_id = rewards_pools::id();
let mut keyed_pool_account = KeyedAccount::new(&pool_id, false, &mut rewards_pool);
let mut owner = StorageAccount {
@ -626,7 +626,7 @@ mod tests {
};
// check that redeeming from depleted pools fails
keyed_pool_account.account.lamports = 0;
keyed_pool_account.account.borrow_mut().lamports = 0;
assert_eq!(
check_redeemable(&mut credits, 1.0, &mut keyed_pool_account, &mut owner),
Err(InstructionError::CustomError(
@ -635,7 +635,7 @@ mod tests {
);
assert_eq!(owner.account.lamports, 1);
keyed_pool_account.account.lamports = 200;
keyed_pool_account.account.borrow_mut().lamports = 200;
assert_eq!(
check_redeemable(&mut credits, 1.0, &mut keyed_pool_account, &mut owner),
Ok(())

View File

@ -19,7 +19,8 @@ pub fn process_instruction(
let (me, rest) = keyed_accounts.split_at_mut(1);
let me_unsigned = me[0].signer_key().is_none();
let mut storage_account = StorageAccount::new(*me[0].unsigned_key(), &mut me[0].account);
let mut me_account = me[0].try_account_ref_mut()?;
let mut storage_account = StorageAccount::new(*me[0].unsigned_key(), &mut me_account);
match limited_deserialize(data)? {
StorageInstruction::InitializeStorage {
@ -68,7 +69,8 @@ pub fn process_instruction(
let rewards = Rewards::from_keyed_account(&rewards[0])?;
let clock = Clock::from_keyed_account(&clock[0])?;
let mut owner = StorageAccount::new(*owner[0].unsigned_key(), &mut owner[0].account);
let mut owner_account = owner[0].try_account_ref_mut()?;
let mut owner = StorageAccount::new(*owner[0].unsigned_key(), &mut owner_account);
storage_account.claim_storage_reward(&mut rewards_pools[0], clock, rewards, &mut owner)
}
@ -84,12 +86,16 @@ pub fn process_instruction(
}
let me_id = storage_account.id;
let clock = Clock::from_keyed_account(&clock[0])?;
let mut rest: Vec<_> = rest
let mut rest = rest
.iter()
.map(|keyed_account| Ok((keyed_account, keyed_account.try_account_ref_mut()?)))
.collect::<Result<Vec<_>, InstructionError>>()?;
let mut rest = rest
.iter_mut()
.map(|keyed_account| {
StorageAccount::new(*keyed_account.unsigned_key(), &mut keyed_account.account)
.map(|(keyed_account, account_ref)| {
StorageAccount::new(*keyed_account.unsigned_key(), account_ref)
})
.collect();
.collect::<Vec<_>>();
storage_account.proof_validation(&me_id, clock, segment, proofs, &mut rest)
}
}
@ -117,15 +123,20 @@ mod tests {
Sysvar,
},
};
use std::cell::RefCell;
fn test_instruction(
ix: &Instruction,
program_accounts: &mut [Account],
) -> Result<(), InstructionError> {
let program_accounts: Vec<_> = program_accounts
.iter()
.map(|account| RefCell::new(account.clone()))
.collect();
let mut keyed_accounts: Vec<_> = ix
.accounts
.iter()
.zip(program_accounts.iter_mut())
.zip(program_accounts.iter())
.map(|(account_meta, account)| {
KeyedAccount::new(&account_meta.pubkey, account_meta.is_signer, account)
})
@ -175,7 +186,7 @@ mod tests {
#[test]
fn test_storage_tx() {
let pubkey = Pubkey::new_rand();
let mut accounts = [(pubkey, Account::default())];
let mut accounts = [(&pubkey, &RefCell::new(Account::default()))];
let mut keyed_accounts = create_keyed_accounts(&mut accounts);
assert!(process_instruction(&id(), &mut keyed_accounts, &[]).is_err());
}
@ -185,8 +196,8 @@ mod tests {
let pubkey = Pubkey::new_rand();
let clock_id = clock::id();
let mut keyed_accounts = Vec::new();
let mut user_account = Account::default();
let mut clock_account = Clock::default().create_account(1);
let mut user_account = RefCell::new(Account::default());
let mut clock_account = RefCell::new(Clock::default().create_account(1));
keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
keyed_accounts.push(KeyedAccount::new(&clock_id, false, &mut clock_account));