Add a constructor to generate random pubkeys

This commit is contained in:
Greg Fitzgerald
2019-03-30 21:37:33 -06:00
parent 32683cac7c
commit fcef54d062
51 changed files with 263 additions and 298 deletions

View File

@ -1570,7 +1570,7 @@ mod tests {
num_vote: usize,
) {
for t in 0..num {
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
let mut default_account = Account::default();
pubkeys.push(pubkey.clone());
default_account.lamports = (t + 1) as u64;
@ -1578,7 +1578,7 @@ mod tests {
accounts.store(fork, &pubkey, &default_account);
}
for t in 0..num_vote {
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
let mut default_account = Account::default();
pubkeys.push(pubkey.clone());
default_account.owner = solana_vote_api::id();
@ -1681,7 +1681,7 @@ mod tests {
let accounts = AccountsDB::new_with_file_size(0, &paths.paths, size, 0);
let mut keys = vec![];
for i in 0..9 {
let key = Keypair::new().pubkey();
let key = Pubkey::new_rand();
let account = Account::new(i + 1, size as usize / 4, &key);
accounts.store(0, &key, &account);
keys.push(key);
@ -1713,7 +1713,7 @@ mod tests {
AccountStorageStatus::StorageAvailable,
AccountStorageStatus::StorageFull,
];
let pubkey1 = Keypair::new().pubkey();
let pubkey1 = Pubkey::new_rand();
let account1 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, &pubkey1);
accounts.store(0, &pubkey1, &account1);
{
@ -1723,7 +1723,7 @@ mod tests {
assert_eq!(stores[0].get_status(), status[0]);
}
let pubkey2 = Keypair::new().pubkey();
let pubkey2 = Pubkey::new_rand();
let account2 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, &pubkey2);
accounts.store(0, &pubkey2, &account2);
{
@ -1785,7 +1785,7 @@ mod tests {
fn test_accounts_vote_filter() {
let accounts = Accounts::new(0, None);
let mut vote_account = Account::new(1, 0, &solana_vote_api::id());
let key = Keypair::new().pubkey();
let key = Pubkey::new_rand();
accounts.store_slow(0, &key, &vote_account);
accounts.new_from_parent(1, 0);
@ -1800,7 +1800,7 @@ mod tests {
assert_eq!(vote_accounts.len(), 0);
let mut vote_account1 = Account::new(2, 0, &solana_vote_api::id());
let key1 = Keypair::new().pubkey();
let key1 = Pubkey::new_rand();
accounts.store_slow(1, &key1, &vote_account1);
accounts.squash(1);
@ -1828,7 +1828,7 @@ mod tests {
accounts.iter().for_each(|(_, account)| {
assert_eq!(account.owner, solana_vote_api::id());
});
let lastkey = Keypair::new().pubkey();
let lastkey = Pubkey::new_rand();
let mut lastaccount = Account::new(1, 0, &solana_vote_api::id());
accounts_db.store(0, &lastkey, &lastaccount);
assert_eq!(accounts_db.get_vote_accounts(0).len(), 2);
@ -1890,7 +1890,7 @@ mod tests {
let accounts = AccountsDB::new(0, &paths.paths);
let mut error_counters = ErrorCounters::default();
assert_eq!(
accounts.load_executable_accounts(0, &Keypair::new().pubkey(), &mut error_counters),
accounts.load_executable_accounts(0, &Pubkey::new_rand(), &mut error_counters),
Err(TransactionError::AccountNotFound)
);
assert_eq!(error_counters.account_not_found, 1);
@ -1921,13 +1921,13 @@ mod tests {
let accounts_db = AccountsDB::new(0, &paths.paths);
// Load accounts owned by various programs into AccountsDB
let pubkey0 = Keypair::new().pubkey();
let pubkey0 = Pubkey::new_rand();
let account0 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
accounts_db.store(0, &pubkey0, &account0);
let pubkey1 = Keypair::new().pubkey();
let pubkey1 = Pubkey::new_rand();
let account1 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
accounts_db.store(0, &pubkey1, &account1);
let pubkey2 = Keypair::new().pubkey();
let pubkey2 = Pubkey::new_rand();
let account2 = Account::new(1, 0, &Pubkey::new(&[3; 32]));
accounts_db.store(0, &pubkey2, &account2);

View File

@ -978,7 +978,7 @@ mod tests {
#[test]
fn test_bank_new_with_leader() {
let dummy_leader_id = Keypair::new().pubkey();
let dummy_leader_id = Pubkey::new_rand();
let dummy_leader_lamports = BOOTSTRAP_LEADER_LAMPORTS;
let (genesis_block, _) =
GenesisBlock::new_with_leader(10_000, &dummy_leader_id, dummy_leader_lamports);
@ -1000,7 +1000,7 @@ mod tests {
#[test]
fn test_two_payments_to_one_party() {
let (genesis_block, mint_keypair) = GenesisBlock::new(10_000);
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
let bank = Bank::new(&genesis_block);
assert_eq!(bank.last_blockhash(), genesis_block.hash());
@ -1015,8 +1015,8 @@ mod tests {
#[test]
fn test_one_source_two_tx_one_batch() {
let (genesis_block, mint_keypair) = GenesisBlock::new(1);
let key1 = Keypair::new().pubkey();
let key2 = Keypair::new().pubkey();
let key1 = Pubkey::new_rand();
let key2 = Pubkey::new_rand();
let bank = Bank::new(&genesis_block);
assert_eq!(bank.last_blockhash(), genesis_block.hash());
@ -1040,8 +1040,8 @@ mod tests {
#[test]
fn test_one_tx_two_out_atomic_fail() {
let (genesis_block, mint_keypair) = GenesisBlock::new(1);
let key1 = Keypair::new().pubkey();
let key2 = Keypair::new().pubkey();
let key1 = Pubkey::new_rand();
let key2 = Pubkey::new_rand();
let bank = Bank::new(&genesis_block);
let instructions =
SystemInstruction::new_move_many(&mint_keypair.pubkey(), &[(key1, 1), (key2, 1)]);
@ -1065,8 +1065,8 @@ mod tests {
#[test]
fn test_one_tx_two_out_atomic_pass() {
let (genesis_block, mint_keypair) = GenesisBlock::new(2);
let key1 = Keypair::new().pubkey();
let key2 = Keypair::new().pubkey();
let key1 = Pubkey::new_rand();
let key2 = Pubkey::new_rand();
let bank = Bank::new(&genesis_block);
let instructions =
SystemInstruction::new_move_many(&mint_keypair.pubkey(), &[(key1, 1), (key2, 1)]);
@ -1132,7 +1132,7 @@ mod tests {
fn test_insufficient_funds() {
let (genesis_block, mint_keypair) = GenesisBlock::new(11_000);
let bank = Bank::new(&genesis_block);
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
bank.transfer(1_000, &mint_keypair, &pubkey).unwrap();
assert_eq!(bank.transaction_count(), 1);
assert_eq!(bank.get_balance(&pubkey), 1_000);
@ -1154,7 +1154,7 @@ mod tests {
fn test_transfer_to_newb() {
let (genesis_block, mint_keypair) = GenesisBlock::new(10_000);
let bank = Bank::new(&genesis_block);
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
bank.transfer(500, &mint_keypair, &pubkey).unwrap();
assert_eq!(bank.get_balance(&pubkey), 500);
}
@ -1202,7 +1202,7 @@ mod tests {
#[test]
fn test_bank_tx_fee() {
let leader = Keypair::new().pubkey();
let leader = Pubkey::new_rand();
let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, &leader, 3);
let mut bank = Bank::new(&genesis_block);
bank.fee_calculator.lamports_per_signature = 3;
@ -1229,7 +1229,7 @@ mod tests {
#[test]
fn test_filter_program_errors_and_collect_fee() {
let leader = Keypair::new().pubkey();
let leader = Pubkey::new_rand();
let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, &leader, 3);
let mut bank = Bank::new(&genesis_block);
@ -1284,7 +1284,7 @@ mod tests {
#[test]
fn test_process_genesis() {
let dummy_leader_id = Keypair::new().pubkey();
let dummy_leader_id = Pubkey::new_rand();
let dummy_leader_lamports = 2;
let (genesis_block, _) =
GenesisBlock::new_with_leader(5, &dummy_leader_id, dummy_leader_lamports);
@ -1440,7 +1440,7 @@ mod tests {
let initial_state = bank0.hash_internal_state();
assert_eq!(bank1.hash_internal_state(), initial_state);
let pubkey = Keypair::new().pubkey();
let pubkey = Pubkey::new_rand();
bank0.transfer(1_000, &mint_keypair, &pubkey).unwrap();
assert_ne!(bank0.hash_internal_state(), initial_state);
bank1.transfer(1_000, &mint_keypair, &pubkey).unwrap();
@ -1554,7 +1554,7 @@ mod tests {
#[test]
fn test_bank_epoch_vote_accounts() {
let leader_id = Keypair::new().pubkey();
let leader_id = Pubkey::new_rand();
let leader_lamports = 3;
let (mut genesis_block, _) = GenesisBlock::new_with_leader(5, &leader_id, leader_lamports);

View File

@ -64,7 +64,7 @@ mod tests {
let bank_client = BankClient::new(&bank);
// Create 2-2 Multisig Move instruction.
let bob_pubkey = Keypair::new().pubkey();
let bob_pubkey = Pubkey::new_rand();
let mut move_instruction = SystemInstruction::new_move(&john_pubkey, &bob_pubkey, 42);
move_instruction
.accounts

View File

@ -235,7 +235,6 @@ impl Runtime {
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::signature::{Keypair, KeypairUtil};
#[test]
fn test_has_duplicates() {
@ -282,8 +281,8 @@ mod tests {
}
let system_program_id = system_program::id();
let alice_program_id = Keypair::new().pubkey();
let mallory_program_id = Keypair::new().pubkey();
let alice_program_id = Pubkey::new_rand();
let mallory_program_id = Pubkey::new_rand();
assert_eq!(
change_program_id(&system_program_id, &system_program_id, &alice_program_id),
@ -300,13 +299,13 @@ mod tests {
#[test]
fn test_verify_instruction_change_data() {
fn change_data(program_id: &Pubkey) -> Result<(), InstructionError> {
let alice_program_id = Keypair::new().pubkey();
let alice_program_id = Pubkey::new_rand();
let account = Account::new(0, 0, &alice_program_id);
verify_instruction(&program_id, &alice_program_id, 0, &[42], &account)
}
let system_program_id = system_program::id();
let mallory_program_id = Keypair::new().pubkey();
let mallory_program_id = Pubkey::new_rand();
assert_eq!(
change_data(&system_program_id),

View File

@ -117,10 +117,10 @@ mod tests {
#[test]
fn test_create_system_account() {
let new_program_owner = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &system_program::id());
let to = Keypair::new().pubkey();
let to = Pubkey::new_rand();
let mut to_account = Account::new(0, 0, &Pubkey::default());
let mut keyed_accounts = [
@ -142,10 +142,10 @@ mod tests {
fn test_create_negative_lamports() {
// Attempt to create account with more lamports than remaining in from_account
let new_program_owner = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &system_program::id());
let to = Keypair::new().pubkey();
let to = Pubkey::new_rand();
let mut to_account = Account::new(0, 0, &Pubkey::default());
let unchanged_account = to_account.clone();
@ -164,11 +164,11 @@ mod tests {
fn test_create_already_owned() {
// Attempt to create system account in account already owned by another program
let new_program_owner = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &system_program::id());
let original_program_owner = Pubkey::new(&[5; 32]);
let owned_key = Keypair::new().pubkey();
let owned_key = Pubkey::new_rand();
let mut owned_account = Account::new(0, 0, &original_program_owner);
let unchanged_account = owned_account.clone();
@ -187,10 +187,10 @@ mod tests {
fn test_create_data_populated() {
// Attempt to create system account in account with populated data
let new_program_owner = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &system_program::id());
let populated_key = Keypair::new().pubkey();
let populated_key = Pubkey::new_rand();
let mut populated_account = Account {
lamports: 0,
data: vec![0, 1, 2, 3],
@ -213,9 +213,9 @@ mod tests {
fn test_create_not_system_account() {
let other_program = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &other_program);
let to = Keypair::new().pubkey();
let to = Pubkey::new_rand();
let mut to_account = Account::new(0, 0, &Pubkey::default());
let mut keyed_accounts = [
KeyedAccount::new(&from, true, &mut from_account),
@ -229,7 +229,7 @@ mod tests {
fn test_assign_account_to_program() {
let new_program_owner = Pubkey::new(&[9; 32]);
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &system_program::id());
let mut keyed_accounts = [KeyedAccount::new(&from, true, &mut from_account)];
assign_account_to_program(&mut keyed_accounts, &new_program_owner).unwrap();
@ -250,9 +250,9 @@ mod tests {
#[test]
fn test_move_lamports() {
let from = Keypair::new().pubkey();
let from = Pubkey::new_rand();
let mut from_account = Account::new(100, 0, &Pubkey::new(&[2; 32])); // account owner should not matter
let to = Keypair::new().pubkey();
let to = Pubkey::new_rand();
let mut to_account = Account::new(1, 0, &Pubkey::new(&[3; 32])); // account owner should not matter
let mut keyed_accounts = [
KeyedAccount::new(&from, true, &mut from_account),