Store FeeCalculator with blockhash in nonce accounts (#8650)

* Copy current state version to v0

* Add `FeeCalculator` to nonce state

* fixup compile

* Dump v0 handling...

Since we new account data is all zeros, new `Current` versioned accounts
look like v0. We could hack around this with some data size checks, but
the `account_utils::*State` traits are applied to `Account`, not the
state data, so we're kind SOL...

* Create more representative test `RecentBlockhashes`

* Improve CLI nonce account display

Co-Authored-By: Michael Vines <mvines@gmail.com>

* Fix that last bank test...

* clippy/fmt

Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
Trent Nelson
2020-03-05 07:40:26 -07:00
committed by GitHub
parent 44fde2d964
commit fd00e5cb35
11 changed files with 133 additions and 81 deletions

View File

@@ -6,7 +6,7 @@ use crate::{
append_vec::StoredAccount,
bank::{HashAgeKind, TransactionProcessResult},
blockhash_queue::BlockhashQueue,
nonce_utils::prepare_if_nonce_account,
nonce_utils,
rent_collector::RentCollector,
system_instruction_processor::{get_system_account_kind, SystemAccountKind},
transaction_utils::OrderedIterator,
@@ -265,13 +265,15 @@ impl Accounts {
.zip(lock_results.into_iter())
.map(|etx| match etx {
(tx, (Ok(()), hash_age_kind)) => {
let fee_hash = if let Some(HashAgeKind::DurableNonce(_, _)) = hash_age_kind {
hash_queue.last_hash()
} else {
tx.message().recent_blockhash
let fee_calculator = match hash_age_kind.as_ref() {
Some(HashAgeKind::DurableNonce(_, account)) => {
nonce_utils::fee_calculator_of(account)
}
_ => hash_queue
.get_fee_calculator(&tx.message().recent_blockhash)
.cloned(),
};
let fee = if let Some(fee_calculator) = hash_queue.get_fee_calculator(&fee_hash)
{
let fee = if let Some(fee_calculator) = fee_calculator {
fee_calculator.calculate_fee(tx.message())
} else {
return (Err(TransactionError::BlockhashNotFound), hash_age_kind);
@@ -630,7 +632,13 @@ impl Accounts {
.enumerate()
.zip(acc.0.iter_mut())
{
prepare_if_nonce_account(account, key, res, maybe_nonce, last_blockhash);
nonce_utils::prepare_if_nonce_account(
account,
key,
res,
maybe_nonce,
last_blockhash,
);
if message.is_writable(i) {
if account.rent_epoch == 0 {
account.rent_epoch = rent_collector.epoch;
@@ -921,10 +929,9 @@ mod tests {
nonce.pubkey(),
Account::new_data(
min_balance * 2,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap(),

View File

@@ -1409,19 +1409,20 @@ impl Bank {
let results = OrderedIterator::new(txs, iteration_order)
.zip(executed.iter())
.map(|(tx, (res, hash_age_kind))| {
let is_durable_nonce = hash_age_kind
.as_ref()
.map(|hash_age_kind| hash_age_kind.is_durable_nonce())
.unwrap_or(false);
let fee_hash = if is_durable_nonce {
self.last_blockhash()
} else {
tx.message().recent_blockhash
let (fee_calculator, is_durable_nonce) = match hash_age_kind {
Some(HashAgeKind::DurableNonce(_, account)) => {
(nonce_utils::fee_calculator_of(account), true)
}
_ => (
hash_queue
.get_fee_calculator(&tx.message().recent_blockhash)
.cloned(),
false,
),
};
let fee = hash_queue
.get_fee_calculator(&fee_hash)
.ok_or(TransactionError::BlockhashNotFound)?
.calculate_fee(tx.message());
let fee_calculator = fee_calculator.ok_or(TransactionError::BlockhashNotFound)?;
let fee = fee_calculator.calculate_fee(tx.message());
let message = tx.message();
match *res {
@@ -3417,10 +3418,9 @@ mod tests {
let nonce = Keypair::new();
let nonce_account = Account::new_data(
min_balance + 42,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -5153,6 +5153,13 @@ mod tests {
genesis_cfg_fn(&mut genesis_config);
let mut bank = Arc::new(Bank::new(&genesis_config));
// Banks 0 and 1 have no fees, wait two blocks before
// initializing our nonce accounts
for _ in 0..2 {
goto_end_of_slot(Arc::get_mut(&mut bank).unwrap());
bank = Arc::new(new_from_parent(&bank));
}
let (custodian_keypair, nonce_keypair) = nonce_setup(
&mut bank,
&mint_keypair,
@@ -5276,10 +5283,9 @@ mod tests {
let nonce = Keypair::new();
let nonce_account = Account::new_data(
42424242,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();

View File

@@ -10,7 +10,7 @@ pub mod genesis_utils;
pub mod loader_utils;
pub mod message_processor;
mod native_loader;
mod nonce_utils;
pub mod nonce_utils;
pub mod rent_collector;
mod serde_utils;
pub mod stakes;

View File

@@ -1,6 +1,7 @@
use solana_sdk::{
account::Account,
account_utils::StateMut,
fee_calculator::FeeCalculator,
hash::Hash,
instruction::CompiledInstruction,
nonce::{self, state::Versions, State},
@@ -66,7 +67,7 @@ pub fn prepare_if_nonce_account(
if let State::Initialized(ref data) = state {
let new_data = Versions::new_current(State::Initialized(nonce::state::Data {
blockhash: *last_blockhash,
..*data
..data.clone()
}));
account.set_state(&new_data).unwrap();
}
@@ -74,6 +75,16 @@ pub fn prepare_if_nonce_account(
}
}
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::*;
@@ -244,10 +255,7 @@ mod tests {
}
fn create_accounts_prepare_if_nonce_account() -> (Pubkey, Account, Account, Hash) {
let data = Versions::new_current(State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}));
let data = Versions::new_current(State::Initialized(nonce::state::Data::default()));
let account = Account::new_data(42, &data, &system_program::id()).unwrap();
let pre_account = Account {
lamports: 43,
@@ -300,8 +308,8 @@ mod tests {
let mut expect_account = post_account.clone();
let data = Versions::new_current(State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: last_blockhash,
..nonce::state::Data::default()
}));
expect_account.set_state(&data).unwrap();
@@ -360,8 +368,8 @@ mod tests {
expect_account
.set_state(&Versions::new_current(State::Initialized(
nonce::state::Data {
authority: Pubkey::default(),
blockhash: last_blockhash,
..nonce::state::Data::default()
},
)))
.unwrap();

View File

@@ -306,8 +306,13 @@ pub fn get_system_account_kind(account: &Account) -> Option<SystemAccountKind> {
if system_program::check_id(&account.owner) {
if account.data.is_empty() {
Some(SystemAccountKind::System)
} else if let Ok(nonce::State::Initialized(_)) = account.state() {
Some(SystemAccountKind::Nonce)
} else if account.data.len() == nonce::State::size() {
match account.state().ok()? {
nonce::state::Versions::Current(state) => match *state {
nonce::State::Initialized(_) => Some(SystemAccountKind::Nonce),
_ => None,
},
}
} else {
None
}
@@ -735,10 +740,9 @@ mod tests {
let nonce = Pubkey::new_rand();
let nonce_account = Account::new_ref_data(
42,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -877,10 +881,10 @@ mod tests {
let from = Pubkey::new_rand();
let from_account = Account::new_ref_data(
100,
&nonce::State::Initialized(nonce::state::Data {
&nonce::state::Versions::new_current(nonce::State::Initialized(nonce::state::Data {
authority: from,
blockhash: Hash::default(),
}),
..nonce::state::Data::default()
})),
&system_program::id(),
)
.unwrap();
@@ -1382,10 +1386,9 @@ mod tests {
fn test_get_system_account_kind_nonce_ok() {
let nonce_account = Account::new_data(
42,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -1413,10 +1416,9 @@ mod tests {
fn test_get_system_account_kind_nonsystem_owner_with_nonce_data_fail() {
let nonce_account = Account::new_data(
42,
&nonce::State::Initialized(nonce::state::Data {
authority: Pubkey::default(),
blockhash: Hash::default(),
}),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&Pubkey::new_rand(),
)
.unwrap();