Skip to content

Commit

Permalink
Replaces MockInvokeContext by ThisInvokeContext in nonce_keyed_account.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 22, 2021
1 parent f6d0e79 commit 90ce4d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
1 change: 1 addition & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod log_collector;
pub mod message_processor;
pub mod neon_evm_program;
pub mod non_circulating_supply;
mod nonce_keyed_account;
mod pubkey_bins;
mod read_only_accounts_cache;
pub mod rent_collector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#![cfg(feature = "full")]

use crate::{
use solana_sdk::{
account::{ReadableAccount, WritableAccount},
account_utils::State as AccountUtilsState,
feature_set, ic_msg,
keyed_account::KeyedAccount,
nonce_account::create_account,
process_instruction::InvokeContext,
};
use solana_program::{
instruction::{checked_add, InstructionError},
keyed_account::KeyedAccount,
nonce::{self, state::Versions, State},
process_instruction::InvokeContext,
pubkey::Pubkey,
system_instruction::{nonce_to_instruction_error, NonceError},
sysvar::rent::Rent,
Expand Down Expand Up @@ -255,45 +250,36 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
}
}

/// Convenience function for working with keyed accounts in tests
pub fn with_test_keyed_account<F>(lamports: u64, signer: bool, f: F)
where
F: Fn(&KeyedAccount),
{
let pubkey = Pubkey::new_unique();
let account = create_account(lamports);
let keyed_account = KeyedAccount::new(&pubkey, signer, &account);
f(&keyed_account)
}

#[cfg(test)]
mod test {
use super::*;
use crate::{
use crate::message_processor::ThisInvokeContext;
use solana_sdk::{
account::ReadableAccount,
account_utils::State as AccountUtilsState,
fee_calculator::FeeCalculator,
hash::{hash, Hash},
keyed_account::KeyedAccount,
nonce::{self, State},
nonce_account::verify_nonce_account,
process_instruction::MockInvokeContext,
system_instruction::SystemError,
};
use solana_program::hash::{hash, Hash};

fn create_test_blockhash(seed: usize) -> (Hash, FeeCalculator) {
(
hash(&bincode::serialize(&seed).unwrap()),
FeeCalculator::new((seed as u64).saturating_mul(100)),
)
fn create_invoke_context_with_blockhash<'a>(seed: usize) -> ThisInvokeContext<'a> {
let mut invoke_context = ThisInvokeContext::new_mock(&[], &[]);
invoke_context.set_blockhash(hash(&bincode::serialize(&seed).unwrap()));
invoke_context.set_fee_calculator(FeeCalculator::new((seed as u64).saturating_mul(100)));
invoke_context
}

fn create_invoke_context_with_blockhash<'a>(seed: usize) -> MockInvokeContext<'a> {
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let (blockhash, fee_calculator) = create_test_blockhash(seed);
invoke_context.blockhash = blockhash;
invoke_context.fee_calculator = fee_calculator;
invoke_context
fn with_test_keyed_account<F>(lamports: u64, signer: bool, f: F)
where
F: Fn(&KeyedAccount),
{
let pubkey = Pubkey::new_unique();
let account = create_account(lamports);
let keyed_account = KeyedAccount::new(&pubkey, signer, &account);
f(&keyed_account)
}

#[test]
Expand Down Expand Up @@ -979,12 +965,11 @@ mod test {
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
let mut signers = HashSet::new();
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
signers.insert(*nonce_account.signer_key().unwrap());
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),
&signers,
&invoke_context,
&ThisInvokeContext::new_mock(&[], &[]),
);
assert_eq!(result, Err(InstructionError::InvalidAccountData));
})
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/system_instruction_processor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::nonce_keyed_account::NonceKeyedAccount;
use log::*;
use solana_sdk::{
account::{AccountSharedData, ReadableAccount, WritableAccount},
Expand All @@ -6,7 +7,6 @@ use solana_sdk::{
instruction::InstructionError,
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index, KeyedAccount},
nonce,
nonce_keyed_account::NonceKeyedAccount,
process_instruction::InvokeContext,
program_utils::limited_deserialize,
pubkey::Pubkey,
Expand Down
1 change: 0 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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 precompiles;
Expand Down

0 comments on commit 90ce4d4

Please sign in to comment.