Durable Nonce - Authorized Noncer (#7417)
* Durable Nonce: Add authorized noncer to initialize instruction * CLI: Adapt to nonce authority * Durable Nonce: Introduce Authorize instruction * Specify who needs to sign ix * 'authorized-noncer' -> 'nonce-authority' * Document signing authority for all instructions
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
account::{get_signers, KeyedAccount},
|
||||
instruction::{AccountMeta, Instruction, InstructionError},
|
||||
instruction::{AccountMeta, Instruction, InstructionError, WithSigner},
|
||||
instruction_processor_utils::{limited_deserialize, next_keyed_account, DecodeError},
|
||||
nonce_program::id,
|
||||
nonce_state::{NonceAccount, NonceState},
|
||||
@ -38,10 +38,11 @@ impl<E> DecodeError<E> for NonceError {
|
||||
pub enum NonceInstruction {
|
||||
/// `Nonce` consumes a stored nonce, replacing it with a successor
|
||||
///
|
||||
/// Expects 3 Accounts:
|
||||
/// Expects 2 Accounts:
|
||||
/// 0 - A NonceAccount
|
||||
/// 1 - RecentBlockhashes sysvar
|
||||
///
|
||||
/// The current authority must sign a transaction executing this instrucion
|
||||
Nonce,
|
||||
|
||||
/// `Withdraw` transfers funds out of the nonce account
|
||||
@ -54,6 +55,8 @@ pub enum NonceInstruction {
|
||||
///
|
||||
/// The `u64` parameter is the lamports to withdraw, which must leave the
|
||||
/// account balance above the rent exempt reserve or at zero.
|
||||
///
|
||||
/// The current authority must sign a transaction executing this instruction
|
||||
Withdraw(u64),
|
||||
|
||||
/// `Initialize` drives state of Uninitalized NonceAccount to Initialized,
|
||||
@ -63,12 +66,30 @@ pub enum NonceInstruction {
|
||||
/// 0 - A NonceAccount in the Uninitialized state
|
||||
/// 1 - RecentBlockHashes sysvar
|
||||
/// 2 - Rent sysvar
|
||||
Initialize,
|
||||
///
|
||||
/// The `Pubkey` parameter specifies the entity authorized to execute nonce
|
||||
/// instruction on the account
|
||||
///
|
||||
/// No signatures are required to execute this instruction, enabling derived
|
||||
/// nonce account addresses
|
||||
Initialize(Pubkey),
|
||||
|
||||
/// `Authorize` changes the entity authorized to execute nonce instructions
|
||||
/// on the account
|
||||
///
|
||||
/// Expects 1 Account:
|
||||
/// 0 - A NonceAccount
|
||||
///
|
||||
/// The `Pubkey` parameter identifies the entity to authorize
|
||||
///
|
||||
/// The current authority must sign a transaction executing this instruction
|
||||
Authorize(Pubkey),
|
||||
}
|
||||
|
||||
pub fn create_nonce_account(
|
||||
from_pubkey: &Pubkey,
|
||||
nonce_pubkey: &Pubkey,
|
||||
authority: &Pubkey,
|
||||
lamports: u64,
|
||||
) -> Vec<Instruction> {
|
||||
vec![
|
||||
@ -79,43 +100,57 @@ pub fn create_nonce_account(
|
||||
NonceState::size() as u64,
|
||||
&id(),
|
||||
),
|
||||
initialize(nonce_pubkey),
|
||||
initialize(nonce_pubkey, authority),
|
||||
]
|
||||
}
|
||||
|
||||
fn initialize(nonce_pubkey: &Pubkey) -> Instruction {
|
||||
fn initialize(nonce_pubkey: &Pubkey, authority: &Pubkey) -> Instruction {
|
||||
Instruction::new(
|
||||
id(),
|
||||
&NonceInstruction::Initialize,
|
||||
&NonceInstruction::Initialize(*authority),
|
||||
vec![
|
||||
AccountMeta::new(*nonce_pubkey, true),
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::id(), false),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn nonce(nonce_pubkey: &Pubkey) -> Instruction {
|
||||
Instruction::new(
|
||||
id(),
|
||||
&NonceInstruction::Nonce,
|
||||
vec![
|
||||
AccountMeta::new(*nonce_pubkey, true),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
],
|
||||
)
|
||||
pub fn nonce(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
Instruction::new(id(), &NonceInstruction::Nonce, account_metas)
|
||||
}
|
||||
|
||||
pub fn withdraw(nonce_pubkey: &Pubkey, to_pubkey: &Pubkey, lamports: u64) -> Instruction {
|
||||
pub fn withdraw(
|
||||
nonce_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::id(), false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
Instruction::new(id(), &NonceInstruction::Withdraw(lamports), account_metas)
|
||||
}
|
||||
|
||||
pub fn authorize(
|
||||
nonce_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
new_authority: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*nonce_pubkey, false)].with_signer(authorized_pubkey);
|
||||
Instruction::new(
|
||||
id(),
|
||||
&NonceInstruction::Withdraw(lamports),
|
||||
vec![
|
||||
AccountMeta::new(*nonce_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::id(), false),
|
||||
],
|
||||
&NonceInstruction::Authorize(*new_authority),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
@ -144,10 +179,12 @@ pub fn process_instruction(
|
||||
&signers,
|
||||
)
|
||||
}
|
||||
NonceInstruction::Initialize => me.initialize(
|
||||
NonceInstruction::Initialize(authorized) => me.initialize(
|
||||
&authorized,
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
||||
&Rent::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
||||
),
|
||||
NonceInstruction::Authorize(nonce_authority) => me.authorize(&nonce_authority, &signers),
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,7 +231,8 @@ mod tests {
|
||||
fn test_create_account() {
|
||||
let from_pubkey = Pubkey::new_rand();
|
||||
let nonce_pubkey = Pubkey::new_rand();
|
||||
let ixs = create_nonce_account(&from_pubkey, &nonce_pubkey, 42);
|
||||
let authorized = nonce_pubkey;
|
||||
let ixs = create_nonce_account(&from_pubkey, &nonce_pubkey, &authorized, 42);
|
||||
assert_eq!(ixs.len(), 2);
|
||||
let ix = &ixs[0];
|
||||
assert_eq!(ix.program_id, system_program::id());
|
||||
@ -206,7 +244,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_process_nonce_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_instruction(&nonce(&Pubkey::default(),)),
|
||||
process_instruction(&nonce(&Pubkey::default(), &Pubkey::default())),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
@ -279,7 +317,7 @@ mod tests {
|
||||
&mut sysvar::rent::create_account(1, &Rent::free()),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
@ -305,7 +343,12 @@ mod tests {
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_instruction(&withdraw(&Pubkey::default(), &Pubkey::default(), 1,)),
|
||||
process_instruction(&withdraw(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
1,
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
@ -420,8 +463,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_invalid_acc_data_fail() {
|
||||
let authorized = Pubkey::default();
|
||||
assert_eq!(
|
||||
process_instruction(&initialize(&Pubkey::default())),
|
||||
process_instruction(&initialize(&Pubkey::default(), &authorized)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
@ -432,7 +476,7 @@ mod tests {
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -448,7 +492,7 @@ mod tests {
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -471,7 +515,7 @@ mod tests {
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
@ -498,7 +542,7 @@ mod tests {
|
||||
),
|
||||
KeyedAccount::new(&sysvar::rent::id(), false, &mut Account::default(),),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
@ -529,12 +573,58 @@ mod tests {
|
||||
&mut sysvar::rent::create_account(1, &Rent::free())
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize).unwrap(),
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_authorize_ix_ok() {
|
||||
let mut nonce_acc = nonce_state::create_account(1_000_000);
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::rent::id(),
|
||||
false,
|
||||
&mut sysvar::rent::create_account(1, &Rent::free()),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc,),],
|
||||
&serialize(&NonceInstruction::Authorize(Pubkey::default(),)).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_authorize_bad_account_data_fail() {
|
||||
assert_eq!(
|
||||
process_instruction(&authorize(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_custom_error_decode() {
|
||||
use num_traits::FromPrimitive;
|
||||
|
@ -13,11 +13,15 @@ use serde_derive::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Clone, Copy)]
|
||||
pub struct Meta {}
|
||||
pub struct Meta {
|
||||
pub nonce_authority: Pubkey,
|
||||
}
|
||||
|
||||
impl Meta {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
pub fn new(nonce_authority: &Pubkey) -> Self {
|
||||
Self {
|
||||
nonce_authority: *nonce_authority,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,9 +60,15 @@ pub trait NonceAccount {
|
||||
) -> Result<(), InstructionError>;
|
||||
fn initialize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
rent: &Rent,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn authorize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError>;
|
||||
}
|
||||
|
||||
impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
@ -71,12 +81,11 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
return Err(NonceError::NoRecentBlockhashes.into());
|
||||
}
|
||||
|
||||
if !signers.contains(self.unsigned_key()) {
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
let meta = match self.state()? {
|
||||
NonceState::Initialized(meta, ref hash) => {
|
||||
if !signers.contains(&meta.nonce_authority) {
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
if *hash == recent_blockhashes[0] {
|
||||
return Err(NonceError::NotExpired.into());
|
||||
}
|
||||
@ -96,17 +105,14 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
rent: &Rent,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError> {
|
||||
if !signers.contains(self.unsigned_key()) {
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
match self.state()? {
|
||||
let signer = match self.state()? {
|
||||
NonceState::Uninitialized => {
|
||||
if lamports > self.account.lamports {
|
||||
return Err(InstructionError::InsufficientFunds);
|
||||
}
|
||||
*self.unsigned_key()
|
||||
}
|
||||
NonceState::Initialized(_meta, ref hash) => {
|
||||
NonceState::Initialized(meta, ref hash) => {
|
||||
if lamports == self.account.lamports {
|
||||
if *hash == recent_blockhashes[0] {
|
||||
return Err(NonceError::NotExpired.into());
|
||||
@ -117,7 +123,12 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
return Err(InstructionError::InsufficientFunds);
|
||||
}
|
||||
}
|
||||
meta.nonce_authority
|
||||
}
|
||||
};
|
||||
|
||||
if !signers.contains(&signer) {
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
self.account.lamports -= lamports;
|
||||
@ -128,6 +139,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
|
||||
fn initialize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
rent: &Rent,
|
||||
) -> Result<(), InstructionError> {
|
||||
@ -141,13 +153,29 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
if self.account.lamports < min_balance {
|
||||
return Err(InstructionError::InsufficientFunds);
|
||||
}
|
||||
Meta::new()
|
||||
Meta::new(nonce_authority)
|
||||
}
|
||||
_ => return Err(NonceError::BadAccountState.into()),
|
||||
};
|
||||
|
||||
self.set_state(&NonceState::Initialized(meta, recent_blockhashes[0]))
|
||||
}
|
||||
|
||||
fn authorize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError> {
|
||||
match self.state()? {
|
||||
NonceState::Initialized(meta, nonce) => {
|
||||
if !signers.contains(&meta.nonce_authority) {
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
self.set_state(&NonceState::Initialized(Meta::new(nonce_authority), nonce))
|
||||
}
|
||||
_ => Err(NonceError::BadAccountState.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64) -> Account {
|
||||
@ -189,7 +217,8 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn new_meta() {
|
||||
assert_eq!(Meta::new(), Meta {});
|
||||
let nonce_authority = Pubkey::default();
|
||||
assert_eq!(Meta::new(&nonce_authority), Meta { nonce_authority });
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -199,16 +228,17 @@ mod test {
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
let meta = Meta::new();
|
||||
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
||||
let meta = Meta::new(&keyed_account.unsigned_key());
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
// New is in Uninitialzed state
|
||||
assert_eq!(state, NonceState::Uninitialized);
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&recent_blockhashes, &rent)
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -255,12 +285,13 @@ mod test {
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
let meta = Meta::new();
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_account
|
||||
.initialize(&recent_blockhashes, &rent)
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let pubkey = nonce_account.account.owner.clone();
|
||||
let mut nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
|
||||
@ -284,8 +315,9 @@ mod test {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&recent_blockhashes, &rent)
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(vec![].into_iter());
|
||||
let result = keyed_account.nonce(&recent_blockhashes, &signers);
|
||||
@ -304,8 +336,9 @@ mod test {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&recent_blockhashes, &rent)
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = keyed_account.nonce(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::NotExpired.into()));
|
||||
@ -328,6 +361,53 @@ mod test {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonce_inx_independent_nonce_authority_ok() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
||||
with_test_keyed_account(42, true, |nonce_authority| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&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(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonce_inx_no_nonce_authority_sig_fail() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
||||
with_test_keyed_account(42, false, |nonce_authority| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.nonce(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn withdraw_inx_unintialized_acc_ok() {
|
||||
let rent = Rent {
|
||||
@ -472,12 +552,15 @@ mod test {
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
let meta = Meta::new();
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
nonce_keyed.initialize(&recent_blockhashes, &rent).unwrap();
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_keyed
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = nonce_keyed.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
@ -527,7 +610,10 @@ mod test {
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
nonce_keyed.initialize(&recent_blockhashes, &rent).unwrap();
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&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());
|
||||
@ -553,7 +639,10 @@ mod test {
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
nonce_keyed.initialize(&recent_blockhashes, &rent).unwrap();
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&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();
|
||||
@ -580,7 +669,10 @@ mod test {
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
nonce_keyed.initialize(&recent_blockhashes, &rent).unwrap();
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&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();
|
||||
@ -612,10 +704,12 @@ mod test {
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let stored = recent_blockhashes[0];
|
||||
let result = keyed_account.initialize(&recent_blockhashes, &rent);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(Meta::new(), stored));
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
})
|
||||
}
|
||||
|
||||
@ -630,7 +724,8 @@ mod test {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(keyed_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(vec![].into_iter());
|
||||
let result = keyed_account.initialize(&recent_blockhashes, &rent);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
||||
})
|
||||
}
|
||||
@ -644,11 +739,12 @@ mod test {
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&recent_blockhashes, &rent)
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = keyed_account.initialize(&recent_blockhashes, &rent);
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -662,8 +758,69 @@ mod test {
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports - 42, true, |keyed_account| {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let result = keyed_account.initialize(&recent_blockhashes, &rent);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorize_inx_ok() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let authorized = &Pubkey::default().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = nonce_account.authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = nonce_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorize_inx_uninitialized_state_fail() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
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.authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorize_inx_bad_authority_fail() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
};
|
||||
let min_lamports = rent.minimum_balance(NonceState::size());
|
||||
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_account.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = &Pubkey::default().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user