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:
@ -45,12 +45,12 @@ impl NonceState {
|
||||
}
|
||||
|
||||
pub trait NonceAccount {
|
||||
fn nonce_advance(
|
||||
fn advance_nonce_account(
|
||||
&mut self,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn nonce_withdraw(
|
||||
fn withdraw_nonce_account(
|
||||
&mut self,
|
||||
lamports: u64,
|
||||
to: &mut KeyedAccount,
|
||||
@ -58,13 +58,13 @@ pub trait NonceAccount {
|
||||
rent: &Rent,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn nonce_initialize(
|
||||
fn initialize_nonce_account(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
rent: &Rent,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn nonce_authorize(
|
||||
fn authorize_nonce_account(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
@ -72,7 +72,7 @@ pub trait NonceAccount {
|
||||
}
|
||||
|
||||
impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
fn nonce_advance(
|
||||
fn advance_nonce_account(
|
||||
&mut self,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
signers: &HashSet<Pubkey>,
|
||||
@ -97,7 +97,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
self.set_state(&NonceState::Initialized(meta, recent_blockhashes[0]))
|
||||
}
|
||||
|
||||
fn nonce_withdraw(
|
||||
fn withdraw_nonce_account(
|
||||
&mut self,
|
||||
lamports: u64,
|
||||
to: &mut KeyedAccount,
|
||||
@ -137,7 +137,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn nonce_initialize(
|
||||
fn initialize_nonce_account(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
@ -161,7 +161,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
self.set_state(&NonceState::Initialized(meta, recent_blockhashes[0]))
|
||||
}
|
||||
|
||||
fn nonce_authorize(
|
||||
fn authorize_nonce_account(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
@ -240,7 +240,7 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -248,7 +248,7 @@ mod test {
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
keyed_account
|
||||
.nonce_advance(&recent_blockhashes, &signers)
|
||||
.advance_nonce_account(&recent_blockhashes, &signers)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -256,7 +256,7 @@ mod test {
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
keyed_account
|
||||
.nonce_advance(&recent_blockhashes, &signers)
|
||||
.advance_nonce_account(&recent_blockhashes, &signers)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -269,7 +269,7 @@ mod test {
|
||||
keyed_account.account.borrow().lamports - withdraw_lamports;
|
||||
let expect_to_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
keyed_account
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -301,7 +301,7 @@ mod test {
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let pubkey = nonce_account.account.borrow().owner.clone();
|
||||
let mut nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
|
||||
@ -309,7 +309,7 @@ mod test {
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
let signers = HashSet::new();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
||||
})
|
||||
}
|
||||
@ -327,10 +327,10 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(vec![].into_iter());
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
||||
})
|
||||
}
|
||||
@ -348,9 +348,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::NotExpired.into()));
|
||||
})
|
||||
}
|
||||
@ -366,7 +366,7 @@ mod test {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -385,12 +385,12 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_authority.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
});
|
||||
});
|
||||
@ -410,9 +410,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
||||
});
|
||||
});
|
||||
@ -437,7 +437,7 @@ mod test {
|
||||
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
||||
let expect_to_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -471,7 +471,7 @@ mod test {
|
||||
let signers = HashSet::new();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let lamports = nonce_keyed.account.borrow().lamports;
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
let result = nonce_keyed.withdraw_nonce_account(
|
||||
lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -498,7 +498,7 @@ mod test {
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let lamports = nonce_keyed.account.borrow().lamports + 1;
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
let result = nonce_keyed.withdraw_nonce_account(
|
||||
lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -527,7 +527,7 @@ mod test {
|
||||
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -544,7 +544,7 @@ mod test {
|
||||
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -574,7 +574,7 @@ mod test {
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_keyed
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = nonce_keyed.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -585,7 +585,7 @@ mod test {
|
||||
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -604,7 +604,7 @@ mod test {
|
||||
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.nonce_withdraw(
|
||||
.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -629,13 +629,13 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
with_test_keyed_account(42, false, |mut to_keyed| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
let result = nonce_keyed.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -658,14 +658,14 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
with_test_keyed_account(42, false, |mut to_keyed| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let withdraw_lamports = nonce_keyed.account.borrow().lamports + 1;
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
let result = nonce_keyed.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -688,14 +688,14 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
with_test_keyed_account(42, false, |mut to_keyed| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let withdraw_lamports = nonce_keyed.account.borrow().lamports - min_lamports + 1;
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
let result = nonce_keyed.withdraw_nonce_account(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -723,7 +723,8 @@ mod test {
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result =
|
||||
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
@ -742,7 +743,8 @@ mod test {
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(vec![].into_iter());
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result =
|
||||
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
||||
})
|
||||
}
|
||||
@ -758,10 +760,11 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result =
|
||||
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -776,7 +779,8 @@ mod test {
|
||||
with_test_keyed_account(min_lamports - 42, true, |keyed_account| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result =
|
||||
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
||||
})
|
||||
}
|
||||
@ -795,11 +799,11 @@ mod test {
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
nonce_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let authorized = &Pubkey::default().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = nonce_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
@ -816,7 +820,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().clone());
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -834,9 +838,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = &Pubkey::default().clone();
|
||||
nonce_account
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
||||
})
|
||||
}
|
||||
|
@ -363,7 +363,7 @@ pub fn create_nonce_account(
|
||||
]
|
||||
}
|
||||
|
||||
pub fn nonce_advance(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
|
||||
pub fn advance_nonce_account(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
@ -376,7 +376,7 @@ pub fn nonce_advance(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instr
|
||||
)
|
||||
}
|
||||
|
||||
pub fn nonce_withdraw(
|
||||
pub fn withdraw_nonce_account(
|
||||
nonce_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
@ -396,7 +396,7 @@ pub fn nonce_withdraw(
|
||||
)
|
||||
}
|
||||
|
||||
pub fn nonce_authorize(
|
||||
pub fn authorize_nonce_account(
|
||||
nonce_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
new_authority: &Pubkey,
|
||||
|
@ -110,8 +110,10 @@ impl Transaction {
|
||||
nonce_authority_pubkey: &Pubkey,
|
||||
nonce_hash: Hash,
|
||||
) -> Self {
|
||||
let nonce_ix =
|
||||
system_instruction::nonce_advance(&nonce_account_pubkey, &nonce_authority_pubkey);
|
||||
let nonce_ix = system_instruction::advance_nonce_account(
|
||||
&nonce_account_pubkey,
|
||||
&nonce_authority_pubkey,
|
||||
);
|
||||
instructions.insert(0, nonce_ix);
|
||||
Self::new_signed_with_payer(instructions, payer, signing_keypairs, nonce_hash)
|
||||
}
|
||||
|
Reference in New Issue
Block a user