@ -5,6 +5,7 @@ use crate::bank::{HashAgeKind, TransactionProcessResult};
|
||||
use crate::blockhash_queue::BlockhashQueue;
|
||||
use crate::message_processor::has_duplicates;
|
||||
use crate::rent_collector::RentCollector;
|
||||
use crate::system_instruction_processor::{get_system_account_kind, SystemAccountKind};
|
||||
use log::*;
|
||||
use rayon::slice::ParallelSliceMut;
|
||||
use solana_metrics::inc_new_counter_error;
|
||||
@ -12,8 +13,8 @@ use solana_sdk::account::Account;
|
||||
use solana_sdk::bank_hash::BankHash;
|
||||
use solana_sdk::clock::Slot;
|
||||
use solana_sdk::native_loader;
|
||||
use solana_sdk::nonce_state::NonceState;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::transaction::Result;
|
||||
use solana_sdk::transaction::{Transaction, TransactionError};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
@ -133,15 +134,24 @@ impl Accounts {
|
||||
if accounts.is_empty() || accounts[0].lamports == 0 {
|
||||
error_counters.account_not_found += 1;
|
||||
Err(TransactionError::AccountNotFound)
|
||||
} else if accounts[0].owner != system_program::id() {
|
||||
error_counters.invalid_account_for_fee += 1;
|
||||
Err(TransactionError::InvalidAccountForFee)
|
||||
} else if accounts[0].lamports < fee {
|
||||
error_counters.insufficient_funds += 1;
|
||||
Err(TransactionError::InsufficientFundsForFee)
|
||||
} else {
|
||||
accounts[0].lamports -= fee;
|
||||
Ok((accounts, tx_rent))
|
||||
let min_balance = match get_system_account_kind(&accounts[0]).ok_or_else(|| {
|
||||
error_counters.invalid_account_for_fee += 1;
|
||||
TransactionError::InvalidAccountForFee
|
||||
})? {
|
||||
SystemAccountKind::System => 0,
|
||||
SystemAccountKind::Nonce => {
|
||||
rent_collector.rent.minimum_balance(NonceState::size())
|
||||
}
|
||||
};
|
||||
|
||||
if accounts[0].lamports < fee + min_balance {
|
||||
error_counters.insufficient_funds += 1;
|
||||
Err(TransactionError::InsufficientFundsForFee)
|
||||
} else {
|
||||
accounts[0].lamports -= fee;
|
||||
Ok((accounts, tx_rent))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -627,14 +637,19 @@ mod tests {
|
||||
use crate::accounts_db::tests::copy_append_vecs;
|
||||
use crate::accounts_db::{get_temp_accounts_paths, AccountsDBSerialize};
|
||||
use crate::bank::HashAgeKind;
|
||||
use crate::rent_collector::RentCollector;
|
||||
use bincode::serialize_into;
|
||||
use rand::{thread_rng, Rng};
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::epoch_schedule::EpochSchedule;
|
||||
use solana_sdk::fee_calculator::FeeCalculator;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::instruction::CompiledInstruction;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::nonce_state;
|
||||
use solana_sdk::rent::Rent;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::sysvar;
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use std::io::Cursor;
|
||||
@ -642,10 +657,11 @@ mod tests {
|
||||
use std::{thread, time};
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn load_accounts_with_fee(
|
||||
fn load_accounts_with_fee_and_rent(
|
||||
tx: Transaction,
|
||||
ka: &Vec<(Pubkey, Account)>,
|
||||
fee_calculator: &FeeCalculator,
|
||||
rent_collector: &RentCollector,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<(Result<TransactionLoadResult>, Option<HashAgeKind>)> {
|
||||
let mut hash_queue = BlockhashQueue::new(100);
|
||||
@ -656,7 +672,6 @@ mod tests {
|
||||
}
|
||||
|
||||
let ancestors = vec![(0, 0)].into_iter().collect();
|
||||
let rent_collector = RentCollector::default();
|
||||
let res = accounts.load_accounts(
|
||||
&ancestors,
|
||||
&[tx],
|
||||
@ -664,11 +679,21 @@ mod tests {
|
||||
vec![(Ok(()), Some(HashAgeKind::Extant))],
|
||||
&hash_queue,
|
||||
error_counters,
|
||||
&rent_collector,
|
||||
rent_collector,
|
||||
);
|
||||
res
|
||||
}
|
||||
|
||||
fn load_accounts_with_fee(
|
||||
tx: Transaction,
|
||||
ka: &Vec<(Pubkey, Account)>,
|
||||
fee_calculator: &FeeCalculator,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<(Result<TransactionLoadResult>, Option<HashAgeKind>)> {
|
||||
let rent_collector = RentCollector::default();
|
||||
load_accounts_with_fee_and_rent(tx, ka, fee_calculator, &rent_collector, error_counters)
|
||||
}
|
||||
|
||||
fn load_accounts(
|
||||
tx: Transaction,
|
||||
ka: &Vec<(Pubkey, Account)>,
|
||||
@ -743,7 +768,7 @@ mod tests {
|
||||
let key0 = keypair.pubkey();
|
||||
let key1 = Pubkey::new(&[5u8; 32]);
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let account = Account::new(1, 0, &Pubkey::default());
|
||||
accounts.push((key0, account));
|
||||
|
||||
let account = Account::new(2, 1, &Pubkey::default());
|
||||
@ -779,7 +804,7 @@ mod tests {
|
||||
let keypair = Keypair::new();
|
||||
let key0 = keypair.pubkey();
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let account = Account::new(1, 0, &Pubkey::default());
|
||||
accounts.push((key0, account));
|
||||
|
||||
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
||||
@ -841,6 +866,84 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_accounts_fee_payer_is_nonce() {
|
||||
let mut error_counters = ErrorCounters::default();
|
||||
let rent_collector = RentCollector::new(
|
||||
0,
|
||||
&EpochSchedule::default(),
|
||||
500_000.0,
|
||||
&Rent {
|
||||
lamports_per_byte_year: 42,
|
||||
..Rent::default()
|
||||
},
|
||||
);
|
||||
let min_balance = rent_collector
|
||||
.rent
|
||||
.minimum_balance(nonce_state::NonceState::size());
|
||||
let fee_calculator = FeeCalculator::new(min_balance, 0);
|
||||
let nonce = Keypair::new();
|
||||
let mut accounts = vec![(
|
||||
nonce.pubkey(),
|
||||
Account::new_data(
|
||||
min_balance * 2,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&system_program::id(),
|
||||
)
|
||||
.unwrap(),
|
||||
)];
|
||||
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
||||
let tx = Transaction::new_with_compiled_instructions(
|
||||
&[&nonce],
|
||||
&[],
|
||||
Hash::default(),
|
||||
vec![native_loader::id()],
|
||||
instructions,
|
||||
);
|
||||
|
||||
// Fee leaves min_balance balance succeeds
|
||||
let loaded_accounts = load_accounts_with_fee_and_rent(
|
||||
tx.clone(),
|
||||
&accounts,
|
||||
&fee_calculator,
|
||||
&rent_collector,
|
||||
&mut error_counters,
|
||||
);
|
||||
assert_eq!(loaded_accounts.len(), 1);
|
||||
let (load_res, _hash_age_kind) = &loaded_accounts[0];
|
||||
let (tx_accounts, _loaders, _rents) = load_res.as_ref().unwrap();
|
||||
assert_eq!(tx_accounts[0].lamports, min_balance);
|
||||
|
||||
// Fee leaves zero balance fails
|
||||
accounts[0].1.lamports = min_balance;
|
||||
let loaded_accounts = load_accounts_with_fee_and_rent(
|
||||
tx.clone(),
|
||||
&accounts,
|
||||
&fee_calculator,
|
||||
&rent_collector,
|
||||
&mut error_counters,
|
||||
);
|
||||
assert_eq!(loaded_accounts.len(), 1);
|
||||
let (load_res, _hash_age_kind) = &loaded_accounts[0];
|
||||
assert_eq!(*load_res, Err(TransactionError::InsufficientFundsForFee));
|
||||
|
||||
// Fee leaves non-zero, but sub-min_balance balance fails
|
||||
accounts[0].1.lamports = 3 * min_balance / 2;
|
||||
let loaded_accounts = load_accounts_with_fee_and_rent(
|
||||
tx.clone(),
|
||||
&accounts,
|
||||
&fee_calculator,
|
||||
&rent_collector,
|
||||
&mut error_counters,
|
||||
);
|
||||
assert_eq!(loaded_accounts.len(), 1);
|
||||
let (load_res, _hash_age_kind) = &loaded_accounts[0];
|
||||
assert_eq!(*load_res, Err(TransactionError::InsufficientFundsForFee));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_accounts_no_loaders() {
|
||||
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
||||
@ -850,7 +953,7 @@ mod tests {
|
||||
let key0 = keypair.pubkey();
|
||||
let key1 = Pubkey::new(&[5u8; 32]);
|
||||
|
||||
let mut account = Account::new(1, 1, &Pubkey::default());
|
||||
let mut account = Account::new(1, 0, &Pubkey::default());
|
||||
account.rent_epoch = 1;
|
||||
accounts.push((key0, account));
|
||||
|
||||
@ -899,7 +1002,7 @@ mod tests {
|
||||
let key5 = Pubkey::new(&[9u8; 32]);
|
||||
let key6 = Pubkey::new(&[10u8; 32]);
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let account = Account::new(1, 0, &Pubkey::default());
|
||||
accounts.push((key0, account));
|
||||
|
||||
let mut account = Account::new(40, 1, &Pubkey::default());
|
||||
@ -966,7 +1069,7 @@ mod tests {
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
accounts.push((key0, account));
|
||||
|
||||
let mut account = Account::new(40, 1, &Pubkey::default());
|
||||
let mut account = Account::new(40, 0, &Pubkey::default());
|
||||
account.executable = true;
|
||||
account.owner = Pubkey::default();
|
||||
accounts.push((key1, account));
|
||||
@ -1002,7 +1105,7 @@ mod tests {
|
||||
let key0 = keypair.pubkey();
|
||||
let key1 = Pubkey::new(&[5u8; 32]);
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let account = Account::new(1, 0, &Pubkey::default());
|
||||
accounts.push((key0, account));
|
||||
|
||||
let mut account = Account::new(40, 1, &Pubkey::default());
|
||||
@ -1041,7 +1144,7 @@ mod tests {
|
||||
let key1 = Pubkey::new(&[5u8; 32]);
|
||||
let key2 = Pubkey::new(&[6u8; 32]);
|
||||
|
||||
let mut account = Account::new(1, 1, &Pubkey::default());
|
||||
let mut account = Account::new(1, 0, &Pubkey::default());
|
||||
account.rent_epoch = 1;
|
||||
accounts.push((key0, account));
|
||||
|
||||
|
@ -16,6 +16,7 @@ use crate::{
|
||||
status_cache::{SlotDelta, StatusCache},
|
||||
storage_utils,
|
||||
storage_utils::StorageAccounts,
|
||||
system_instruction_processor::{get_system_account_kind, SystemAccountKind},
|
||||
transaction_batch::TransactionBatch,
|
||||
transaction_utils::OrderedIterator,
|
||||
};
|
||||
@ -37,6 +38,7 @@ use solana_sdk::{
|
||||
hash::{hashv, Hash},
|
||||
inflation::Inflation,
|
||||
native_loader,
|
||||
nonce_state::NonceState,
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signature},
|
||||
slot_hashes::SlotHashes,
|
||||
@ -1495,7 +1497,13 @@ impl Bank {
|
||||
pub fn withdraw(&self, pubkey: &Pubkey, lamports: u64) -> Result<()> {
|
||||
match self.get_account(pubkey) {
|
||||
Some(mut account) => {
|
||||
if lamports > account.lamports {
|
||||
let min_balance = match get_system_account_kind(&account) {
|
||||
Some(SystemAccountKind::Nonce) => {
|
||||
self.rent_collector.rent.minimum_balance(NonceState::size())
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
if lamports + min_balance > account.lamports {
|
||||
return Err(TransactionError::InsufficientFundsForFee);
|
||||
}
|
||||
|
||||
@ -1873,7 +1881,7 @@ mod tests {
|
||||
genesis_config::create_genesis_config,
|
||||
instruction::{CompiledInstruction, Instruction, InstructionError},
|
||||
message::{Message, MessageHeader},
|
||||
nonce_instruction, nonce_state,
|
||||
nonce_state,
|
||||
poh_config::PohConfig,
|
||||
rent::Rent,
|
||||
signature::{Keypair, KeypairUtil},
|
||||
@ -2041,11 +2049,11 @@ mod tests {
|
||||
assert_eq!(bank.last_blockhash(), genesis_config.hash());
|
||||
|
||||
// Initialize credit-debit and credit only accounts
|
||||
let account1 = Account::new(264, 1, &Pubkey::default());
|
||||
let account1 = Account::new(264, 0, &Pubkey::default());
|
||||
let account2 = Account::new(264, 1, &Pubkey::default());
|
||||
let account3 = Account::new(264, 1, &Pubkey::default());
|
||||
let account3 = Account::new(264, 0, &Pubkey::default());
|
||||
let account4 = Account::new(264, 1, &Pubkey::default());
|
||||
let account5 = Account::new(10, 1, &Pubkey::default());
|
||||
let account5 = Account::new(10, 0, &Pubkey::default());
|
||||
let account6 = Account::new(10, 1, &Pubkey::default());
|
||||
|
||||
bank.store_account(&keypair1.pubkey(), &account1);
|
||||
@ -2161,7 +2169,7 @@ mod tests {
|
||||
keypairs[0].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 2,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
@ -2169,7 +2177,7 @@ mod tests {
|
||||
keypairs[1].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 2,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
@ -2177,7 +2185,7 @@ mod tests {
|
||||
keypairs[2].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 2,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
@ -2185,23 +2193,23 @@ mod tests {
|
||||
keypairs[3].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 2,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[4].pubkey(),
|
||||
Account::new(10, 1, &Pubkey::default()),
|
||||
Account::new(10, 0, &Pubkey::default()),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[5].pubkey(),
|
||||
Account::new(10, 1, &Pubkey::default()),
|
||||
Account::new(10, 0, &Pubkey::default()),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[6].pubkey(),
|
||||
Account::new(
|
||||
(2 * generic_rent_due_for_system_account) + 24,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
@ -2210,13 +2218,13 @@ mod tests {
|
||||
keypairs[8].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 2 + 929,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[9].pubkey(),
|
||||
Account::new(10, 1, &Pubkey::default()),
|
||||
Account::new(10, 0, &Pubkey::default()),
|
||||
));
|
||||
|
||||
// Feeding to MockProgram to test read only rent behaviour
|
||||
@ -2224,21 +2232,21 @@ mod tests {
|
||||
keypairs[10].pubkey(),
|
||||
Account::new(
|
||||
generic_rent_due_for_system_account + 3,
|
||||
1,
|
||||
0,
|
||||
&Pubkey::default(),
|
||||
),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[11].pubkey(),
|
||||
Account::new(generic_rent_due_for_system_account + 3, 1, &mock_program_id),
|
||||
Account::new(generic_rent_due_for_system_account + 3, 0, &mock_program_id),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[12].pubkey(),
|
||||
Account::new(generic_rent_due_for_system_account + 3, 1, &mock_program_id),
|
||||
Account::new(generic_rent_due_for_system_account + 3, 0, &mock_program_id),
|
||||
));
|
||||
account_pairs.push((
|
||||
keypairs[13].pubkey(),
|
||||
Account::new(14, 23, &mock_program_id),
|
||||
Account::new(14, 22, &mock_program_id),
|
||||
));
|
||||
|
||||
for account_pair in account_pairs.iter() {
|
||||
@ -2408,7 +2416,7 @@ mod tests {
|
||||
bank.rent_collector.slots_per_year = 192.0;
|
||||
|
||||
let payer = Keypair::new();
|
||||
let payer_account = Account::new(400, 2, &system_program::id());
|
||||
let payer_account = Account::new(400, 0, &system_program::id());
|
||||
bank.store_account(&payer.pubkey(), &payer_account);
|
||||
|
||||
let payee = Keypair::new();
|
||||
@ -2424,9 +2432,9 @@ mod tests {
|
||||
|
||||
let mut total_rent_deducted = 0;
|
||||
|
||||
// 400 - 130(Rent) - 180(Transfer)
|
||||
assert_eq!(bank.get_balance(&payer.pubkey()), 90);
|
||||
total_rent_deducted += 130;
|
||||
// 400 - 128(Rent) - 180(Transfer)
|
||||
assert_eq!(bank.get_balance(&payer.pubkey()), 92);
|
||||
total_rent_deducted += 128;
|
||||
|
||||
// 70 - 70(Rent) + 180(Transfer) - 21(Rent)
|
||||
assert_eq!(bank.get_balance(&payee.pubkey()), 159);
|
||||
@ -2454,26 +2462,30 @@ mod tests {
|
||||
|
||||
// Since, validator 1 and validator 2 has equal smallest stake, it comes down to comparison
|
||||
// between their pubkey.
|
||||
let mut validator_1_portion =
|
||||
((validator_1_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64;
|
||||
if validator_1_pubkey > validator_2_pubkey {
|
||||
validator_1_portion += 1;
|
||||
}
|
||||
let tweak_1 = if validator_1_pubkey > validator_2_pubkey {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let validator_1_portion =
|
||||
((validator_1_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64 + tweak_1;
|
||||
assert_eq!(
|
||||
bank.get_balance(&validator_1_pubkey),
|
||||
validator_1_portion + 42
|
||||
validator_1_portion + 42 - tweak_1,
|
||||
);
|
||||
|
||||
// Since, validator 1 and validator 2 has equal smallest stake, it comes down to comparison
|
||||
// between their pubkey.
|
||||
let mut validator_2_portion =
|
||||
((validator_2_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64;
|
||||
if validator_2_pubkey > validator_1_pubkey {
|
||||
validator_2_portion += 1;
|
||||
}
|
||||
let tweak_2 = if validator_2_pubkey > validator_1_pubkey {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let validator_2_portion =
|
||||
((validator_2_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64 + tweak_2;
|
||||
assert_eq!(
|
||||
bank.get_balance(&validator_2_pubkey),
|
||||
validator_2_portion + 42
|
||||
validator_2_portion + 42 - tweak_2,
|
||||
);
|
||||
|
||||
let validator_3_portion =
|
||||
@ -2521,8 +2533,8 @@ mod tests {
|
||||
})
|
||||
.sum();
|
||||
let (generic_rent_due_for_system_account, _) = bank.rent_collector.rent.due(
|
||||
bank.get_minimum_balance_for_rent_exemption(1) - 1,
|
||||
1,
|
||||
bank.get_minimum_balance_for_rent_exemption(0) - 1,
|
||||
0,
|
||||
slots_elapsed as f64 / bank.rent_collector.slots_per_year,
|
||||
);
|
||||
|
||||
@ -2554,7 +2566,7 @@ mod tests {
|
||||
let t4 = system_transaction::transfer(
|
||||
&keypairs[6],
|
||||
&keypairs[7].pubkey(),
|
||||
49373,
|
||||
48991,
|
||||
genesis_config.hash(),
|
||||
);
|
||||
let t5 = system_transaction::transfer(
|
||||
@ -2594,19 +2606,19 @@ mod tests {
|
||||
|
||||
let mut rent_collected = 0;
|
||||
|
||||
// 49374 - 49372(Rent) - 1(transfer)
|
||||
// 48992 - 48990(Rent) - 1(transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[0].pubkey()), 1);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 49374 - 49372(Rent) - 1(transfer)
|
||||
// 48992 - 48990(Rent) + 1(transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[1].pubkey()), 3);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 49374 - 49372(Rent) - 1(transfer)
|
||||
// 48992 - 48990(Rent) - 1(transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[2].pubkey()), 1);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 49374 - 49372(Rent) - 1(transfer)
|
||||
// 48992 - 48990(Rent) + 1(transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[3].pubkey()), 3);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
@ -2614,11 +2626,11 @@ mod tests {
|
||||
assert_eq!(bank.get_balance(&keypairs[4].pubkey()), 10);
|
||||
assert_eq!(bank.get_balance(&keypairs[5].pubkey()), 10);
|
||||
|
||||
// 98768 - 49372(Rent) - 49373(transfer)
|
||||
// 98004 - 48990(Rent) - 48991(transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[6].pubkey()), 23);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 0 + 49373(transfer) - 917(Rent)
|
||||
// 0 + 48990(transfer) - 917(Rent)
|
||||
assert_eq!(
|
||||
bank.get_balance(&keypairs[7].pubkey()),
|
||||
generic_rent_due_for_system_account + 1 - 917
|
||||
@ -2630,7 +2642,7 @@ mod tests {
|
||||
assert_eq!(account8.rent_epoch, bank.epoch + 1);
|
||||
rent_collected += 917;
|
||||
|
||||
// 50303 - 49372(Rent) - 929(Transfer)
|
||||
// 49921 - 48900(Rent) - 929(Transfer)
|
||||
assert_eq!(bank.get_balance(&keypairs[8].pubkey()), 2);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
@ -2644,15 +2656,15 @@ mod tests {
|
||||
assert_eq!(account10.lamports, 12);
|
||||
rent_collected += 927;
|
||||
|
||||
// 49375 - 49372(Rent)
|
||||
// 48993 - 48990(Rent)
|
||||
assert_eq!(bank.get_balance(&keypairs[10].pubkey()), 3);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 49375 - 49372(Rent) + 1(Addition by program)
|
||||
// 48993 - 48990(Rent) + 1(Addition by program)
|
||||
assert_eq!(bank.get_balance(&keypairs[11].pubkey()), 4);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
// 49375 - 49372(Rent) - 1(Deduction by program)
|
||||
// 48993 - 48990(Rent) - 1(Deduction by program)
|
||||
assert_eq!(bank.get_balance(&keypairs[12].pubkey()), 2);
|
||||
rent_collected += generic_rent_due_for_system_account;
|
||||
|
||||
@ -2996,6 +3008,45 @@ mod tests {
|
||||
assert_eq!(bank.get_balance(&key.pubkey()), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bank_withdraw_from_nonce_account() {
|
||||
let (mut genesis_config, _mint_keypair) = create_genesis_config(100_000);
|
||||
genesis_config.rent.lamports_per_byte_year = 42;
|
||||
let bank = Bank::new(&genesis_config);
|
||||
|
||||
let min_balance =
|
||||
bank.get_minimum_balance_for_rent_exemption(nonce_state::NonceState::size());
|
||||
let nonce = Keypair::new();
|
||||
let nonce_account = Account::new_data(
|
||||
min_balance + 42,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&system_program::id(),
|
||||
)
|
||||
.unwrap();
|
||||
bank.store_account(&nonce.pubkey(), &nonce_account);
|
||||
assert_eq!(bank.get_balance(&nonce.pubkey()), min_balance + 42);
|
||||
|
||||
// Resulting in non-zero, but sub-min_balance balance fails
|
||||
assert_eq!(
|
||||
bank.withdraw(&nonce.pubkey(), min_balance / 2),
|
||||
Err(TransactionError::InsufficientFundsForFee)
|
||||
);
|
||||
assert_eq!(bank.get_balance(&nonce.pubkey()), min_balance + 42);
|
||||
|
||||
// Resulting in exactly rent-exempt balance succeeds
|
||||
bank.withdraw(&nonce.pubkey(), 42).unwrap();
|
||||
assert_eq!(bank.get_balance(&nonce.pubkey()), min_balance);
|
||||
|
||||
// Account closure fails
|
||||
assert_eq!(
|
||||
bank.withdraw(&nonce.pubkey(), min_balance),
|
||||
Err(TransactionError::InsufficientFundsForFee),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bank_tx_fee() {
|
||||
let arbitrary_transfer_amount = 42;
|
||||
@ -4538,7 +4589,7 @@ mod tests {
|
||||
custodian_lamports,
|
||||
)];
|
||||
let nonce_authority = nonce_authority.unwrap_or(nonce_keypair.pubkey());
|
||||
setup_ixs.extend_from_slice(&nonce_instruction::create_nonce_account(
|
||||
setup_ixs.extend_from_slice(&system_instruction::create_nonce_account(
|
||||
&custodian_keypair.pubkey(),
|
||||
&nonce_keypair.pubkey(),
|
||||
&nonce_authority,
|
||||
@ -4588,7 +4639,7 @@ mod tests {
|
||||
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4609,7 +4660,7 @@ mod tests {
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
&[&custodian_keypair, &nonce_keypair],
|
||||
@ -4628,7 +4679,7 @@ mod tests {
|
||||
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
|
||||
let mut tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4651,7 +4702,7 @@ mod tests {
|
||||
let nonce_hash = get_nonce(&bank, &nonce_pubkey).unwrap();
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&missing_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&missing_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4670,7 +4721,7 @@ mod tests {
|
||||
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &nonce_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4680,6 +4731,39 @@ mod tests {
|
||||
assert!(!bank.check_tx_durable_nonce(&tx));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assign_from_nonce_account_fail() {
|
||||
let (genesis_config, _mint_keypair) = create_genesis_config(100_000_000);
|
||||
let bank = Arc::new(Bank::new(&genesis_config));
|
||||
let nonce = Keypair::new();
|
||||
let nonce_account = Account::new_data(
|
||||
42424242,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&system_program::id(),
|
||||
)
|
||||
.unwrap();
|
||||
let blockhash = bank.last_blockhash();
|
||||
bank.store_account(&nonce.pubkey(), &nonce_account);
|
||||
|
||||
let tx = Transaction::new_signed_instructions(
|
||||
&[&nonce],
|
||||
vec![system_instruction::assign(
|
||||
&nonce.pubkey(),
|
||||
&Pubkey::new(&[9u8; 32]),
|
||||
)],
|
||||
blockhash,
|
||||
);
|
||||
|
||||
let expect = Err(TransactionError::InstructionError(
|
||||
0,
|
||||
InstructionError::ModifiedProgramId,
|
||||
));
|
||||
assert_eq!(bank.process_transaction(&tx), expect);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_durable_nonce_transaction() {
|
||||
let (mut bank, _mint_keypair, custodian_keypair, nonce_keypair) = setup_nonce_with_bank(
|
||||
@ -4725,7 +4809,7 @@ mod tests {
|
||||
/* Durable Nonce transfer */
|
||||
let durable_tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4746,7 +4830,7 @@ mod tests {
|
||||
/* Durable Nonce re-use fails */
|
||||
let durable_tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
@ -4770,7 +4854,7 @@ mod tests {
|
||||
|
||||
let durable_tx = Transaction::new_signed_with_payer(
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&custodian_pubkey, &alice_pubkey, 100_000_000),
|
||||
],
|
||||
Some(&custodian_pubkey),
|
||||
|
@ -2,7 +2,6 @@ use solana_sdk::{
|
||||
account::Account,
|
||||
fee_calculator::FeeCalculator,
|
||||
genesis_config::GenesisConfig,
|
||||
nonce_program::solana_nonce_program,
|
||||
pubkey::Pubkey,
|
||||
rent::Rent,
|
||||
signature::{Keypair, KeypairUtil},
|
||||
@ -75,7 +74,6 @@ pub fn create_genesis_config_with_leader(
|
||||
// Bare minimum program set
|
||||
let native_instruction_processors = vec![
|
||||
solana_system_program(),
|
||||
solana_nonce_program(),
|
||||
solana_bpf_loader_program!(),
|
||||
solana_vote_program!(),
|
||||
solana_stake_program!(),
|
||||
|
@ -7,8 +7,6 @@ use solana_sdk::instruction::{CompiledInstruction, InstructionError};
|
||||
use solana_sdk::instruction_processor_utils;
|
||||
use solana_sdk::loader_instruction::LoaderInstruction;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::nonce_instruction;
|
||||
use solana_sdk::nonce_program;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::transaction::TransactionError;
|
||||
@ -200,13 +198,10 @@ pub struct MessageProcessor {
|
||||
|
||||
impl Default for MessageProcessor {
|
||||
fn default() -> Self {
|
||||
let instruction_processors: Vec<(Pubkey, ProcessInstruction)> = vec![
|
||||
(
|
||||
system_program::id(),
|
||||
system_instruction_processor::process_instruction,
|
||||
),
|
||||
(nonce_program::id(), nonce_instruction::process_instruction),
|
||||
];
|
||||
let instruction_processors: Vec<(Pubkey, ProcessInstruction)> = vec![(
|
||||
system_program::id(),
|
||||
system_instruction_processor::process_instruction,
|
||||
)];
|
||||
|
||||
Self {
|
||||
instruction_processors,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use solana_sdk::{
|
||||
account::Account, account_utils::State, hash::Hash, instruction::CompiledInstruction,
|
||||
instruction_processor_utils::limited_deserialize, nonce_instruction::NonceInstruction,
|
||||
nonce_program, nonce_state::NonceState, pubkey::Pubkey, transaction::Transaction,
|
||||
instruction_processor_utils::limited_deserialize, nonce_state::NonceState, pubkey::Pubkey,
|
||||
system_instruction::SystemInstruction, system_program, transaction::Transaction,
|
||||
};
|
||||
|
||||
pub fn transaction_uses_durable_nonce(tx: &Transaction) -> Option<&CompiledInstruction> {
|
||||
@ -12,12 +12,12 @@ pub fn transaction_uses_durable_nonce(tx: &Transaction) -> Option<&CompiledInstr
|
||||
.filter(|maybe_ix| {
|
||||
let prog_id_idx = maybe_ix.program_id_index as usize;
|
||||
match message.account_keys.get(prog_id_idx) {
|
||||
Some(program_id) => nonce_program::check_id(&program_id),
|
||||
Some(program_id) => system_program::check_id(&program_id),
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
.filter(|maybe_ix| match limited_deserialize(&maybe_ix.data) {
|
||||
Ok(NonceInstruction::Nonce) => true,
|
||||
Ok(SystemInstruction::NonceAdvance) => true,
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
@ -44,7 +44,6 @@ mod tests {
|
||||
use super::*;
|
||||
use solana_sdk::{
|
||||
hash::Hash,
|
||||
nonce_instruction,
|
||||
nonce_state::{with_test_keyed_account, NonceAccount},
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, KeypairUtil},
|
||||
@ -61,7 +60,7 @@ mod tests {
|
||||
let tx = Transaction::new_signed_instructions(
|
||||
&[&from_keypair, &nonce_keypair],
|
||||
vec![
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
|
||||
],
|
||||
Hash::default(),
|
||||
@ -99,7 +98,7 @@ mod tests {
|
||||
&[&from_keypair, &nonce_keypair],
|
||||
vec![
|
||||
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
|
||||
nonce_instruction::nonce(&nonce_pubkey, &nonce_pubkey),
|
||||
system_instruction::nonce_advance(&nonce_pubkey, &nonce_pubkey),
|
||||
],
|
||||
Hash::default(),
|
||||
);
|
||||
@ -115,7 +114,7 @@ mod tests {
|
||||
let tx = Transaction::new_signed_instructions(
|
||||
&[&from_keypair, &nonce_keypair],
|
||||
vec![
|
||||
nonce_instruction::withdraw(&nonce_pubkey, &nonce_pubkey, &from_pubkey, 42),
|
||||
system_instruction::nonce_withdraw(&nonce_pubkey, &nonce_pubkey, &from_pubkey, 42),
|
||||
system_instruction::transfer(&from_pubkey, &nonce_pubkey, 42),
|
||||
],
|
||||
Hash::default(),
|
||||
@ -162,7 +161,7 @@ mod tests {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &Rent::free())
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &Rent::free())
|
||||
.unwrap();
|
||||
assert!(verify_nonce(&nonce_account.account, &recent_blockhashes[0]));
|
||||
});
|
||||
@ -186,7 +185,7 @@ mod tests {
|
||||
let recent_blockhashes = create_test_recent_blockhashes(0);
|
||||
let authorized = nonce_account.unsigned_key().clone();
|
||||
nonce_account
|
||||
.initialize(&authorized, &recent_blockhashes, &Rent::free())
|
||||
.nonce_initialize(&authorized, &recent_blockhashes, &Rent::free())
|
||||
.unwrap();
|
||||
assert!(!verify_nonce(
|
||||
&nonce_account.account,
|
||||
|
@ -1,14 +1,17 @@
|
||||
use log::*;
|
||||
|
||||
use solana_sdk::{
|
||||
account::{get_signers, KeyedAccount},
|
||||
account::{get_signers, Account, KeyedAccount},
|
||||
account_utils::State,
|
||||
instruction::InstructionError,
|
||||
instruction_processor_utils::{limited_deserialize, next_keyed_account},
|
||||
nonce_state::{NonceAccount, NonceState},
|
||||
pubkey::Pubkey,
|
||||
system_instruction::{
|
||||
create_address_with_seed, SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH,
|
||||
},
|
||||
system_program, sysvar,
|
||||
system_program,
|
||||
sysvar::{self, recent_blockhashes::RecentBlockhashes, rent::Rent, Sysvar},
|
||||
};
|
||||
|
||||
use std::collections::HashSet;
|
||||
@ -69,9 +72,15 @@ fn finish_create_account(
|
||||
program_id: &Pubkey,
|
||||
) -> Result<(), InstructionError> {
|
||||
// if lamports == 0, the `from` account isn't touched
|
||||
if lamports != 0 && from.signer_key().is_none() {
|
||||
debug!("CreateAccount: `from` must sign transfer");
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
if lamports != 0 {
|
||||
if from.signer_key().is_none() {
|
||||
debug!("CreateAccount: `from` must sign transfer");
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
if !from.account.data.is_empty() {
|
||||
debug!("CreateAccount: `from` must not carry data");
|
||||
return Err(InstructionError::InvalidArgument);
|
||||
}
|
||||
}
|
||||
|
||||
// if it looks like the `to` account is already in use, bail
|
||||
@ -112,6 +121,7 @@ fn finish_create_account(
|
||||
debug!("Assign: program id {} invalid", program_id);
|
||||
return Err(SystemError::InvalidProgramId.into());
|
||||
}
|
||||
|
||||
to.account.owner = *program_id;
|
||||
|
||||
from.account.lamports -= lamports;
|
||||
@ -155,6 +165,11 @@ fn transfer_lamports(
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
if !from.account.data.is_empty() {
|
||||
debug!("Transfer: `from` must not carry data");
|
||||
return Err(InstructionError::InvalidArgument);
|
||||
}
|
||||
|
||||
if lamports > from.account.lamports {
|
||||
debug!(
|
||||
"Transfer: insufficient lamports ({}, need {})",
|
||||
@ -162,6 +177,7 @@ fn transfer_lamports(
|
||||
);
|
||||
return Err(SystemError::ResultWithNegativeLamports.into());
|
||||
}
|
||||
|
||||
from.account.lamports -= lamports;
|
||||
to.account.lamports += lamports;
|
||||
Ok(())
|
||||
@ -220,6 +236,56 @@ pub fn process_instruction(
|
||||
let to = next_keyed_account(keyed_accounts_iter)?;
|
||||
transfer_lamports(from, to, lamports)
|
||||
}
|
||||
SystemInstruction::NonceAdvance => {
|
||||
let me = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
me.nonce_advance(
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
|
||||
&signers,
|
||||
)
|
||||
}
|
||||
SystemInstruction::NonceWithdraw(lamports) => {
|
||||
let me = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
let to = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
me.nonce_withdraw(
|
||||
lamports,
|
||||
to,
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
|
||||
&Rent::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
|
||||
&signers,
|
||||
)
|
||||
}
|
||||
SystemInstruction::NonceInitialize(authorized) => {
|
||||
let me = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
me.nonce_initialize(
|
||||
&authorized,
|
||||
&RecentBlockhashes::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
|
||||
&Rent::from_keyed_account(next_keyed_account(keyed_accounts_iter)?)?,
|
||||
)
|
||||
}
|
||||
SystemInstruction::NonceAuthorize(nonce_authority) => {
|
||||
let me = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
me.nonce_authorize(&nonce_authority, &signers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum SystemAccountKind {
|
||||
System,
|
||||
Nonce,
|
||||
}
|
||||
|
||||
pub fn get_system_account_kind(account: &Account) -> Option<SystemAccountKind> {
|
||||
if system_program::check_id(&account.owner) {
|
||||
if account.data.is_empty() {
|
||||
Some(SystemAccountKind::System)
|
||||
} else if let Ok(NonceState::Initialized(_, _)) = account.state() {
|
||||
Some(SystemAccountKind::Nonce)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@ -232,9 +298,13 @@ mod tests {
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::client::SyncClient;
|
||||
use solana_sdk::genesis_config::create_genesis_config;
|
||||
use solana_sdk::hash::{hash, Hash};
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction, InstructionError};
|
||||
use solana_sdk::nonce_state;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction;
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::sysvar;
|
||||
use solana_sdk::transaction::TransactionError;
|
||||
|
||||
#[test]
|
||||
@ -359,7 +429,7 @@ mod tests {
|
||||
// create account with zero lamports tranferred
|
||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
||||
let from = Pubkey::new_rand();
|
||||
let mut from_account = Account::new(100, 0, &Pubkey::new_rand()); // not from system account
|
||||
let mut from_account = Account::new(100, 1, &Pubkey::new_rand()); // not from system account
|
||||
|
||||
let to = Pubkey::new_rand();
|
||||
let mut to_account = Account::new(0, 0, &Pubkey::default());
|
||||
@ -593,6 +663,29 @@ mod tests {
|
||||
assert_eq!(populated_account, unchanged_account);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_from_account_is_nonce_fail() {
|
||||
let nonce = Pubkey::new_rand();
|
||||
let mut nonce_account = Account::new_data(
|
||||
42,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&system_program::id(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut from = KeyedAccount::new(&nonce, true, &mut nonce_account);
|
||||
let new = Pubkey::new_rand();
|
||||
|
||||
let mut new_account = Account::default();
|
||||
let mut to = KeyedAccount::new(&new, true, &mut new_account);
|
||||
assert_eq!(
|
||||
create_account(&mut from, &mut to, 42, 0, &Pubkey::new_rand()),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assign_account_to_program() {
|
||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
||||
@ -698,6 +791,31 @@ mod tests {
|
||||
assert_eq!(to_account.lamports, 51);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transfer_lamports_from_nonce_account_fail() {
|
||||
let from = Pubkey::new_rand();
|
||||
let mut from_account = Account::new_data(
|
||||
100,
|
||||
&nonce_state::NonceState::Initialized(nonce_state::Meta::new(&from), Hash::default()),
|
||||
&system_program::id(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
get_system_account_kind(&from_account),
|
||||
Some(SystemAccountKind::Nonce)
|
||||
);
|
||||
let to = Pubkey::new_rand();
|
||||
let mut to_account = Account::new(1, 0, &Pubkey::new(&[3; 32])); // account owner should not matter
|
||||
assert_eq!(
|
||||
transfer_lamports(
|
||||
&mut KeyedAccount::new(&from, true, &mut from_account),
|
||||
&mut KeyedAccount::new(&to, false, &mut to_account),
|
||||
50,
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_system_unsigned_transaction() {
|
||||
let (genesis_config, alice_keypair) = create_genesis_config(100);
|
||||
@ -733,4 +851,469 @@ mod tests {
|
||||
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 50);
|
||||
assert_eq!(bank_client.get_balance(&mallory_pubkey).unwrap(), 50);
|
||||
}
|
||||
|
||||
fn process_nonce_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
let mut accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|meta| {
|
||||
if sysvar::recent_blockhashes::check_id(&meta.pubkey) {
|
||||
sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
)
|
||||
} else if sysvar::rent::check_id(&meta.pubkey) {
|
||||
sysvar::rent::create_account(1, &Rent::free())
|
||||
} else {
|
||||
Account::default()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
{
|
||||
let mut keyed_accounts_iter: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.zip(accounts.iter_mut())
|
||||
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
||||
.collect();
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut keyed_accounts_iter,
|
||||
&instruction.data,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_nonce_instruction(&system_instruction::nonce_advance(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default()
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&SystemInstruction::NonceAdvance).unwrap()
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut Account::default(),
|
||||
),],
|
||||
&serialize(&SystemInstruction::NonceAdvance).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceAdvance).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_nonce_ix_ok() {
|
||||
let mut nonce_acc = nonce_state::create_account(1_000_000);
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::rent::id(),
|
||||
false,
|
||||
&mut sysvar::rent::create_account(1, &Rent::free()),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc,),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &hash(&serialize(&0).unwrap())); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceAdvance).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_no_acc_data_fail() {
|
||||
assert_eq!(
|
||||
process_nonce_instruction(&system_instruction::nonce_withdraw(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
1,
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&SystemInstruction::NonceWithdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut Account::default(),
|
||||
),],
|
||||
&serialize(&SystemInstruction::NonceWithdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(&Pubkey::default(), false, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceWithdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_bad_rent_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(&sysvar::rent::id(), false, &mut Account::default(),),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceWithdraw(42)).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_withdraw_ix_ok() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut Account::default(),),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::rent::id(),
|
||||
false,
|
||||
&mut sysvar::rent::create_account(1, &Rent::free())
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceWithdraw(42)).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_no_keyed_accs_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_only_nonce_acc_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_bad_recent_blockhash_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut Account::default(),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_bad_rent_state_fail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(&sysvar::rent::id(), false, &mut Account::default(),),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_initialize_ix_ok() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(
|
||||
&Pubkey::default(),
|
||||
true,
|
||||
&mut nonce_state::create_account(1_000_000),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::rent::id(),
|
||||
false,
|
||||
&mut sysvar::rent::create_account(1, &Rent::free())
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_authorize_ix_ok() {
|
||||
let mut nonce_acc = nonce_state::create_account(1_000_000);
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [
|
||||
KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc),
|
||||
KeyedAccount::new(
|
||||
&sysvar::recent_blockhashes::id(),
|
||||
false,
|
||||
&mut sysvar::recent_blockhashes::create_account_with_data(
|
||||
1,
|
||||
vec![(0u64, &Hash::default()); 32].into_iter(),
|
||||
),
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&sysvar::rent::id(),
|
||||
false,
|
||||
&mut sysvar::rent::create_account(1, &Rent::free()),
|
||||
),
|
||||
],
|
||||
&serialize(&SystemInstruction::NonceInitialize(Pubkey::default())).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&mut [KeyedAccount::new(&Pubkey::default(), true, &mut nonce_acc,),],
|
||||
&serialize(&SystemInstruction::NonceAuthorize(Pubkey::default(),)).unwrap(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_authorize_bad_account_data_fail() {
|
||||
assert_eq!(
|
||||
process_nonce_instruction(&system_instruction::nonce_authorize(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_system_account_kind_system_ok() {
|
||||
let system_account = Account::default();
|
||||
assert_eq!(
|
||||
get_system_account_kind(&system_account),
|
||||
Some(SystemAccountKind::System)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_system_account_kind_nonce_ok() {
|
||||
let nonce_account = Account::new_data(
|
||||
42,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&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!(
|
||||
get_system_account_kind(&nonce_state::create_account(42)),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_system_account_kind_system_owner_nonzero_nonnonce_data_fail() {
|
||||
let other_data_account = Account::new_data(42, b"other", &Pubkey::default()).unwrap();
|
||||
assert_eq!(get_system_account_kind(&other_data_account), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_system_account_kind_nonsystem_owner_with_nonce_state_data_fail() {
|
||||
let nonce_account = Account::new_data(
|
||||
42,
|
||||
&nonce_state::NonceState::Initialized(
|
||||
nonce_state::Meta::new(&Pubkey::default()),
|
||||
Hash::default(),
|
||||
),
|
||||
&Pubkey::new_rand(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(get_system_account_kind(&nonce_account), None);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user