Verb-noun-ify Nonce API (#7925)

* Verb-noun-ify Nonce API

* Unify instruction naming with API naming

The more verbose nonce_account/NonceAccount was chosen for clarity
that these instructions work on a unique species of system account
This commit is contained in:
Trent Nelson
2020-01-22 16:31:39 -07:00
committed by GitHub
parent 934c32cbc6
commit 964ff522be
7 changed files with 103 additions and 91 deletions

View File

@ -1104,7 +1104,7 @@ impl Bank {
.map(|acc| (*nonce_pubkey, acc))
})
.filter(|(_pubkey, nonce_account)| {
nonce_utils::verify_nonce(nonce_account, &tx.message().recent_blockhash)
nonce_utils::verify_nonce_account(nonce_account, &tx.message().recent_blockhash)
})
}
@ -4945,7 +4945,7 @@ mod tests {
}
}
fn get_nonce(bank: &Bank, nonce_pubkey: &Pubkey) -> Option<Hash> {
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),
@ -5016,10 +5016,10 @@ mod tests {
let custodian_pubkey = custodian_keypair.pubkey();
let nonce_pubkey = nonce_keypair.pubkey();
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
let nonce_hash = get_nonce_account(&bank, &nonce_pubkey).unwrap();
let tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5040,11 +5040,11 @@ mod tests {
let custodian_pubkey = custodian_keypair.pubkey();
let nonce_pubkey = nonce_keypair.pubkey();
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
let nonce_hash = get_nonce_account(&bank, &nonce_pubkey).unwrap();
let tx = Transaction::new_signed_with_payer(
vec![
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
],
Some(&custodian_pubkey),
&[&custodian_keypair, &nonce_keypair],
@ -5060,10 +5060,10 @@ mod tests {
let custodian_pubkey = custodian_keypair.pubkey();
let nonce_pubkey = nonce_keypair.pubkey();
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
let nonce_hash = get_nonce_account(&bank, &nonce_pubkey).unwrap();
let mut tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5083,10 +5083,10 @@ mod tests {
let missing_keypair = Keypair::new();
let missing_pubkey = missing_keypair.pubkey();
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
let nonce_hash = get_nonce_account(&bank, &nonce_pubkey).unwrap();
let tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&missing_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&missing_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5105,7 +5105,7 @@ mod tests {
let tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5169,7 +5169,7 @@ mod tests {
assert_eq!(bank.get_balance(&nonce_pubkey), 250_000);
/* Grab the hash stored in the nonce account */
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
let nonce_hash = get_nonce_account(&bank, &nonce_pubkey).unwrap();
/* Kick nonce hash off the blockhash_queue */
for _ in 0..MAX_RECENT_BLOCKHASHES + 1 {
@ -5193,7 +5193,7 @@ mod tests {
/* Durable Nonce transfer */
let durable_tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5208,13 +5208,13 @@ mod tests {
assert_eq!(bank.get_balance(&alice_pubkey), 100_000);
/* Confirm stored nonce has advanced */
let new_nonce = get_nonce(&bank, &nonce_pubkey).unwrap();
let new_nonce = get_nonce_account(&bank, &nonce_pubkey).unwrap();
assert_ne!(nonce_hash, new_nonce);
/* Durable Nonce re-use fails */
let durable_tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000),
],
Some(&custodian_pubkey),
@ -5227,7 +5227,7 @@ mod tests {
);
/* Check fee not charged and nonce not advanced */
assert_eq!(bank.get_balance(&custodian_pubkey), 4_640_000);
assert_eq!(new_nonce, get_nonce(&bank, &nonce_pubkey).unwrap());
assert_eq!(new_nonce, get_nonce_account(&bank, &nonce_pubkey).unwrap());
let nonce_hash = new_nonce;
@ -5239,7 +5239,7 @@ mod tests {
let durable_tx = Transaction::new_signed_with_payer(
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000_000),
],
Some(&custodian_pubkey),
@ -5255,7 +5255,7 @@ mod tests {
);
/* Check fee charged and nonce has advanced */
assert_eq!(bank.get_balance(&custodian_pubkey), 4_630_000);
assert_ne!(nonce_hash, get_nonce(&bank, &nonce_pubkey).unwrap());
assert_ne!(nonce_hash, get_nonce_account(&bank, &nonce_pubkey).unwrap());
/* Confirm replaying a TX that failed with InstructionError::* now
* fails with TransactionError::BlockhashNotFound
*/

View File

@ -39,7 +39,7 @@ pub fn get_nonce_pubkey_from_instruction<'a>(
})
}
pub fn verify_nonce(acc: &Account, hash: &Hash) -> bool {
pub fn verify_nonce_account(acc: &Account, hash: &Hash) -> bool {
match acc.state() {
Ok(NonceState::Initialized(_meta, ref nonce)) => hash == nonce,
_ => false,
@ -93,7 +93,7 @@ mod tests {
let tx = Transaction::new_signed_instructions(
&[&from_keypair, &nonce_keypair],
vec![
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
],
Hash::default(),
@ -131,7 +131,7 @@ mod tests {
&[&from_keypair, &nonce_keypair],
vec![
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
system_instruction::advance_nonce_account(&nonce_pubkey, &nonce_pubkey),
],
Hash::default(),
);
@ -147,7 +147,12 @@ mod tests {
let tx = Transaction::new_signed_instructions(
&[&from_keypair, &nonce_keypair],
vec![
system_instruction::nonce_withdraw(&nonce_pubkey, &nonce_pubkey, &from_pubkey, 42),
system_instruction::withdraw_nonce_account(
&nonce_pubkey,
&nonce_pubkey,
&from_pubkey,
42,
),
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
],
Hash::default(),
@ -194,9 +199,9 @@ mod tests {
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key().clone();
nonce_account
.nonce_initialize(&authorized, &recent_blockhashes, &Rent::free())
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
.unwrap();
assert!(verify_nonce(
assert!(verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[0]
));
@ -206,7 +211,7 @@ mod tests {
#[test]
fn verify_nonce_bad_acc_state_fail() {
with_test_keyed_account(42, true, |nonce_account| {
assert!(!verify_nonce(
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&Hash::default()
));
@ -224,9 +229,9 @@ mod tests {
let recent_blockhashes = create_test_recent_blockhashes(0);
let authorized = nonce_account.unsigned_key().clone();
nonce_account
.nonce_initialize(&authorized, &recent_blockhashes, &Rent::free())
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
.unwrap();
assert!(!verify_nonce(
assert!(!verify_nonce_account(
&nonce_account.account.borrow(),
&recent_blockhashes[1]
));

View File

@ -237,7 +237,7 @@ pub fn process_instruction(
}
SystemInstruction::AdvanceNonceAccount => {
let me = &mut next_keyed_account(keyed_accounts_iter)?;
me.nonce_advance(
me.advance_nonce_account(
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
&signers,
)
@ -245,7 +245,7 @@ pub fn process_instruction(
SystemInstruction::WithdrawNonceAccount(lamports) => {
let me = &mut next_keyed_account(keyed_accounts_iter)?;
let to = &mut next_keyed_account(keyed_accounts_iter)?;
me.nonce_withdraw(
me.withdraw_nonce_account(
lamports,
to,
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
@ -255,7 +255,7 @@ pub fn process_instruction(
}
SystemInstruction::InitializeNonceAccount(authorized) => {
let me = &mut next_keyed_account(keyed_accounts_iter)?;
me.nonce_initialize(
me.initialize_nonce_account(
&authorized,
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
&Rent::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
@ -263,7 +263,7 @@ pub fn process_instruction(
}
SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {
let me = &mut next_keyed_account(keyed_accounts_iter)?;
me.nonce_authorize(&nonce_authority, &signers)
me.authorize_nonce_account(&nonce_authority, &signers)
}
SystemInstruction::Allocate { space } => {
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
@ -1045,7 +1045,7 @@ mod tests {
#[test]
fn test_process_nonce_ix_no_acc_data_fail() {
assert_eq!(
process_nonce_instruction(&system_instruction::nonce_advance(
process_nonce_instruction(&system_instruction::advance_nonce_account(
&Pubkey::default(),
&Pubkey::default()
)),
@ -1142,7 +1142,7 @@ mod tests {
#[test]
fn test_process_withdraw_ix_no_acc_data_fail() {
assert_eq!(
process_nonce_instruction(&system_instruction::nonce_withdraw(
process_nonce_instruction(&system_instruction::withdraw_nonce_account(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default(),
@ -1379,7 +1379,7 @@ mod tests {
#[test]
fn test_process_authorize_bad_account_data_fail() {
assert_eq!(
process_nonce_instruction(&system_instruction::nonce_authorize(
process_nonce_instruction(&system_instruction::authorize_nonce_account(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default(),