@ -7,7 +7,6 @@ use crate::{
|
||||
fee_calculator::FeeCalculator,
|
||||
hash::{hash, Hash},
|
||||
inflation::Inflation,
|
||||
nonce_program::solana_nonce_program,
|
||||
poh_config::PohConfig,
|
||||
pubkey::Pubkey,
|
||||
rent::Rent,
|
||||
@ -65,7 +64,7 @@ pub fn create_genesis_config(lamports: u64) -> (GenesisConfig, Keypair) {
|
||||
faucet_keypair.pubkey(),
|
||||
Account::new(lamports, 0, &system_program::id()),
|
||||
)],
|
||||
&[solana_nonce_program(), solana_system_program()],
|
||||
&[solana_system_program()],
|
||||
),
|
||||
faucet_keypair,
|
||||
)
|
||||
|
@ -16,8 +16,6 @@ pub mod message;
|
||||
pub mod move_loader;
|
||||
pub mod native_loader;
|
||||
pub mod native_token;
|
||||
pub mod nonce_instruction;
|
||||
pub mod nonce_program;
|
||||
pub mod nonce_state;
|
||||
pub mod packet;
|
||||
pub mod poh_config;
|
||||
|
@ -1,665 +0,0 @@
|
||||
use crate::{
|
||||
account::{get_signers, KeyedAccount},
|
||||
instruction::{AccountMeta, Instruction, InstructionError, WithSigner},
|
||||
instruction_processor_utils::{limited_deserialize, next_keyed_account, DecodeError},
|
||||
nonce_program::id,
|
||||
nonce_state::{NonceAccount, NonceState},
|
||||
pubkey::Pubkey,
|
||||
system_instruction,
|
||||
sysvar::{
|
||||
recent_blockhashes::{self, RecentBlockhashes},
|
||||
rent::{self, Rent},
|
||||
Sysvar,
|
||||
},
|
||||
};
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum NonceError {
|
||||
#[error("recent blockhash list is empty")]
|
||||
NoRecentBlockhashes,
|
||||
#[error("stored nonce is still in recent_blockhashes")]
|
||||
NotExpired,
|
||||
#[error("specified nonce does not match stored nonce")]
|
||||
UnexpectedValue,
|
||||
#[error("cannot handle request in current account state")]
|
||||
BadAccountState,
|
||||
}
|
||||
|
||||
impl<E> DecodeError<E> for NonceError {
|
||||
fn type_of() -> &'static str {
|
||||
"NonceError"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
||||
pub enum NonceInstruction {
|
||||
/// `Nonce` consumes a stored nonce, replacing it with a successor
|
||||
///
|
||||
/// 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
|
||||
///
|
||||
/// Expects 4 Accounts:
|
||||
/// 0 - A NonceAccount
|
||||
/// 1 - A system account to which the lamports will be transferred
|
||||
/// 2 - RecentBlockhashes sysvar
|
||||
/// 3 - Rent sysvar
|
||||
///
|
||||
/// 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,
|
||||
/// setting the nonce value.
|
||||
///
|
||||
/// Expects 3 Accounts:
|
||||
/// 0 - A NonceAccount in the Uninitialized state
|
||||
/// 1 - RecentBlockHashes sysvar
|
||||
/// 2 - Rent sysvar
|
||||
///
|
||||
/// 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![
|
||||
system_instruction::create_account(
|
||||
from_pubkey,
|
||||
nonce_pubkey,
|
||||
lamports,
|
||||
NonceState::size() as u64,
|
||||
&id(),
|
||||
),
|
||||
initialize(nonce_pubkey, authority),
|
||||
]
|
||||
}
|
||||
|
||||
fn initialize(nonce_pubkey: &Pubkey, authority: &Pubkey) -> Instruction {
|
||||
Instruction::new(
|
||||
id(),
|
||||
&NonceInstruction::Initialize(*authority),
|
||||
vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::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,
|
||||
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::Authorize(*new_authority),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
data: &[u8],
|
||||
) -> Result<(), InstructionError> {
|
||||
let signers = get_signers(keyed_accounts);
|
||||
|
||||
let keyed_accounts = &mut keyed_accounts.iter_mut();
|
||||
let me = &mut next_keyed_account(keyed_accounts)?;
|
||||
|
||||
match limited_deserialize(data)? {
|
||||
NonceInstruction::Nonce => me.nonce(
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
||||
&signers,
|
||||
),
|
||||
NonceInstruction::Withdraw(lamports) => {
|
||||
let to = &mut next_keyed_account(keyed_accounts)?;
|
||||
me.withdraw(
|
||||
lamports,
|
||||
to,
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
||||
&Rent::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
||||
&signers,
|
||||
)
|
||||
}
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
account::Account,
|
||||
hash::{hash, Hash},
|
||||
nonce_state, system_program, sysvar,
|
||||
};
|
||||
use bincode::serialize;
|
||||
|
||||
fn process_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
let mut accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|meta| {
|
||||
if sysvar::recent_blockhashes::check_id(&meta.pubkey) {
|
||||
sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
)
|
||||
} else if sysvar::rent::check_id(&meta.pubkey) {
|
||||
sysvar::rent::create_account(1, &Rent::free())
|
||||
} else {
|
||||
Account::default()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
{
|
||||
let mut keyed_accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.zip(accounts.iter_mut())
|
||||
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
||||
.collect();
|
||||
super::process_instruction(&Pubkey::default(), &mut keyed_accounts, &instruction.data)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let from_pubkey = Pubkey::new_rand();
|
||||
let nonce_pubkey = Pubkey::new_rand();
|
||||
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());
|
||||
let pubkeys: Vec<_> = ix.accounts.iter().map(|am| am.pubkey).collect();
|
||||
assert!(pubkeys.contains(&from_pubkey));
|
||||
assert!(pubkeys.contains(&nonce_pubkey));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_instruction(&nonce(&Pubkey::default(), &Pubkey::default())),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&NonceInstruction::Nonce).unwrap()
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut Account::default(),
|
||||
),],
|
||||
&serialize(&NonceInstruction::Nonce).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Nonce).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_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,),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &hash(&serialize(&0).unwrap())); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Nonce).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_instruction(&withdraw(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
1,
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&NonceInstruction::Withdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut Account::default(),
|
||||
),],
|
||||
&serialize(&NonceInstruction::Withdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(&Pubkey::default(), false, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Withdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_bad_rent_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
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 Account::default(),),
|
||||
],
|
||||
&serialize(&NonceInstruction::Withdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_ok() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
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::Withdraw(42)).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_invalid_acc_data_fail() {
|
||||
let authorized = Pubkey::default();
|
||||
assert_eq!(
|
||||
process_instruction(&initialize(&Pubkey::default(), &authorized)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),],
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_bad_rent_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
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 Account::default(),),
|
||||
],
|
||||
&serialize(&NonceInstruction::Initialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_ok() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
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(),
|
||||
),
|
||||
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;
|
||||
fn pretty_err<T>(err: InstructionError) -> String
|
||||
where
|
||||
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
|
||||
{
|
||||
if let InstructionError::CustomError(code) = err {
|
||||
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
|
||||
format!(
|
||||
"{:?}: {}::{:?} - {}",
|
||||
err,
|
||||
T::type_of(),
|
||||
specific_error,
|
||||
specific_error,
|
||||
)
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
"CustomError(0): NonceError::NoRecentBlockhashes - recent blockhash list is empty",
|
||||
pretty_err::<NonceError>(NonceError::NoRecentBlockhashes.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(1): NonceError::NotExpired - stored nonce is still in recent_blockhashes",
|
||||
pretty_err::<NonceError>(NonceError::NotExpired.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(2): NonceError::UnexpectedValue - specified nonce does not match stored nonce",
|
||||
pretty_err::<NonceError>(NonceError::UnexpectedValue.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(3): NonceError::BadAccountState - cannot handle request in current account state",
|
||||
pretty_err::<NonceError>(NonceError::BadAccountState.into())
|
||||
);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
crate::declare_id!("Nonce11111111111111111111111111111111111111");
|
||||
|
||||
pub fn solana_nonce_program() -> (String, crate::pubkey::Pubkey) {
|
||||
("solana_nonce_program".to_string(), id())
|
||||
}
|
@ -3,9 +3,9 @@ use crate::{
|
||||
account_utils::State,
|
||||
hash::Hash,
|
||||
instruction::InstructionError,
|
||||
nonce_instruction::NonceError,
|
||||
nonce_program,
|
||||
pubkey::Pubkey,
|
||||
system_instruction::NonceError,
|
||||
system_program,
|
||||
sysvar::recent_blockhashes::RecentBlockhashes,
|
||||
sysvar::rent::Rent,
|
||||
};
|
||||
@ -45,12 +45,12 @@ impl NonceState {
|
||||
}
|
||||
|
||||
pub trait NonceAccount {
|
||||
fn nonce(
|
||||
fn nonce_advance(
|
||||
&mut self,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn withdraw(
|
||||
fn nonce_withdraw(
|
||||
&mut self,
|
||||
lamports: u64,
|
||||
to: &mut KeyedAccount,
|
||||
@ -58,13 +58,13 @@ pub trait NonceAccount {
|
||||
rent: &Rent,
|
||||
signers: &HashSet<Pubkey>,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn initialize(
|
||||
fn nonce_initialize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
recent_blockhashes: &RecentBlockhashes,
|
||||
rent: &Rent,
|
||||
) -> Result<(), InstructionError>;
|
||||
fn authorize(
|
||||
fn nonce_authorize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
@ -72,7 +72,7 @@ pub trait NonceAccount {
|
||||
}
|
||||
|
||||
impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
fn nonce(
|
||||
fn nonce_advance(
|
||||
&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 withdraw(
|
||||
fn nonce_withdraw(
|
||||
&mut self,
|
||||
lamports: u64,
|
||||
to: &mut KeyedAccount,
|
||||
@ -137,7 +137,7 @@ impl<'a> NonceAccount for KeyedAccount<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize(
|
||||
fn nonce_initialize(
|
||||
&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 authorize(
|
||||
fn nonce_authorize(
|
||||
&mut self,
|
||||
nonce_authority: &Pubkey,
|
||||
signers: &HashSet<Pubkey>,
|
||||
@ -183,7 +183,7 @@ pub fn create_account(lamports: u64) -> Account {
|
||||
lamports,
|
||||
&NonceState::Uninitialized,
|
||||
NonceState::size(),
|
||||
&nonce_program::id(),
|
||||
&system_program::id(),
|
||||
)
|
||||
.expect("nonce_account")
|
||||
}
|
||||
@ -205,7 +205,7 @@ mod test {
|
||||
use super::*;
|
||||
use crate::{
|
||||
account::KeyedAccount,
|
||||
nonce_instruction::NonceError,
|
||||
system_instruction::NonceError,
|
||||
sysvar::recent_blockhashes::{create_test_recent_blockhashes, RecentBlockhashes},
|
||||
};
|
||||
use std::iter::FromIterator;
|
||||
@ -238,20 +238,24 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
// First nonce instruction drives state from Uninitialized to Initialized
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
keyed_account.nonce(&recent_blockhashes, &signers).unwrap();
|
||||
keyed_account
|
||||
.nonce_advance(&recent_blockhashes, &signers)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
// Second nonce instruction consumes and replaces stored nonce
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
keyed_account.nonce(&recent_blockhashes, &signers).unwrap();
|
||||
keyed_account
|
||||
.nonce_advance(&recent_blockhashes, &signers)
|
||||
.unwrap();
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
// Third nonce instruction for fun and profit
|
||||
@ -262,7 +266,7 @@ mod test {
|
||||
let expect_nonce_lamports = keyed_account.account.lamports - withdraw_lamports;
|
||||
let expect_to_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
keyed_account
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -291,7 +295,7 @@ mod test {
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let pubkey = nonce_account.account.owner.clone();
|
||||
let mut nonce_account = KeyedAccount::new(&pubkey, false, nonce_account.account);
|
||||
@ -299,7 +303,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(&recent_blockhashes, &signers);
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
||||
})
|
||||
}
|
||||
@ -317,10 +321,10 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(vec![].into_iter());
|
||||
let result = keyed_account.nonce(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
||||
})
|
||||
}
|
||||
@ -338,9 +342,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = keyed_account.nonce(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::NotExpired.into()));
|
||||
})
|
||||
}
|
||||
@ -356,7 +360,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(&recent_blockhashes, &signers);
|
||||
let result = keyed_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -375,12 +379,12 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_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);
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
});
|
||||
});
|
||||
@ -400,9 +404,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(63);
|
||||
let authorized = nonce_authority.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.nonce(&recent_blockhashes, &signers);
|
||||
let result = nonce_account.nonce_advance(&recent_blockhashes, &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature),);
|
||||
});
|
||||
});
|
||||
@ -426,7 +430,7 @@ mod test {
|
||||
let expect_nonce_lamports = nonce_keyed.account.lamports - withdraw_lamports;
|
||||
let expect_to_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -459,7 +463,7 @@ mod test {
|
||||
with_test_keyed_account(42, false, |mut to_keyed| {
|
||||
let signers = HashSet::new();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = nonce_keyed.withdraw(
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
nonce_keyed.account.lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -485,7 +489,7 @@ mod test {
|
||||
let mut signers = HashSet::new();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = nonce_keyed.withdraw(
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
nonce_keyed.account.lamports + 1,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -513,7 +517,7 @@ mod test {
|
||||
let nonce_expect_lamports = nonce_keyed.account.lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -529,7 +533,7 @@ mod test {
|
||||
let nonce_expect_lamports = nonce_keyed.account.lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -559,7 +563,7 @@ mod test {
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
nonce_keyed
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let state: NonceState = nonce_keyed.state().unwrap();
|
||||
let stored = recent_blockhashes[0];
|
||||
@ -569,7 +573,7 @@ mod test {
|
||||
let nonce_expect_lamports = nonce_keyed.account.lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -587,7 +591,7 @@ mod test {
|
||||
let nonce_expect_lamports = nonce_keyed.account.lamports - withdraw_lamports;
|
||||
let to_expect_lamports = to_keyed.account.lamports + withdraw_lamports;
|
||||
nonce_keyed
|
||||
.withdraw(
|
||||
.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -612,13 +616,13 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_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());
|
||||
let withdraw_lamports = nonce_keyed.account.lamports;
|
||||
let result = nonce_keyed.withdraw(
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -641,14 +645,14 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_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();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let withdraw_lamports = nonce_keyed.account.lamports + 1;
|
||||
let result = nonce_keyed.withdraw(
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -671,14 +675,14 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(95);
|
||||
let authorized = nonce_keyed.unsigned_key().clone();
|
||||
nonce_keyed
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_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();
|
||||
signers.insert(nonce_keyed.signer_key().unwrap().clone());
|
||||
let withdraw_lamports = nonce_keyed.account.lamports - min_lamports + 1;
|
||||
let result = nonce_keyed.withdraw(
|
||||
let result = nonce_keyed.nonce_withdraw(
|
||||
withdraw_lamports,
|
||||
&mut to_keyed,
|
||||
&recent_blockhashes,
|
||||
@ -706,7 +710,7 @@ mod test {
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = keyed_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
@ -725,7 +729,7 @@ 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.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::NoRecentBlockhashes.into()));
|
||||
})
|
||||
}
|
||||
@ -741,10 +745,10 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = keyed_account.unsigned_key().clone();
|
||||
keyed_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let result = keyed_account.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -759,7 +763,7 @@ 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.initialize(&authorized, &recent_blockhashes, &rent);
|
||||
let result = keyed_account.nonce_initialize(&authorized, &recent_blockhashes, &rent);
|
||||
assert_eq!(result, Err(InstructionError::InsufficientFunds));
|
||||
})
|
||||
}
|
||||
@ -778,11 +782,11 @@ mod test {
|
||||
let stored = recent_blockhashes[0];
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let authorized = &Pubkey::default().clone();
|
||||
let meta = Meta::new(&authorized);
|
||||
let result = nonce_account.authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Ok(()));
|
||||
let state: NonceState = nonce_account.state().unwrap();
|
||||
assert_eq!(state, NonceState::Initialized(meta, stored));
|
||||
@ -799,7 +803,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.authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(NonceError::BadAccountState.into()));
|
||||
})
|
||||
}
|
||||
@ -817,9 +821,9 @@ mod test {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(31);
|
||||
let authorized = &Pubkey::default().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &rent)
|
||||
.unwrap();
|
||||
let result = nonce_account.authorize(&Pubkey::default(), &signers);
|
||||
let result = nonce_account.nonce_authorize(&Pubkey::default(), &signers);
|
||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
||||
})
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
use crate::hash::hashv;
|
||||
use crate::instruction::{AccountMeta, Instruction};
|
||||
use crate::instruction::{AccountMeta, Instruction, WithSigner};
|
||||
use crate::instruction_processor_utils::DecodeError;
|
||||
use crate::nonce_state::NonceState;
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::system_program;
|
||||
use crate::sysvar::{recent_blockhashes, rent};
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Serialize, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum SystemError {
|
||||
@ -30,6 +33,24 @@ impl std::fmt::Display for SystemError {
|
||||
}
|
||||
impl std::error::Error for SystemError {}
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum NonceError {
|
||||
#[error("recent blockhash list is empty")]
|
||||
NoRecentBlockhashes,
|
||||
#[error("stored nonce is still in recent_blockhashes")]
|
||||
NotExpired,
|
||||
#[error("specified nonce does not match stored nonce")]
|
||||
UnexpectedValue,
|
||||
#[error("cannot handle request in current account state")]
|
||||
BadAccountState,
|
||||
}
|
||||
|
||||
impl<E> DecodeError<E> for NonceError {
|
||||
fn type_of() -> &'static str {
|
||||
"NonceError"
|
||||
}
|
||||
}
|
||||
|
||||
/// maximum length of derived address seed
|
||||
pub const MAX_ADDRESS_SEED_LEN: usize = 32;
|
||||
|
||||
@ -71,6 +92,51 @@ pub enum SystemInstruction {
|
||||
space: u64,
|
||||
program_id: Pubkey,
|
||||
},
|
||||
/// `NonceAdvance` consumes a stored nonce, replacing it with a successor
|
||||
///
|
||||
/// Expects 2 Accounts:
|
||||
/// 0 - A NonceAccount
|
||||
/// 1 - RecentBlockhashes sysvar
|
||||
///
|
||||
/// The current authority must sign a transaction executing this instrucion
|
||||
NonceAdvance,
|
||||
/// `NonceWithdraw` transfers funds out of the nonce account
|
||||
///
|
||||
/// Expects 4 Accounts:
|
||||
/// 0 - A NonceAccount
|
||||
/// 1 - A system account to which the lamports will be transferred
|
||||
/// 2 - RecentBlockhashes sysvar
|
||||
/// 3 - Rent sysvar
|
||||
///
|
||||
/// 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
|
||||
NonceWithdraw(u64),
|
||||
/// `NonceInitialize` drives state of Uninitalized NonceAccount to Initialized,
|
||||
/// setting the nonce value.
|
||||
///
|
||||
/// Expects 3 Accounts:
|
||||
/// 0 - A NonceAccount in the Uninitialized state
|
||||
/// 1 - RecentBlockHashes sysvar
|
||||
/// 2 - Rent sysvar
|
||||
///
|
||||
/// 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
|
||||
NonceInitialize(Pubkey),
|
||||
/// `NonceAuthorize` 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
|
||||
NonceAuthorize(Pubkey),
|
||||
}
|
||||
|
||||
pub fn create_account(
|
||||
@ -167,9 +233,82 @@ pub fn create_address_with_seed(
|
||||
))
|
||||
}
|
||||
|
||||
pub fn create_nonce_account(
|
||||
from_pubkey: &Pubkey,
|
||||
nonce_pubkey: &Pubkey,
|
||||
authority: &Pubkey,
|
||||
lamports: u64,
|
||||
) -> Vec<Instruction> {
|
||||
vec![
|
||||
create_account(
|
||||
from_pubkey,
|
||||
nonce_pubkey,
|
||||
lamports,
|
||||
NonceState::size() as u64,
|
||||
&system_program::id(),
|
||||
),
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::NonceInitialize(*authority),
|
||||
vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::id(), false),
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn nonce_advance(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(
|
||||
system_program::id(),
|
||||
&SystemInstruction::NonceAdvance,
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn nonce_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(
|
||||
system_program::id(),
|
||||
&SystemInstruction::NonceWithdraw(lamports),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn nonce_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(
|
||||
system_program::id(),
|
||||
&SystemInstruction::NonceAuthorize(*new_authority),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::instruction::{Instruction, InstructionError};
|
||||
|
||||
fn get_keys(instruction: &Instruction) -> Vec<Pubkey> {
|
||||
instruction.accounts.iter().map(|x| x.pubkey).collect()
|
||||
@ -239,4 +378,56 @@ mod tests {
|
||||
assert_eq!(get_keys(&instructions[0]), vec![alice_pubkey, bob_pubkey]);
|
||||
assert_eq!(get_keys(&instructions[1]), vec![alice_pubkey, carol_pubkey]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_nonce_account() {
|
||||
let from_pubkey = Pubkey::new_rand();
|
||||
let nonce_pubkey = Pubkey::new_rand();
|
||||
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());
|
||||
let pubkeys: Vec<_> = ix.accounts.iter().map(|am| am.pubkey).collect();
|
||||
assert!(pubkeys.contains(&from_pubkey));
|
||||
assert!(pubkeys.contains(&nonce_pubkey));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nonce_error_decode() {
|
||||
use num_traits::FromPrimitive;
|
||||
fn pretty_err<T>(err: InstructionError) -> String
|
||||
where
|
||||
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
|
||||
{
|
||||
if let InstructionError::CustomError(code) = err {
|
||||
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
|
||||
format!(
|
||||
"{:?}: {}::{:?} - {}",
|
||||
err,
|
||||
T::type_of(),
|
||||
specific_error,
|
||||
specific_error,
|
||||
)
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
"CustomError(0): NonceError::NoRecentBlockhashes - recent blockhash list is empty",
|
||||
pretty_err::<NonceError>(NonceError::NoRecentBlockhashes.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(1): NonceError::NotExpired - stored nonce is still in recent_blockhashes",
|
||||
pretty_err::<NonceError>(NonceError::NotExpired.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(2): NonceError::UnexpectedValue - specified nonce does not match stored nonce",
|
||||
pretty_err::<NonceError>(NonceError::UnexpectedValue.into())
|
||||
);
|
||||
assert_eq!(
|
||||
"CustomError(3): NonceError::BadAccountState - cannot handle request in current account state",
|
||||
pretty_err::<NonceError>(NonceError::BadAccountState.into())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,10 @@
|
||||
use crate::hash::Hash;
|
||||
use crate::instruction::{CompiledInstruction, Instruction, InstructionError};
|
||||
use crate::message::Message;
|
||||
use crate::nonce_instruction;
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::short_vec;
|
||||
use crate::signature::{KeypairUtil, Signature};
|
||||
use crate::system_instruction;
|
||||
use bincode::serialize;
|
||||
use std::result;
|
||||
|
||||
@ -103,7 +103,8 @@ impl Transaction {
|
||||
nonce_authority_pubkey: &Pubkey,
|
||||
nonce_hash: Hash,
|
||||
) -> Self {
|
||||
let nonce_ix = nonce_instruction::nonce(&nonce_account_pubkey, &nonce_authority_pubkey);
|
||||
let nonce_ix =
|
||||
system_instruction::nonce_advance(&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