pass Pubkeys as refs, copy only where values needed (#3213)
* pass Pubkeys as refs, copy only where values needed * Pubkey is pervasive * fixup
This commit is contained in:
		@@ -635,11 +635,12 @@ impl AccountsDB {
 | 
			
		||||
    fn load_executable_accounts(
 | 
			
		||||
        &self,
 | 
			
		||||
        fork: Fork,
 | 
			
		||||
        mut program_id: Pubkey,
 | 
			
		||||
        program_id: &Pubkey,
 | 
			
		||||
        error_counters: &mut ErrorCounters,
 | 
			
		||||
    ) -> Result<Vec<(Pubkey, Account)>> {
 | 
			
		||||
        let mut accounts = Vec::new();
 | 
			
		||||
        let mut depth = 0;
 | 
			
		||||
        let mut program_id = *program_id;
 | 
			
		||||
        loop {
 | 
			
		||||
            if native_loader::check_id(&program_id) {
 | 
			
		||||
                // at the root of the chain, ready to dispatch
 | 
			
		||||
@@ -687,7 +688,7 @@ impl AccountsDB {
 | 
			
		||||
                    return Err(BankError::AccountNotFound);
 | 
			
		||||
                }
 | 
			
		||||
                let program_id = tx.program_ids[ix.program_ids_index as usize];
 | 
			
		||||
                self.load_executable_accounts(fork, program_id, error_counters)
 | 
			
		||||
                self.load_executable_accounts(fork, &program_id, error_counters)
 | 
			
		||||
            })
 | 
			
		||||
            .collect()
 | 
			
		||||
    }
 | 
			
		||||
@@ -1092,10 +1093,10 @@ 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, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let account = Account::new(2, 1, Pubkey::default());
 | 
			
		||||
        let account = Account::new(2, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
 | 
			
		||||
        let instructions = vec![Instruction::new(1, &(), vec![0])];
 | 
			
		||||
@@ -1123,7 +1124,7 @@ mod tests {
 | 
			
		||||
        let keypair = Keypair::new();
 | 
			
		||||
        let key0 = keypair.pubkey();
 | 
			
		||||
 | 
			
		||||
        let account = Account::new(1, 1, Pubkey::default());
 | 
			
		||||
        let account = Account::new(1, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let instructions = vec![Instruction::new(1, &(), vec![0])];
 | 
			
		||||
@@ -1152,10 +1153,10 @@ 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, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let account = Account::new(2, 1, Pubkey::default());
 | 
			
		||||
        let account = Account::new(2, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
 | 
			
		||||
        let instructions = vec![Instruction::new(0, &(), vec![0, 1])];
 | 
			
		||||
@@ -1197,35 +1198,35 @@ 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, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(40, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(40, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = native_loader::id();
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(41, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(41, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key1;
 | 
			
		||||
        accounts.push((key2, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(42, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(42, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key2;
 | 
			
		||||
        accounts.push((key3, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(43, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(43, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key3;
 | 
			
		||||
        accounts.push((key4, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(44, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(44, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key4;
 | 
			
		||||
        accounts.push((key5, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(45, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(45, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key5;
 | 
			
		||||
        accounts.push((key6, account));
 | 
			
		||||
@@ -1256,10 +1257,10 @@ 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, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(40, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(40, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = Pubkey::default();
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
@@ -1290,10 +1291,10 @@ 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, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((key0, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(40, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(40, 1, &Pubkey::default());
 | 
			
		||||
        account.owner = native_loader::id();
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
 | 
			
		||||
@@ -1325,20 +1326,20 @@ mod tests {
 | 
			
		||||
        let key2 = Pubkey::new(&[6u8; 32]);
 | 
			
		||||
        let key3 = Pubkey::new(&[7u8; 32]);
 | 
			
		||||
 | 
			
		||||
        let account = Account::new(1, 1, Pubkey::default());
 | 
			
		||||
        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, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = native_loader::id();
 | 
			
		||||
        accounts.push((key1, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(41, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(41, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key1;
 | 
			
		||||
        accounts.push((key2, account));
 | 
			
		||||
 | 
			
		||||
        let mut account = Account::new(42, 1, Pubkey::default());
 | 
			
		||||
        let mut account = Account::new(42, 1, &Pubkey::default());
 | 
			
		||||
        account.executable = true;
 | 
			
		||||
        account.owner = key2;
 | 
			
		||||
        accounts.push((key3, account));
 | 
			
		||||
@@ -1386,7 +1387,7 @@ mod tests {
 | 
			
		||||
        let keypair = Keypair::new();
 | 
			
		||||
        let pubkey = keypair.pubkey();
 | 
			
		||||
 | 
			
		||||
        let account = Account::new(10, 1, Pubkey::default());
 | 
			
		||||
        let account = Account::new(10, 1, &Pubkey::default());
 | 
			
		||||
        accounts.push((pubkey, account));
 | 
			
		||||
 | 
			
		||||
        let instructions = vec![Instruction::new(0, &(), vec![0, 1])];
 | 
			
		||||
@@ -1448,7 +1449,7 @@ mod tests {
 | 
			
		||||
        let paths = get_tmp_accounts_path!();
 | 
			
		||||
        let db = AccountsDB::new(0, &paths.paths);
 | 
			
		||||
        let key = Pubkey::default();
 | 
			
		||||
        let account0 = Account::new(1, 0, key);
 | 
			
		||||
        let account0 = Account::new(1, 0, &key);
 | 
			
		||||
 | 
			
		||||
        // store value 1 in the "root", i.e. db zero
 | 
			
		||||
        db.store(0, &key, &account0);
 | 
			
		||||
@@ -1466,7 +1467,7 @@ mod tests {
 | 
			
		||||
        //                                       (via root0)
 | 
			
		||||
 | 
			
		||||
        // store value 0 in one child
 | 
			
		||||
        let account1 = Account::new(0, 0, key);
 | 
			
		||||
        let account1 = Account::new(0, 0, &key);
 | 
			
		||||
        db.store(1, &key, &account1);
 | 
			
		||||
 | 
			
		||||
        // masking accounts is done at the Accounts level, at accountsDB we see
 | 
			
		||||
@@ -1546,12 +1547,12 @@ mod tests {
 | 
			
		||||
        // 1 token in the "root", i.e. db zero
 | 
			
		||||
        let paths = get_tmp_accounts_path!();
 | 
			
		||||
        let db0 = AccountsDB::new(0, &paths.paths);
 | 
			
		||||
        let account0 = Account::new(1, 0, key);
 | 
			
		||||
        let account0 = Account::new(1, 0, &key);
 | 
			
		||||
        db0.store(0, &key, &account0);
 | 
			
		||||
 | 
			
		||||
        db0.add_fork(1, Some(0));
 | 
			
		||||
        // 0 lamports in the child
 | 
			
		||||
        let account1 = Account::new(0, 0, key);
 | 
			
		||||
        let account1 = Account::new(0, 0, &key);
 | 
			
		||||
        db0.store(1, &key, &account1);
 | 
			
		||||
 | 
			
		||||
        // masking accounts is done at the Accounts level, at accountsDB we see
 | 
			
		||||
@@ -1670,7 +1671,7 @@ mod tests {
 | 
			
		||||
        let mut keys = vec![];
 | 
			
		||||
        for i in 0..9 {
 | 
			
		||||
            let key = Keypair::new().pubkey();
 | 
			
		||||
            let account = Account::new(i + 1, size as usize / 4, key);
 | 
			
		||||
            let account = Account::new(i + 1, size as usize / 4, &key);
 | 
			
		||||
            accounts.store(0, &key, &account);
 | 
			
		||||
            keys.push(key);
 | 
			
		||||
        }
 | 
			
		||||
@@ -1703,7 +1704,7 @@ mod tests {
 | 
			
		||||
            AccountStorageStatus::StorageFull,
 | 
			
		||||
        ];
 | 
			
		||||
        let pubkey1 = Keypair::new().pubkey();
 | 
			
		||||
        let account1 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, pubkey1);
 | 
			
		||||
        let account1 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, &pubkey1);
 | 
			
		||||
        accounts.store(0, &pubkey1, &account1);
 | 
			
		||||
        {
 | 
			
		||||
            let stores = accounts.storage.read().unwrap();
 | 
			
		||||
@@ -1713,7 +1714,7 @@ mod tests {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let pubkey2 = Keypair::new().pubkey();
 | 
			
		||||
        let account2 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, pubkey2);
 | 
			
		||||
        let account2 = Account::new(1, ACCOUNT_DATA_FILE_SIZE as usize / 2, &pubkey2);
 | 
			
		||||
        accounts.store(0, &pubkey2, &account2);
 | 
			
		||||
        {
 | 
			
		||||
            let stores = accounts.storage.read().unwrap();
 | 
			
		||||
@@ -1747,7 +1748,7 @@ mod tests {
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_accounts_vote_filter() {
 | 
			
		||||
        let accounts = Accounts::new(0, None);
 | 
			
		||||
        let mut vote_account = Account::new(1, 0, solana_vote_api::id());
 | 
			
		||||
        let mut vote_account = Account::new(1, 0, &solana_vote_api::id());
 | 
			
		||||
        let key = Keypair::new().pubkey();
 | 
			
		||||
        accounts.store_slow(0, &key, &vote_account);
 | 
			
		||||
 | 
			
		||||
@@ -1762,7 +1763,7 @@ mod tests {
 | 
			
		||||
        vote_accounts = accounts.get_vote_accounts(1).collect();
 | 
			
		||||
        assert_eq!(vote_accounts.len(), 0);
 | 
			
		||||
 | 
			
		||||
        let mut vote_account1 = Account::new(2, 0, solana_vote_api::id());
 | 
			
		||||
        let mut vote_account1 = Account::new(2, 0, &solana_vote_api::id());
 | 
			
		||||
        let key1 = Keypair::new().pubkey();
 | 
			
		||||
        accounts.store_slow(1, &key1, &vote_account1);
 | 
			
		||||
 | 
			
		||||
@@ -1792,7 +1793,7 @@ mod tests {
 | 
			
		||||
            assert_eq!(account.owner, solana_vote_api::id());
 | 
			
		||||
        });
 | 
			
		||||
        let lastkey = Keypair::new().pubkey();
 | 
			
		||||
        let mut lastaccount = Account::new(1, 0, solana_vote_api::id());
 | 
			
		||||
        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);
 | 
			
		||||
 | 
			
		||||
@@ -1862,7 +1863,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, &Keypair::new().pubkey(), &mut error_counters),
 | 
			
		||||
            Err(BankError::AccountNotFound)
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(error_counters.account_not_found, 1);
 | 
			
		||||
@@ -1894,13 +1895,13 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
        // Load accounts owned by various programs into AccountsDB
 | 
			
		||||
        let pubkey0 = Keypair::new().pubkey();
 | 
			
		||||
        let account0 = Account::new(1, 0, Pubkey::new(&[2; 32]));
 | 
			
		||||
        let account0 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
 | 
			
		||||
        accounts_db.store(0, &pubkey0, &account0);
 | 
			
		||||
        let pubkey1 = Keypair::new().pubkey();
 | 
			
		||||
        let account1 = Account::new(1, 0, Pubkey::new(&[2; 32]));
 | 
			
		||||
        let account1 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
 | 
			
		||||
        accounts_db.store(0, &pubkey1, &account1);
 | 
			
		||||
        let pubkey2 = Keypair::new().pubkey();
 | 
			
		||||
        let account2 = Account::new(1, 0, Pubkey::new(&[3; 32]));
 | 
			
		||||
        let account2 = Account::new(1, 0, &Pubkey::new(&[3; 32]));
 | 
			
		||||
        accounts_db.store(0, &pubkey2, &account2);
 | 
			
		||||
 | 
			
		||||
        let accounts = accounts_db.load_by_program(0, &Pubkey::new(&[2; 32]), false);
 | 
			
		||||
 
 | 
			
		||||
@@ -224,7 +224,7 @@ impl Bank {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Create a new bank that points to an immutable checkpoint of another bank.
 | 
			
		||||
    pub fn new_from_parent(parent: &Arc<Bank>, collector_id: Pubkey, slot: u64) -> Self {
 | 
			
		||||
    pub fn new_from_parent(parent: &Arc<Bank>, collector_id: &Pubkey, slot: u64) -> Self {
 | 
			
		||||
        parent.freeze();
 | 
			
		||||
 | 
			
		||||
        assert_ne!(slot, parent.slot());
 | 
			
		||||
@@ -239,7 +239,7 @@ impl Bank {
 | 
			
		||||
        bank.slot = slot;
 | 
			
		||||
        bank.parent = RwLock::new(Some(parent.clone()));
 | 
			
		||||
        bank.parent_hash = parent.hash();
 | 
			
		||||
        bank.collector_id = collector_id;
 | 
			
		||||
        bank.collector_id = *collector_id;
 | 
			
		||||
 | 
			
		||||
        // Accounts needs a unique id
 | 
			
		||||
        static BANK_ACCOUNTS_ID: AtomicUsize = AtomicUsize::new(1);
 | 
			
		||||
@@ -340,7 +340,7 @@ impl Bank {
 | 
			
		||||
            executable: false,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let mut vote_state = VoteState::new(genesis_block.bootstrap_leader_id);
 | 
			
		||||
        let mut vote_state = VoteState::new(&genesis_block.bootstrap_leader_id);
 | 
			
		||||
        vote_state.votes.push_back(Lockout::new(&Vote::new(0)));
 | 
			
		||||
        vote_state
 | 
			
		||||
            .serialize(&mut bootstrap_leader_vote_account.userdata)
 | 
			
		||||
@@ -731,7 +731,7 @@ impl Bank {
 | 
			
		||||
        &self,
 | 
			
		||||
        n: u64,
 | 
			
		||||
        keypair: &Keypair,
 | 
			
		||||
        to: Pubkey,
 | 
			
		||||
        to: &Pubkey,
 | 
			
		||||
        blockhash: Hash,
 | 
			
		||||
    ) -> Result<Signature> {
 | 
			
		||||
        let tx = SystemTransaction::new_account(keypair, to, n, blockhash, 0);
 | 
			
		||||
@@ -909,7 +909,7 @@ mod tests {
 | 
			
		||||
        let dummy_leader_id = Keypair::new().pubkey();
 | 
			
		||||
        let dummy_leader_lamports = BOOTSTRAP_LEADER_LAMPORTS;
 | 
			
		||||
        let (genesis_block, _) =
 | 
			
		||||
            GenesisBlock::new_with_leader(10_000, dummy_leader_id, dummy_leader_lamports);
 | 
			
		||||
            GenesisBlock::new_with_leader(10_000, &dummy_leader_id, dummy_leader_lamports);
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            genesis_block.bootstrap_leader_lamports,
 | 
			
		||||
            dummy_leader_lamports
 | 
			
		||||
@@ -932,11 +932,11 @@ mod tests {
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        assert_eq!(bank.last_blockhash(), genesis_block.hash());
 | 
			
		||||
 | 
			
		||||
        bank.transfer(1_000, &mint_keypair, pubkey, genesis_block.hash())
 | 
			
		||||
        bank.transfer(1_000, &mint_keypair, &pubkey, genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank.get_balance(&pubkey), 1_000);
 | 
			
		||||
 | 
			
		||||
        bank.transfer(500, &mint_keypair, pubkey, genesis_block.hash())
 | 
			
		||||
        bank.transfer(500, &mint_keypair, &pubkey, genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank.get_balance(&pubkey), 1_500);
 | 
			
		||||
        assert_eq!(bank.transaction_count(), 2);
 | 
			
		||||
@@ -950,8 +950,8 @@ mod tests {
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        assert_eq!(bank.last_blockhash(), genesis_block.hash());
 | 
			
		||||
 | 
			
		||||
        let t1 = SystemTransaction::new_move(&mint_keypair, key1, 1, genesis_block.hash(), 0);
 | 
			
		||||
        let t2 = SystemTransaction::new_move(&mint_keypair, key2, 1, genesis_block.hash(), 0);
 | 
			
		||||
        let t1 = SystemTransaction::new_move(&mint_keypair, &key1, 1, genesis_block.hash(), 0);
 | 
			
		||||
        let t2 = SystemTransaction::new_move(&mint_keypair, &key2, 1, genesis_block.hash(), 0);
 | 
			
		||||
        let res = bank.process_transactions(&vec![t1.clone(), t2.clone()]);
 | 
			
		||||
        assert_eq!(res.len(), 2);
 | 
			
		||||
        assert_eq!(res[0], Ok(()));
 | 
			
		||||
@@ -1040,7 +1040,7 @@ mod tests {
 | 
			
		||||
        // source with 0 program context
 | 
			
		||||
        let tx = SystemTransaction::new_account(
 | 
			
		||||
            &mint_keypair,
 | 
			
		||||
            dest.pubkey(),
 | 
			
		||||
            &dest.pubkey(),
 | 
			
		||||
            2,
 | 
			
		||||
            genesis_block.hash(),
 | 
			
		||||
            1,
 | 
			
		||||
@@ -1069,7 +1069,7 @@ mod tests {
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        let keypair = Keypair::new();
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            bank.transfer(1, &keypair, mint_keypair.pubkey(), genesis_block.hash()),
 | 
			
		||||
            bank.transfer(1, &keypair, &mint_keypair.pubkey(), genesis_block.hash()),
 | 
			
		||||
            Err(BankError::AccountNotFound)
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(bank.transaction_count(), 0);
 | 
			
		||||
@@ -1080,12 +1080,12 @@ mod tests {
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new(11_000);
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        let pubkey = Keypair::new().pubkey();
 | 
			
		||||
        bank.transfer(1_000, &mint_keypair, pubkey, genesis_block.hash())
 | 
			
		||||
        bank.transfer(1_000, &mint_keypair, &pubkey, genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank.transaction_count(), 1);
 | 
			
		||||
        assert_eq!(bank.get_balance(&pubkey), 1_000);
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            bank.transfer(10_001, &mint_keypair, pubkey, genesis_block.hash()),
 | 
			
		||||
            bank.transfer(10_001, &mint_keypair, &pubkey, genesis_block.hash()),
 | 
			
		||||
            Err(BankError::ProgramError(
 | 
			
		||||
                0,
 | 
			
		||||
                ProgramError::ResultWithNegativeLamports
 | 
			
		||||
@@ -1103,7 +1103,7 @@ mod tests {
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new(10_000);
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        let pubkey = Keypair::new().pubkey();
 | 
			
		||||
        bank.transfer(500, &mint_keypair, pubkey, genesis_block.hash())
 | 
			
		||||
        bank.transfer(500, &mint_keypair, &pubkey, genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank.get_balance(&pubkey), 500);
 | 
			
		||||
    }
 | 
			
		||||
@@ -1152,20 +1152,20 @@ mod tests {
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_bank_tx_fee() {
 | 
			
		||||
        let leader = Keypair::new().pubkey();
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, leader, 3);
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, &leader, 3);
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        let key1 = Keypair::new();
 | 
			
		||||
        let key2 = Keypair::new();
 | 
			
		||||
 | 
			
		||||
        let tx =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 2, genesis_block.hash(), 3);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key1.pubkey(), 2, genesis_block.hash(), 3);
 | 
			
		||||
        let initial_balance = bank.get_balance(&leader);
 | 
			
		||||
        assert_eq!(bank.process_transaction(&tx), Ok(()));
 | 
			
		||||
        assert_eq!(bank.get_balance(&leader), initial_balance + 3);
 | 
			
		||||
        assert_eq!(bank.get_balance(&key1.pubkey()), 2);
 | 
			
		||||
        assert_eq!(bank.get_balance(&mint_keypair.pubkey()), 100 - 5 - 3);
 | 
			
		||||
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 1);
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, &key2.pubkey(), 1, genesis_block.hash(), 1);
 | 
			
		||||
        assert_eq!(bank.process_transaction(&tx), Ok(()));
 | 
			
		||||
        assert_eq!(bank.get_balance(&leader), initial_balance + 4);
 | 
			
		||||
        assert_eq!(bank.get_balance(&key1.pubkey()), 0);
 | 
			
		||||
@@ -1176,14 +1176,14 @@ mod tests {
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_filter_program_errors_and_collect_fee() {
 | 
			
		||||
        let leader = Keypair::new().pubkey();
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, leader, 3);
 | 
			
		||||
        let (genesis_block, mint_keypair) = GenesisBlock::new_with_leader(100, &leader, 3);
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
 | 
			
		||||
        let key = Keypair::new();
 | 
			
		||||
        let tx1 =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key.pubkey(), 2, genesis_block.hash(), 3);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key.pubkey(), 2, genesis_block.hash(), 3);
 | 
			
		||||
        let tx2 =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key.pubkey(), 5, genesis_block.hash(), 1);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key.pubkey(), 5, genesis_block.hash(), 1);
 | 
			
		||||
 | 
			
		||||
        let results = vec![
 | 
			
		||||
            Ok(()),
 | 
			
		||||
@@ -1207,14 +1207,14 @@ mod tests {
 | 
			
		||||
        let keypair = Keypair::new();
 | 
			
		||||
        let tx0 = SystemTransaction::new_account(
 | 
			
		||||
            &mint_keypair,
 | 
			
		||||
            keypair.pubkey(),
 | 
			
		||||
            &keypair.pubkey(),
 | 
			
		||||
            2,
 | 
			
		||||
            genesis_block.hash(),
 | 
			
		||||
            0,
 | 
			
		||||
        );
 | 
			
		||||
        let tx1 = SystemTransaction::new_account(
 | 
			
		||||
            &keypair,
 | 
			
		||||
            mint_keypair.pubkey(),
 | 
			
		||||
            &mint_keypair.pubkey(),
 | 
			
		||||
            1,
 | 
			
		||||
            genesis_block.hash(),
 | 
			
		||||
            0,
 | 
			
		||||
@@ -1232,7 +1232,7 @@ mod tests {
 | 
			
		||||
        let dummy_leader_id = Keypair::new().pubkey();
 | 
			
		||||
        let dummy_leader_lamports = 3;
 | 
			
		||||
        let (genesis_block, _) =
 | 
			
		||||
            GenesisBlock::new_with_leader(5, dummy_leader_id, dummy_leader_lamports);
 | 
			
		||||
            GenesisBlock::new_with_leader(5, &dummy_leader_id, dummy_leader_lamports);
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
        assert_eq!(bank.get_balance(&genesis_block.mint_id), 2);
 | 
			
		||||
        assert_eq!(bank.get_balance(&dummy_leader_id), 2);
 | 
			
		||||
@@ -1247,7 +1247,7 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
        let tx1 = SystemTransaction::new_account(
 | 
			
		||||
            &mint_keypair,
 | 
			
		||||
            alice.pubkey(),
 | 
			
		||||
            &alice.pubkey(),
 | 
			
		||||
            1,
 | 
			
		||||
            genesis_block.hash(),
 | 
			
		||||
            0,
 | 
			
		||||
@@ -1264,20 +1264,20 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
        // try executing an interleaved transfer twice
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            bank.transfer(1, &mint_keypair, bob.pubkey(), genesis_block.hash()),
 | 
			
		||||
            bank.transfer(1, &mint_keypair, &bob.pubkey(), genesis_block.hash()),
 | 
			
		||||
            Err(BankError::AccountInUse)
 | 
			
		||||
        );
 | 
			
		||||
        // the second time should fail as well
 | 
			
		||||
        // this verifies that `unlock_accounts` doesn't unlock `AccountInUse` accounts
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            bank.transfer(1, &mint_keypair, bob.pubkey(), genesis_block.hash()),
 | 
			
		||||
            bank.transfer(1, &mint_keypair, &bob.pubkey(), genesis_block.hash()),
 | 
			
		||||
            Err(BankError::AccountInUse)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        bank.unlock_accounts(&pay_alice, &results_alice);
 | 
			
		||||
 | 
			
		||||
        assert!(bank
 | 
			
		||||
            .transfer(2, &mint_keypair, bob.pubkey(), genesis_block.hash())
 | 
			
		||||
            .transfer(2, &mint_keypair, &bob.pubkey(), genesis_block.hash())
 | 
			
		||||
            .is_ok());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1342,10 +1342,10 @@ mod tests {
 | 
			
		||||
        let key1 = Keypair::new();
 | 
			
		||||
        let bank = Bank::new(&genesis_block);
 | 
			
		||||
 | 
			
		||||
        bank.transfer(1, &mint_keypair, key1.pubkey(), genesis_block.hash())
 | 
			
		||||
        bank.transfer(1, &mint_keypair, &key1.pubkey(), genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank.get_balance(&key1.pubkey()), 1);
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, &key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        let res = bank.process_transactions(&vec![tx.clone()]);
 | 
			
		||||
        assert_eq!(res.len(), 1);
 | 
			
		||||
        assert_eq!(bank.get_balance(&key1.pubkey()), 1);
 | 
			
		||||
@@ -1353,7 +1353,7 @@ mod tests {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn new_from_parent(parent: &Arc<Bank>) -> Bank {
 | 
			
		||||
        Bank::new_from_parent(parent, Pubkey::default(), parent.slot() + 1)
 | 
			
		||||
        Bank::new_from_parent(parent, &Pubkey::default(), parent.slot() + 1)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Verify that the parent's vector is computed correctly
 | 
			
		||||
@@ -1374,7 +1374,7 @@ mod tests {
 | 
			
		||||
        let parent = Arc::new(Bank::new(&genesis_block));
 | 
			
		||||
 | 
			
		||||
        let tx =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        assert_eq!(parent.process_transaction(&tx), Ok(()));
 | 
			
		||||
        let bank = new_from_parent(&parent);
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
@@ -1392,10 +1392,10 @@ mod tests {
 | 
			
		||||
        let parent = Arc::new(Bank::new(&genesis_block));
 | 
			
		||||
 | 
			
		||||
        let tx =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        assert_eq!(parent.process_transaction(&tx), Ok(()));
 | 
			
		||||
        let bank = new_from_parent(&parent);
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        let tx = SystemTransaction::new_move(&key1, &key2.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        assert_eq!(bank.process_transaction(&tx), Ok(()));
 | 
			
		||||
        assert_eq!(parent.get_signature_status(&tx.signatures[0]), None);
 | 
			
		||||
    }
 | 
			
		||||
@@ -1410,11 +1410,11 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
        let pubkey = Keypair::new().pubkey();
 | 
			
		||||
        bank0
 | 
			
		||||
            .transfer(1_000, &mint_keypair, pubkey, bank0.last_blockhash())
 | 
			
		||||
            .transfer(1_000, &mint_keypair, &pubkey, bank0.last_blockhash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_ne!(bank0.hash_internal_state(), initial_state);
 | 
			
		||||
        bank1
 | 
			
		||||
            .transfer(1_000, &mint_keypair, pubkey, bank1.last_blockhash())
 | 
			
		||||
            .transfer(1_000, &mint_keypair, &pubkey, bank1.last_blockhash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(bank0.hash_internal_state(), bank1.hash_internal_state());
 | 
			
		||||
 | 
			
		||||
@@ -1434,7 +1434,7 @@ mod tests {
 | 
			
		||||
    fn test_bank_hash_internal_state_squash() {
 | 
			
		||||
        let collector_id = Pubkey::default();
 | 
			
		||||
        let bank0 = Arc::new(Bank::new(&GenesisBlock::new(10).0));
 | 
			
		||||
        let bank1 = Bank::new_from_parent(&bank0, collector_id, 1);
 | 
			
		||||
        let bank1 = Bank::new_from_parent(&bank0, &collector_id, 1);
 | 
			
		||||
 | 
			
		||||
        // no delta in bank1, hashes match
 | 
			
		||||
        assert_eq!(bank0.hash_internal_state(), bank1.hash_internal_state());
 | 
			
		||||
@@ -1456,14 +1456,14 @@ mod tests {
 | 
			
		||||
        let parent = Arc::new(Bank::new(&genesis_block));
 | 
			
		||||
 | 
			
		||||
        let tx_move_mint_to_1 =
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
            SystemTransaction::new_move(&mint_keypair, &key1.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        assert_eq!(parent.process_transaction(&tx_move_mint_to_1), Ok(()));
 | 
			
		||||
        assert_eq!(parent.transaction_count(), 1);
 | 
			
		||||
 | 
			
		||||
        let bank = new_from_parent(&parent);
 | 
			
		||||
        assert_eq!(bank.transaction_count(), parent.transaction_count());
 | 
			
		||||
        let tx_move_1_to_2 =
 | 
			
		||||
            SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
            SystemTransaction::new_move(&key1, &key2.pubkey(), 1, genesis_block.hash(), 0);
 | 
			
		||||
        assert_eq!(bank.process_transaction(&tx_move_1_to_2), Ok(()));
 | 
			
		||||
        assert_eq!(bank.transaction_count(), 2);
 | 
			
		||||
        assert_eq!(parent.transaction_count(), 1);
 | 
			
		||||
@@ -1502,7 +1502,7 @@ mod tests {
 | 
			
		||||
        let key1 = Keypair::new();
 | 
			
		||||
 | 
			
		||||
        parent
 | 
			
		||||
            .transfer(1, &mint_keypair, key1.pubkey(), genesis_block.hash())
 | 
			
		||||
            .transfer(1, &mint_keypair, &key1.pubkey(), genesis_block.hash())
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        assert_eq!(parent.get_balance(&key1.pubkey()), 1);
 | 
			
		||||
        let bank = new_from_parent(&parent);
 | 
			
		||||
@@ -1514,7 +1514,7 @@ mod tests {
 | 
			
		||||
    fn test_bank_epoch_vote_accounts() {
 | 
			
		||||
        let leader_id = Keypair::new().pubkey();
 | 
			
		||||
        let leader_lamports = 3;
 | 
			
		||||
        let (mut genesis_block, _) = GenesisBlock::new_with_leader(5, leader_id, leader_lamports);
 | 
			
		||||
        let (mut genesis_block, _) = GenesisBlock::new_with_leader(5, &leader_id, leader_lamports);
 | 
			
		||||
 | 
			
		||||
        // set this up weird, forces future generation, odd mod(), etc.
 | 
			
		||||
        //  this says: "stakes for slot X should be generated at slot index 3 in slot X-2...
 | 
			
		||||
@@ -1557,7 +1557,7 @@ mod tests {
 | 
			
		||||
        // child crosses epoch boundary and is the first slot in the epoch
 | 
			
		||||
        let child = Bank::new_from_parent(
 | 
			
		||||
            &parent,
 | 
			
		||||
            leader_id,
 | 
			
		||||
            &leader_id,
 | 
			
		||||
            SLOTS_PER_EPOCH - (STAKERS_SLOT_OFFSET % SLOTS_PER_EPOCH),
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
@@ -1566,7 +1566,7 @@ mod tests {
 | 
			
		||||
        // child crosses epoch boundary but isn't the first slot in the epoch
 | 
			
		||||
        let child = Bank::new_from_parent(
 | 
			
		||||
            &parent,
 | 
			
		||||
            leader_id,
 | 
			
		||||
            &leader_id,
 | 
			
		||||
            SLOTS_PER_EPOCH - (STAKERS_SLOT_OFFSET % SLOTS_PER_EPOCH) + 1,
 | 
			
		||||
        );
 | 
			
		||||
        assert!(child.epoch_vote_accounts(i).is_some());
 | 
			
		||||
@@ -1584,7 +1584,7 @@ mod tests {
 | 
			
		||||
        let mut tx = Transaction::new_unsigned(
 | 
			
		||||
            &mint_keypair.pubkey(),
 | 
			
		||||
            &[key.pubkey()],
 | 
			
		||||
            system_program::id(),
 | 
			
		||||
            &system_program::id(),
 | 
			
		||||
            &move_lamports,
 | 
			
		||||
            bank.last_blockhash(),
 | 
			
		||||
            2,
 | 
			
		||||
 
 | 
			
		||||
@@ -4,12 +4,12 @@ use solana_sdk::pubkey::Pubkey;
 | 
			
		||||
use solana_sdk::signature::{Keypair, KeypairUtil};
 | 
			
		||||
use solana_sdk::system_transaction::SystemTransaction;
 | 
			
		||||
 | 
			
		||||
pub fn load_program(bank: &Bank, from: &Keypair, loader_id: Pubkey, program: Vec<u8>) -> Pubkey {
 | 
			
		||||
pub fn load_program(bank: &Bank, from: &Keypair, loader_id: &Pubkey, program: Vec<u8>) -> Pubkey {
 | 
			
		||||
    let program_account = Keypair::new();
 | 
			
		||||
 | 
			
		||||
    let tx = SystemTransaction::new_program_account(
 | 
			
		||||
        from,
 | 
			
		||||
        program_account.pubkey(),
 | 
			
		||||
        &program_account.pubkey(),
 | 
			
		||||
        bank.last_blockhash(),
 | 
			
		||||
        1,
 | 
			
		||||
        program.len() as u64,
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ use solana_sdk::transaction::Transaction;
 | 
			
		||||
/// Reasons the runtime might have rejected a transaction.
 | 
			
		||||
#[derive(Debug, PartialEq, Eq, Clone)]
 | 
			
		||||
pub enum RuntimeError {
 | 
			
		||||
    /// Executing the instruction at the given index produced an error.
 | 
			
		||||
    /// Executing the instruction at the give&n index produced an error.
 | 
			
		||||
    ProgramError(u8, ProgramError),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -199,7 +199,7 @@ where
 | 
			
		||||
    F: Fn(&Pubkey, &mut [KeyedAccount], &[u8]) -> Result<(), E>,
 | 
			
		||||
{
 | 
			
		||||
    for _ in tx_accounts.len()..tx.account_keys.len() {
 | 
			
		||||
        tx_accounts.push(Account::new(0, 0, system_program::id()));
 | 
			
		||||
        tx_accounts.push(Account::new(0, 0, &system_program::id()));
 | 
			
		||||
    }
 | 
			
		||||
    for (i, ix) in tx.instructions.iter().enumerate() {
 | 
			
		||||
        let mut ix_accounts = get_subset_unchecked_mut(tx_accounts, &ix.accounts);
 | 
			
		||||
@@ -262,7 +262,7 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_verify_instruction_change_program_id() {
 | 
			
		||||
        fn change_program_id(ix: Pubkey, pre: Pubkey, post: Pubkey) -> Result<(), ProgramError> {
 | 
			
		||||
        fn change_program_id(ix: &Pubkey, pre: &Pubkey, post: &Pubkey) -> Result<(), ProgramError> {
 | 
			
		||||
            verify_instruction(&ix, &pre, 0, &[], &Account::new(0, 0, post))
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -271,12 +271,12 @@ mod tests {
 | 
			
		||||
        let mallory_program_id = Keypair::new().pubkey();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            change_program_id(system_program_id, system_program_id, alice_program_id),
 | 
			
		||||
            change_program_id(&system_program_id, &system_program_id, &alice_program_id),
 | 
			
		||||
            Ok(()),
 | 
			
		||||
            "system program should be able to change the account owner"
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            change_program_id(mallory_program_id, system_program_id, alice_program_id),
 | 
			
		||||
            change_program_id(&mallory_program_id, &system_program_id, &alice_program_id),
 | 
			
		||||
            Err(ProgramError::ModifiedProgramId),
 | 
			
		||||
            "malicious Mallory should not be able to change the account owner"
 | 
			
		||||
        );
 | 
			
		||||
@@ -284,9 +284,9 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_verify_instruction_change_userdata() {
 | 
			
		||||
        fn change_userdata(program_id: Pubkey) -> Result<(), ProgramError> {
 | 
			
		||||
        fn change_userdata(program_id: &Pubkey) -> Result<(), ProgramError> {
 | 
			
		||||
            let alice_program_id = Keypair::new().pubkey();
 | 
			
		||||
            let account = Account::new(0, 0, alice_program_id);
 | 
			
		||||
            let account = Account::new(0, 0, &alice_program_id);
 | 
			
		||||
            verify_instruction(&program_id, &alice_program_id, 0, &[42], &account)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -294,12 +294,12 @@ mod tests {
 | 
			
		||||
        let mallory_program_id = Keypair::new().pubkey();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            change_userdata(system_program_id),
 | 
			
		||||
            change_userdata(&system_program_id),
 | 
			
		||||
            Ok(()),
 | 
			
		||||
            "system program should be able to change the userdata"
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            change_userdata(mallory_program_id),
 | 
			
		||||
            change_userdata(&mallory_program_id),
 | 
			
		||||
            Err(ProgramError::ExternalAccountUserdataModified),
 | 
			
		||||
            "malicious Mallory should not be able to change the account userdata"
 | 
			
		||||
        );
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user