Nonce fees 1.0 (#8664)

automerge
This commit is contained in:
Grimes
2020-03-05 09:41:45 -08:00
committed by GitHub
parent 4d9aee4794
commit d94f5c94a3
19 changed files with 664 additions and 439 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,
@@ -17,8 +17,7 @@ use solana_sdk::{
account::Account,
clock::Slot,
hash::Hash,
native_loader,
nonce_state::NonceState,
native_loader, nonce,
pubkey::Pubkey,
transaction::Result,
transaction::{Transaction, TransactionError},
@@ -160,7 +159,7 @@ impl Accounts {
})? {
SystemAccountKind::System => 0,
SystemAccountKind::Nonce => {
rent_collector.rent.minimum_balance(NonceState::size())
rent_collector.rent.minimum_balance(nonce::State::size())
}
};
@@ -266,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);
@@ -631,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;
@@ -681,7 +688,7 @@ mod tests {
hash::Hash,
instruction::CompiledInstruction,
message::Message,
nonce_state,
nonce,
rent::Rent,
signature::{Keypair, Signer},
system_program,
@@ -915,19 +922,16 @@ mod tests {
..Rent::default()
},
);
let min_balance = rent_collector
.rent
.minimum_balance(nonce_state::NonceState::size());
let min_balance = rent_collector.rent.minimum_balance(nonce::State::size());
let fee_calculator = FeeCalculator::new(min_balance);
let nonce = Keypair::new();
let mut accounts = vec![(
nonce.pubkey(),
Account::new_data(
min_balance * 2,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap(),

View File

@@ -40,8 +40,7 @@ use solana_sdk::{
hard_forks::HardForks,
hash::{extend_and_hash, hashv, Hash},
inflation::Inflation,
native_loader,
nonce_state::NonceState,
native_loader, nonce,
pubkey::Pubkey,
signature::{Keypair, Signature},
slot_hashes::SlotHashes,
@@ -1410,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 {
@@ -1691,9 +1691,10 @@ impl Bank {
match self.get_account(pubkey) {
Some(mut account) => {
let min_balance = match get_system_account_kind(&account) {
Some(SystemAccountKind::Nonce) => {
self.rent_collector.rent.minimum_balance(NonceState::size())
}
Some(SystemAccountKind::Nonce) => self
.rent_collector
.rent
.minimum_balance(nonce::State::size()),
_ => 0,
};
if lamports + min_balance > account.lamports {
@@ -2165,7 +2166,7 @@ mod tests {
genesis_config::create_genesis_config,
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
message::{Message, MessageHeader},
nonce_state,
nonce,
poh_config::PohConfig,
rent::Rent,
signature::{Keypair, Signer},
@@ -3413,15 +3414,13 @@ mod tests {
genesis_config.rent.lamports_per_byte_year = 42;
let bank = Bank::new(&genesis_config);
let min_balance =
bank.get_minimum_balance_for_rent_exemption(nonce_state::NonceState::size());
let min_balance = bank.get_minimum_balance_for_rent_exemption(nonce::State::size());
let nonce = Keypair::new();
let nonce_account = Account::new_data(
min_balance + 42,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -4992,9 +4991,9 @@ mod tests {
sysvar::recent_blockhashes::RecentBlockhashes::from_account(&bhq_account).unwrap();
// Check length
assert_eq!(recent_blockhashes.len(), i);
let most_recent_hash = recent_blockhashes.iter().nth(0).unwrap();
let most_recent_hash = recent_blockhashes.iter().nth(0).unwrap().blockhash;
// Check order
assert_eq!(Some(true), bank.check_hash_age(most_recent_hash, 0));
assert_eq!(Some(true), bank.check_hash_age(&most_recent_hash, 0));
goto_end_of_slot(Arc::get_mut(&mut bank).unwrap());
bank = Arc::new(new_from_parent(&bank));
}
@@ -5098,11 +5097,14 @@ mod tests {
}
fn get_nonce_account(bank: &Bank, nonce_pubkey: &Pubkey) -> Option<Hash> {
bank.get_account(&nonce_pubkey)
.and_then(|acc| match acc.state() {
Ok(nonce_state::NonceState::Initialized(_meta, hash)) => Some(hash),
bank.get_account(&nonce_pubkey).and_then(|acc| {
let state =
StateMut::<nonce::state::Versions>::state(&acc).map(|v| v.convert_to_current());
match state {
Ok(nonce::State::Initialized(ref data)) => Some(data.blockhash),
_ => None,
})
}
})
}
fn nonce_setup(
@@ -5151,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,
@@ -5274,10 +5283,9 @@ mod tests {
let nonce = Keypair::new();
let nonce_account = Account::new_data(
42424242,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();

View File

@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use solana_sdk::{fee_calculator::FeeCalculator, hash::Hash, timing::timestamp};
use solana_sdk::{
fee_calculator::FeeCalculator, hash::Hash, sysvar::recent_blockhashes, timing::timestamp,
};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
@@ -112,15 +114,19 @@ impl BlockhashQueue {
None
}
pub fn get_recent_blockhashes(&self) -> impl Iterator<Item = (u64, &Hash)> {
(&self.ages).iter().map(|(k, v)| (v.hash_height, k))
pub fn get_recent_blockhashes(&self) -> impl Iterator<Item = recent_blockhashes::IterItem> {
(&self.ages)
.iter()
.map(|(k, v)| recent_blockhashes::IterItem(v.hash_height, k, &v.fee_calculator))
}
}
#[cfg(test)]
mod tests {
use super::*;
use bincode::serialize;
use solana_sdk::{clock::MAX_RECENT_BLOCKHASHES, hash::hash};
use solana_sdk::{
clock::MAX_RECENT_BLOCKHASHES, hash::hash, sysvar::recent_blockhashes::IterItem,
};
#[test]
fn test_register_hash() {
@@ -172,7 +178,7 @@ mod tests {
}
let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
// Verify that the returned hashes are most recent
for (_slot, hash) in recent_blockhashes {
for IterItem(_slot, hash, _fee_calc) in recent_blockhashes {
assert_eq!(
Some(true),
blockhash_queue.check_hash_age(hash, MAX_RECENT_BLOCKHASHES)

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,9 +1,10 @@
use solana_sdk::{
account::Account,
account_utils::StateMut,
fee_calculator::FeeCalculator,
hash::Hash,
instruction::CompiledInstruction,
nonce_state::NonceState,
nonce::{self, state::Versions, State},
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction::SystemInstruction,
@@ -39,8 +40,8 @@ pub fn get_nonce_pubkey_from_instruction<'a>(
}
pub fn verify_nonce_account(acc: &Account, hash: &Hash) -> bool {
match acc.state() {
Ok(NonceState::Initialized(_meta, ref nonce)) => hash == nonce,
match StateMut::<Versions>::state(acc).map(|v| v.convert_to_current()) {
Ok(State::Initialized(ref data)) => *hash == data.blockhash,
_ => false,
}
}
@@ -60,24 +61,39 @@ pub fn prepare_if_nonce_account(
*account = nonce_acc.clone()
}
// Since hash_age_kind is DurableNonce, unwrap is safe here
if let NonceState::Initialized(meta, _) = account.state().unwrap() {
account
.set_state(&NonceState::Initialized(meta, *last_blockhash))
.unwrap();
let state = StateMut::<Versions>::state(nonce_acc)
.unwrap()
.convert_to_current();
if let State::Initialized(ref data) = state {
let new_data = Versions::new_current(State::Initialized(nonce::state::Data {
blockhash: *last_blockhash,
..data.clone()
}));
account.set_state(&new_data).unwrap();
}
}
}
}
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::Account,
account_utils::State,
account_utils::State as AccountUtilsState,
hash::Hash,
instruction::InstructionError,
nonce_state::{with_test_keyed_account, Meta, NonceAccount},
nonce::{self, account::with_test_keyed_account, Account as NonceAccount, State},
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction,
@@ -193,9 +209,9 @@ mod tests {
with_test_keyed_account(42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(nonce_account.signer_key().unwrap().clone());
let state: NonceState = nonce_account.state().unwrap();
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, NonceState::Uninitialized);
assert_eq!(state, State::Uninitialized);
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key().clone();
nonce_account
@@ -203,7 +219,7 @@ mod tests {
.unwrap();
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[0]
&recent_blockhashes[0].blockhash,
));
});
}
@@ -223,9 +239,9 @@ mod tests {
with_test_keyed_account(42, true, |nonce_account| {
let mut signers = HashSet::new();
signers.insert(nonce_account.signer_key().unwrap().clone());
let state: NonceState = nonce_account.state().unwrap();
let state: State = nonce_account.state().unwrap();
// New is in Uninitialzed state
assert_eq!(state, NonceState::Uninitialized);
assert_eq!(state, State::Uninitialized);
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key().clone();
nonce_account
@@ -233,19 +249,14 @@ mod tests {
.unwrap();
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[1]
&recent_blockhashes[1].blockhash,
));
});
}
fn create_accounts_prepare_if_nonce_account() -> (Pubkey, Account, Account, Hash) {
let stored_nonce = Hash::default();
let account = Account::new_data(
42,
&NonceState::Initialized(Meta::new(&Pubkey::default()), stored_nonce),
&system_program::id(),
)
.unwrap();
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,
..account.clone()
@@ -296,12 +307,11 @@ mod tests {
let post_account_pubkey = pre_account_pubkey;
let mut expect_account = post_account.clone();
expect_account
.set_state(&NonceState::Initialized(
Meta::new(&Pubkey::default()),
last_blockhash,
))
.unwrap();
let data = Versions::new_current(State::Initialized(nonce::state::Data {
blockhash: last_blockhash,
..nonce::state::Data::default()
}));
expect_account.set_state(&data).unwrap();
assert!(run_prepare_if_nonce_account_test(
&mut post_account,
@@ -356,10 +366,12 @@ mod tests {
let mut expect_account = pre_account.clone();
expect_account
.set_state(&NonceState::Initialized(
Meta::new(&Pubkey::default()),
last_blockhash,
))
.set_state(&Versions::new_current(State::Initialized(
nonce::state::Data {
blockhash: last_blockhash,
..nonce::state::Data::default()
},
)))
.unwrap();
assert!(run_prepare_if_nonce_account_test(

View File

@@ -3,7 +3,7 @@ use solana_sdk::{
account::{get_signers, Account, KeyedAccount},
account_utils::StateMut,
instruction::InstructionError,
nonce_state::{NonceAccount, NonceState},
nonce::{self, Account as NonceAccount},
program_utils::{limited_deserialize, next_keyed_account},
pubkey::Pubkey,
system_instruction::{
@@ -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(NonceState::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
}
@@ -324,13 +329,15 @@ mod tests {
use solana_sdk::{
account::Account,
client::SyncClient,
fee_calculator::FeeCalculator,
genesis_config::create_genesis_config,
hash::{hash, Hash},
instruction::{AccountMeta, Instruction, InstructionError},
message::Message,
nonce_state,
nonce,
signature::{Keypair, Signer},
system_instruction, system_program, sysvar,
sysvar::recent_blockhashes::IterItem,
transaction::TransactionError,
};
use std::cell::RefCell;
@@ -351,14 +358,11 @@ mod tests {
fn create_default_recent_blockhashes_account() -> RefCell<Account> {
RefCell::new(sysvar::recent_blockhashes::create_account_with_data(
1,
vec![(0u64, &Hash::default()); 32].into_iter(),
vec![IterItem(0u64, &Hash::default(), &FeeCalculator::default()); 32].into_iter(),
))
}
fn create_default_rent_account() -> RefCell<Account> {
RefCell::new(sysvar::recent_blockhashes::create_account_with_data(
1,
vec![(0u64, &Hash::default()); 32].into_iter(),
))
RefCell::new(sysvar::rent::create_account(1, &Rent::free()))
}
#[test]
@@ -737,10 +741,9 @@ mod tests {
let nonce = Pubkey::new_rand();
let nonce_account = Account::new_ref_data(
42,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -879,7 +882,10 @@ mod tests {
let from = Pubkey::new_rand();
let from_account = Account::new_ref_data(
100,
&nonce_state::NonceState::Initialized(nonce_state::Meta::new(&from), Hash::default()),
&nonce::state::Versions::new_current(nonce::State::Initialized(nonce::state::Data {
authority: from,
..nonce::state::Data::default()
})),
&system_program::id(),
)
.unwrap();
@@ -1084,7 +1090,8 @@ mod tests {
RefCell::new(if sysvar::recent_blockhashes::check_id(&meta.pubkey) {
sysvar::recent_blockhashes::create_account_with_data(
1,
vec![(0u64, &Hash::default()); 32].into_iter(),
vec![IterItem(0u64, &Hash::default(), &FeeCalculator::default()); 32]
.into_iter(),
)
} else if sysvar::rent::check_id(&meta.pubkey) {
sysvar::rent::create_account(1, &Rent::free())
@@ -1165,7 +1172,7 @@ mod tests {
#[test]
fn test_process_nonce_ix_ok() {
let nonce_acc = nonce_state::create_account(1_000_000);
let nonce_acc = nonce::create_account(1_000_000);
super::process_instruction(
&Pubkey::default(),
&[
@@ -1183,7 +1190,15 @@ mod tests {
let new_recent_blockhashes_account =
RefCell::new(sysvar::recent_blockhashes::create_account_with_data(
1,
vec![(0u64, &hash(&serialize(&0).unwrap())); 32].into_iter(),
vec![
IterItem(
0u64,
&hash(&serialize(&0).unwrap()),
&FeeCalculator::default()
);
32
]
.into_iter(),
));
assert_eq!(
super::process_instruction(
@@ -1269,11 +1284,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[
KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &nonce::create_account(1_000_000),),
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
@@ -1294,11 +1305,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[
KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &nonce::create_account(1_000_000),),
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
@@ -1333,7 +1340,7 @@ mod tests {
&[KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
&nonce::create_account(1_000_000),
),],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
@@ -1347,11 +1354,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[
KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &nonce::create_account(1_000_000),),
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
false,
@@ -1370,11 +1373,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[
KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &nonce::create_account(1_000_000),),
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
false,
@@ -1394,11 +1393,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[
KeyedAccount::new(
&Pubkey::default(),
true,
&nonce_state::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &nonce::create_account(1_000_000),),
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
false,
@@ -1414,7 +1409,7 @@ mod tests {
#[test]
fn test_process_authorize_ix_ok() {
let nonce_acc = nonce_state::create_account(1_000_000);
let nonce_acc = nonce::create_account(1_000_000);
super::process_instruction(
&Pubkey::default(),
&[
@@ -1464,10 +1459,9 @@ mod tests {
fn test_get_system_account_kind_nonce_ok() {
let nonce_account = Account::new_data(
42,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&system_program::id(),
)
.unwrap();
@@ -1480,7 +1474,7 @@ mod tests {
#[test]
fn test_get_system_account_kind_uninitialized_nonce_account_fail() {
assert_eq!(
get_system_account_kind(&nonce_state::create_account(42).borrow()),
get_system_account_kind(&nonce::create_account(42).borrow()),
None
);
}
@@ -1492,13 +1486,12 @@ mod tests {
}
#[test]
fn test_get_system_account_kind_nonsystem_owner_with_nonce_state_data_fail() {
fn test_get_system_account_kind_nonsystem_owner_with_nonce_data_fail() {
let nonce_account = Account::new_data(
42,
&nonce_state::NonceState::Initialized(
nonce_state::Meta::new(&Pubkey::default()),
Hash::default(),
),
&nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Data::default(),
)),
&Pubkey::new_rand(),
)
.unwrap();