2020-10-28 22:01:07 -07:00
|
|
|
use crate::{
|
|
|
|
account_utils::State as AccountUtilsState, keyed_account::KeyedAccount,
|
|
|
|
nonce_account::create_account,
|
|
|
|
};
|
2020-10-26 09:14:57 -07:00
|
|
|
use solana_program::{
|
2019-12-07 12:54:10 -07:00
|
|
|
instruction::InstructionError,
|
2020-03-03 19:39:09 -07:00
|
|
|
nonce::{self, state::Versions, State},
|
2019-12-07 12:54:10 -07:00
|
|
|
pubkey::Pubkey,
|
2020-01-03 19:34:58 -05:00
|
|
|
system_instruction::NonceError,
|
2020-01-28 16:11:22 -08:00
|
|
|
sysvar::{recent_blockhashes::RecentBlockhashes, rent::Rent},
|
2019-12-07 12:54:10 -07:00
|
|
|
};
|
2020-10-26 09:14:57 -07:00
|
|
|
use std::collections::HashSet;
|
2019-12-07 12:54:10 -07:00
|
|
|
|
2020-10-26 09:14:57 -07:00
|
|
|
pub trait NonceKeyedAccount {
|
2020-01-22 16:31:39 -07:00
|
|
|
fn advance_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-07 12:54:10 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError>;
|
2020-01-22 16:31:39 -07:00
|
|
|
fn withdraw_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-07 12:54:10 -07:00
|
|
|
lamports: u64,
|
2020-01-22 17:54:06 -08:00
|
|
|
to: &KeyedAccount,
|
2019-12-07 12:54:10 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
rent: &Rent,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError>;
|
2020-01-22 16:31:39 -07:00
|
|
|
fn initialize_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_authority: &Pubkey,
|
2019-12-08 10:16:55 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
rent: &Rent,
|
|
|
|
) -> Result<(), InstructionError>;
|
2020-01-22 16:31:39 -07:00
|
|
|
fn authorize_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_authority: &Pubkey,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError>;
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:14:57 -07:00
|
|
|
impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
|
2020-01-22 16:31:39 -07:00
|
|
|
fn advance_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-07 12:54:10 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
if recent_blockhashes.is_empty() {
|
|
|
|
return Err(NonceError::NoRecentBlockhashes.into());
|
|
|
|
}
|
|
|
|
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(self)?.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
match state {
|
|
|
|
State::Initialized(data) => {
|
|
|
|
if !signers.contains(&data.authority) {
|
2019-12-17 09:34:21 -05:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2020-03-04 12:01:32 -07:00
|
|
|
let recent_blockhash = recent_blockhashes[0].blockhash;
|
|
|
|
if data.blockhash == recent_blockhash {
|
2019-12-07 12:54:10 -07:00
|
|
|
return Err(NonceError::NotExpired.into());
|
|
|
|
}
|
|
|
|
|
2020-03-04 09:51:48 -07:00
|
|
|
let new_data = nonce::state::Data {
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
..data
|
|
|
|
};
|
|
|
|
self.set_state(&Versions::new_current(State::Initialized(new_data)))
|
|
|
|
}
|
|
|
|
_ => Err(NonceError::BadAccountState.into()),
|
|
|
|
}
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
2020-01-22 16:31:39 -07:00
|
|
|
fn withdraw_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-07 12:54:10 -07:00
|
|
|
lamports: u64,
|
2020-01-22 17:54:06 -08:00
|
|
|
to: &KeyedAccount,
|
2019-12-07 12:54:10 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
rent: &Rent,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError> {
|
2020-03-03 19:39:09 -07:00
|
|
|
let signer = match AccountUtilsState::<Versions>::state(self)?.convert_to_current() {
|
2020-03-03 18:00:39 -07:00
|
|
|
State::Uninitialized => {
|
2020-01-22 09:11:56 -08:00
|
|
|
if lamports > self.lamports()? {
|
2019-12-07 12:54:10 -07:00
|
|
|
return Err(InstructionError::InsufficientFunds);
|
|
|
|
}
|
2019-12-17 09:34:21 -05:00
|
|
|
*self.unsigned_key()
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
2020-03-04 09:51:48 -07:00
|
|
|
State::Initialized(ref data) => {
|
2020-01-22 09:11:56 -08:00
|
|
|
if lamports == self.lamports()? {
|
2020-03-04 12:01:32 -07:00
|
|
|
if data.blockhash == recent_blockhashes[0].blockhash {
|
2019-12-07 12:54:10 -07:00
|
|
|
return Err(NonceError::NotExpired.into());
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-22 09:11:56 -08:00
|
|
|
let min_balance = rent.minimum_balance(self.data_len()?);
|
|
|
|
if lamports + min_balance > self.lamports()? {
|
2019-12-07 12:54:10 -07:00
|
|
|
return Err(InstructionError::InsufficientFunds);
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 09:51:48 -07:00
|
|
|
data.authority
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
2019-12-17 09:34:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if !signers.contains(&signer) {
|
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
self.try_account_ref_mut()?.lamports -= lamports;
|
|
|
|
to.try_account_ref_mut()?.lamports += lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-08 10:16:55 -07:00
|
|
|
|
2020-01-22 16:31:39 -07:00
|
|
|
fn initialize_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_authority: &Pubkey,
|
2019-12-08 10:16:55 -07:00
|
|
|
recent_blockhashes: &RecentBlockhashes,
|
|
|
|
rent: &Rent,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
if recent_blockhashes.is_empty() {
|
|
|
|
return Err(NonceError::NoRecentBlockhashes.into());
|
|
|
|
}
|
|
|
|
|
2020-03-04 09:51:48 -07:00
|
|
|
match AccountUtilsState::<Versions>::state(self)?.convert_to_current() {
|
2020-03-03 18:00:39 -07:00
|
|
|
State::Uninitialized => {
|
2020-01-22 09:11:56 -08:00
|
|
|
let min_balance = rent.minimum_balance(self.data_len()?);
|
|
|
|
if self.lamports()? < min_balance {
|
2019-12-08 10:16:55 -07:00
|
|
|
return Err(InstructionError::InsufficientFunds);
|
|
|
|
}
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority: *nonce_authority,
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
|
|
|
self.set_state(&Versions::new_current(State::Initialized(data)))
|
2019-12-08 10:16:55 -07:00
|
|
|
}
|
2020-03-04 09:51:48 -07:00
|
|
|
_ => Err(NonceError::BadAccountState.into()),
|
|
|
|
}
|
2019-12-08 10:16:55 -07:00
|
|
|
}
|
2019-12-17 09:34:21 -05:00
|
|
|
|
2020-01-22 16:31:39 -07:00
|
|
|
fn authorize_nonce_account(
|
2020-01-22 17:54:06 -08:00
|
|
|
&self,
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_authority: &Pubkey,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError> {
|
2020-03-03 19:39:09 -07:00
|
|
|
match AccountUtilsState::<Versions>::state(self)?.convert_to_current() {
|
2020-03-04 09:51:48 -07:00
|
|
|
State::Initialized(data) => {
|
|
|
|
if !signers.contains(&data.authority) {
|
2019-12-17 09:34:21 -05:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2020-03-04 09:51:48 -07:00
|
|
|
let new_data = nonce::state::Data {
|
|
|
|
authority: *nonce_authority,
|
|
|
|
..data
|
|
|
|
};
|
|
|
|
self.set_state(&Versions::new_current(State::Initialized(new_data)))
|
2019-12-17 09:34:21 -05:00
|
|
|
}
|
|
|
|
_ => Err(NonceError::BadAccountState.into()),
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for working with keyed accounts in tests
|
2020-01-22 17:54:06 -08:00
|
|
|
pub fn with_test_keyed_account<F>(lamports: u64, signer: bool, f: F)
|
2019-12-07 12:54:10 -07:00
|
|
|
where
|
2020-01-22 17:54:06 -08:00
|
|
|
F: Fn(&KeyedAccount),
|
2019-12-07 12:54:10 -07:00
|
|
|
{
|
2020-10-19 13:19:24 -07:00
|
|
|
let pubkey = Pubkey::new_unique();
|
2020-10-28 22:01:07 -07:00
|
|
|
let account = create_account(lamports);
|
2020-01-22 17:54:06 -08:00
|
|
|
let keyed_account = KeyedAccount::new(&pubkey, signer, &account);
|
|
|
|
f(&keyed_account)
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use crate::{
|
2020-03-03 19:39:09 -07:00
|
|
|
account_utils::State as AccountUtilsState,
|
2020-10-26 09:14:57 -07:00
|
|
|
keyed_account::KeyedAccount,
|
2020-03-03 18:00:39 -07:00
|
|
|
nonce::{self, State},
|
2020-10-28 22:01:07 -07:00
|
|
|
nonce_account::verify_nonce_account,
|
2020-01-03 19:34:58 -05:00
|
|
|
system_instruction::NonceError,
|
2020-12-13 17:26:34 -08:00
|
|
|
sysvar::recent_blockhashes::create_test_recent_blockhashes,
|
2019-12-07 12:54:10 -07:00
|
|
|
};
|
2020-10-28 22:01:07 -07:00
|
|
|
use solana_program::hash::Hash;
|
2019-12-07 12:54:10 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_is_uninitialized() {
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(State::default(), State::Uninitialized)
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keyed_account_expected_behavior() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority: *keyed_account.unsigned_key(),
|
|
|
|
..nonce::state::Data::default()
|
|
|
|
};
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2019-12-07 12:54:10 -07:00
|
|
|
// New is in Uninitialzed state
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(95);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = keyed_account.unsigned_key();
|
2019-12-07 12:54:10 -07:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-07 12:54:10 -07:00
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-05-15 17:35:43 +01:00
|
|
|
..data
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2019-12-07 12:54:10 -07:00
|
|
|
// First nonce instruction drives state from Uninitialized to Initialized
|
2020-03-05 07:40:26 -07:00
|
|
|
assert_eq!(state, State::Initialized(data.clone()));
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-01-03 19:34:58 -05:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.advance_nonce_account(&recent_blockhashes, &signers)
|
2020-01-03 19:34:58 -05:00
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-05-15 17:35:43 +01:00
|
|
|
..data
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2019-12-07 12:54:10 -07:00
|
|
|
// Second nonce instruction consumes and replaces stored nonce
|
2020-03-05 07:40:26 -07:00
|
|
|
assert_eq!(state, State::Initialized(data.clone()));
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-01-03 19:34:58 -05:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.advance_nonce_account(&recent_blockhashes, &signers)
|
2020-01-03 19:34:58 -05:00
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-05-15 17:35:43 +01:00
|
|
|
..data
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2019-12-07 12:54:10 -07:00
|
|
|
// Third nonce instruction for fun and profit
|
2020-05-15 17:35:43 +01:00
|
|
|
assert_eq!(state, State::Initialized(data));
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = keyed_account.account.borrow().lamports;
|
|
|
|
let expect_nonce_lamports =
|
|
|
|
keyed_account.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let expect_to_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-03 18:00:39 -07:00
|
|
|
// Empties Account balance
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(
|
|
|
|
keyed_account.account.borrow().lamports,
|
|
|
|
expect_nonce_lamports
|
|
|
|
);
|
2020-03-03 18:00:39 -07:00
|
|
|
// Account balance goes to `to`
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, expect_to_lamports);
|
2019-12-07 12:54:10 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonce_inx_initialized_account_not_signer_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authority = *nonce_account.unsigned_key();
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_account
|
2020-03-04 09:51:48 -07:00
|
|
|
.initialize_nonce_account(&authority, &recent_blockhashes, &rent)
|
2019-12-07 12:54:10 -07:00
|
|
|
.unwrap();
|
2020-05-15 17:35:43 +01:00
|
|
|
let pubkey = nonce_account.account.borrow().owner;
|
2020-01-22 17:54:06 -08:00
|
|
|
let nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(&nonce_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority,
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
|
|
|
assert_eq!(state, State::Initialized(data));
|
2019-12-07 12:54:10 -07:00
|
|
|
let signers = HashSet::new();
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-07 12:54:10 -07:00
|
|
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonce_inx_with_empty_recent_blockhashes_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2019-12-08 10:16:55 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *keyed_account.unsigned_key();
|
2019-12-08 10:16:55 -07:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-08 10:16:55 -07:00
|
|
|
.unwrap();
|
2020-12-13 17:26:34 -08:00
|
|
|
let recent_blockhashes = vec![].into_iter().collect();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-07 12:54:10 -07:00
|
|
|
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonce_inx_too_early_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *keyed_account.unsigned_key();
|
2019-12-07 12:54:10 -07:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-07 12:54:10 -07:00
|
|
|
.unwrap();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-07 12:54:10 -07:00
|
|
|
assert_eq!(result, Err(NonceError::NotExpired.into()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-08 10:16:55 -07:00
|
|
|
fn nonce_inx_uninitialized_account_fail() {
|
2019-12-07 12:54:10 -07:00
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-08 10:16:55 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = keyed_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-08 10:16:55 -07:00
|
|
|
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
2019-12-07 12:54:10 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:34:21 -05:00
|
|
|
#[test]
|
|
|
|
fn nonce_inx_independent_nonce_authority_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-17 09:34:21 -05:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
with_test_keyed_account(42, true, |nonce_authority| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_account.signer_key().unwrap());
|
2019-12-17 09:34:21 -05:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_authority.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_authority.signer_key().unwrap());
|
2019-12-17 09:34:21 -05:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-17 09:34:21 -05:00
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonce_inx_no_nonce_authority_sig_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-17 09:34:21 -05:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
with_test_keyed_account(42, false, |nonce_authority| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_account.signer_key().unwrap());
|
2019-12-17 09:34:21 -05:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_authority.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.advance_nonce_account(&recent_blockhashes, &signers);
|
2019-12-17 09:34:21 -05:00
|
|
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:10 -07:00
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_unintialized_acc_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
|
|
|
|
let expect_nonce_lamports =
|
|
|
|
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let expect_to_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2019-12-07 12:54:10 -07:00
|
|
|
// Withdraw instruction...
|
2020-03-03 18:00:39 -07:00
|
|
|
// Deinitializes Account state
|
|
|
|
assert_eq!(state, State::Uninitialized);
|
|
|
|
// Empties Account balance
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(nonce_keyed.account.borrow().lamports, expect_nonce_lamports);
|
2020-03-03 18:00:39 -07:00
|
|
|
// Account balance goes to `to`
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, expect_to_lamports);
|
2019-12-07 12:54:10 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_unintialized_acc_unsigned_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, false, |nonce_keyed| {
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let signers = HashSet::new();
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let lamports = nonce_keyed.account.borrow().lamports;
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_keyed.withdraw_nonce_account(
|
2020-01-22 09:11:56 -08:00
|
|
|
lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
);
|
|
|
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_unintialized_acc_insuff_funds_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let lamports = nonce_keyed.account.borrow().lamports + 1;
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_keyed.withdraw_nonce_account(
|
2020-01-22 09:11:56 -08:00
|
|
|
lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
);
|
|
|
|
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_uninitialized_acc_two_withdraws_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports / 2;
|
|
|
|
let nonce_expect_lamports =
|
|
|
|
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(nonce_keyed.account.borrow().lamports, nonce_expect_lamports);
|
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, to_expect_lamports);
|
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
|
|
|
|
let nonce_expect_lamports =
|
|
|
|
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(nonce_keyed.account.borrow().lamports, nonce_expect_lamports);
|
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, to_expect_lamports);
|
2019-12-07 12:54:10 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_initialized_acc_two_withdraws_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authority = *nonce_keyed.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_keyed
|
2020-03-04 09:51:48 -07:00
|
|
|
.initialize_nonce_account(&authority, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority,
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2020-03-05 07:40:26 -07:00
|
|
|
assert_eq!(state, State::Initialized(data.clone()));
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports - min_lamports;
|
|
|
|
let nonce_expect_lamports =
|
|
|
|
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_keyed)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
let data = nonce::state::Data {
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
|
|
|
..data.clone()
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
|
|
|
assert_eq!(state, State::Initialized(data));
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(nonce_keyed.account.borrow().lamports, nonce_expect_lamports);
|
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, to_expect_lamports);
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
|
|
|
|
let nonce_expect_lamports =
|
|
|
|
nonce_keyed.account.borrow().lamports - withdraw_lamports;
|
|
|
|
let to_expect_lamports = to_keyed.account.borrow().lamports + withdraw_lamports;
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(nonce_keyed.account.borrow().lamports, nonce_expect_lamports);
|
|
|
|
assert_eq!(to_keyed.account.borrow().lamports, to_expect_lamports);
|
2019-12-07 12:54:10 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_initialized_acc_nonce_too_early_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_keyed.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports;
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_keyed.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
);
|
|
|
|
assert_eq!(result, Err(NonceError::NotExpired.into()));
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_initialized_acc_insuff_funds_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(95);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_keyed.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports + 1;
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_keyed.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
);
|
|
|
|
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn withdraw_inx_initialized_acc_insuff_rent_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-07 12:54:10 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_keyed| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(95);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_keyed.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_keyed
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-01-22 17:54:06 -08:00
|
|
|
with_test_keyed_account(42, false, |to_keyed| {
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_keyed.signer_key().unwrap());
|
2020-01-22 09:11:56 -08:00
|
|
|
let withdraw_lamports = nonce_keyed.account.borrow().lamports - min_lamports + 1;
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_keyed.withdraw_nonce_account(
|
2019-12-07 12:54:10 -07:00
|
|
|
withdraw_lamports,
|
2020-01-22 17:54:06 -08:00
|
|
|
&to_keyed,
|
2019-12-07 12:54:10 -07:00
|
|
|
&recent_blockhashes,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
);
|
|
|
|
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-08 10:16:55 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initialize_inx_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-08 10:16:55 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2019-12-08 10:16:55 -07:00
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2019-12-08 10:16:55 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authority = *keyed_account.unsigned_key();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result =
|
2020-03-04 09:51:48 -07:00
|
|
|
keyed_account.initialize_nonce_account(&authority, &recent_blockhashes, &rent);
|
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority,
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2019-12-08 10:16:55 -07:00
|
|
|
assert_eq!(result, Ok(()));
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(keyed_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
assert_eq!(state, State::Initialized(data));
|
2019-12-08 10:16:55 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initialize_inx_empty_recent_blockhashes_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-08 10:16:55 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*keyed_account.signer_key().unwrap());
|
2020-12-13 17:26:34 -08:00
|
|
|
let recent_blockhashes = vec![].into_iter().collect();
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *keyed_account.unsigned_key();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result =
|
|
|
|
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
2019-12-08 10:16:55 -07:00
|
|
|
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initialize_inx_initialized_account_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-08 10:16:55 -07:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |keyed_account| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *keyed_account.unsigned_key();
|
2019-12-08 10:16:55 -07:00
|
|
|
keyed_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-08 10:16:55 -07:00
|
|
|
.unwrap();
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-01-22 16:31:39 -07:00
|
|
|
let result =
|
|
|
|
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
2019-12-08 10:16:55 -07:00
|
|
|
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initialize_inx_uninitialized_acc_insuff_funds_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-08 10:16:55 -07:00
|
|
|
with_test_keyed_account(min_lamports - 42, true, |keyed_account| {
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(63);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *keyed_account.unsigned_key();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result =
|
|
|
|
keyed_account.initialize_nonce_account(&authorized, &recent_blockhashes, &rent);
|
2019-12-08 10:16:55 -07:00
|
|
|
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
|
|
|
})
|
|
|
|
}
|
2019-12-17 09:34:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn authorize_inx_ok() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-17 09:34:21 -05:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_account.signer_key().unwrap());
|
2019-12-17 09:34:21 -05:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = *nonce_account.unsigned_key();
|
2019-12-17 09:34:21 -05:00
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-03-04 09:51:48 -07:00
|
|
|
let authority = Pubkey::default();
|
|
|
|
let data = nonce::state::Data {
|
|
|
|
authority,
|
2020-03-04 12:01:32 -07:00
|
|
|
blockhash: recent_blockhashes[0].blockhash,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator: recent_blockhashes[0].fee_calculator.clone(),
|
2020-03-04 09:51:48 -07:00
|
|
|
};
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
2019-12-17 09:34:21 -05:00
|
|
|
assert_eq!(result, Ok(()));
|
2020-03-03 19:39:09 -07:00
|
|
|
let state = AccountUtilsState::<Versions>::state(nonce_account)
|
|
|
|
.unwrap()
|
|
|
|
.convert_to_current();
|
2020-03-04 09:51:48 -07:00
|
|
|
assert_eq!(state, State::Initialized(data));
|
2019-12-17 09:34:21 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn authorize_inx_uninitialized_state_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-17 09:34:21 -05:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_account.signer_key().unwrap());
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
2019-12-17 09:34:21 -05:00
|
|
|
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn authorize_inx_bad_authority_fail() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 42,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2020-03-03 18:00:39 -07:00
|
|
|
let min_lamports = rent.minimum_balance(State::size());
|
2019-12-17 09:34:21 -05:00
|
|
|
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(*nonce_account.signer_key().unwrap());
|
2019-12-17 09:34:21 -05:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(31);
|
|
|
|
let authorized = &Pubkey::default().clone();
|
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &rent)
|
2019-12-17 09:34:21 -05:00
|
|
|
.unwrap();
|
2020-01-22 16:31:39 -07:00
|
|
|
let result = nonce_account.authorize_nonce_account(&Pubkey::default(), &signers);
|
2019-12-17 09:34:21 -05:00
|
|
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
})
|
|
|
|
}
|
2020-10-26 09:14:57 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_ok() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
|
|
|
signers.insert(nonce_account.signer_key().unwrap());
|
|
|
|
let state: State = nonce_account.state().unwrap();
|
|
|
|
// New is in Uninitialzed state
|
|
|
|
assert_eq!(state, State::Uninitialized);
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
|
|
|
let authorized = nonce_account.unsigned_key();
|
|
|
|
nonce_account
|
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
|
|
|
|
.unwrap();
|
|
|
|
assert!(verify_nonce_account(
|
|
|
|
&nonce_account.account.borrow(),
|
|
|
|
&recent_blockhashes[0].blockhash,
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_bad_acc_state_fail() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
|
|
|
assert!(!verify_nonce_account(
|
|
|
|
&nonce_account.account.borrow(),
|
|
|
|
&Hash::default()
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_bad_query_hash_fail() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
|
|
|
signers.insert(nonce_account.signer_key().unwrap());
|
|
|
|
let state: State = nonce_account.state().unwrap();
|
|
|
|
// New is in Uninitialzed state
|
|
|
|
assert_eq!(state, State::Uninitialized);
|
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
|
|
|
let authorized = nonce_account.unsigned_key();
|
|
|
|
nonce_account
|
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
|
|
|
|
.unwrap();
|
|
|
|
assert!(!verify_nonce_account(
|
|
|
|
&nonce_account.account.borrow(),
|
|
|
|
&recent_blockhashes[1].blockhash,
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
2019-12-07 12:54:10 -07:00
|
|
|
}
|