2019-04-05 09:42:54 -07:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use solana_sdk::transaction::{Result, Transaction};
|
2019-04-02 03:55:42 -07:00
|
|
|
|
|
|
|
// Represents the results of trying to lock a set of accounts
|
2019-06-10 22:05:46 -07:00
|
|
|
pub struct LockedAccountsResults<'a, 'b> {
|
2019-04-02 03:55:42 -07:00
|
|
|
locked_accounts_results: Vec<Result<()>>,
|
|
|
|
bank: &'a Bank,
|
2019-06-10 22:05:46 -07:00
|
|
|
transactions: &'b [Transaction],
|
2019-04-02 03:55:42 -07:00
|
|
|
pub(crate) needs_unlock: bool,
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:05:46 -07:00
|
|
|
impl<'a, 'b> LockedAccountsResults<'a, 'b> {
|
2019-04-02 03:55:42 -07:00
|
|
|
pub fn new(
|
|
|
|
locked_accounts_results: Vec<Result<()>>,
|
|
|
|
bank: &'a Bank,
|
2019-06-10 22:05:46 -07:00
|
|
|
transactions: &'b [Transaction],
|
2019-04-02 03:55:42 -07:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
locked_accounts_results,
|
|
|
|
bank,
|
|
|
|
transactions,
|
|
|
|
needs_unlock: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn locked_accounts_results(&self) -> &Vec<Result<()>> {
|
|
|
|
&self.locked_accounts_results
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:05:46 -07:00
|
|
|
pub fn transactions(&self) -> &[Transaction] {
|
2019-04-02 03:55:42 -07:00
|
|
|
self.transactions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock all locked accounts in destructor.
|
2019-06-10 22:05:46 -07:00
|
|
|
impl<'a, 'b> Drop for LockedAccountsResults<'a, 'b> {
|
2019-04-02 03:55:42 -07:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.needs_unlock {
|
|
|
|
self.bank.unlock_accounts(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-05-22 20:39:00 -07:00
|
|
|
use crate::genesis_utils::{create_genesis_block_with_leader, GenesisBlockInfo};
|
2019-05-07 15:51:35 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
|
|
use solana_sdk::system_transaction;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_account_locks() {
|
|
|
|
let (bank, txs) = setup();
|
|
|
|
|
|
|
|
// Test getting locked accounts
|
|
|
|
let lock_results = bank.lock_accounts(&txs);
|
|
|
|
|
|
|
|
// Grab locks
|
|
|
|
assert!(lock_results
|
|
|
|
.locked_accounts_results()
|
|
|
|
.iter()
|
|
|
|
.all(|x| x.is_ok()));
|
|
|
|
|
|
|
|
// Trying to grab locks again should fail
|
|
|
|
let lock_results2 = bank.lock_accounts(&txs);
|
|
|
|
assert!(lock_results2
|
|
|
|
.locked_accounts_results()
|
|
|
|
.iter()
|
|
|
|
.all(|x| x.is_err()));
|
|
|
|
|
|
|
|
// Drop the first set of locks
|
|
|
|
drop(lock_results);
|
|
|
|
|
|
|
|
// Now grabbing locks should work again
|
|
|
|
let lock_results2 = bank.lock_accounts(&txs);
|
|
|
|
assert!(lock_results2
|
|
|
|
.locked_accounts_results()
|
|
|
|
.iter()
|
|
|
|
.all(|x| x.is_ok()));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup() -> (Bank, Vec<Transaction>) {
|
2019-05-23 23:20:04 -07:00
|
|
|
let dummy_leader_pubkey = Pubkey::new_rand();
|
2019-05-22 20:39:00 -07:00
|
|
|
let GenesisBlockInfo {
|
|
|
|
genesis_block,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-05-23 23:20:04 -07:00
|
|
|
} = create_genesis_block_with_leader(500, &dummy_leader_pubkey, 100);
|
2019-05-07 15:51:35 -07:00
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let pubkey2 = Pubkey::new_rand();
|
|
|
|
|
|
|
|
let txs = vec![
|
2019-05-20 10:03:19 -07:00
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_block.hash()),
|
|
|
|
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_block.hash()),
|
2019-05-07 15:51:35 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
(bank, txs)
|
|
|
|
}
|
|
|
|
}
|