Nonce gets blockhash from invoke_context (#18950)

This commit is contained in:
Jack May
2021-07-29 01:50:20 -07:00
committed by GitHub
parent c0d2e81e89
commit 9529284194
6 changed files with 306 additions and 394 deletions

View File

@ -3262,6 +3262,18 @@ impl Bank {
compute_budget.max_units,
)));
let (blockhash, fee_calculator) = {
let blockhash_queue = self.blockhash_queue.read().unwrap();
let blockhash = blockhash_queue.last_hash();
(
blockhash,
blockhash_queue
.get_fee_calculator(&blockhash)
.cloned()
.unwrap_or_else(|| self.fee_calculator.clone()),
)
};
process_result = self.message_processor.process_message(
tx.message(),
&loader_refcells,
@ -3276,6 +3288,8 @@ impl Bank {
&mut timings.details,
self.rc.accounts.clone(),
&self.ancestors,
blockhash,
fee_calculator,
);
transaction_log_messages.push(Self::collect_log_messages(log_collector));

View File

@ -14,6 +14,8 @@ use solana_sdk::{
instructions_sysvar_enabled, neon_evm_compute_budget, tx_wide_compute_cap,
updated_verify_policy, FeatureSet,
},
fee_calculator::FeeCalculator,
hash::Hash,
ic_logger_msg, ic_msg,
instruction::{CompiledInstruction, Instruction, InstructionError},
keyed_account::{create_keyed_accounts_unified, keyed_account_at_index, KeyedAccount},
@ -303,6 +305,8 @@ pub struct ThisInvokeContext<'a> {
ancestors: &'a Ancestors,
#[allow(clippy::type_complexity)]
sysvars: RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>,
blockhash: &'a Hash,
fee_calculator: &'a FeeCalculator,
}
impl<'a> ThisInvokeContext<'a> {
#[allow(clippy::too_many_arguments)]
@ -322,6 +326,8 @@ impl<'a> ThisInvokeContext<'a> {
feature_set: Arc<FeatureSet>,
account_db: Arc<Accounts>,
ancestors: &'a Ancestors,
blockhash: &'a Hash,
fee_calculator: &'a FeeCalculator,
) -> Self {
let pre_accounts = MessageProcessor::create_pre_accounts(message, instruction, accounts);
let keyed_accounts = MessageProcessor::create_keyed_accounts(
@ -354,6 +360,8 @@ impl<'a> ThisInvokeContext<'a> {
account_db,
ancestors,
sysvars: RefCell::new(vec![]),
blockhash,
fee_calculator,
};
invoke_context
.invoke_stack
@ -539,6 +547,12 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
fn get_compute_budget(&self) -> &ComputeBudget {
&self.compute_budget
}
fn get_blockhash(&self) -> &Hash {
self.blockhash
}
fn get_fee_calculator(&self) -> &FeeCalculator {
self.fee_calculator
}
}
pub struct ThisLogger {
log_collector: Option<Rc<LogCollector>>,
@ -1168,6 +1182,8 @@ impl MessageProcessor {
timings: &mut ExecuteDetailsTimings,
account_db: Arc<Accounts>,
ancestors: &Ancestors,
blockhash: &Hash,
fee_calculator: &FeeCalculator,
) -> Result<(), InstructionError> {
// Fixup the special instructions key if present
// before the account pre-values are taken care of
@ -1211,6 +1227,8 @@ impl MessageProcessor {
feature_set,
account_db,
ancestors,
blockhash,
fee_calculator,
);
self.process_instruction(program_id, &instruction.data, &mut invoke_context)?;
Self::verify(
@ -1250,6 +1268,8 @@ impl MessageProcessor {
timings: &mut ExecuteDetailsTimings,
account_db: Arc<Accounts>,
ancestors: &Ancestors,
blockhash: Hash,
fee_calculator: FeeCalculator,
) -> Result<(), TransactionError> {
for (instruction_index, instruction) in message.instructions.iter().enumerate() {
let mut time = Measure::start("execute_instruction");
@ -1274,6 +1294,8 @@ impl MessageProcessor {
timings,
account_db.clone(),
ancestors,
&blockhash,
&fee_calculator,
)
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err));
time.stop();
@ -1337,6 +1359,8 @@ mod tests {
None,
);
let ancestors = Ancestors::default();
let blockhash = Hash::default();
let fee_calculator = FeeCalculator::default();
let mut invoke_context = ThisInvokeContext::new(
&invoke_stack[0],
Rent::default(),
@ -1353,6 +1377,8 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
&blockhash,
&fee_calculator,
);
// Check call depth increases and has a limit
@ -1965,6 +1991,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(accounts[0].1.borrow().lamports(), 100);
@ -1993,6 +2021,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(
result,
@ -2025,6 +2055,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(
result,
@ -2149,6 +2181,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(
result,
@ -2181,6 +2215,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(result, Ok(()));
@ -2211,6 +2247,8 @@ mod tests {
&mut ExecuteDetailsTimings::default(),
Arc::new(Accounts::default()),
&ancestors,
Hash::default(),
FeeCalculator::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(accounts[0].1.borrow().lamports(), 80);
@ -2299,6 +2337,8 @@ mod tests {
let message = Message::new(&[instruction], None);
let ancestors = Ancestors::default();
let blockhash = Hash::default();
let fee_calculator = FeeCalculator::default();
let mut invoke_context = ThisInvokeContext::new(
&caller_program_id,
Rent::default(),
@ -2315,6 +2355,8 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
&blockhash,
&fee_calculator,
);
// not owned account modified by the caller (before the invoke)
@ -2356,6 +2398,8 @@ mod tests {
let message = Message::new(&[instruction], None);
let ancestors = Ancestors::default();
let blockhash = Hash::default();
let fee_calculator = FeeCalculator::default();
let mut invoke_context = ThisInvokeContext::new(
&caller_program_id,
Rent::default(),
@ -2372,6 +2416,8 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
&blockhash,
&fee_calculator,
);
let caller_write_privileges = message

View File

@ -1,6 +1,4 @@
use log::*;
#[allow(deprecated)]
use solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes;
use solana_sdk::{
account::{AccountSharedData, ReadableAccount, WritableAccount},
account_utils::StateMut,
@ -12,7 +10,7 @@ use solana_sdk::{
process_instruction::InvokeContext,
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction::{SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH},
system_instruction::{NonceError, SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH},
system_program,
sysvar::{self, rent::Rent},
};
@ -353,27 +351,30 @@ pub fn process_instruction(
}
SystemInstruction::AdvanceNonceAccount => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
me.advance_nonce_account(
#[allow(deprecated)]
&from_keyed_account::<RecentBlockhashes>(keyed_account_at_index(
keyed_accounts,
1,
)?)?,
&signers,
invoke_context,
)
#[allow(deprecated)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 1)?,
)?
.is_empty()
{
ic_msg!(
invoke_context,
"Advance nonce account: recent blockhash list is empty",
);
return Err(NonceError::NoRecentBlockhashes.into());
}
me.advance_nonce_account(&signers, invoke_context)
}
SystemInstruction::WithdrawNonceAccount(lamports) => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
let to = &mut keyed_account_at_index(keyed_accounts, 1)?;
#[allow(deprecated)]
let _ = from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 2)?,
)?;
me.withdraw_nonce_account(
lamports,
to,
#[allow(deprecated)]
&from_keyed_account::<RecentBlockhashes>(keyed_account_at_index(
keyed_accounts,
2,
)?)?,
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 3)?)?,
&signers,
invoke_context,
@ -381,13 +382,20 @@ pub fn process_instruction(
}
SystemInstruction::InitializeNonceAccount(authorized) => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
#[allow(deprecated)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 1)?,
)?
.is_empty()
{
ic_msg!(
invoke_context,
"Initialize nonce account: recent blockhash list is empty",
);
return Err(NonceError::NoRecentBlockhashes.into());
}
me.initialize_nonce_account(
&authorized,
#[allow(deprecated)]
&from_keyed_account::<RecentBlockhashes>(keyed_account_at_index(
keyed_accounts,
1,
)?)?,
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 2)?)?,
invoke_context,
)
@ -1562,33 +1570,30 @@ mod tests {
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
)
.unwrap();
let blockhash = &hash(&serialize(&0).unwrap());
let new_recent_blockhashes_account = RefCell::new(
#[allow(deprecated)]
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
vec![
IterItem(
0u64,
&hash(&serialize(&0).unwrap()),
&FeeCalculator::default()
);
IterItem(0u64, blockhash, &FeeCalculator::default());
sysvar::recent_blockhashes::MAX_ENTRIES
]
.into_iter(),
),
);
let owner = Pubkey::default();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
let mut invoke_context = &mut MockInvokeContext::new(vec![
KeyedAccount::new(&owner, true, &nonce_acc),
KeyedAccount::new(&blockhash_id, false, &new_recent_blockhashes_account),
]);
invoke_context.blockhash = *blockhash;
assert_eq!(
process_instruction(
super::process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc,),
#[allow(deprecated)]
KeyedAccount::new(
&sysvar::recent_blockhashes::id(),
false,
&new_recent_blockhashes_account,
),
],
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,
),
Ok(()),
);
@ -1902,4 +1907,75 @@ mod tests {
.unwrap();
assert_eq!(get_system_account_kind(&nonce_account), None);
}
#[test]
fn test_nonce_initialize_with_empty_recent_blockhashes_fail() {
let nonce_acc = nonce_account::create_account(1_000_000);
let new_recent_blockhashes_account = RefCell::new(
#[allow(deprecated)]
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
vec![].into_iter(),
),
);
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&new_recent_blockhashes_account,
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Err(NonceError::NoRecentBlockhashes.into())
);
}
#[test]
fn test_nonce_advance_with_empty_recent_blockhashes_fail() {
let nonce_acc = nonce_account::create_account(1_000_000);
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
)
.unwrap();
let blockhash = &hash(&serialize(&0).unwrap());
let new_recent_blockhashes_account = RefCell::new(
#[allow(deprecated)]
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
vec![].into_iter(),
),
);
let owner = Pubkey::default();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
let mut invoke_context = &mut MockInvokeContext::new(vec![
KeyedAccount::new(&owner, true, &nonce_acc),
KeyedAccount::new(&blockhash_id, false, &new_recent_blockhashes_account),
]);
invoke_context.blockhash = *blockhash;
assert_eq!(
super::process_instruction(
&Pubkey::default(),
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,
),
Err(NonceError::NoRecentBlockhashes.into()),
);
}
}