Deprecate SysvarRecentBlockhashes (#18875)

This commit is contained in:
Jack May
2021-07-27 16:34:21 -07:00
committed by GitHub
parent f1b9f97aef
commit 72e374d0f3
10 changed files with 167 additions and 33 deletions

View File

@ -1,10 +1,12 @@
#[allow(deprecated)]
use crate::sysvar::recent_blockhashes;
use crate::{
decode_error::DecodeError,
instruction::{AccountMeta, Instruction},
nonce,
pubkey::Pubkey,
system_program,
sysvar::{recent_blockhashes, rent},
sysvar::rent,
};
use num_derive::{FromPrimitive, ToPrimitive};
use thiserror::Error;
@ -400,6 +402,7 @@ pub fn create_nonce_account_with_seed(
&SystemInstruction::InitializeNonceAccount(*authority),
vec![
AccountMeta::new(*nonce_pubkey, false),
#[allow(deprecated)]
AccountMeta::new_readonly(recent_blockhashes::id(), false),
AccountMeta::new_readonly(rent::id(), false),
],
@ -426,6 +429,7 @@ pub fn create_nonce_account(
&SystemInstruction::InitializeNonceAccount(*authority),
vec![
AccountMeta::new(*nonce_pubkey, false),
#[allow(deprecated)]
AccountMeta::new_readonly(recent_blockhashes::id(), false),
AccountMeta::new_readonly(rent::id(), false),
],
@ -436,6 +440,7 @@ pub fn create_nonce_account(
pub fn advance_nonce_account(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*nonce_pubkey, false),
#[allow(deprecated)]
AccountMeta::new_readonly(recent_blockhashes::id(), false),
AccountMeta::new_readonly(*authorized_pubkey, true),
];
@ -455,6 +460,7 @@ pub fn withdraw_nonce_account(
let account_metas = vec![
AccountMeta::new(*nonce_pubkey, false),
AccountMeta::new(*to_pubkey, false),
#[allow(deprecated)]
AccountMeta::new_readonly(recent_blockhashes::id(), false),
AccountMeta::new_readonly(rent::id(), false),
AccountMeta::new_readonly(*authorized_pubkey, true),

View File

@ -13,6 +13,7 @@ pub mod slot_hashes;
pub mod slot_history;
pub mod stake_history;
#[allow(deprecated)]
pub fn is_sysvar_id(id: &Pubkey) -> bool {
clock::check_id(id)
|| epoch_schedule::check_id(id)

View File

@ -1,26 +1,38 @@
#![allow(deprecated)]
#![allow(clippy::integer_arithmetic)]
use crate::{
declare_sysvar_id,
declare_deprecated_sysvar_id,
fee_calculator::FeeCalculator,
hash::{hash, Hash},
sysvar::Sysvar,
};
use std::{cmp::Ordering, collections::BinaryHeap, iter::FromIterator, ops::Deref};
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
pub const MAX_ENTRIES: usize = 150;
declare_sysvar_id!(
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
declare_deprecated_sysvar_id!(
"SysvarRecentB1ockHashes11111111111111111111",
RecentBlockhashes
);
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Entry {
pub blockhash: Hash,
pub fee_calculator: FeeCalculator,
}
impl Entry {
pub fn new(blockhash: &Hash, fee_calculator: &FeeCalculator) -> Self {
Self {
@ -30,6 +42,10 @@ impl Entry {
}
}
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[derive(Clone, Debug)]
pub struct IterItem<'a>(pub u64, pub &'a Hash, pub &'a FeeCalculator);
@ -57,6 +73,10 @@ impl<'a> PartialOrd for IterItem<'a> {
///
/// The entries are ordered by descending block height, so the first entry holds
/// the most recent block hash, and the last entry holds an old block hash.
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RecentBlockhashes(Vec<Entry>);

View File

@ -13,10 +13,13 @@ use solana_program::{
nonce::{self, state::Versions, State},
pubkey::Pubkey,
system_instruction::NonceError,
sysvar::{recent_blockhashes::RecentBlockhashes, rent::Rent},
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,
@ -49,6 +52,7 @@ pub trait NonceKeyedAccount {
}
impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
#[allow(deprecated)]
fn advance_nonce_account(
&self,
recent_blockhashes: &RecentBlockhashes,
@ -101,6 +105,7 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
}
}
#[allow(deprecated)]
fn withdraw_nonce_account(
&self,
lamports: u64,
@ -175,6 +180,7 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
Ok(())
}
#[allow(deprecated)]
fn initialize_nonce_account(
&self,
nonce_authority: &Pubkey,
@ -268,6 +274,8 @@ 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,
@ -276,7 +284,6 @@ mod test {
nonce_account::verify_nonce_account,
process_instruction::MockInvokeContext,
system_instruction::NonceError,
sysvar::recent_blockhashes::create_test_recent_blockhashes,
};
use solana_program::hash::Hash;
@ -304,6 +311,7 @@ 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 authorized = keyed_account.unsigned_key();
keyed_account
@ -317,6 +325,7 @@ mod test {
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(),
@ -324,6 +333,7 @@ mod test {
};
// 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);
keyed_account
.advance_nonce_account(
@ -335,6 +345,7 @@ mod test {
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(),
@ -342,6 +353,7 @@ mod test {
};
// 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);
keyed_account
.advance_nonce_account(
@ -353,6 +365,7 @@ mod test {
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(),
@ -361,6 +374,7 @@ mod test {
// 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 withdraw_lamports = keyed_account.account.borrow().lamports();
let expect_nonce_lamports =
@ -400,6 +414,7 @@ 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 authority = *nonce_account.unsigned_key();
nonce_account
@ -415,6 +430,7 @@ mod test {
let state = AccountUtilsState::<Versions>::state(&nonce_account)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
@ -422,6 +438,7 @@ mod test {
};
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,
@ -442,6 +459,7 @@ 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(0);
let authorized = *keyed_account.unsigned_key();
keyed_account
@ -472,6 +490,7 @@ 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 authorized = *keyed_account.unsigned_key();
keyed_account
@ -501,6 +520,7 @@ 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,
@ -522,6 +542,7 @@ 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 authorized = *nonce_authority.unsigned_key();
nonce_account
@ -534,6 +555,7 @@ mod test {
.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,
@ -556,6 +578,7 @@ 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 authorized = *nonce_authority.unsigned_key();
nonce_account
@ -591,6 +614,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 withdraw_lamports = nonce_keyed.account.borrow().lamports();
let expect_nonce_lamports =
@ -637,6 +661,7 @@ 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 lamports = nonce_keyed.account.borrow().lamports();
let result = nonce_keyed.withdraw_nonce_account(
@ -667,6 +692,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 lamports = nonce_keyed.account.borrow().lamports() + 1;
let result = nonce_keyed.withdraw_nonce_account(
@ -693,6 +719,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 withdraw_lamports = nonce_keyed.account.borrow().lamports() / 2;
let nonce_expect_lamports =
@ -754,6 +781,7 @@ 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 authority = *nonce_keyed.unsigned_key();
nonce_keyed
@ -767,6 +795,7 @@ mod test {
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
.unwrap()
.convert_to_current();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
@ -791,6 +820,7 @@ mod test {
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(),
@ -802,6 +832,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 withdraw_lamports = nonce_keyed.account.borrow().lamports();
let nonce_expect_lamports =
@ -838,6 +869,7 @@ 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 authorized = *nonce_keyed.unsigned_key();
nonce_keyed
@ -873,6 +905,7 @@ 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 authorized = *nonce_keyed.unsigned_key();
nonce_keyed
@ -884,6 +917,7 @@ mod test {
)
.unwrap();
with_test_keyed_account(42, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
@ -909,6 +943,7 @@ 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 authorized = *nonce_keyed.unsigned_key();
nonce_keyed
@ -920,6 +955,7 @@ mod test {
)
.unwrap();
with_test_keyed_account(42, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
@ -945,6 +981,7 @@ 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 authorized = *nonce_keyed.unsigned_key();
nonce_keyed
@ -956,6 +993,7 @@ mod test {
)
.unwrap();
with_test_keyed_account(55, false, |to_keyed| {
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(63);
let mut signers = HashSet::new();
signers.insert(*nonce_keyed.signer_key().unwrap());
@ -987,6 +1025,7 @@ 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 authority = *keyed_account.unsigned_key();
let result = keyed_account.initialize_nonce_account(
@ -995,6 +1034,7 @@ mod test {
&rent,
&MockInvokeContext::new(vec![]),
);
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
@ -1038,6 +1078,7 @@ 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 authorized = *keyed_account.unsigned_key();
keyed_account
@ -1048,6 +1089,7 @@ mod test {
&MockInvokeContext::new(vec![]),
)
.unwrap();
#[allow(deprecated)]
let recent_blockhashes = create_test_recent_blockhashes(0);
let result = keyed_account.initialize_nonce_account(
&authorized,
@ -1067,6 +1109,7 @@ 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 authorized = *keyed_account.unsigned_key();
let result = keyed_account.initialize_nonce_account(
@ -1089,6 +1132,7 @@ 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 authorized = *nonce_account.unsigned_key();
nonce_account
@ -1100,6 +1144,7 @@ mod test {
)
.unwrap();
let authority = Pubkey::default();
#[allow(deprecated)]
let data = nonce::state::Data {
authority,
blockhash: recent_blockhashes[0].blockhash,
@ -1147,6 +1192,7 @@ 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 authorized = &Pubkey::default().clone();
nonce_account
@ -1174,6 +1220,7 @@ 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 authorized = nonce_account.unsigned_key();
nonce_account
@ -1186,6 +1233,7 @@ mod test {
.unwrap();
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
#[allow(deprecated)]
&recent_blockhashes[0].blockhash,
));
});
@ -1209,6 +1257,7 @@ 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 authorized = nonce_account.unsigned_key();
nonce_account
@ -1221,6 +1270,7 @@ mod test {
.unwrap();
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
#[allow(deprecated)]
&recent_blockhashes[1].blockhash,
));
});

View File

@ -3,11 +3,17 @@ use crate::account::{
InheritableAccountFields, DUMMY_INHERITABLE_ACCOUNT_FIELDS,
};
use crate::clock::INITIAL_RENT_EPOCH;
#[allow(deprecated)]
use solana_program::sysvar::recent_blockhashes::{
IntoIterSorted, IterItem, RecentBlockhashes, MAX_ENTRIES,
};
use std::{collections::BinaryHeap, iter::FromIterator};
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[allow(deprecated)]
pub fn update_account<'a, I>(
account: &mut AccountSharedData,
recent_blockhash_iter: I,
@ -16,8 +22,11 @@ where
I: IntoIterator<Item = IterItem<'a>>,
{
let sorted = BinaryHeap::from_iter(recent_blockhash_iter);
#[allow(deprecated)]
let sorted_iter = IntoIterSorted::new(sorted);
#[allow(deprecated)]
let recent_blockhash_iter = sorted_iter.take(MAX_ENTRIES);
#[allow(deprecated)]
let recent_blockhashes: RecentBlockhashes = recent_blockhash_iter.collect();
to_account(&recent_blockhashes, account)
}
@ -26,13 +35,20 @@ where
since = "1.5.17",
note = "Please use `create_account_with_data_for_test` instead"
)]
#[allow(deprecated)]
pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> AccountSharedData
where
I: IntoIterator<Item = IterItem<'a>>,
{
#[allow(deprecated)]
create_account_with_data_and_fields(recent_blockhash_iter, (lamports, INITIAL_RENT_EPOCH))
}
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[allow(deprecated)]
pub fn create_account_with_data_and_fields<'a, I>(
recent_blockhash_iter: I,
fields: InheritableAccountFields,
@ -48,6 +64,11 @@ where
account
}
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
#[allow(deprecated)]
pub fn create_account_with_data_for_test<'a, I>(recent_blockhash_iter: I) -> AccountSharedData
where
I: IntoIterator<Item = IterItem<'a>>,
@ -57,6 +78,7 @@ where
#[cfg(test)]
mod tests {
#![allow(deprecated)]
use super::*;
use crate::account::from_account;
use rand::{seq::SliceRandom, thread_rng};