Nonce gets blockhash from invoke_context (#18950)

This commit is contained in:
Jack May
2021-07-29 01:50:20 -07:00
committed by GitHub
parent c0d2e81e89
commit 9529284194
6 changed files with 306 additions and 394 deletions

View File

@ -15,15 +15,11 @@ use solana_program::{
system_instruction::NonceError,
sysvar::rent::Rent,
};
#[allow(deprecated)]
use solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes;
use std::collections::HashSet;
#[allow(deprecated)]
pub trait NonceKeyedAccount {
fn advance_nonce_account(
&self,
recent_blockhashes: &RecentBlockhashes,
signers: &HashSet<Pubkey>,
invoke_context: &dyn InvokeContext,
) -> Result<(), InstructionError>;
@ -31,7 +27,6 @@ pub trait NonceKeyedAccount {
&self,
lamports: u64,
to: &KeyedAccount,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
signers: &HashSet<Pubkey>,
invoke_context: &dyn InvokeContext,
@ -39,7 +34,6 @@ pub trait NonceKeyedAccount {
fn initialize_nonce_account(
&self,
nonce_authority: &Pubkey,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
invoke_context: &dyn InvokeContext,
) -> Result<(), InstructionError>;
@ -52,21 +46,11 @@ pub trait NonceKeyedAccount {
}
impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
#[allow(deprecated)]
fn advance_nonce_account(
&self,
recent_blockhashes: &RecentBlockhashes,
signers: &HashSet<Pubkey>,
invoke_context: &dyn InvokeContext,
) -> Result<(), InstructionError> {
if recent_blockhashes.is_empty() {
ic_msg!(
invoke_context,
"Advance nonce account: recent blockhash list is empty",
);
return Err(NonceError::NoRecentBlockhashes.into());
}
let state = AccountUtilsState::<Versions>::state(self)?.convert_to_current();
match state {
State::Initialized(data) => {
@ -78,7 +62,7 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
);
return Err(InstructionError::MissingRequiredSignature);
}
let recent_blockhash = recent_blockhashes[0].blockhash;
let recent_blockhash = *invoke_context.get_blockhash();
if data.blockhash == recent_blockhash {
ic_msg!(
invoke_context,
@ -89,7 +73,7 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
let new_data = nonce::state::Data {
blockhash: recent_blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
..data
};
self.set_state(&Versions::new_current(State::Initialized(new_data)))
@ -105,12 +89,10 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
}
}
#[allow(deprecated)]
fn withdraw_nonce_account(
&self,
lamports: u64,
to: &KeyedAccount,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
signers: &HashSet<Pubkey>,
invoke_context: &dyn InvokeContext,
@ -130,7 +112,7 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
}
State::Initialized(ref data) => {
if lamports == self.lamports()? {
if data.blockhash == recent_blockhashes[0].blockhash {
if data.blockhash == *invoke_context.get_blockhash() {
ic_msg!(
invoke_context,
"Withdraw nonce account: nonce can only advance once per slot"
@ -180,22 +162,12 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
Ok(())
}
#[allow(deprecated)]
fn initialize_nonce_account(
&self,
nonce_authority: &Pubkey,
recent_blockhashes: &RecentBlockhashes,
rent: &Rent,
invoke_context: &dyn InvokeContext,
) -> Result<(), InstructionError> {
if recent_blockhashes.is_empty() {
ic_msg!(
invoke_context,
"Initialize nonce account: recent blockhash list is empty",
);
return Err(NonceError::NoRecentBlockhashes.into());
}
match AccountUtilsState::<Versions>::state(self)?.convert_to_current() {
State::Uninitialized => {
let min_balance = rent.minimum_balance(self.data_len()?);
@ -210,8 +182,8 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
}
let data = nonce::state::Data {
authority: *nonce_authority,
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
};
self.set_state(&Versions::new_current(State::Initialized(data)))
}
@ -274,18 +246,32 @@ where
#[cfg(test)]
mod test {
use super::*;
#[allow(deprecated)]
use crate::sysvar::recent_blockhashes::create_test_recent_blockhashes;
use crate::{
account::ReadableAccount,
account_utils::State as AccountUtilsState,
fee_calculator::FeeCalculator,
keyed_account::KeyedAccount,
nonce::{self, State},
nonce_account::verify_nonce_account,
process_instruction::MockInvokeContext,
system_instruction::NonceError,
};
use solana_program::hash::Hash;
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) -> MockInvokeContext<'a> {
let mut invoke_context = MockInvokeContext::new(vec![]);
let (blockhash, fee_calculator) = create_test_blockhash(seed);
invoke_context.blockhash = blockhash;
invoke_context.fee_calculator = fee_calculator;
invoke_context
}
#[test]
fn default_is_uninitialized() {
@ -311,71 +297,51 @@ mod test {
.convert_to_current();
// New is in Uninitialzed state
assert_eq!(state, State::Uninitialized);
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(95);
let invoke_context = create_invoke_context_with_blockhash(95);
let authorized = keyed_account.unsigned_key();
keyed_account
.initialize_nonce_account(
authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(authorized, &rent, &invoke_context)
.unwrap();
let state = AccountUtilsState::<Versions>::state(keyed_account)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
..data
};
// First nonce instruction drives state from Uninitialized to Initialized
assert_eq!(state, State::Initialized(data.clone()));
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
keyed_account
.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
)
.advance_nonce_account(&signers, &invoke_context)
.unwrap();
let state = AccountUtilsState::<Versions>::state(keyed_account)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
..data
};
// Second nonce instruction consumes and replaces stored nonce
assert_eq!(state, State::Initialized(data.clone()));
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
keyed_account
.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
)
.advance_nonce_account(&signers, &invoke_context)
.unwrap();
let state = AccountUtilsState::<Versions>::state(keyed_account)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
..data
};
// Third nonce instruction for fun and profit
assert_eq!(state, State::Initialized(data));
with_test_keyed_account(42, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let withdraw_lamports = keyed_account.account.borrow().lamports();
let expect_nonce_lamports =
keyed_account.account.borrow().lamports() - withdraw_lamports;
@ -384,10 +350,9 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
// Empties Account balance
@ -414,69 +379,27 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
let authority = *nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(
&authority,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authority, &rent, &invoke_context)
.unwrap();
let pubkey = *nonce_account.account.borrow().owner();
let nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
let state = AccountUtilsState::<Versions>::state(&nonce_account)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
};
assert_eq!(state, State::Initialized(data));
let signers = HashSet::new();
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let result = nonce_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
})
}
let invoke_context = create_invoke_context_with_blockhash(0);
#[test]
fn nonce_inx_with_empty_recent_blockhashes_fail() {
let rent = Rent {
lamports_per_byte_year: 42,
..Rent::default()
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
let mut signers = HashSet::new();
signers.insert(*keyed_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = *keyed_account.unsigned_key();
keyed_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.unwrap();
let recent_blockhashes = vec![].into_iter().collect();
let result = keyed_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
let result = nonce_account.advance_nonce_account(&signers, &invoke_context);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
})
}
@ -490,22 +413,12 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
let mut signers = HashSet::new();
signers.insert(*keyed_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let authorized = *keyed_account.unsigned_key();
keyed_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
let result = keyed_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
let result = keyed_account.advance_nonce_account(&signers, &invoke_context);
assert_eq!(result, Err(NonceError::NotExpired.into()));
})
}
@ -520,13 +433,8 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
let mut signers = HashSet::new();
signers.insert(*keyed_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let result = keyed_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
let invoke_context = create_invoke_context_with_blockhash(63);
let result = keyed_account.advance_nonce_account(&signers, &invoke_context);
assert_eq!(result, Err(NonceError::BadAccountState.into()));
})
}
@ -542,26 +450,15 @@ mod test {
with_test_keyed_account(42, true, |nonce_authority| {
let mut signers = HashSet::new();
signers.insert(*nonce_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let authorized = *nonce_authority.unsigned_key();
nonce_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
let mut signers = HashSet::new();
signers.insert(*nonce_authority.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let result = nonce_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
let invoke_context = create_invoke_context_with_blockhash(31);
let result = nonce_account.advance_nonce_account(&signers, &invoke_context);
assert_eq!(result, Ok(()));
});
});
@ -578,22 +475,12 @@ mod test {
with_test_keyed_account(42, false, |nonce_authority| {
let mut signers = HashSet::new();
signers.insert(*nonce_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let authorized = *nonce_authority.unsigned_key();
nonce_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
let result = nonce_account.advance_nonce_account(
&recent_blockhashes,
&signers,
&MockInvokeContext::new(vec![]),
);
let result = nonce_account.advance_nonce_account(&signers, &invoke_context);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
});
});
@ -614,8 +501,7 @@ mod test {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let withdraw_lamports = nonce_keyed.account.borrow().lamports();
let expect_nonce_lamports =
nonce_keyed.account.borrow().lamports() - withdraw_lamports;
@ -624,10 +510,9 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
@ -661,16 +546,14 @@ mod test {
assert_eq!(state, State::Uninitialized);
with_test_keyed_account(42, false, |to_keyed| {
let signers = HashSet::new();
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let lamports = nonce_keyed.account.borrow().lamports();
let result = nonce_keyed.withdraw_nonce_account(
lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
})
@ -692,16 +575,14 @@ mod test {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let lamports = nonce_keyed.account.borrow().lamports() + 1;
let result = nonce_keyed.withdraw_nonce_account(
lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::InsufficientFunds));
})
@ -719,8 +600,7 @@ mod test {
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let withdraw_lamports = nonce_keyed.account.borrow().lamports() / 2;
let nonce_expect_lamports =
nonce_keyed.account.borrow().lamports() - withdraw_lamports;
@ -729,10 +609,9 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
@ -752,10 +631,9 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
@ -781,25 +659,18 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
let authority = *nonce_keyed.unsigned_key();
nonce_keyed
.initialize_nonce_account(
&authority,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authority, &rent, &invoke_context)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
};
assert_eq!(state, State::Initialized(data.clone()));
with_test_keyed_account(42, false, |to_keyed| {
@ -811,19 +682,17 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
..data.clone()
};
assert_eq!(state, State::Initialized(data));
@ -832,8 +701,7 @@ mod test {
nonce_expect_lamports
);
assert_eq!(to_keyed.account.borrow().lamports(), to_expect_lamports);
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let withdraw_lamports = nonce_keyed.account.borrow().lamports();
let nonce_expect_lamports =
nonce_keyed.account.borrow().lamports() - withdraw_lamports;
@ -842,10 +710,9 @@ mod test {
.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
)
.unwrap();
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
@ -869,16 +736,10 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let authorized = *nonce_keyed.unsigned_key();
nonce_keyed
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
with_test_keyed_account(42, false, |to_keyed| {
let mut signers = HashSet::new();
@ -887,10 +748,9 @@ mod test {
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(NonceError::NotExpired.into()));
})
@ -905,30 +765,22 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(95);
let invoke_context = create_invoke_context_with_blockhash(95);
let authorized = *nonce_keyed.unsigned_key();
nonce_keyed
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
with_test_keyed_account(42, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
let withdraw_lamports = nonce_keyed.account.borrow().lamports() + 1;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::InsufficientFunds));
})
@ -943,30 +795,22 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(95);
let invoke_context = create_invoke_context_with_blockhash(95);
let authorized = *nonce_keyed.unsigned_key();
nonce_keyed
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
with_test_keyed_account(42, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
let withdraw_lamports = nonce_keyed.account.borrow().lamports() - min_lamports + 1;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::InsufficientFunds));
})
@ -981,30 +825,22 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(95);
let invoke_context = create_invoke_context_with_blockhash(95);
let authorized = *nonce_keyed.unsigned_key();
nonce_keyed
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
with_test_keyed_account(55, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
let withdraw_lamports = u64::MAX - 54;
let result = nonce_keyed.withdraw_nonce_account(
withdraw_lamports,
to_keyed,
&recent_blockhashes,
&rent,
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::InsufficientFunds));
})
@ -1025,20 +861,13 @@ mod test {
assert_eq!(state, State::Uninitialized);
let mut signers = HashSet::new();
signers.insert(*keyed_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let authority = *keyed_account.unsigned_key();
let result = keyed_account.initialize_nonce_account(
&authority,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
);
#[allow(deprecated)]
let result = keyed_account.initialize_nonce_account(&authority, &rent, &invoke_context);
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
};
assert_eq!(result, Ok(()));
let state = AccountUtilsState::<Versions>::state(keyed_account)
@ -1048,28 +877,6 @@ mod test {
})
}
#[test]
fn initialize_inx_empty_recent_blockhashes_fail() {
let rent = Rent {
lamports_per_byte_year: 42,
..Rent::default()
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
let mut signers = HashSet::new();
signers.insert(*keyed_account.signer_key().unwrap());
let recent_blockhashes = vec![].into_iter().collect();
let authorized = *keyed_account.unsigned_key();
let result = keyed_account.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
);
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
})
}
#[test]
fn initialize_inx_initialized_account_fail() {
let rent = Rent {
@ -1078,25 +885,14 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
let authorized = *keyed_account.unsigned_key();
keyed_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let result = keyed_account.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
);
let invoke_context = create_invoke_context_with_blockhash(0);
let result =
keyed_account.initialize_nonce_account(&authorized, &rent, &invoke_context);
assert_eq!(result, Err(NonceError::BadAccountState.into()));
})
}
@ -1109,15 +905,10 @@ mod test {
};
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports - 42, true, |keyed_account| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let invoke_context = create_invoke_context_with_blockhash(63);
let authorized = *keyed_account.unsigned_key();
let result = keyed_account.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
);
let result =
keyed_account.initialize_nonce_account(&authorized, &rent, &invoke_context);
assert_eq!(result, Err(InstructionError::InsufficientFunds));
})
}
@ -1132,28 +923,21 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(*nonce_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
let authorized = *nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(
&authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(&authorized, &rent, &invoke_context)
.unwrap();
let authority = Pubkey::default();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
blockhash: *invoke_context.get_blockhash(),
fee_calculator: invoke_context.get_fee_calculator().clone(),
};
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Ok(()));
let state = AccountUtilsState::<Versions>::state(nonce_account)
@ -1172,11 +956,12 @@ 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(vec![]);
signers.insert(*nonce_account.signer_key().unwrap());
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(NonceError::BadAccountState.into()));
})
@ -1192,21 +977,15 @@ mod test {
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(*nonce_account.signer_key().unwrap());
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(31);
let invoke_context = create_invoke_context_with_blockhash(31);
let authorized = &Pubkey::default().clone();
nonce_account
.initialize_nonce_account(
authorized,
&recent_blockhashes,
&rent,
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(authorized, &rent, &invoke_context)
.unwrap();
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),
&signers,
&MockInvokeContext::new(vec![]),
&invoke_context,
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
})
@ -1220,21 +999,14 @@ mod test {
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, State::Uninitialized);
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let authorized = nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(
authorized,
&recent_blockhashes,
&Rent::free(),
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(authorized, &Rent::free(), &invoke_context)
.unwrap();
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
#[allow(deprecated)]
&recent_blockhashes[0].blockhash,
invoke_context.get_blockhash(),
));
});
}
@ -1257,21 +1029,15 @@ mod test {
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, State::Uninitialized);
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let invoke_context = create_invoke_context_with_blockhash(0);
let authorized = nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(
authorized,
&recent_blockhashes,
&Rent::free(),
&MockInvokeContext::new(vec![]),
)
.initialize_nonce_account(authorized, &Rent::free(), &invoke_context)
.unwrap();
let invoke_context = create_invoke_context_with_blockhash(1);
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
#[allow(deprecated)]
&recent_blockhashes[1].blockhash,
invoke_context.get_blockhash(),
));
});
}

View File

@ -3,6 +3,8 @@
use solana_sdk::{
account::AccountSharedData,
compute_budget::ComputeBudget,
fee_calculator::FeeCalculator,
hash::Hash,
instruction::{CompiledInstruction, Instruction, InstructionError},
keyed_account::{create_keyed_accounts_unified, KeyedAccount},
pubkey::Pubkey,
@ -107,6 +109,10 @@ pub trait InvokeContext {
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>;
/// Get this invocation's compute budget
fn get_compute_budget(&self) -> &ComputeBudget;
/// Get this invocation's blockhash
fn get_blockhash(&self) -> &Hash;
/// Get this invocation's `FeeCalculator`
fn get_fee_calculator(&self) -> &FeeCalculator;
}
/// Convenience macro to log a message with an `Rc<RefCell<dyn Logger>>`
@ -379,13 +385,14 @@ pub struct MockInvokeContext<'a> {
pub invoke_stack: Vec<InvokeContextStackFrame<'a>>,
pub logger: MockLogger,
pub compute_budget: ComputeBudget,
#[allow(deprecated)]
pub bpf_compute_budget: BpfComputeBudget,
pub compute_meter: MockComputeMeter,
pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>,
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
pub sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
pub disabled_features: HashSet<Pubkey>,
pub blockhash: Hash,
pub fee_calculator: FeeCalculator,
}
impl<'a> MockInvokeContext<'a> {
pub fn new(keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
@ -394,7 +401,6 @@ impl<'a> MockInvokeContext<'a> {
invoke_stack: Vec::with_capacity(compute_budget.max_invoke_depth),
logger: MockLogger::default(),
compute_budget,
#[allow(deprecated)]
bpf_compute_budget: compute_budget.into(),
compute_meter: MockComputeMeter {
remaining: std::i64::MAX as u64,
@ -403,6 +409,8 @@ impl<'a> MockInvokeContext<'a> {
accounts: vec![],
sysvars: vec![],
disabled_features: HashSet::default(),
blockhash: Hash::default(),
fee_calculator: FeeCalculator::default(),
};
invoke_context
.invoke_stack
@ -525,4 +533,10 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
fn get_compute_budget(&self) -> &ComputeBudget {
&self.compute_budget
}
fn get_blockhash(&self) -> &Hash {
&self.blockhash
}
fn get_fee_calculator(&self) -> &FeeCalculator {
&self.fee_calculator
}
}