SDK: Store FeeCalculator in recent_blockhashes sysvar (#8609)

* SDK: Store FeeCalculators in recent_blockhashes sysvar

* nits
This commit is contained in:
Trent Nelson
2020-03-04 12:01:32 -07:00
committed by GitHub
parent 25df95be6f
commit 561808cf90
6 changed files with 131 additions and 54 deletions

View File

@ -4991,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));
}

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

@ -208,7 +208,7 @@ mod tests {
.unwrap();
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[0]
&recent_blockhashes[0].blockhash,
));
});
}
@ -238,7 +238,7 @@ mod tests {
.unwrap();
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[1]
&recent_blockhashes[1].blockhash,
));
});
}

View File

@ -324,6 +324,7 @@ mod tests {
use solana_sdk::{
account::Account,
client::SyncClient,
fee_calculator::FeeCalculator,
genesis_config::create_genesis_config,
hash::{hash, Hash},
instruction::{AccountMeta, Instruction, InstructionError},
@ -331,6 +332,7 @@ mod tests {
nonce,
signature::{Keypair, Signer},
system_instruction, system_program, sysvar,
sysvar::recent_blockhashes::IterItem,
transaction::TransactionError,
};
use std::cell::RefCell;
@ -350,7 +352,7 @@ 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> {
@ -1011,7 +1013,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())
@ -1110,7 +1113,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(