2021-12-03 09:00:31 -08:00
|
|
|
use {
|
2022-03-22 18:09:30 -07:00
|
|
|
crate::nonce_keyed_account::{
|
|
|
|
advance_nonce_account, authorize_nonce_account, initialize_nonce_account,
|
|
|
|
withdraw_nonce_account,
|
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
log::*,
|
2022-02-03 04:03:50 -08:00
|
|
|
solana_program_runtime::{
|
2022-03-16 03:30:01 -07:00
|
|
|
ic_msg, invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,
|
2022-02-03 04:03:50 -08:00
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
solana_sdk::{
|
2022-04-19 00:28:07 -07:00
|
|
|
account::AccountSharedData,
|
2021-12-03 09:00:31 -08:00
|
|
|
account_utils::StateMut,
|
|
|
|
feature_set,
|
|
|
|
instruction::InstructionError,
|
|
|
|
nonce,
|
|
|
|
program_utils::limited_deserialize,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
system_instruction::{
|
|
|
|
NonceError, SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH,
|
|
|
|
},
|
|
|
|
system_program,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context::{BorrowedAccount, InstructionContext, TransactionContext},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
|
|
|
std::collections::HashSet,
|
2019-12-12 11:12:09 -08:00
|
|
|
};
|
2019-12-29 16:42:24 -08:00
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
// represents an address that may or may not have been generated
|
|
|
|
// from a seed
|
|
|
|
#[derive(PartialEq, Default, Debug)]
|
|
|
|
struct Address {
|
|
|
|
address: Pubkey,
|
|
|
|
base: Option<Pubkey>,
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
impl Address {
|
|
|
|
fn is_signer(&self, signers: &HashSet<Pubkey>) -> bool {
|
|
|
|
if let Some(base) = self.base {
|
|
|
|
signers.contains(&base)
|
|
|
|
} else {
|
|
|
|
signers.contains(&self.address)
|
|
|
|
}
|
2019-11-08 02:27:35 -08:00
|
|
|
}
|
2020-01-17 09:29:15 -08:00
|
|
|
fn create(
|
|
|
|
address: &Pubkey,
|
|
|
|
with_seed: Option<(&Pubkey, &str, &Pubkey)>,
|
2021-12-02 09:47:16 -08:00
|
|
|
invoke_context: &InvokeContext,
|
2020-01-17 09:29:15 -08:00
|
|
|
) -> Result<Self, InstructionError> {
|
2020-05-18 12:55:41 -07:00
|
|
|
let base = if let Some((base, seed, owner)) = with_seed {
|
2021-03-02 21:44:25 -08:00
|
|
|
let address_with_seed = Pubkey::create_with_seed(base, seed, owner)?;
|
2020-01-17 09:29:15 -08:00
|
|
|
// re-derive the address, must match the supplied address
|
2021-03-02 21:44:25 -08:00
|
|
|
if *address != address_with_seed {
|
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Create: address {} does not match derived address {}",
|
|
|
|
address,
|
|
|
|
address_with_seed
|
|
|
|
);
|
2020-01-17 09:29:15 -08:00
|
|
|
return Err(SystemError::AddressWithSeedMismatch.into());
|
|
|
|
}
|
|
|
|
Some(*base)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-08 02:27:35 -08:00
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
Ok(Self {
|
|
|
|
address: *address,
|
|
|
|
base,
|
|
|
|
})
|
|
|
|
}
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
fn allocate(
|
2022-04-19 00:28:07 -07:00
|
|
|
account: &mut BorrowedAccount,
|
2020-01-17 09:29:15 -08:00
|
|
|
address: &Address,
|
|
|
|
space: u64,
|
2022-03-16 03:30:01 -07:00
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
invoke_context: &InvokeContext,
|
2019-12-12 11:12:09 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2020-01-17 09:29:15 -08:00
|
|
|
if !address.is_signer(signers) {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Allocate: 'to' account {:?} must sign",
|
|
|
|
address
|
|
|
|
);
|
2020-01-17 09:29:15 -08:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
2019-12-29 16:42:24 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:12:09 -08:00
|
|
|
// if it looks like the `to` account is already in use, bail
|
2020-01-15 13:03:22 -08:00
|
|
|
// (note that the id check is also enforced by message_processor)
|
2022-04-19 00:28:07 -07:00
|
|
|
if !account.get_data().is_empty() || !system_program::check_id(account.get_owner()) {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Allocate: account {:?} already in use",
|
2020-01-17 09:29:15 -08:00
|
|
|
address
|
2019-03-05 11:11:05 -08:00
|
|
|
);
|
2019-10-10 16:43:49 -07:00
|
|
|
return Err(SystemError::AccountAlreadyInUse.into());
|
2019-03-05 11:11:05 -08:00
|
|
|
}
|
2019-06-06 19:27:49 -07:00
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
if space > MAX_PERMITTED_DATA_LENGTH {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Allocate: requested {}, max allowed {}",
|
|
|
|
space,
|
|
|
|
MAX_PERMITTED_DATA_LENGTH
|
2019-12-10 08:47:11 -08:00
|
|
|
);
|
|
|
|
return Err(SystemError::InvalidAccountDataLength.into());
|
|
|
|
}
|
|
|
|
|
2022-05-21 08:47:09 -07:00
|
|
|
account.set_data(&vec![0; space as usize])?;
|
2019-12-12 11:12:09 -08:00
|
|
|
|
2019-03-05 11:11:05 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
fn assign(
|
2022-04-19 00:28:07 -07:00
|
|
|
account: &mut BorrowedAccount,
|
2020-01-17 09:29:15 -08:00
|
|
|
address: &Address,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner: &Pubkey,
|
2022-03-16 03:30:01 -07:00
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
invoke_context: &InvokeContext,
|
2019-10-10 16:43:49 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2020-01-15 15:13:11 -08:00
|
|
|
// no work to do, just return
|
2022-04-19 00:28:07 -07:00
|
|
|
if account.get_owner() == owner {
|
2020-01-15 15:13:11 -08:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:34:46 -07:00
|
|
|
if !address.is_signer(signers) {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(invoke_context, "Assign: account {:?} must sign", address);
|
2019-10-10 16:43:49 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
|
|
|
|
2022-05-21 08:47:09 -07:00
|
|
|
account.set_owner(&owner.to_bytes())
|
2019-03-05 11:11:05 -08:00
|
|
|
}
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
fn allocate_and_assign(
|
2022-04-19 00:28:07 -07:00
|
|
|
to: &mut BorrowedAccount,
|
2020-01-17 09:29:15 -08:00
|
|
|
to_address: &Address,
|
|
|
|
space: u64,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner: &Pubkey,
|
2022-03-16 03:30:01 -07:00
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
invoke_context: &InvokeContext,
|
2020-01-17 09:29:15 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2022-03-16 03:30:01 -07:00
|
|
|
allocate(to, to_address, space, signers, invoke_context)?;
|
|
|
|
assign(to, to_address, owner, signers, invoke_context)
|
2020-01-17 09:29:15 -08:00
|
|
|
}
|
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-01-17 09:29:15 -08:00
|
|
|
fn create_account(
|
2022-04-19 00:28:07 -07:00
|
|
|
from_account_index: usize,
|
|
|
|
to_account_index: usize,
|
2020-01-17 09:29:15 -08:00
|
|
|
to_address: &Address,
|
|
|
|
lamports: u64,
|
|
|
|
space: u64,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner: &Pubkey,
|
2022-03-16 03:30:01 -07:00
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
invoke_context: &InvokeContext,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-01-17 09:29:15 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2020-05-22 16:39:01 -07:00
|
|
|
// if it looks like the `to` account is already in use, bail
|
2020-05-30 19:12:48 -07:00
|
|
|
{
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut to =
|
|
|
|
instruction_context.try_borrow_account(transaction_context, to_account_index)?;
|
|
|
|
if to.get_lamports() > 0 {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Create Account: account {:?} already in use",
|
2020-05-30 19:12:48 -07:00
|
|
|
to_address
|
|
|
|
);
|
|
|
|
return Err(SystemError::AccountAlreadyInUse.into());
|
|
|
|
}
|
2022-03-16 03:30:01 -07:00
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
allocate_and_assign(&mut to, to_address, space, owner, signers, invoke_context)?;
|
2020-05-30 19:12:48 -07:00
|
|
|
}
|
2022-04-19 00:28:07 -07:00
|
|
|
transfer(
|
|
|
|
from_account_index,
|
|
|
|
to_account_index,
|
|
|
|
lamports,
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
)
|
2020-01-17 09:29:15 -08:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:28:01 -07:00
|
|
|
fn transfer_verified(
|
2022-04-19 00:28:07 -07:00
|
|
|
from_account_index: usize,
|
|
|
|
to_account_index: usize,
|
2022-02-18 17:06:33 -08:00
|
|
|
lamports: u64,
|
2022-03-16 03:30:01 -07:00
|
|
|
invoke_context: &InvokeContext,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-08-21 11:28:01 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut from =
|
|
|
|
instruction_context.try_borrow_account(transaction_context, from_account_index)?;
|
|
|
|
if !from.get_data().is_empty() {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(invoke_context, "Transfer: `from` must not carry data");
|
2020-01-03 16:34:58 -08:00
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
2022-04-19 00:28:07 -07:00
|
|
|
if lamports > from.get_lamports() {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Transfer: insufficient lamports {}, need {}",
|
2022-04-19 00:28:07 -07:00
|
|
|
from.get_lamports(),
|
2020-01-22 09:11:56 -08:00
|
|
|
lamports
|
2019-03-05 11:11:05 -08:00
|
|
|
);
|
2019-10-10 16:43:49 -07:00
|
|
|
return Err(SystemError::ResultWithNegativeLamports.into());
|
2019-03-05 11:11:05 -08:00
|
|
|
}
|
2022-03-16 03:30:01 -07:00
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
from.checked_sub_lamports(lamports)?;
|
|
|
|
drop(from);
|
|
|
|
let mut to = instruction_context.try_borrow_account(transaction_context, to_account_index)?;
|
|
|
|
to.checked_add_lamports(lamports)?;
|
2019-03-05 11:11:05 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:44:25 -08:00
|
|
|
fn transfer(
|
2022-04-19 00:28:07 -07:00
|
|
|
from_account_index: usize,
|
|
|
|
to_account_index: usize,
|
2022-02-18 17:06:33 -08:00
|
|
|
lamports: u64,
|
2022-03-16 03:30:01 -07:00
|
|
|
invoke_context: &InvokeContext,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2021-03-02 21:44:25 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-12-03 03:15:22 -08:00
|
|
|
if !invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::system_transfer_zero_check::id())
|
2021-06-05 16:45:45 -07:00
|
|
|
&& lamports == 0
|
|
|
|
{
|
2020-08-21 11:28:01 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
if !instruction_context.is_signer(from_account_index)? {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
2021-03-04 23:16:53 -08:00
|
|
|
"Transfer: `from` account {} must sign",
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(from_account_index)?,
|
|
|
|
)?,
|
2021-03-02 21:44:25 -08:00
|
|
|
);
|
2020-08-21 11:28:01 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
transfer_verified(
|
|
|
|
from_account_index,
|
|
|
|
to_account_index,
|
|
|
|
lamports,
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
)
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn transfer_with_seed(
|
2022-04-19 00:28:07 -07:00
|
|
|
from_account_index: usize,
|
|
|
|
from_base_account_index: usize,
|
2020-08-21 11:28:01 -07:00
|
|
|
from_seed: &str,
|
|
|
|
from_owner: &Pubkey,
|
2022-04-19 00:28:07 -07:00
|
|
|
to_account_index: usize,
|
2020-08-21 11:28:01 -07:00
|
|
|
lamports: u64,
|
2022-03-16 03:30:01 -07:00
|
|
|
invoke_context: &InvokeContext,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-08-21 11:28:01 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-12-03 03:15:22 -08:00
|
|
|
if !invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::system_transfer_zero_check::id())
|
2021-06-05 16:45:45 -07:00
|
|
|
&& lamports == 0
|
|
|
|
{
|
2020-08-21 11:28:01 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
if !instruction_context.is_signer(from_base_account_index)? {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Transfer: 'from' account {:?} must sign",
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(from_base_account_index)?,
|
|
|
|
)?,
|
2021-03-02 21:44:25 -08:00
|
|
|
);
|
2020-08-21 11:28:01 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2022-04-19 00:28:07 -07:00
|
|
|
let address_from_seed = Pubkey::create_with_seed(
|
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(from_base_account_index)?,
|
|
|
|
)?,
|
|
|
|
from_seed,
|
|
|
|
from_owner,
|
|
|
|
)?;
|
2020-08-21 11:28:01 -07:00
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
let from_key = transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(from_account_index)?,
|
|
|
|
)?;
|
|
|
|
if *from_key != address_from_seed {
|
2021-03-02 21:44:25 -08:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Transfer: 'from' address {} does not match derived address {}",
|
2022-04-19 00:28:07 -07:00
|
|
|
from_key,
|
2022-03-16 03:30:01 -07:00
|
|
|
address_from_seed
|
2021-03-02 21:44:25 -08:00
|
|
|
);
|
2020-08-21 11:28:01 -07:00
|
|
|
return Err(SystemError::AddressWithSeedMismatch.into());
|
|
|
|
}
|
|
|
|
|
2022-04-19 00:28:07 -07:00
|
|
|
transfer_verified(
|
|
|
|
from_account_index,
|
|
|
|
to_account_index,
|
|
|
|
lamports,
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
)
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
|
|
|
|
2019-04-02 07:36:10 -07:00
|
|
|
pub fn process_instruction(
|
2022-03-16 03:30:01 -07:00
|
|
|
first_instruction_account: usize,
|
2021-12-02 09:47:16 -08:00
|
|
|
invoke_context: &mut InvokeContext,
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2022-03-24 04:57:51 -07:00
|
|
|
let transaction_context = &invoke_context.transaction_context;
|
|
|
|
let instruction_context = transaction_context.get_current_instruction_context()?;
|
2022-04-01 06:48:05 -07:00
|
|
|
let instruction_data = instruction_context.get_instruction_data();
|
2020-01-14 13:38:17 -08:00
|
|
|
let instruction = limited_deserialize(instruction_data)?;
|
2019-10-10 16:43:49 -07:00
|
|
|
|
|
|
|
trace!("process_instruction: {:?}", instruction);
|
|
|
|
|
2022-03-24 04:57:51 -07:00
|
|
|
let signers = instruction_context.get_signers(transaction_context);
|
2019-10-10 16:43:49 -07:00
|
|
|
match instruction {
|
|
|
|
SystemInstruction::CreateAccount {
|
|
|
|
lamports,
|
|
|
|
space,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner,
|
2019-10-10 16:43:49 -07:00
|
|
|
} => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
let to_address = Address::create(
|
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account + 1)?,
|
|
|
|
)?,
|
|
|
|
None,
|
|
|
|
invoke_context,
|
|
|
|
)?;
|
2021-03-02 21:44:25 -08:00
|
|
|
create_account(
|
2022-04-19 00:28:07 -07:00
|
|
|
first_instruction_account,
|
|
|
|
first_instruction_account + 1,
|
2021-03-02 21:44:25 -08:00
|
|
|
&to_address,
|
|
|
|
lamports,
|
|
|
|
space,
|
|
|
|
&owner,
|
2022-03-16 03:30:01 -07:00
|
|
|
&signers,
|
|
|
|
invoke_context,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2021-03-02 21:44:25 -08:00
|
|
|
)
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
|
|
|
SystemInstruction::CreateAccountWithSeed {
|
2019-12-29 16:42:24 -08:00
|
|
|
base,
|
2019-12-12 11:12:09 -08:00
|
|
|
seed,
|
|
|
|
lamports,
|
|
|
|
space,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner,
|
2019-12-12 11:12:09 -08:00
|
|
|
} => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
let to_address = Address::create(
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account + 1)?,
|
|
|
|
)?,
|
2022-03-16 03:30:01 -07:00
|
|
|
Some((&base, &seed, &owner)),
|
|
|
|
invoke_context,
|
2021-03-02 21:44:25 -08:00
|
|
|
)?;
|
|
|
|
create_account(
|
2022-04-19 00:28:07 -07:00
|
|
|
first_instruction_account,
|
|
|
|
first_instruction_account + 1,
|
2021-03-02 21:44:25 -08:00
|
|
|
&to_address,
|
|
|
|
lamports,
|
|
|
|
space,
|
|
|
|
&owner,
|
2022-03-16 03:30:01 -07:00
|
|
|
&signers,
|
|
|
|
invoke_context,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2021-03-02 21:44:25 -08:00
|
|
|
)
|
2019-10-10 15:43:32 -07:00
|
|
|
}
|
2020-05-18 12:55:41 -07:00
|
|
|
SystemInstruction::Assign { owner } => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut account = instruction_context
|
|
|
|
.try_borrow_account(transaction_context, first_instruction_account)?;
|
|
|
|
let address = Address::create(
|
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account)?,
|
|
|
|
)?,
|
|
|
|
None,
|
|
|
|
invoke_context,
|
|
|
|
)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
assign(&mut account, &address, &owner, &signers, invoke_context)
|
2018-12-03 13:32:31 -08:00
|
|
|
}
|
2019-10-10 16:43:49 -07:00
|
|
|
SystemInstruction::Transfer { lamports } => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
transfer(
|
|
|
|
first_instruction_account,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
lamports,
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
)
|
2018-12-03 13:32:31 -08:00
|
|
|
}
|
2020-08-21 11:28:01 -07:00
|
|
|
SystemInstruction::TransferWithSeed {
|
|
|
|
lamports,
|
|
|
|
from_seed,
|
|
|
|
from_owner,
|
|
|
|
} => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(3)?;
|
2021-03-02 21:44:25 -08:00
|
|
|
transfer_with_seed(
|
2022-04-19 00:28:07 -07:00
|
|
|
first_instruction_account,
|
|
|
|
first_instruction_account + 1,
|
2021-03-02 21:44:25 -08:00
|
|
|
&from_seed,
|
|
|
|
&from_owner,
|
2022-04-19 00:28:07 -07:00
|
|
|
first_instruction_account + 2,
|
2021-03-02 21:44:25 -08:00
|
|
|
lamports,
|
2022-03-16 03:30:01 -07:00
|
|
|
invoke_context,
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2021-03-02 21:44:25 -08:00
|
|
|
)
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
2020-01-13 15:34:43 -08:00
|
|
|
SystemInstruction::AdvanceNonceAccount => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-18 13:19:51 -07:00
|
|
|
let mut me =
|
|
|
|
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
2021-07-29 01:50:20 -07:00
|
|
|
#[allow(deprecated)]
|
2022-03-16 03:30:01 -07:00
|
|
|
let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
|
2022-02-03 04:03:50 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
1,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
|
|
|
if recent_blockhashes.is_empty() {
|
2021-07-29 01:50:20 -07:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Advance nonce account: recent blockhash list is empty",
|
|
|
|
);
|
|
|
|
return Err(NonceError::NoRecentBlockhashes.into());
|
|
|
|
}
|
2022-04-18 13:19:51 -07:00
|
|
|
advance_nonce_account(&mut me, &signers, invoke_context)
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
2020-01-13 15:34:43 -08:00
|
|
|
SystemInstruction::WithdrawNonceAccount(lamports) => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2021-07-29 01:50:20 -07:00
|
|
|
#[allow(deprecated)]
|
2022-03-16 03:30:01 -07:00
|
|
|
let _recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
|
2021-03-02 21:44:25 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
2,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 3)?;
|
2022-04-18 13:19:51 -07:00
|
|
|
withdraw_nonce_account(
|
|
|
|
first_instruction_account,
|
|
|
|
lamports,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
&rent,
|
|
|
|
&signers,
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
)
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
2020-01-13 15:34:43 -08:00
|
|
|
SystemInstruction::InitializeNonceAccount(authorized) => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-18 13:19:51 -07:00
|
|
|
let mut me =
|
|
|
|
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
2021-07-29 01:50:20 -07:00
|
|
|
#[allow(deprecated)]
|
2022-03-16 03:30:01 -07:00
|
|
|
let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
|
2022-02-03 04:03:50 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
1,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
|
|
|
if recent_blockhashes.is_empty() {
|
2021-07-29 01:50:20 -07:00
|
|
|
ic_msg!(
|
|
|
|
invoke_context,
|
|
|
|
"Initialize nonce account: recent blockhash list is empty",
|
|
|
|
);
|
|
|
|
return Err(NonceError::NoRecentBlockhashes.into());
|
|
|
|
}
|
2022-03-24 11:30:42 -07:00
|
|
|
let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 2)?;
|
2022-04-18 13:19:51 -07:00
|
|
|
initialize_nonce_account(&mut me, &authorized, &rent, invoke_context)
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
2020-01-13 15:34:43 -08:00
|
|
|
SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-18 13:19:51 -07:00
|
|
|
let mut me =
|
|
|
|
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
|
|
|
authorize_nonce_account(&mut me, &nonce_authority, &signers, invoke_context)
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
2020-01-17 09:29:15 -08:00
|
|
|
SystemInstruction::Allocate { space } => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut account = instruction_context
|
|
|
|
.try_borrow_account(transaction_context, first_instruction_account)?;
|
|
|
|
let address = Address::create(
|
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account)?,
|
|
|
|
)?,
|
|
|
|
None,
|
|
|
|
invoke_context,
|
|
|
|
)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
allocate(&mut account, &address, space, &signers, invoke_context)
|
2020-01-17 09:29:15 -08:00
|
|
|
}
|
|
|
|
SystemInstruction::AllocateWithSeed {
|
|
|
|
base,
|
|
|
|
seed,
|
|
|
|
space,
|
2020-05-18 12:55:41 -07:00
|
|
|
owner,
|
2020-01-17 09:29:15 -08:00
|
|
|
} => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut account = instruction_context
|
|
|
|
.try_borrow_account(transaction_context, first_instruction_account)?;
|
2021-03-02 21:44:25 -08:00
|
|
|
let address = Address::create(
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account)?,
|
|
|
|
)?,
|
2021-03-02 21:44:25 -08:00
|
|
|
Some((&base, &seed, &owner)),
|
|
|
|
invoke_context,
|
|
|
|
)?;
|
|
|
|
allocate_and_assign(
|
|
|
|
&mut account,
|
|
|
|
&address,
|
|
|
|
space,
|
|
|
|
&owner,
|
2022-03-16 03:30:01 -07:00
|
|
|
&signers,
|
|
|
|
invoke_context,
|
2021-03-02 21:44:25 -08:00
|
|
|
)
|
2020-01-17 09:29:15 -08:00
|
|
|
}
|
2020-05-18 12:55:41 -07:00
|
|
|
SystemInstruction::AssignWithSeed { base, seed, owner } => {
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(1)?;
|
2022-04-19 00:28:07 -07:00
|
|
|
let mut account = instruction_context
|
|
|
|
.try_borrow_account(transaction_context, first_instruction_account)?;
|
2021-03-02 21:44:25 -08:00
|
|
|
let address = Address::create(
|
2022-04-19 00:28:07 -07:00
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_in_transaction(first_instruction_account)?,
|
|
|
|
)?,
|
2021-03-02 21:44:25 -08:00
|
|
|
Some((&base, &seed, &owner)),
|
|
|
|
invoke_context,
|
|
|
|
)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
assign(&mut account, &address, &owner, &signers, invoke_context)
|
2020-01-17 09:29:15 -08:00
|
|
|
}
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum SystemAccountKind {
|
|
|
|
System,
|
|
|
|
Nonce,
|
|
|
|
}
|
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
pub fn get_system_account_kind(account: &AccountSharedData) -> Option<SystemAccountKind> {
|
2022-04-19 00:28:07 -07:00
|
|
|
use solana_sdk::account::ReadableAccount;
|
2021-04-23 15:49:47 -07:00
|
|
|
if system_program::check_id(account.owner()) {
|
2021-03-09 14:31:33 -08:00
|
|
|
if account.data().is_empty() {
|
2020-01-03 16:34:58 -08:00
|
|
|
Some(SystemAccountKind::System)
|
2021-03-09 14:31:33 -08:00
|
|
|
} else if account.data().len() == nonce::State::size() {
|
2020-03-05 06:40:26 -08:00
|
|
|
match account.state().ok()? {
|
|
|
|
nonce::state::Versions::Current(state) => match *state {
|
|
|
|
nonce::State::Initialized(_) => Some(SystemAccountKind::Nonce),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
}
|
2020-01-03 16:34:58 -08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2019-03-05 11:11:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2020-01-17 09:29:15 -08:00
|
|
|
use solana_sdk::{
|
2022-04-19 00:28:07 -07:00
|
|
|
account::{self, Account, AccountSharedData, ReadableAccount},
|
2020-01-17 09:29:15 -08:00
|
|
|
client::SyncClient,
|
|
|
|
genesis_config::create_genesis_config,
|
|
|
|
hash::{hash, Hash},
|
|
|
|
instruction::{AccountMeta, Instruction, InstructionError},
|
|
|
|
message::Message,
|
2021-11-04 13:47:32 -07:00
|
|
|
nonce, nonce_account, recent_blockhashes_account,
|
2020-02-20 13:28:55 -08:00
|
|
|
signature::{Keypair, Signer},
|
2022-02-03 04:03:50 -08:00
|
|
|
system_instruction, system_program,
|
|
|
|
sysvar::{self, recent_blockhashes::IterItem, rent::Rent},
|
2020-01-17 09:29:15 -08:00
|
|
|
transaction::TransactionError,
|
2021-12-27 09:49:32 -08:00
|
|
|
transaction_context::TransactionContext,
|
2020-01-17 09:29:15 -08:00
|
|
|
};
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
crate::{bank::Bank, bank_client::BankClient},
|
|
|
|
bincode::serialize,
|
2022-02-03 04:03:50 -08:00
|
|
|
solana_program_runtime::invoke_context::{
|
|
|
|
mock_process_instruction, InvokeContext, ProcessInstructionWithContext,
|
|
|
|
},
|
2022-02-14 15:00:49 -08:00
|
|
|
std::sync::Arc,
|
2021-12-03 09:00:31 -08:00
|
|
|
};
|
2020-01-17 09:29:15 -08:00
|
|
|
|
|
|
|
impl From<Pubkey> for Address {
|
|
|
|
fn from(address: Pubkey) -> Self {
|
|
|
|
Self {
|
|
|
|
address,
|
|
|
|
base: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 11:11:05 -08:00
|
|
|
|
2020-10-28 20:21:50 -07:00
|
|
|
fn process_instruction(
|
|
|
|
instruction_data: &[u8],
|
2021-12-17 05:01:12 -08:00
|
|
|
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
|
|
|
instruction_accounts: Vec<AccountMeta>,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
2022-02-03 04:03:50 -08:00
|
|
|
process_instruction: ProcessInstructionWithContext,
|
2021-12-17 05:01:12 -08:00
|
|
|
) -> Vec<AccountSharedData> {
|
2021-11-04 13:47:32 -07:00
|
|
|
mock_process_instruction(
|
|
|
|
&system_program::id(),
|
|
|
|
Vec::new(),
|
2020-10-28 20:21:50 -07:00
|
|
|
instruction_data,
|
2021-12-17 05:01:12 -08:00
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
2022-03-31 01:46:35 -07:00
|
|
|
None,
|
2022-04-15 11:43:04 -07:00
|
|
|
None,
|
2021-12-17 05:01:12 -08:00
|
|
|
expected_result,
|
2022-02-03 04:03:50 -08:00
|
|
|
process_instruction,
|
2020-10-28 20:21:50 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_account() -> AccountSharedData {
|
|
|
|
AccountSharedData::new(0, 0, &Pubkey::new_unique())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_recent_blockhashes_account() -> AccountSharedData {
|
|
|
|
#[allow(deprecated)]
|
|
|
|
recent_blockhashes_account::create_account_with_data_for_test(
|
|
|
|
vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES]
|
|
|
|
.into_iter(),
|
|
|
|
)
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_rent_account() -> AccountSharedData {
|
|
|
|
account::create_account_shared_data_for_test(&Rent::free())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
|
|
|
|
2019-03-05 11:11:05 -08:00
|
|
|
#[test]
|
2019-12-12 11:12:09 -08:00
|
|
|
fn test_create_account() {
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
|
|
|
let to = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2019-12-12 11:12:09 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].owner(), &new_owner);
|
|
|
|
assert_eq!(accounts[1].data(), &[0, 0]);
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_account_with_seed() {
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2019-12-12 11:12:09 -08:00
|
|
|
let seed = "shiny pepper";
|
2020-05-18 12:55:41 -07:00
|
|
|
let to = Pubkey::create_with_seed(&from, seed, &new_owner).unwrap();
|
2021-12-17 05:01:12 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccountWithSeed {
|
|
|
|
base: from,
|
|
|
|
seed: seed.to_string(),
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-11-10 15:51:53 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].owner(), &new_owner);
|
|
|
|
assert_eq!(accounts[1].data(), &[0, 0]);
|
2020-11-10 15:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_account_with_seed_separate_base_account() {
|
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
|
|
|
let base = Pubkey::new_unique();
|
2020-11-10 15:51:53 -08:00
|
|
|
let seed = "shiny pepper";
|
|
|
|
let to = Pubkey::create_with_seed(&base, seed, &new_owner).unwrap();
|
2021-12-17 05:01:12 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
let base_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
2020-11-10 15:51:53 -08:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccountWithSeed {
|
|
|
|
base,
|
|
|
|
seed: seed.to_string(),
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account), (base, base_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: base,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2019-12-12 11:12:09 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].owner(), &new_owner);
|
|
|
|
assert_eq!(accounts[1].data(), &[0, 0]);
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-17 09:29:15 -08:00
|
|
|
fn test_address_create_with_seed_mismatch() {
|
2022-01-19 13:40:09 -08:00
|
|
|
let mut transaction_context = TransactionContext::new(Vec::new(), 1, 1);
|
2022-01-03 14:30:56 -08:00
|
|
|
let invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2019-12-12 11:12:09 -08:00
|
|
|
let seed = "dull boy";
|
2021-11-04 13:47:32 -07:00
|
|
|
let to = Pubkey::new_unique();
|
|
|
|
let owner = Pubkey::new_unique();
|
2019-03-05 13:43:05 -08:00
|
|
|
|
2019-10-19 18:23:27 -07:00
|
|
|
assert_eq!(
|
2021-11-04 13:47:32 -07:00
|
|
|
Address::create(&to, Some((&from, seed, &owner)), &invoke_context),
|
2019-12-12 11:12:09 -08:00
|
|
|
Err(SystemError::AddressWithSeedMismatch.into())
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
2019-12-12 11:12:09 -08:00
|
|
|
}
|
2020-01-17 09:29:15 -08:00
|
|
|
|
2019-12-12 11:12:09 -08:00
|
|
|
#[test]
|
|
|
|
fn test_create_account_with_seed_missing_sig() {
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2019-12-12 11:12:09 -08:00
|
|
|
let seed = "dull boy";
|
2020-05-18 12:55:41 -07:00
|
|
|
let to = Pubkey::create_with_seed(&from, seed, &new_owner).unwrap();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2019-12-12 11:12:09 -08:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1], AccountSharedData::default());
|
2019-03-05 13:43:05 -08:00
|
|
|
}
|
2019-03-05 11:11:05 -08:00
|
|
|
|
2019-10-19 18:23:27 -07:00
|
|
|
#[test]
|
|
|
|
fn test_create_with_zero_lamports() {
|
2021-03-04 23:16:53 -08:00
|
|
|
// create account with zero lamports transferred
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-06-05 16:45:45 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &Pubkey::new_unique()); // not from system account
|
2021-06-05 16:45:45 -07:00
|
|
|
let to = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 0,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1].lamports(), 0);
|
|
|
|
assert_eq!(*accounts[1].owner(), new_owner);
|
|
|
|
assert_eq!(accounts[1].data(), &[0, 0]);
|
2019-10-19 18:23:27 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:43:05 -08:00
|
|
|
#[test]
|
2019-03-05 16:28:14 -08:00
|
|
|
fn test_create_negative_lamports() {
|
2022-02-14 15:00:49 -08:00
|
|
|
// Attempt to create account with more lamports than from_account has
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &Pubkey::new_unique());
|
2021-11-04 13:47:32 -07:00
|
|
|
let to = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 150,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(SystemError::ResultWithNegativeLamports.into()),
|
|
|
|
super::process_instruction,
|
2019-10-10 16:43:49 -07:00
|
|
|
);
|
2019-03-05 13:43:05 -08:00
|
|
|
}
|
2019-03-05 11:11:05 -08:00
|
|
|
|
2019-12-10 08:47:11 -08:00
|
|
|
#[test]
|
|
|
|
fn test_request_more_than_allowed_data_length() {
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &Pubkey::new_unique());
|
2021-11-04 13:47:32 -07:00
|
|
|
let to = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
2019-12-10 08:47:11 -08:00
|
|
|
|
|
|
|
// Trying to request more data length than permitted will result in failure
|
2022-02-14 15:00:49 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: MAX_PERMITTED_DATA_LENGTH + 1,
|
|
|
|
owner: system_program::id(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account.clone()), (to, to_account.clone())],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(SystemError::InvalidAccountDataLength.into()),
|
|
|
|
super::process_instruction,
|
2019-12-10 08:47:11 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Trying to request equal or less data length than permitted will be successful
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: MAX_PERMITTED_DATA_LENGTH,
|
|
|
|
owner: system_program::id(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2020-05-30 19:12:48 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[1].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].data().len() as u64, MAX_PERMITTED_DATA_LENGTH);
|
2019-12-10 08:47:11 -08:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:43:05 -08:00
|
|
|
#[test]
|
2019-10-19 18:23:27 -07:00
|
|
|
fn test_create_already_in_use() {
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
|
|
|
let owned_key = Pubkey::new_unique();
|
2019-03-05 13:43:05 -08:00
|
|
|
|
2022-02-14 15:00:49 -08:00
|
|
|
// Attempt to create system account in account already owned by another program
|
2019-03-05 11:11:05 -08:00
|
|
|
let original_program_owner = Pubkey::new(&[5; 32]);
|
2022-02-14 15:00:49 -08:00
|
|
|
let owned_account = AccountSharedData::new(0, 0, &original_program_owner);
|
2019-03-05 11:11:05 -08:00
|
|
|
let unchanged_account = owned_account.clone();
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account.clone()), (owned_key, owned_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(SystemError::AccountAlreadyInUse.into()),
|
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1], unchanged_account);
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2020-01-15 13:03:22 -08:00
|
|
|
// Attempt to create system account in account that already has data
|
2022-02-14 15:00:49 -08:00
|
|
|
let owned_account = AccountSharedData::new(0, 1, &Pubkey::default());
|
|
|
|
let unchanged_account = owned_account.clone();
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account.clone()), (owned_key, owned_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(SystemError::AccountAlreadyInUse.into()),
|
|
|
|
super::process_instruction,
|
2019-10-10 16:43:49 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1], unchanged_account);
|
2020-01-15 13:03:22 -08:00
|
|
|
|
2020-05-22 16:39:01 -07:00
|
|
|
// Attempt to create an account that already has lamports
|
2022-02-14 15:00:49 -08:00
|
|
|
let owned_account = AccountSharedData::new(1, 0, &Pubkey::default());
|
|
|
|
let unchanged_account = owned_account.clone();
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (owned_key, owned_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(SystemError::AccountAlreadyInUse.into()),
|
|
|
|
super::process_instruction,
|
2020-01-15 13:03:22 -08:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1], unchanged_account);
|
2019-03-05 13:43:05 -08:00
|
|
|
}
|
2019-03-05 11:11:05 -08:00
|
|
|
|
2019-10-19 18:23:27 -07:00
|
|
|
#[test]
|
|
|
|
fn test_create_unsigned() {
|
|
|
|
// Attempt to create an account without signing the transfer
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
2021-11-04 13:47:32 -07:00
|
|
|
let owned_key = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let owned_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2019-11-08 02:27:35 -08:00
|
|
|
// Haven't signed from account
|
2022-02-14 15:00:49 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![
|
|
|
|
(from, from_account.clone()),
|
|
|
|
(owned_key, owned_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2019-11-08 02:27:35 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Haven't signed to account
|
2022-02-14 15:00:49 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account.clone()), (owned_key, owned_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
|
|
|
|
2021-06-05 16:45:45 -07:00
|
|
|
// Don't support unsigned creation with zero lamports (ephemeral account)
|
2022-02-14 15:00:49 -08:00
|
|
|
let owned_account = AccountSharedData::new(0, 0, &Pubkey::default());
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (owned_key, owned_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: owned_key,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:27:49 -07:00
|
|
|
#[test]
|
2021-05-17 08:30:48 -07:00
|
|
|
fn test_create_sysvar_invalid_id_with_feature() {
|
2019-06-06 19:27:49 -07:00
|
|
|
// Attempt to create system account in account already owned by another program
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
2021-11-04 13:47:32 -07:00
|
|
|
let to = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(0, 0, &system_program::id());
|
2020-01-17 09:29:15 -08:00
|
|
|
|
2019-07-12 16:38:15 -07:00
|
|
|
// fail to create a sysvar::id() owned account
|
2022-02-14 15:00:49 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: sysvar::id(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2019-10-10 16:43:49 -07:00
|
|
|
);
|
2021-05-17 08:30:48 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:43:05 -08:00
|
|
|
#[test]
|
2019-03-14 09:48:27 -07:00
|
|
|
fn test_create_data_populated() {
|
|
|
|
// Attempt to create system account in account with populated data
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
2021-11-04 13:47:32 -07:00
|
|
|
let populated_key = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let populated_account = AccountSharedData::from(Account {
|
2019-03-14 09:48:27 -07:00
|
|
|
data: vec![0, 1, 2, 3],
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
2022-02-14 15:00:49 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 50,
|
|
|
|
space: 2,
|
|
|
|
owner: new_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(from, from_account), (populated_key, populated_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: populated_key,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(SystemError::AccountAlreadyInUse.into()),
|
|
|
|
super::process_instruction,
|
2019-10-10 16:43:49 -07:00
|
|
|
);
|
2019-03-05 11:11:05 -08:00
|
|
|
}
|
|
|
|
|
2020-01-03 16:34:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_create_from_account_is_nonce_fail() {
|
2021-11-04 13:47:32 -07:00
|
|
|
let nonce = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let nonce_account = AccountSharedData::new_data(
|
|
|
|
42,
|
|
|
|
&nonce::state::Versions::new_current(nonce::State::Initialized(
|
|
|
|
nonce::state::Data::default(),
|
|
|
|
)),
|
|
|
|
&system_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let new = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let new_account = AccountSharedData::new(0, 0, &system_program::id());
|
2020-01-03 16:34:58 -08:00
|
|
|
|
2022-02-14 15:00:49 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
|
|
|
lamports: 42,
|
|
|
|
space: 0,
|
|
|
|
owner: Pubkey::new_unique(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(nonce, nonce_account), (new, new_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: new,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::InvalidArgument),
|
2022-02-14 15:00:49 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:11:05 -08:00
|
|
|
#[test]
|
2020-01-17 09:29:15 -08:00
|
|
|
fn test_assign() {
|
2020-05-18 12:55:41 -07:00
|
|
|
let new_owner = Pubkey::new(&[9; 32]);
|
2021-11-04 13:47:32 -07:00
|
|
|
let pubkey = Pubkey::new_unique();
|
2022-02-18 17:06:33 -08:00
|
|
|
let account = AccountSharedData::new(100, 0, &system_program::id());
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2022-02-18 17:06:33 -08:00
|
|
|
// owner does not change, no signature needed
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Assign {
|
|
|
|
owner: system_program::id(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(pubkey, account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
2021-10-08 02:41:07 -07:00
|
|
|
|
2022-02-18 17:06:33 -08:00
|
|
|
// owner does change, signature needed
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Assign { owner: new_owner }).unwrap(),
|
|
|
|
vec![(pubkey, account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2020-01-15 15:13:11 -08:00
|
|
|
);
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Assign { owner: new_owner }).unwrap(),
|
2022-02-18 17:06:33 -08:00
|
|
|
vec![(pubkey, account.clone())],
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2019-10-19 18:23:27 -07:00
|
|
|
);
|
2019-03-05 11:11:05 -08:00
|
|
|
|
2022-02-18 17:06:33 -08:00
|
|
|
// assign to sysvar instead of system_program
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Assign {
|
|
|
|
owner: sysvar::id(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
vec![(pubkey, account)],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2021-05-17 08:30:48 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:43:32 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_bogus_instruction() {
|
|
|
|
// Attempt to assign with no accounts
|
|
|
|
let instruction = SystemInstruction::Assign {
|
2021-11-04 13:47:32 -07:00
|
|
|
owner: Pubkey::new_unique(),
|
2019-10-10 15:43:32 -07:00
|
|
|
};
|
|
|
|
let data = serialize(&instruction).unwrap();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&data,
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2019-10-10 15:43:32 -07:00
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
// Attempt to transfer with no destination
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &system_program::id());
|
2019-10-10 15:43:32 -07:00
|
|
|
let instruction = SystemInstruction::Transfer { lamports: 0 };
|
|
|
|
let data = serialize(&instruction).unwrap();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&data,
|
|
|
|
vec![(from, from_account)],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2019-10-10 15:43:32 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 11:11:05 -08:00
|
|
|
#[test]
|
2019-04-23 12:30:42 -07:00
|
|
|
fn test_transfer_lamports() {
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new(100, 0, &Pubkey::new(&[2; 32])); // account owner should not matter
|
2020-05-30 19:12:48 -07:00
|
|
|
let to = Pubkey::new(&[3; 32]);
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(1, 0, &to); // account owner should not matter
|
|
|
|
let transaction_accounts = vec![(from, from_account), (to, to_account)];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Success case
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Transfer { lamports: 50 }).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 50);
|
|
|
|
assert_eq!(accounts[1].lamports(), 51);
|
|
|
|
|
|
|
|
// Attempt to move more lamports than from_account has
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Transfer { lamports: 101 }).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(SystemError::ResultWithNegativeLamports.into()),
|
|
|
|
super::process_instruction,
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1].lamports(), 1);
|
2019-10-19 18:23:27 -07:00
|
|
|
|
2021-06-05 16:45:45 -07:00
|
|
|
// test signed transfer of zero
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Transfer { lamports: 0 }).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1].lamports(), 1);
|
2021-06-05 16:45:45 -07:00
|
|
|
|
|
|
|
// test unsigned transfer of zero
|
2022-02-14 15:00:49 -08:00
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Transfer { lamports: 0 }).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
super::process_instruction,
|
2021-06-05 16:45:45 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[1].lamports(), 1);
|
2018-12-03 13:32:31 -08:00
|
|
|
}
|
2019-03-16 16:37:18 -07:00
|
|
|
|
2020-08-21 11:28:01 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_with_seed() {
|
2021-11-04 13:47:32 -07:00
|
|
|
let base = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let base_account = AccountSharedData::new(100, 0, &Pubkey::new(&[2; 32])); // account owner should not matter
|
|
|
|
let from_seed = "42".to_string();
|
2020-08-21 11:28:01 -07:00
|
|
|
let from_owner = system_program::id();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from = Pubkey::create_with_seed(&base, from_seed.as_str(), &from_owner).unwrap();
|
|
|
|
let from_account = AccountSharedData::new(100, 0, &Pubkey::new(&[2; 32])); // account owner should not matter
|
2020-08-21 11:28:01 -07:00
|
|
|
let to = Pubkey::new(&[3; 32]);
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(1, 0, &to); // account owner should not matter
|
|
|
|
let transaction_accounts =
|
|
|
|
vec![(from, from_account), (base, base_account), (to, to_account)];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: base,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Success case
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::TransferWithSeed {
|
|
|
|
lamports: 50,
|
|
|
|
from_seed: from_seed.clone(),
|
|
|
|
from_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
2020-08-21 11:28:01 -07:00
|
|
|
);
|
2022-02-14 15:00:49 -08:00
|
|
|
assert_eq!(accounts[0].lamports(), 50);
|
|
|
|
assert_eq!(accounts[2].lamports(), 51);
|
2020-08-21 11:28:01 -07:00
|
|
|
|
2022-02-14 15:00:49 -08:00
|
|
|
// Attempt to move more lamports than from_account has
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::TransferWithSeed {
|
|
|
|
lamports: 101,
|
|
|
|
from_seed: from_seed.clone(),
|
|
|
|
from_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(SystemError::ResultWithNegativeLamports.into()),
|
|
|
|
super::process_instruction,
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[2].lamports(), 1);
|
|
|
|
|
|
|
|
// Test unsigned transfer of zero
|
|
|
|
let accounts = process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::TransferWithSeed {
|
|
|
|
lamports: 0,
|
|
|
|
from_seed,
|
|
|
|
from_owner,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
super::process_instruction,
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 100);
|
|
|
|
assert_eq!(accounts[2].lamports(), 1);
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
|
|
|
|
2020-01-03 16:34:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_lamports_from_nonce_account_fail() {
|
2021-11-04 13:47:32 -07:00
|
|
|
let from = Pubkey::new_unique();
|
2022-02-14 15:00:49 -08:00
|
|
|
let from_account = AccountSharedData::new_data(
|
|
|
|
100,
|
|
|
|
&nonce::state::Versions::new_current(nonce::State::Initialized(nonce::state::Data {
|
|
|
|
authority: from,
|
|
|
|
..nonce::state::Data::default()
|
|
|
|
})),
|
|
|
|
&system_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-03 16:34:58 -08:00
|
|
|
assert_eq!(
|
2022-02-14 15:00:49 -08:00
|
|
|
get_system_account_kind(&from_account),
|
2020-01-03 16:34:58 -08:00
|
|
|
Some(SystemAccountKind::Nonce)
|
|
|
|
);
|
2020-05-30 19:12:48 -07:00
|
|
|
let to = Pubkey::new(&[3; 32]);
|
2022-02-14 15:00:49 -08:00
|
|
|
let to_account = AccountSharedData::new(1, 0, &to); // account owner should not matter
|
|
|
|
|
|
|
|
process_instruction(
|
|
|
|
&bincode::serialize(&SystemInstruction::Transfer { lamports: 50 }).unwrap(),
|
|
|
|
vec![(from, from_account), (to, to_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: from,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::InvalidArgument),
|
2022-02-14 15:00:49 -08:00
|
|
|
super::process_instruction,
|
|
|
|
);
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
#[test]
|
|
|
|
fn test_allocate() {
|
|
|
|
let (genesis_config, mint_keypair) = create_genesis_config(100);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2020-01-17 09:29:15 -08:00
|
|
|
let bank_client = BankClient::new(bank);
|
|
|
|
|
|
|
|
let alice_keypair = Keypair::new();
|
|
|
|
let alice_pubkey = alice_keypair.pubkey();
|
|
|
|
let seed = "seed";
|
2021-11-04 13:47:32 -07:00
|
|
|
let owner = Pubkey::new_unique();
|
2020-05-18 12:55:41 -07:00
|
|
|
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
|
2020-01-17 09:29:15 -08:00
|
|
|
|
|
|
|
bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
|
2020-01-17 09:29:15 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2020-06-24 13:52:38 -07:00
|
|
|
let allocate_with_seed = Message::new(
|
2020-03-11 14:37:23 -07:00
|
|
|
&[system_instruction::allocate_with_seed(
|
2020-01-17 09:29:15 -08:00
|
|
|
&alice_with_seed,
|
|
|
|
&alice_pubkey,
|
|
|
|
seed,
|
|
|
|
2,
|
2020-05-18 12:55:41 -07:00
|
|
|
&owner,
|
2020-01-17 09:29:15 -08:00
|
|
|
)],
|
|
|
|
Some(&alice_pubkey),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.send_and_confirm_message(&[&alice_keypair], allocate_with_seed)
|
2020-01-17 09:29:15 -08:00
|
|
|
.is_ok());
|
|
|
|
|
|
|
|
let allocate = system_instruction::allocate(&alice_pubkey, 2);
|
|
|
|
|
|
|
|
assert!(bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.send_and_confirm_instruction(&alice_keypair, allocate)
|
2020-01-17 09:29:15 -08:00
|
|
|
.is_ok());
|
|
|
|
}
|
|
|
|
|
2020-03-05 08:14:40 -08:00
|
|
|
fn with_create_zero_lamport<F>(callback: F)
|
|
|
|
where
|
2020-06-08 17:38:14 -07:00
|
|
|
F: Fn(&Bank),
|
2020-03-05 08:14:40 -08:00
|
|
|
{
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let alice_keypair = Keypair::new();
|
|
|
|
let bob_keypair = Keypair::new();
|
|
|
|
|
|
|
|
let alice_pubkey = alice_keypair.pubkey();
|
|
|
|
let bob_pubkey = bob_keypair.pubkey();
|
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
let program = Pubkey::new_unique();
|
|
|
|
let collector = Pubkey::new_unique();
|
2020-03-05 08:14:40 -08:00
|
|
|
|
|
|
|
let mint_lamports = 10000;
|
|
|
|
let len1 = 123;
|
|
|
|
let len2 = 456;
|
|
|
|
|
|
|
|
// create initial bank and fund the alice account
|
|
|
|
let (genesis_config, mint_keypair) = create_genesis_config(mint_lamports);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Arc::new(Bank::new_for_tests(&genesis_config));
|
2020-03-05 08:14:40 -08:00
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.transfer_and_confirm(mint_lamports, &mint_keypair, &alice_pubkey)
|
2020-03-05 08:14:40 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// create zero-lamports account to be cleaned
|
|
|
|
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
|
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 0, len1, &program);
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(&[ix], Some(&alice_keypair.pubkey()));
|
2020-06-24 20:35:38 -07:00
|
|
|
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
|
2020-03-05 08:14:40 -08:00
|
|
|
assert!(r.is_ok());
|
|
|
|
|
|
|
|
// transfer some to bogus pubkey just to make previous bank (=slot) really cleanable
|
|
|
|
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
|
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
bank_client
|
2021-11-04 13:47:32 -07:00
|
|
|
.transfer_and_confirm(50, &alice_keypair, &Pubkey::new_unique())
|
2020-03-05 08:14:40 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2020-09-28 16:04:46 -07:00
|
|
|
// super fun time; callback chooses to .clean_accounts(None) or not
|
2020-03-05 08:14:40 -08:00
|
|
|
callback(&*bank);
|
|
|
|
|
|
|
|
// create a normal account at the same pubkey as the zero-lamports account
|
|
|
|
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
|
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 1, len2, &program);
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(&[ix], Some(&alice_pubkey));
|
2020-06-24 20:35:38 -07:00
|
|
|
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
|
2020-03-05 08:14:40 -08:00
|
|
|
assert!(r.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_zero_lamport_with_clean() {
|
|
|
|
with_create_zero_lamport(|bank| {
|
2021-01-11 17:00:23 -08:00
|
|
|
bank.freeze();
|
2020-03-05 08:14:40 -08:00
|
|
|
bank.squash();
|
2021-01-11 17:00:23 -08:00
|
|
|
bank.force_flush_accounts_cache();
|
2020-03-05 08:14:40 -08:00
|
|
|
// do clean and assert that it actually did its job
|
2021-08-20 14:23:43 -07:00
|
|
|
assert_eq!(3, bank.get_snapshot_storages(None).len());
|
2021-08-12 13:56:08 -07:00
|
|
|
bank.clean_accounts(false, false, None);
|
2021-08-20 14:23:43 -07:00
|
|
|
assert_eq!(2, bank.get_snapshot_storages(None).len());
|
2020-03-05 08:14:40 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_zero_lamport_without_clean() {
|
|
|
|
with_create_zero_lamport(|_| {
|
|
|
|
// just do nothing; this should behave identically with test_create_zero_lamport_with_clean
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:29:15 -08:00
|
|
|
#[test]
|
|
|
|
fn test_assign_with_seed() {
|
|
|
|
let (genesis_config, mint_keypair) = create_genesis_config(100);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2020-01-17 09:29:15 -08:00
|
|
|
let bank_client = BankClient::new(bank);
|
|
|
|
|
|
|
|
let alice_keypair = Keypair::new();
|
|
|
|
let alice_pubkey = alice_keypair.pubkey();
|
|
|
|
let seed = "seed";
|
2021-11-04 13:47:32 -07:00
|
|
|
let owner = Pubkey::new_unique();
|
2020-05-18 12:55:41 -07:00
|
|
|
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
|
2020-01-17 09:29:15 -08:00
|
|
|
|
|
|
|
bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
|
2020-01-17 09:29:15 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2020-06-24 13:52:38 -07:00
|
|
|
let assign_with_seed = Message::new(
|
2020-03-11 14:37:23 -07:00
|
|
|
&[system_instruction::assign_with_seed(
|
2020-01-17 09:29:15 -08:00
|
|
|
&alice_with_seed,
|
|
|
|
&alice_pubkey,
|
|
|
|
seed,
|
2020-05-18 12:55:41 -07:00
|
|
|
&owner,
|
2020-01-17 09:29:15 -08:00
|
|
|
)],
|
|
|
|
Some(&alice_pubkey),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.send_and_confirm_message(&[&alice_keypair], assign_with_seed)
|
2020-01-17 09:29:15 -08:00
|
|
|
.is_ok());
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:37:18 -07:00
|
|
|
#[test]
|
|
|
|
fn test_system_unsigned_transaction() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let (genesis_config, alice_keypair) = create_genesis_config(100);
|
2019-03-27 06:34:01 -07:00
|
|
|
let alice_pubkey = alice_keypair.pubkey();
|
|
|
|
let mallory_keypair = Keypair::new();
|
|
|
|
let mallory_pubkey = mallory_keypair.pubkey();
|
2019-03-16 16:37:18 -07:00
|
|
|
|
|
|
|
// Fund to account to bypass AccountNotFound error
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2019-04-11 11:29:59 -07:00
|
|
|
let bank_client = BankClient::new(bank);
|
2019-03-27 06:34:01 -07:00
|
|
|
bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.transfer_and_confirm(50, &alice_keypair, &mallory_pubkey)
|
2019-03-27 06:34:01 -07:00
|
|
|
.unwrap();
|
2019-03-16 16:37:18 -07:00
|
|
|
|
|
|
|
// Erroneously sign transaction with recipient account key
|
|
|
|
// No signature case is tested by bank `test_zero_signatures()`
|
2019-03-19 12:03:20 -07:00
|
|
|
let account_metas = vec![
|
2019-03-19 14:25:48 -07:00
|
|
|
AccountMeta::new(alice_pubkey, false),
|
|
|
|
AccountMeta::new(mallory_pubkey, true),
|
2019-03-19 12:03:20 -07:00
|
|
|
];
|
2021-03-03 21:46:48 -08:00
|
|
|
let malicious_instruction = Instruction::new_with_bincode(
|
2019-03-16 16:37:18 -07:00
|
|
|
system_program::id(),
|
2019-04-02 20:52:07 -07:00
|
|
|
&SystemInstruction::Transfer { lamports: 10 },
|
2019-03-19 12:03:20 -07:00
|
|
|
account_metas,
|
2019-03-23 04:15:15 -07:00
|
|
|
);
|
2019-03-16 16:37:18 -07:00
|
|
|
assert_eq!(
|
2019-04-03 20:40:29 -07:00
|
|
|
bank_client
|
2020-06-24 20:35:38 -07:00
|
|
|
.send_and_confirm_instruction(&mallory_keypair, malicious_instruction)
|
2019-04-03 20:40:29 -07:00
|
|
|
.unwrap_err()
|
|
|
|
.unwrap(),
|
|
|
|
TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature)
|
2019-03-16 16:37:18 -07:00
|
|
|
);
|
2019-04-03 20:40:29 -07:00
|
|
|
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 50);
|
|
|
|
assert_eq!(bank_client.get_balance(&mallory_pubkey).unwrap(), 50);
|
2019-03-16 16:37:18 -07:00
|
|
|
}
|
2020-01-03 16:34:58 -08:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn process_nonce_instruction(
|
|
|
|
instruction: Instruction,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) -> Vec<AccountSharedData> {
|
|
|
|
let transaction_accounts = instruction
|
2020-01-03 16:34:58 -08:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2021-12-17 05:01:12 -08:00
|
|
|
.map(|meta| {
|
|
|
|
#[allow(deprecated)]
|
|
|
|
(
|
|
|
|
meta.pubkey,
|
|
|
|
if sysvar::recent_blockhashes::check_id(&meta.pubkey) {
|
|
|
|
create_default_recent_blockhashes_account()
|
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
|
|
|
account::create_account_shared_data_for_test(&Rent::free())
|
|
|
|
} else {
|
|
|
|
AccountSharedData::new(0, 0, &Pubkey::new_unique())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2020-01-03 16:34:58 -08:00
|
|
|
.collect();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&instruction.data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction.accounts,
|
|
|
|
expected_result,
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
)
|
2020-01-03 16:34:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_nonce_ix_no_acc_data_fail() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let none_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_nonce_instruction(
|
|
|
|
system_instruction::advance_nonce_account(&none_address, &none_address),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_nonce_ix_no_keyed_accs_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_nonce_ix_only_nonce_acc_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
let pubkey = Pubkey::new_unique();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
|
|
|
|
vec![(pubkey, create_default_account())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
}],
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_nonce_ix_ok() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
|
|
|
let accounts = process_instruction(
|
2021-11-16 15:01:00 -08:00
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(blockhash_id, create_default_recent_blockhashes_account()),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
2020-01-03 16:34:58 -08:00
|
|
|
],
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2021-11-04 13:47:32 -07:00
|
|
|
let blockhash = hash(&serialize(&0).unwrap());
|
|
|
|
#[allow(deprecated)]
|
2021-12-17 05:01:12 -08:00
|
|
|
let new_recent_blockhashes_account =
|
2021-03-24 23:23:20 -07:00
|
|
|
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
|
2021-11-04 13:47:32 -07:00
|
|
|
vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES]
|
2021-10-22 14:32:40 -07:00
|
|
|
.into_iter(),
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2022-02-03 04:03:50 -08:00
|
|
|
process_instruction(
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
|
|
|
|
vec![
|
|
|
|
(nonce_address, accounts[0].clone()),
|
|
|
|
(blockhash_id, new_recent_blockhashes_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
2021-11-04 13:47:32 -07:00
|
|
|
},
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 16:34:58 -08:00
|
|
|
Ok(()),
|
2022-04-01 06:48:05 -07:00
|
|
|
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
2021-12-17 05:01:12 -08:00
|
|
|
invoke_context.blockhash = hash(&serialize(&0).unwrap());
|
2022-04-01 06:48:05 -07:00
|
|
|
super::process_instruction(first_instruction_account, invoke_context)
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_withdraw_ix_no_acc_data_fail() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_nonce_instruction(
|
|
|
|
system_instruction::withdraw_nonce_account(
|
2021-11-16 15:01:00 -08:00
|
|
|
&nonce_address,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&nonce_address,
|
2020-01-03 16:34:58 -08:00
|
|
|
1,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_withdraw_ix_no_keyed_accs_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_withdraw_ix_only_nonce_acc_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
|
|
|
|
vec![(nonce_address, create_default_account())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
}],
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_withdraw_ix_ok() {
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
let pubkey = Pubkey::new_unique();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(pubkey, create_default_account()),
|
|
|
|
(blockhash_id, create_default_recent_blockhashes_account()),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 16:34:58 -08:00
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_initialize_ix_no_keyed_accs_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_initialize_ix_only_nonce_acc_fail() {
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
|
|
|
vec![(nonce_address, nonce_account)],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
}],
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_initialize_ix_ok() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(blockhash_id, create_default_recent_blockhashes_account()),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 16:34:58 -08:00
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_authorize_ix_ok() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
|
|
|
let accounts = process_instruction(
|
2021-11-16 15:01:00 -08:00
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(blockhash_id, create_default_recent_blockhashes_account()),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2020-01-03 16:34:58 -08:00
|
|
|
],
|
2021-12-17 05:01:12 -08:00
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::AuthorizeNonceAccount(nonce_address)).unwrap(),
|
|
|
|
vec![(nonce_address, accounts[0].clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
}],
|
2020-01-03 16:34:58 -08:00
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2020-01-03 16:34:58 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_authorize_bad_account_data_fail() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
process_nonce_instruction(
|
|
|
|
system_instruction::authorize_nonce_account(
|
2021-11-16 15:01:00 -08:00
|
|
|
&nonce_address,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&nonce_address,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2020-01-03 16:34:58 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_system_account_kind_system_ok() {
|
2021-03-09 13:06:07 -08:00
|
|
|
let system_account = AccountSharedData::default();
|
2020-01-03 16:34:58 -08:00
|
|
|
assert_eq!(
|
|
|
|
get_system_account_kind(&system_account),
|
|
|
|
Some(SystemAccountKind::System)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_system_account_kind_nonce_ok() {
|
2021-03-09 13:06:07 -08:00
|
|
|
let nonce_account = AccountSharedData::new_data(
|
2020-01-03 16:34:58 -08:00
|
|
|
42,
|
2020-03-05 06:40:26 -08:00
|
|
|
&nonce::state::Versions::new_current(nonce::State::Initialized(
|
|
|
|
nonce::state::Data::default(),
|
|
|
|
)),
|
2020-01-03 16:34:58 -08:00
|
|
|
&system_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
get_system_account_kind(&nonce_account),
|
|
|
|
Some(SystemAccountKind::Nonce)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_system_account_kind_uninitialized_nonce_account_fail() {
|
|
|
|
assert_eq!(
|
2020-10-28 22:01:07 -07:00
|
|
|
get_system_account_kind(&nonce_account::create_account(42).borrow()),
|
2020-01-03 16:34:58 -08:00
|
|
|
None
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_system_account_kind_system_owner_nonzero_nonnonce_data_fail() {
|
2021-03-09 13:06:07 -08:00
|
|
|
let other_data_account =
|
|
|
|
AccountSharedData::new_data(42, b"other", &Pubkey::default()).unwrap();
|
2020-01-03 16:34:58 -08:00
|
|
|
assert_eq!(get_system_account_kind(&other_data_account), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-03 17:00:39 -08:00
|
|
|
fn test_get_system_account_kind_nonsystem_owner_with_nonce_data_fail() {
|
2021-03-09 13:06:07 -08:00
|
|
|
let nonce_account = AccountSharedData::new_data(
|
2020-01-03 16:34:58 -08:00
|
|
|
42,
|
2020-03-05 06:40:26 -08:00
|
|
|
&nonce::state::Versions::new_current(nonce::State::Initialized(
|
|
|
|
nonce::state::Data::default(),
|
|
|
|
)),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-01-03 16:34:58 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(get_system_account_kind(&nonce_account), None);
|
|
|
|
}
|
2021-07-29 01:50:20 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonce_initialize_with_empty_recent_blockhashes_fail() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
#[allow(deprecated)]
|
2021-12-17 05:01:12 -08:00
|
|
|
let new_recent_blockhashes_account =
|
2021-07-29 01:50:20 -07:00
|
|
|
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
|
|
|
|
vec![].into_iter(),
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(blockhash_id, new_recent_blockhashes_account),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(NonceError::NoRecentBlockhashes.into()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-07-29 01:50:20 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonce_advance_with_empty_recent_blockhashes_fail() {
|
2021-11-16 15:01:00 -08:00
|
|
|
let nonce_address = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let blockhash_id = sysvar::recent_blockhashes::id();
|
|
|
|
let accounts = process_instruction(
|
2021-11-16 15:01:00 -08:00
|
|
|
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(nonce_address, nonce_account),
|
|
|
|
(blockhash_id, create_default_recent_blockhashes_account()),
|
|
|
|
(sysvar::rent::id(), create_default_rent_account()),
|
2021-07-29 01:50:20 -07:00
|
|
|
],
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: sysvar::rent::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
2022-02-03 04:03:50 -08:00
|
|
|
super::process_instruction,
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2021-11-04 13:47:32 -07:00
|
|
|
#[allow(deprecated)]
|
2021-12-17 05:01:12 -08:00
|
|
|
let new_recent_blockhashes_account =
|
2021-07-29 01:50:20 -07:00
|
|
|
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
|
|
|
|
vec![].into_iter(),
|
2021-12-17 05:01:12 -08:00
|
|
|
);
|
2022-02-03 04:03:50 -08:00
|
|
|
process_instruction(
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
|
|
|
|
vec![
|
|
|
|
(nonce_address, accounts[0].clone()),
|
|
|
|
(blockhash_id, new_recent_blockhashes_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: nonce_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: true,
|
2021-11-04 13:47:32 -07:00
|
|
|
},
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: blockhash_id,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-29 01:50:20 -07:00
|
|
|
Err(NonceError::NoRecentBlockhashes.into()),
|
2022-04-01 06:48:05 -07:00
|
|
|
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
2021-12-17 05:01:12 -08:00
|
|
|
invoke_context.blockhash = hash(&serialize(&0).unwrap());
|
2022-04-01 06:48:05 -07:00
|
|
|
super::process_instruction(first_instruction_account, invoke_context)
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
2021-07-29 01:50:20 -07:00
|
|
|
);
|
|
|
|
}
|
2018-12-03 13:32:31 -08:00
|
|
|
}
|