Move remaining nonce utils from runtime to SDK

This commit is contained in:
Trent Nelson
2020-09-29 22:15:11 -06:00
committed by mergify[bot]
parent 65b868f4eb
commit 3c7b9c2938
6 changed files with 6 additions and 8 deletions

View File

@ -7,7 +7,6 @@ use crate::{
bank::{HashAgeKind, TransactionProcessResult},
blockhash_queue::BlockhashQueue,
feature_set::{self, FeatureSet},
nonce_utils,
rent_collector::RentCollector,
system_instruction_processor::{get_system_account_kind, SystemAccountKind},
transaction_utils::OrderedIterator,
@ -316,7 +315,7 @@ impl Accounts {
((_, tx), (Ok(()), hash_age_kind)) => {
let fee_calculator = match hash_age_kind.as_ref() {
Some(HashAgeKind::DurableNonce(_, account)) => {
nonce_utils::fee_calculator_of(account)
nonce::utils::fee_calculator_of(account)
}
_ => hash_queue
.get_fee_calculator(&tx.message().recent_blockhash)

View File

@ -17,7 +17,6 @@ use crate::{
instruction_recorder::InstructionRecorder,
log_collector::LogCollector,
message_processor::{Executors, MessageProcessor},
nonce_utils,
process_instruction::{Executor, ProcessInstruction, ProcessInstructionWithContext},
rent_collector::RentCollector,
stakes::Stakes,
@ -1828,7 +1827,7 @@ impl Bank {
.map(|acc| (*nonce_pubkey, acc))
})
.filter(|(_pubkey, nonce_account)| {
nonce_utils::verify_nonce_account(nonce_account, &tx.message().recent_blockhash)
nonce::utils::verify_nonce_account(nonce_account, &tx.message().recent_blockhash)
})
}
@ -2244,7 +2243,7 @@ impl Bank {
.map(|((_, tx), (res, hash_age_kind))| {
let (fee_calculator, is_durable_nonce) = match hash_age_kind {
Some(HashAgeKind::DurableNonce(_, account)) => {
(nonce_utils::fee_calculator_of(account), true)
(nonce::utils::fee_calculator_of(account), true)
}
_ => (
hash_queue

View File

@ -22,7 +22,6 @@ pub mod loader_utils;
pub mod log_collector;
pub mod message_processor;
mod native_loader;
pub mod nonce_utils;
pub mod process_instruction;
pub mod rent_collector;
pub mod serde_snapshot;

View File

@ -1,86 +0,0 @@
use solana_sdk::{
account::Account,
account_utils::StateMut,
fee_calculator::FeeCalculator,
hash::Hash,
nonce::{state::Versions, State},
};
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,
}
}
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::{
account_utils::State as AccountUtilsState,
hash::Hash,
nonce::{account::with_test_keyed_account, Account as NonceAccount, State},
sysvar::{recent_blockhashes::create_test_recent_blockhashes, rent::Rent},
};
use std::collections::HashSet;
#[test]
fn verify_nonce_ok() {
with_test_keyed_account(42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(nonce_account.signer_key().unwrap());
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, State::Uninitialized);
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
.unwrap();
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[0].blockhash,
));
});
}
#[test]
fn verify_nonce_bad_acc_state_fail() {
with_test_keyed_account(42, true, |nonce_account| {
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&Hash::default()
));
});
}
#[test]
fn verify_nonce_bad_query_hash_fail() {
with_test_keyed_account(42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(nonce_account.signer_key().unwrap());
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, State::Uninitialized);
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key();
nonce_account
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
.unwrap();
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[1].blockhash,
));
});
}
}