2019-04-16 13:32:22 -07:00
|
|
|
use crate::accounts_db::{
|
2019-05-09 19:27:27 -07:00
|
|
|
get_paths_vec, AccountInfo, AccountStorage, AccountsDB, ErrorCounters, InstructionAccounts,
|
|
|
|
InstructionLoaders,
|
2019-04-16 13:32:22 -07:00
|
|
|
};
|
2019-04-15 17:15:50 -07:00
|
|
|
use crate::accounts_index::{AccountsIndex, Fork};
|
2019-04-16 13:32:22 -07:00
|
|
|
use crate::append_vec::StoredAccount;
|
2019-04-02 08:35:38 -07:00
|
|
|
use crate::message_processor::has_duplicates;
|
2018-12-17 12:41:23 -08:00
|
|
|
use bincode::serialize;
|
2019-05-09 19:27:27 -07:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
2019-02-27 20:57:01 -08:00
|
|
|
use log::*;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_metrics::counter::Counter;
|
2018-12-17 12:41:23 -08:00
|
|
|
use solana_sdk::account::Account;
|
2019-03-29 15:11:21 -07:00
|
|
|
use solana_sdk::fee_calculator::FeeCalculator;
|
2019-04-16 08:50:05 -07:00
|
|
|
use solana_sdk::hash::{Hash, Hasher};
|
2019-02-07 08:03:40 -08:00
|
|
|
use solana_sdk::native_loader;
|
2018-12-17 12:41:23 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-02-20 21:31:24 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-04-05 09:42:54 -07:00
|
|
|
use solana_sdk::transaction::Result;
|
2019-03-13 13:37:24 -07:00
|
|
|
use solana_sdk::transaction::{Transaction, TransactionError};
|
2019-05-07 15:51:35 -07:00
|
|
|
use std::borrow::Borrow;
|
2019-02-23 14:23:55 -08:00
|
|
|
use std::env;
|
2019-04-16 13:32:22 -07:00
|
|
|
use std::fs::remove_dir_all;
|
2019-04-17 13:45:33 -07:00
|
|
|
use std::iter::once;
|
2019-04-15 17:15:50 -07:00
|
|
|
use std::ops::Neg;
|
2018-12-24 16:11:20 -08:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2019-04-16 13:32:22 -07:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-05-07 15:51:35 -07:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2019-02-20 21:31:24 -08:00
|
|
|
const ACCOUNTSDB_DIR: &str = "accountsdb";
|
|
|
|
const NUM_ACCOUNT_DIRS: usize = 4;
|
2019-05-07 15:51:35 -07:00
|
|
|
const WAIT_FOR_PARENT_MS: u64 = 5;
|
2018-12-24 16:11:20 -08:00
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum AccountLockType {
|
|
|
|
AccountLock,
|
|
|
|
RecordLock,
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountLocks = Mutex<HashSet<Pubkey>>;
|
|
|
|
|
|
|
|
// Locks for accounts that are currently being recorded + committed
|
|
|
|
type RecordLocks = (
|
|
|
|
// Record Locks for the current bank
|
|
|
|
Arc<AccountLocks>,
|
|
|
|
// Any unreleased record locks from all parent/grandparent banks. We use Arc<Mutex> to
|
2019-04-17 13:45:33 -07:00
|
|
|
// avoid copies when calling new_from_parent().
|
2019-05-07 15:51:35 -07:00
|
|
|
Vec<Arc<AccountLocks>>,
|
2019-04-17 13:45:33 -07:00
|
|
|
);
|
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
/// This structure handles synchronization for db
|
2019-05-09 19:27:27 -07:00
|
|
|
#[derive(Default)]
|
2018-12-17 12:41:23 -08:00
|
|
|
pub struct Accounts {
|
2019-04-15 17:15:50 -07:00
|
|
|
/// Single global AccountsDB
|
|
|
|
pub accounts_db: Arc<AccountsDB>,
|
2018-12-17 12:41:23 -08:00
|
|
|
|
|
|
|
/// set of accounts which are currently in the pipeline
|
2019-05-07 15:51:35 -07:00
|
|
|
account_locks: AccountLocks,
|
|
|
|
|
|
|
|
/// set of accounts which are about to record + commit
|
|
|
|
record_locks: Mutex<RecordLocks>,
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2018-12-24 16:11:20 -08:00
|
|
|
/// List of persistent stores
|
|
|
|
paths: String,
|
2019-03-02 14:02:11 -08:00
|
|
|
|
|
|
|
/// set to true if object created the directories in paths
|
|
|
|
/// when true, delete parents of 'paths' on drop
|
|
|
|
own_paths: bool,
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:11:20 -08:00
|
|
|
impl Drop for Accounts {
|
|
|
|
fn drop(&mut self) {
|
2019-05-09 19:27:27 -07:00
|
|
|
let paths = get_paths_vec(&self.paths);
|
|
|
|
paths.iter().for_each(|p| {
|
|
|
|
let _ignored = remove_dir_all(p);
|
2019-03-02 14:02:11 -08:00
|
|
|
|
2019-05-09 19:27:27 -07:00
|
|
|
// it is safe to delete the parent
|
|
|
|
if self.own_paths {
|
2019-03-02 14:02:11 -08:00
|
|
|
let path = Path::new(p);
|
|
|
|
let _ignored = remove_dir_all(path.parent().unwrap());
|
2019-05-09 19:27:27 -07:00
|
|
|
}
|
|
|
|
});
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
impl Accounts {
|
|
|
|
fn make_new_dir() -> String {
|
|
|
|
static ACCOUNT_DIR: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
let dir = ACCOUNT_DIR.fetch_add(1, Ordering::Relaxed);
|
|
|
|
let out_dir = env::var("OUT_DIR").unwrap_or_else(|_| "target".to_string());
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
format!(
|
|
|
|
"{}/{}/{}/{}",
|
|
|
|
out_dir,
|
|
|
|
ACCOUNTSDB_DIR,
|
|
|
|
keypair.pubkey(),
|
|
|
|
dir.to_string()
|
2019-03-07 12:41:00 -08:00
|
|
|
)
|
2019-03-06 15:12:50 -08:00
|
|
|
}
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
fn make_default_paths() -> String {
|
|
|
|
let mut paths = "".to_string();
|
|
|
|
for index in 0..NUM_ACCOUNT_DIRS {
|
|
|
|
if index > 0 {
|
|
|
|
paths.push_str(",");
|
2019-02-20 12:17:32 -08:00
|
|
|
}
|
2019-04-16 13:32:22 -07:00
|
|
|
paths.push_str(&Self::make_new_dir());
|
2019-02-20 12:17:32 -08:00
|
|
|
}
|
2019-04-16 13:32:22 -07:00
|
|
|
paths
|
2019-04-16 08:50:05 -07:00
|
|
|
}
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
pub fn new(in_paths: Option<String>) -> Self {
|
|
|
|
let (paths, own_paths) = if in_paths.is_none() {
|
|
|
|
(Self::make_default_paths(), true)
|
2019-04-15 17:15:50 -07:00
|
|
|
} else {
|
2019-04-16 13:32:22 -07:00
|
|
|
(in_paths.unwrap(), false)
|
|
|
|
};
|
|
|
|
let accounts_db = Arc::new(AccountsDB::new(&paths));
|
|
|
|
Accounts {
|
|
|
|
accounts_db,
|
2019-05-07 15:51:35 -07:00
|
|
|
account_locks: Mutex::new(HashSet::new()),
|
|
|
|
record_locks: Mutex::new((Arc::new(Mutex::new(HashSet::new())), vec![])),
|
2019-04-16 13:32:22 -07:00
|
|
|
paths,
|
|
|
|
own_paths,
|
2018-12-24 16:11:20 -08:00
|
|
|
}
|
2019-02-28 17:34:37 -08:00
|
|
|
}
|
2019-04-17 13:45:33 -07:00
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
pub fn new_from_parent(parent: &Accounts) -> Self {
|
|
|
|
let accounts_db = parent.accounts_db.clone();
|
2019-05-07 15:51:35 -07:00
|
|
|
let parent_record_locks: Vec<_> = {
|
|
|
|
let (ref record_locks, ref mut grandparent_record_locks) =
|
|
|
|
*parent.record_locks.lock().unwrap();
|
|
|
|
|
|
|
|
// Note that when creating a child bank, we only care about the locks that are held for
|
|
|
|
// accounts that are in txs that are currently recording + committing, because other
|
|
|
|
// incoming txs on this bank that are not yet recording will not make it to bank commit.
|
|
|
|
//
|
2019-04-17 13:45:33 -07:00
|
|
|
// Thus:
|
|
|
|
// 1) The child doesn't need to care about potential "future" account locks on its parent
|
|
|
|
// bank that the parent does not currently hold.
|
2019-05-07 15:51:35 -07:00
|
|
|
// 2) The child only needs the currently held "record locks" from the parent.
|
|
|
|
// 3) The parent doesn't need to retain any of the locks other than the ones it owns so
|
2019-04-17 13:45:33 -07:00
|
|
|
// that unlock() can be called later (the grandparent locks can be given to the child).
|
2019-05-07 15:51:35 -07:00
|
|
|
once(record_locks.clone())
|
|
|
|
.chain(grandparent_record_locks.drain(..))
|
2019-04-17 13:45:33 -07:00
|
|
|
.filter(|a| !a.lock().unwrap().is_empty())
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
Accounts {
|
|
|
|
accounts_db,
|
2019-05-07 15:51:35 -07:00
|
|
|
account_locks: Mutex::new(HashSet::new()),
|
|
|
|
record_locks: Mutex::new((Arc::new(Mutex::new(HashSet::new())), parent_record_locks)),
|
2019-04-16 13:32:22 -07:00
|
|
|
paths: parent.paths.clone(),
|
|
|
|
own_paths: parent.own_paths,
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2018-12-24 16:11:20 -08:00
|
|
|
|
|
|
|
fn load_tx_accounts(
|
2019-04-23 09:56:36 -07:00
|
|
|
storage: &AccountStorage,
|
2019-04-15 17:15:50 -07:00
|
|
|
ancestors: &HashMap<Fork, usize>,
|
|
|
|
accounts_index: &AccountsIndex<AccountInfo>,
|
2018-12-17 12:41:23 -08:00
|
|
|
tx: &Transaction,
|
2019-03-29 15:11:21 -07:00
|
|
|
fee: u64,
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
2018-12-24 16:11:20 -08:00
|
|
|
) -> Result<Vec<Account>> {
|
2018-12-17 12:41:23 -08:00
|
|
|
// Copy all the accounts
|
2019-03-29 09:05:06 -07:00
|
|
|
let message = tx.message();
|
2019-03-29 15:11:21 -07:00
|
|
|
if tx.signatures.is_empty() && fee != 0 {
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::MissingSignatureForFee)
|
2018-12-17 12:41:23 -08:00
|
|
|
} else {
|
2019-02-07 11:14:10 -08:00
|
|
|
// Check for unique account keys
|
2019-03-29 09:05:06 -07:00
|
|
|
if has_duplicates(&message.account_keys) {
|
2019-02-07 11:14:10 -08:00
|
|
|
error_counters.account_loaded_twice += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountLoadedTwice);
|
2019-02-07 11:14:10 -08:00
|
|
|
}
|
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
// There is no way to predict what program will execute without an error
|
|
|
|
// If a fee can pay for execution then the program will be scheduled
|
2019-01-29 16:33:28 -08:00
|
|
|
let mut called_accounts: Vec<Account> = vec![];
|
2019-03-29 09:05:06 -07:00
|
|
|
for key in &message.account_keys {
|
2019-04-16 13:32:22 -07:00
|
|
|
called_accounts.push(
|
2019-05-06 07:31:50 -07:00
|
|
|
AccountsDB::load(storage, ancestors, accounts_index, key)
|
|
|
|
.map(|(account, _)| account)
|
|
|
|
.unwrap_or_default(),
|
2019-04-16 13:32:22 -07:00
|
|
|
);
|
2019-01-29 16:33:28 -08:00
|
|
|
}
|
2019-03-05 16:28:14 -08:00
|
|
|
if called_accounts.is_empty() || called_accounts[0].lamports == 0 {
|
2019-01-29 16:33:28 -08:00
|
|
|
error_counters.account_not_found += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountNotFound)
|
2019-03-29 15:11:21 -07:00
|
|
|
} else if called_accounts[0].lamports < fee {
|
2019-01-29 16:33:28 -08:00
|
|
|
error_counters.insufficient_funds += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::InsufficientFundsForFee)
|
2019-01-29 16:33:28 -08:00
|
|
|
} else {
|
2019-03-29 15:11:21 -07:00
|
|
|
called_accounts[0].lamports -= fee;
|
2019-01-29 16:33:28 -08:00
|
|
|
Ok(called_accounts)
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
2019-01-04 16:39:04 -08:00
|
|
|
|
2018-12-24 16:11:20 -08:00
|
|
|
fn load_executable_accounts(
|
2019-04-23 09:56:36 -07:00
|
|
|
storage: &AccountStorage,
|
2019-04-15 17:15:50 -07:00
|
|
|
ancestors: &HashMap<Fork, usize>,
|
|
|
|
accounts_index: &AccountsIndex<AccountInfo>,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: &Pubkey,
|
2019-01-08 09:20:25 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
2018-12-24 16:11:20 -08:00
|
|
|
) -> Result<Vec<(Pubkey, Account)>> {
|
2019-01-06 22:06:55 -08:00
|
|
|
let mut accounts = Vec::new();
|
|
|
|
let mut depth = 0;
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut program_id = *program_id;
|
2019-01-06 22:06:55 -08:00
|
|
|
loop {
|
2019-02-07 08:03:40 -08:00
|
|
|
if native_loader::check_id(&program_id) {
|
2019-01-06 22:06:55 -08:00
|
|
|
// at the root of the chain, ready to dispatch
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if depth >= 5 {
|
2019-01-08 09:20:25 -08:00
|
|
|
error_counters.call_chain_too_deep += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::CallChainTooDeep);
|
2019-01-06 22:06:55 -08:00
|
|
|
}
|
|
|
|
depth += 1;
|
|
|
|
|
2019-05-06 07:31:50 -07:00
|
|
|
let program = match AccountsDB::load(storage, ancestors, accounts_index, &program_id)
|
|
|
|
.map(|(account, _)| account)
|
|
|
|
{
|
2019-01-06 22:06:55 -08:00
|
|
|
Some(program) => program,
|
2019-01-08 09:20:25 -08:00
|
|
|
None => {
|
|
|
|
error_counters.account_not_found += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountNotFound);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
2019-01-06 22:06:55 -08:00
|
|
|
};
|
2019-02-14 09:57:54 -08:00
|
|
|
if !program.executable || program.owner == Pubkey::default() {
|
2019-01-08 09:20:25 -08:00
|
|
|
error_counters.account_not_found += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountNotFound);
|
2019-01-06 22:06:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add loader to chain
|
2019-02-14 09:57:54 -08:00
|
|
|
program_id = program.owner;
|
2019-04-15 17:15:50 -07:00
|
|
|
accounts.insert(0, (program_id, program));
|
2019-01-06 22:06:55 -08:00
|
|
|
}
|
|
|
|
Ok(accounts)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For each program_id in the transaction, load its loaders.
|
2018-12-24 16:11:20 -08:00
|
|
|
fn load_loaders(
|
2019-04-23 09:56:36 -07:00
|
|
|
storage: &AccountStorage,
|
2019-04-15 17:15:50 -07:00
|
|
|
ancestors: &HashMap<Fork, usize>,
|
|
|
|
accounts_index: &AccountsIndex<AccountInfo>,
|
2019-01-08 09:20:25 -08:00
|
|
|
tx: &Transaction,
|
|
|
|
error_counters: &mut ErrorCounters,
|
2018-12-24 16:11:20 -08:00
|
|
|
) -> Result<Vec<Vec<(Pubkey, Account)>>> {
|
2019-03-29 09:05:06 -07:00
|
|
|
let message = tx.message();
|
|
|
|
message
|
|
|
|
.instructions
|
2019-01-06 22:06:55 -08:00
|
|
|
.iter()
|
|
|
|
.map(|ix| {
|
2019-04-02 15:02:57 -07:00
|
|
|
if message.program_ids().len() <= ix.program_ids_index as usize {
|
2019-01-08 09:20:25 -08:00
|
|
|
error_counters.account_not_found += 1;
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountNotFound);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
2019-04-02 15:02:57 -07:00
|
|
|
let program_id = message.program_ids()[ix.program_ids_index as usize];
|
2019-04-15 17:15:50 -07:00
|
|
|
Self::load_executable_accounts(
|
|
|
|
storage,
|
|
|
|
ancestors,
|
|
|
|
accounts_index,
|
|
|
|
&program_id,
|
|
|
|
error_counters,
|
|
|
|
)
|
2019-01-06 22:06:55 -08:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
fn load_accounts_internal(
|
2018-12-24 16:11:20 -08:00
|
|
|
&self,
|
2019-04-15 17:15:50 -07:00
|
|
|
ancestors: &HashMap<Fork, usize>,
|
2018-12-17 12:41:23 -08:00
|
|
|
txs: &[Transaction],
|
2019-01-06 22:06:55 -08:00
|
|
|
lock_results: Vec<Result<()>>,
|
2019-03-29 15:11:21 -07:00
|
|
|
fee_calculator: &FeeCalculator,
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
2018-12-24 16:11:20 -08:00
|
|
|
) -> Vec<Result<(InstructionAccounts, InstructionLoaders)>> {
|
2019-04-15 17:15:50 -07:00
|
|
|
//PERF: hold the lock to scan for the references, but not to clone the accounts
|
|
|
|
//TODO: two locks usually leads to deadlocks, should this be one structure?
|
2019-04-16 13:32:22 -07:00
|
|
|
let accounts_index = self.accounts_db.accounts_index.read().unwrap();
|
|
|
|
let storage = self.accounts_db.storage.read().unwrap();
|
2018-12-17 12:41:23 -08:00
|
|
|
txs.iter()
|
2019-01-06 22:06:55 -08:00
|
|
|
.zip(lock_results.into_iter())
|
2018-12-17 12:41:23 -08:00
|
|
|
.map(|etx| match etx {
|
2019-01-06 22:06:55 -08:00
|
|
|
(tx, Ok(())) => {
|
2019-03-29 15:11:21 -07:00
|
|
|
let fee = fee_calculator.calculate_fee(tx.message());
|
2019-04-15 17:15:50 -07:00
|
|
|
let accounts = Self::load_tx_accounts(
|
|
|
|
&storage,
|
|
|
|
ancestors,
|
|
|
|
&accounts_index,
|
|
|
|
tx,
|
|
|
|
fee,
|
|
|
|
error_counters,
|
|
|
|
)?;
|
|
|
|
let loaders = Self::load_loaders(
|
|
|
|
&storage,
|
|
|
|
ancestors,
|
|
|
|
&accounts_index,
|
|
|
|
tx,
|
|
|
|
error_counters,
|
|
|
|
)?;
|
2019-01-06 22:06:55 -08:00
|
|
|
Ok((accounts, loaders))
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
(_, Err(e)) => Err(e),
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2019-01-04 16:04:31 -08:00
|
|
|
|
2019-03-07 14:58:11 -08:00
|
|
|
/// Slow because lock is held for 1 operation instead of many
|
2019-05-06 07:31:50 -07:00
|
|
|
pub fn load_slow(
|
|
|
|
&self,
|
|
|
|
ancestors: &HashMap<Fork, usize>,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
) -> Option<(Account, Fork)> {
|
2019-02-26 21:41:05 -08:00
|
|
|
self.accounts_db
|
2019-04-15 17:15:50 -07:00
|
|
|
.load_slow(ancestors, pubkey)
|
2019-05-06 07:31:50 -07:00
|
|
|
.filter(|(acc, _)| acc.lamports != 0)
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2018-12-24 16:11:20 -08:00
|
|
|
|
2019-04-15 17:15:50 -07:00
|
|
|
pub fn load_by_program(&self, fork: Fork, program_id: &Pubkey) -> Vec<(Pubkey, Account)> {
|
2019-04-16 08:50:05 -07:00
|
|
|
let accumulator: Vec<Vec<(Pubkey, u64, Account)>> = self.accounts_db.scan_account_storage(
|
2019-04-15 17:15:50 -07:00
|
|
|
fork,
|
2019-05-09 19:27:27 -07:00
|
|
|
|stored_account: &StoredAccount, accum: &mut Vec<(Pubkey, u64, Account)>| {
|
2019-04-16 08:50:05 -07:00
|
|
|
if stored_account.balance.owner == *program_id {
|
|
|
|
let val = (
|
|
|
|
stored_account.meta.pubkey,
|
|
|
|
stored_account.meta.write_version,
|
|
|
|
stored_account.clone_account(),
|
|
|
|
);
|
|
|
|
accum.push(val)
|
2019-04-15 17:15:50 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-04-16 08:50:05 -07:00
|
|
|
let mut versions: Vec<(Pubkey, u64, Account)> =
|
|
|
|
accumulator.into_iter().flat_map(|x| x).collect();
|
|
|
|
versions.sort_by_key(|s| (s.0, (s.1 as i64).neg()));
|
|
|
|
versions.dedup_by_key(|s| s.0);
|
|
|
|
versions.into_iter().map(|s| (s.0, s.2)).collect()
|
2019-03-07 08:51:56 -08:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:58:11 -08:00
|
|
|
/// Slow because lock is held for 1 operation instead of many
|
2019-02-27 16:06:06 -08:00
|
|
|
pub fn store_slow(&self, fork: Fork, pubkey: &Pubkey, account: &Account) {
|
2019-04-15 17:15:50 -07:00
|
|
|
self.accounts_db.store(fork, &[(pubkey, account)]);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2019-01-04 16:04:31 -08:00
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
fn lock_account(
|
2019-05-07 15:51:35 -07:00
|
|
|
(fork_locks, parent_locks): (&mut HashSet<Pubkey>, &mut Vec<Arc<AccountLocks>>),
|
2018-12-17 12:41:23 -08:00
|
|
|
keys: &[Pubkey],
|
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Copy all the accounts
|
|
|
|
for k in keys {
|
2019-04-17 13:45:33 -07:00
|
|
|
let is_locked = {
|
|
|
|
if fork_locks.contains(k) {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// Check parent locks. As soon as a set of parent locks is empty,
|
|
|
|
// we can remove it from the list b/c that means the parent has
|
|
|
|
// released the locks.
|
|
|
|
parent_locks.retain(|p| {
|
2019-05-07 15:51:35 -07:00
|
|
|
loop {
|
|
|
|
{
|
|
|
|
// If a parent bank holds a record lock for this account, then loop
|
|
|
|
// until that lock is released
|
|
|
|
let p = p.lock().unwrap();
|
|
|
|
if !p.contains(k) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a parent is currently recording for this key, then drop lock and wait
|
|
|
|
sleep(Duration::from_millis(WAIT_FOR_PARENT_MS));
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
let p = p.lock().unwrap();
|
2019-04-17 13:45:33 -07:00
|
|
|
!p.is_empty()
|
|
|
|
});
|
2019-05-07 15:51:35 -07:00
|
|
|
false
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if is_locked {
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters.account_in_use += 1;
|
2019-03-28 11:45:34 -07:00
|
|
|
debug!("Account in use: {:?}", k);
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountInUse);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k in keys {
|
2019-04-17 13:45:33 -07:00
|
|
|
fork_locks.insert(*k);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
fn lock_record_account(record_locks: &AccountLocks, keys: &[Pubkey]) {
|
|
|
|
let mut fork_locks = record_locks.lock().unwrap();
|
|
|
|
for k in keys {
|
|
|
|
// The fork locks should always be a subset of the account locks, so
|
|
|
|
// the account locks should prevent record locks from ever touching the
|
|
|
|
// same accounts
|
|
|
|
assert!(!fork_locks.contains(k));
|
|
|
|
fork_locks.insert(*k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:15:50 -07:00
|
|
|
fn unlock_account(tx: &Transaction, result: &Result<()>, locks: &mut HashSet<Pubkey>) {
|
2018-12-17 12:41:23 -08:00
|
|
|
match result {
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountInUse) => (),
|
2018-12-17 12:41:23 -08:00
|
|
|
_ => {
|
2019-04-15 17:15:50 -07:00
|
|
|
for k in &tx.message().account_keys {
|
|
|
|
locks.remove(k);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 16:04:31 -08:00
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
fn unlock_record_account<I>(tx: &I, locks: &mut HashSet<Pubkey>)
|
|
|
|
where
|
|
|
|
I: Borrow<Transaction>,
|
|
|
|
{
|
|
|
|
for k in &tx.borrow().message().account_keys {
|
|
|
|
locks.remove(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
fn hash_account(stored_account: &StoredAccount) -> Hash {
|
|
|
|
let mut hasher = Hasher::default();
|
|
|
|
hasher.hash(&serialize(&stored_account.balance).unwrap());
|
|
|
|
hasher.hash(stored_account.data);
|
|
|
|
hasher.result()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_internal_state(&self, fork_id: Fork) -> Option<Hash> {
|
|
|
|
let accumulator: Vec<Vec<(Pubkey, u64, Hash)>> = self.accounts_db.scan_account_storage(
|
|
|
|
fork_id,
|
2019-05-09 19:27:27 -07:00
|
|
|
|stored_account: &StoredAccount, accum: &mut Vec<(Pubkey, u64, Hash)>| {
|
2019-04-16 13:32:22 -07:00
|
|
|
accum.push((
|
|
|
|
stored_account.meta.pubkey,
|
|
|
|
stored_account.meta.write_version,
|
|
|
|
Self::hash_account(stored_account),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let mut account_hashes: Vec<_> = accumulator.into_iter().flat_map(|x| x).collect();
|
|
|
|
account_hashes.sort_by_key(|s| (s.0, (s.1 as i64).neg()));
|
|
|
|
account_hashes.dedup_by_key(|s| s.0);
|
|
|
|
if account_hashes.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let mut hasher = Hasher::default();
|
|
|
|
for (_, _, hash) in account_hashes {
|
|
|
|
hasher.hash(hash.as_ref());
|
|
|
|
}
|
|
|
|
Some(hasher.result())
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function will prevent multiple threads from modifying the same account state at the
|
|
|
|
/// same time
|
|
|
|
#[must_use]
|
2019-05-07 15:51:35 -07:00
|
|
|
pub fn lock_accounts<I>(&self, txs: &[I]) -> Vec<Result<()>>
|
|
|
|
where
|
|
|
|
I: Borrow<Transaction>,
|
|
|
|
{
|
|
|
|
let (_, ref mut parent_record_locks) = *self.record_locks.lock().unwrap();
|
2018-12-17 12:41:23 -08:00
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
let rv = txs
|
|
|
|
.iter()
|
2019-02-20 21:31:24 -08:00
|
|
|
.map(|tx| {
|
|
|
|
Self::lock_account(
|
2019-05-07 15:51:35 -07:00
|
|
|
(&mut self.account_locks.lock().unwrap(), parent_record_locks),
|
|
|
|
&tx.borrow().message().account_keys,
|
2019-02-20 21:31:24 -08:00
|
|
|
&mut error_counters,
|
|
|
|
)
|
|
|
|
})
|
2018-12-17 12:41:23 -08:00
|
|
|
.collect();
|
|
|
|
if error_counters.account_in_use != 0 {
|
|
|
|
inc_new_counter_info!(
|
|
|
|
"bank-process_transactions-account_in_use",
|
|
|
|
error_counters.account_in_use
|
|
|
|
);
|
|
|
|
}
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
pub fn lock_record_accounts<I>(&self, txs: &[I])
|
|
|
|
where
|
|
|
|
I: Borrow<Transaction>,
|
|
|
|
{
|
|
|
|
let record_locks = self.record_locks.lock().unwrap();
|
|
|
|
for tx in txs {
|
|
|
|
Self::lock_record_account(&record_locks.0, &tx.borrow().message().account_keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
/// Once accounts are unlocked, new transactions that modify that state can enter the pipeline
|
2019-05-07 15:51:35 -07:00
|
|
|
pub fn unlock_accounts<I>(&self, txs: &[I], results: &[Result<()>])
|
|
|
|
where
|
|
|
|
I: Borrow<Transaction>,
|
|
|
|
{
|
|
|
|
let my_locks = &mut self.account_locks.lock().unwrap();
|
2018-12-17 12:41:23 -08:00
|
|
|
debug!("bank unlock accounts");
|
2019-05-07 15:51:35 -07:00
|
|
|
txs.iter()
|
|
|
|
.zip(results.iter())
|
|
|
|
.for_each(|(tx, result)| Self::unlock_account(tx.borrow(), result, my_locks));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unlock_record_accounts<I>(&self, txs: &[I])
|
|
|
|
where
|
|
|
|
I: Borrow<Transaction>,
|
|
|
|
{
|
|
|
|
let (ref my_record_locks, _) = *self.record_locks.lock().unwrap();
|
|
|
|
for tx in txs {
|
|
|
|
Self::unlock_record_account(tx, &mut my_record_locks.lock().unwrap())
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:17:32 -08:00
|
|
|
pub fn has_accounts(&self, fork: Fork) -> bool {
|
|
|
|
self.accounts_db.has_accounts(fork)
|
|
|
|
}
|
|
|
|
|
2018-12-24 16:11:20 -08:00
|
|
|
pub fn load_accounts(
|
|
|
|
&self,
|
2019-04-15 17:15:50 -07:00
|
|
|
ancestors: &HashMap<Fork, usize>,
|
2018-12-17 12:41:23 -08:00
|
|
|
txs: &[Transaction],
|
2019-01-29 16:33:28 -08:00
|
|
|
results: Vec<Result<()>>,
|
2019-03-29 15:11:21 -07:00
|
|
|
fee_calculator: &FeeCalculator,
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
2018-12-24 16:11:20 -08:00
|
|
|
) -> Vec<Result<(InstructionAccounts, InstructionLoaders)>> {
|
2019-04-16 13:32:22 -07:00
|
|
|
self.load_accounts_internal(ancestors, txs, results, fee_calculator, error_counters)
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
/// Store the accounts into the DB
|
2018-12-17 12:41:23 -08:00
|
|
|
pub fn store_accounts(
|
|
|
|
&self,
|
2018-12-24 16:11:20 -08:00
|
|
|
fork: Fork,
|
2018-12-17 12:41:23 -08:00
|
|
|
txs: &[Transaction],
|
|
|
|
res: &[Result<()>],
|
2019-01-06 22:06:55 -08:00
|
|
|
loaded: &[Result<(InstructionAccounts, InstructionLoaders)>],
|
2019-02-19 17:11:43 -08:00
|
|
|
) {
|
2019-04-16 13:32:22 -07:00
|
|
|
let mut accounts: Vec<(&Pubkey, &Account)> = vec![];
|
|
|
|
for (i, raccs) in loaded.iter().enumerate() {
|
|
|
|
if res[i].is_err() || raccs.is_err() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = &txs[i].message();
|
|
|
|
let acc = raccs.as_ref().unwrap();
|
|
|
|
for (key, account) in message.account_keys.iter().zip(acc.0.iter()) {
|
|
|
|
accounts.push((key, account));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.accounts_db.store(fork, &accounts);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:15:50 -07:00
|
|
|
/// Purge a fork if it is not a root
|
|
|
|
/// Root forks cannot be purged
|
|
|
|
pub fn purge_fork(&self, fork: Fork) {
|
|
|
|
self.accounts_db.purge_fork(fork);
|
2018-12-24 16:11:20 -08:00
|
|
|
}
|
2019-04-15 17:15:50 -07:00
|
|
|
/// Add a fork to root. Root forks cannot be purged
|
|
|
|
pub fn add_root(&self, fork: Fork) {
|
|
|
|
self.accounts_db.add_root(fork)
|
2019-03-21 17:36:10 -07:00
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
// TODO: all the bank tests are bank specific, issue: 2194
|
2019-01-08 09:20:25 -08:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use solana_sdk::account::Account;
|
|
|
|
use solana_sdk::hash::Hash;
|
2019-03-23 20:12:27 -07:00
|
|
|
use solana_sdk::instruction::CompiledInstruction;
|
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-01-08 09:20:25 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2019-05-07 15:51:35 -07:00
|
|
|
use std::thread::{sleep, Builder};
|
|
|
|
use std::time::{Duration, Instant};
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-03-29 15:11:21 -07:00
|
|
|
fn load_accounts_with_fee(
|
2019-01-08 09:20:25 -08:00
|
|
|
tx: Transaction,
|
|
|
|
ka: &Vec<(Pubkey, Account)>,
|
2019-03-29 15:11:21 -07:00
|
|
|
fee_calculator: &FeeCalculator,
|
2019-01-08 09:20:25 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Vec<Result<(InstructionAccounts, InstructionLoaders)>> {
|
2019-04-15 17:15:50 -07:00
|
|
|
let accounts = Accounts::new(None);
|
2019-01-08 09:20:25 -08:00
|
|
|
for ka in ka.iter() {
|
2019-02-27 16:06:06 -08:00
|
|
|
accounts.store_slow(0, &ka.0, &ka.1);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:15:50 -07:00
|
|
|
let ancestors = vec![(0, 0)].into_iter().collect();
|
|
|
|
let res = accounts.load_accounts(
|
|
|
|
&ancestors,
|
|
|
|
&[tx],
|
|
|
|
vec![Ok(())],
|
|
|
|
&fee_calculator,
|
|
|
|
error_counters,
|
|
|
|
);
|
2018-12-24 16:11:20 -08:00
|
|
|
res
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:11:21 -07:00
|
|
|
fn load_accounts(
|
|
|
|
tx: Transaction,
|
|
|
|
ka: &Vec<(Pubkey, Account)>,
|
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Vec<Result<(InstructionAccounts, InstructionLoaders)>> {
|
|
|
|
let fee_calculator = FeeCalculator::default();
|
|
|
|
load_accounts_with_fee(tx, ka, &fee_calculator, error_counters)
|
|
|
|
}
|
|
|
|
|
2019-01-08 09:20:25 -08:00
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_no_key() {
|
|
|
|
let accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions::<Keypair>(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
2019-02-07 08:03:40 -08:00
|
|
|
vec![native_loader::id()],
|
2019-01-08 09:20:25 -08:00
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::AccountNotFound));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_no_account_0_exists() {
|
|
|
|
let accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
2019-02-07 08:03:40 -08:00
|
|
|
vec![native_loader::id()],
|
2019-01-08 09:20:25 -08:00
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::AccountNotFound));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_unknown_program_id() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(2, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![Pubkey::default()],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::AccountNotFound));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_insufficient_funds() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
2019-02-07 08:03:40 -08:00
|
|
|
vec![native_loader::id()],
|
2019-01-08 09:20:25 -08:00
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-03-29 15:11:21 -07:00
|
|
|
let fee_calculator = FeeCalculator::new(10);
|
|
|
|
assert_eq!(fee_calculator.calculate_fee(tx.message()), 10);
|
|
|
|
|
|
|
|
let loaded_accounts =
|
|
|
|
load_accounts_with_fee(tx, &accounts, &fee_calculator, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.insufficient_funds, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(
|
|
|
|
loaded_accounts[0],
|
|
|
|
Err(TransactionError::InsufficientFundsForFee)
|
|
|
|
);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_no_loaders() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(2, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(0, &(), vec![0, 1])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[key1],
|
|
|
|
Hash::default(),
|
2019-02-07 08:03:40 -08:00
|
|
|
vec![native_loader::id()],
|
2019-01-08 09:20:25 -08:00
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 0);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
|
|
|
match &loaded_accounts[0] {
|
|
|
|
Ok((a, l)) => {
|
|
|
|
assert_eq!(a.len(), 2);
|
|
|
|
assert_eq!(a[0], accounts[0].1);
|
|
|
|
assert_eq!(l.len(), 1);
|
|
|
|
assert_eq!(l[0].len(), 0);
|
|
|
|
}
|
|
|
|
Err(e) => Err(e).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_max_call_depth() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
let key2 = Pubkey::new(&[6u8; 32]);
|
|
|
|
let key3 = Pubkey::new(&[7u8; 32]);
|
|
|
|
let key4 = Pubkey::new(&[8u8; 32]);
|
|
|
|
let key5 = Pubkey::new(&[9u8; 32]);
|
|
|
|
let key6 = Pubkey::new(&[10u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(40, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = native_loader::id();
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(41, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key1;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key2, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(42, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key2;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key3, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(43, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key3;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key4, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(44, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key4;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key5, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(45, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key5;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key6, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(0, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![key6],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.call_chain_too_deep, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::CallChainTooDeep));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_bad_program_id() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(40, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = Pubkey::default();
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(0, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![key1],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::AccountNotFound));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_not_executable() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(40, 1, &Pubkey::default());
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = native_loader::id();
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(0, &(), vec![0])];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![key1],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(loaded_accounts[0], Err(TransactionError::AccountNotFound));
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_multiple_loaders() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
let key1 = Pubkey::new(&[5u8; 32]);
|
|
|
|
let key2 = Pubkey::new(&[6u8; 32]);
|
|
|
|
let key3 = Pubkey::new(&[7u8; 32]);
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(1, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(40, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = native_loader::id();
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(41, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key1;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key2, account));
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let mut account = Account::new(42, 1, &Pubkey::default());
|
2019-01-08 09:20:25 -08:00
|
|
|
account.executable = true;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key2;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key3, account));
|
|
|
|
|
|
|
|
let instructions = vec![
|
2019-03-15 08:47:25 -07:00
|
|
|
CompiledInstruction::new(0, &(), vec![0]),
|
|
|
|
CompiledInstruction::new(1, &(), vec![0]),
|
2019-01-08 09:20:25 -08:00
|
|
|
];
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-01-08 09:20:25 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![key1, key2],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:33:28 -08:00
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-02-07 01:54:27 -08:00
|
|
|
assert_eq!(error_counters.account_not_found, 0);
|
2019-01-08 09:20:25 -08:00
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
|
|
|
match &loaded_accounts[0] {
|
|
|
|
Ok((a, l)) => {
|
|
|
|
assert_eq!(a.len(), 1);
|
|
|
|
assert_eq!(a[0], accounts[0].1);
|
|
|
|
assert_eq!(l.len(), 2);
|
|
|
|
assert_eq!(l[0].len(), 1);
|
|
|
|
assert_eq!(l[1].len(), 2);
|
|
|
|
for instruction_loaders in l.iter() {
|
|
|
|
for (i, a) in instruction_loaders.iter().enumerate() {
|
|
|
|
// +1 to skip first not loader account
|
|
|
|
assert_eq![a.1, accounts[i + 1].1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 11:14:10 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_account_pay_to_self() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let pubkey = keypair.pubkey();
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
let account = Account::new(10, 1, &Pubkey::default());
|
2019-02-07 11:14:10 -08:00
|
|
|
accounts.push((pubkey, account));
|
|
|
|
|
2019-03-15 08:47:25 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(0, &(), vec![0, 1])];
|
2019-02-07 11:14:10 -08:00
|
|
|
// Simulate pay-to-self transaction, which loads the same account twice
|
2019-03-15 09:02:28 -07:00
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
2019-02-07 11:14:10 -08:00
|
|
|
&[&keypair],
|
|
|
|
&[pubkey],
|
|
|
|
Hash::default(),
|
|
|
|
vec![native_loader::id()],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
|
|
|
|
|
|
|
assert_eq!(error_counters.account_loaded_twice, 1);
|
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
|
|
|
loaded_accounts[0].clone().unwrap_err();
|
2019-03-13 12:58:44 -07:00
|
|
|
assert_eq!(
|
|
|
|
loaded_accounts[0],
|
|
|
|
Err(TransactionError::AccountLoadedTwice)
|
|
|
|
);
|
2019-02-07 11:14:10 -08:00
|
|
|
}
|
2019-02-21 12:08:50 -08:00
|
|
|
|
|
|
|
#[test]
|
2019-04-16 13:32:22 -07:00
|
|
|
fn test_load_by_program() {
|
|
|
|
let accounts = Accounts::new(None);
|
2019-03-07 12:41:00 -08:00
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
// Load accounts owned by various programs into AccountsDB
|
|
|
|
let pubkey0 = Pubkey::new_rand();
|
|
|
|
let account0 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
|
|
|
|
accounts.store_slow(0, &pubkey0, &account0);
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey1 = Pubkey::new_rand();
|
2019-04-16 13:32:22 -07:00
|
|
|
let account1 = Account::new(1, 0, &Pubkey::new(&[2; 32]));
|
|
|
|
accounts.store_slow(0, &pubkey1, &account1);
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey2 = Pubkey::new_rand();
|
2019-04-16 13:32:22 -07:00
|
|
|
let account2 = Account::new(1, 0, &Pubkey::new(&[3; 32]));
|
|
|
|
accounts.store_slow(0, &pubkey2, &account2);
|
2019-02-27 21:42:14 -08:00
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
let loaded = accounts.load_by_program(0, &Pubkey::new(&[2; 32]));
|
|
|
|
assert_eq!(loaded.len(), 2);
|
|
|
|
let loaded = accounts.load_by_program(0, &Pubkey::new(&[3; 32]));
|
|
|
|
assert_eq!(loaded, vec![(pubkey2, account2)]);
|
|
|
|
let loaded = accounts.load_by_program(0, &Pubkey::new(&[4; 32]));
|
|
|
|
assert_eq!(loaded, vec![]);
|
2019-03-01 11:53:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-16 13:32:22 -07:00
|
|
|
fn test_accounts_account_not_found() {
|
|
|
|
let accounts = Accounts::new(None);
|
2019-03-01 11:53:39 -08:00
|
|
|
let mut error_counters = ErrorCounters::default();
|
2019-04-15 17:15:50 -07:00
|
|
|
let ancestors = vec![(0, 0)].into_iter().collect();
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
let accounts_index = accounts.accounts_db.accounts_index.read().unwrap();
|
|
|
|
let storage = accounts.accounts_db.storage.read().unwrap();
|
2019-03-01 11:53:39 -08:00
|
|
|
assert_eq!(
|
2019-04-16 13:32:22 -07:00
|
|
|
Accounts::load_executable_accounts(
|
2019-04-15 17:15:50 -07:00
|
|
|
&storage,
|
|
|
|
&ancestors,
|
|
|
|
&accounts_index,
|
|
|
|
&Pubkey::new_rand(),
|
|
|
|
&mut error_counters
|
|
|
|
),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountNotFound)
|
2019-03-01 11:53:39 -08:00
|
|
|
);
|
|
|
|
assert_eq!(error_counters.account_not_found, 1);
|
|
|
|
}
|
2019-03-05 12:34:21 -08:00
|
|
|
|
2019-03-07 08:51:56 -08:00
|
|
|
#[test]
|
2019-04-16 13:32:22 -07:00
|
|
|
fn test_accounts_empty_hash_internal_state() {
|
|
|
|
let accounts = Accounts::new(None);
|
|
|
|
assert_eq!(accounts.hash_internal_state(0), None);
|
2019-03-07 08:51:56 -08:00
|
|
|
}
|
2019-04-16 13:32:22 -07:00
|
|
|
|
2019-04-17 13:45:33 -07:00
|
|
|
#[test]
|
2019-05-07 15:51:35 -07:00
|
|
|
fn test_parent_locked_record_accounts() {
|
2019-04-17 13:45:33 -07:00
|
|
|
let mut parent = Accounts::new(None);
|
|
|
|
let locked_pubkey = Keypair::new().pubkey();
|
|
|
|
let mut locked_accounts = HashSet::new();
|
|
|
|
locked_accounts.insert(locked_pubkey);
|
2019-05-07 15:51:35 -07:00
|
|
|
parent.record_locks = Mutex::new((Arc::new(Mutex::new(locked_accounts.clone())), vec![]));
|
|
|
|
let parent = Arc::new(parent);
|
2019-04-17 13:45:33 -07:00
|
|
|
let child = Accounts::new_from_parent(&parent);
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
// Make sure child record locks contains the parent's locked record accounts
|
2019-04-17 13:45:33 -07:00
|
|
|
{
|
2019-05-07 15:51:35 -07:00
|
|
|
let (_, ref parent_record_locks) = *child.record_locks.lock().unwrap();
|
|
|
|
assert_eq!(parent_record_locks.len(), 1);
|
|
|
|
assert_eq!(locked_accounts, *parent_record_locks[0].lock().unwrap());
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
let parent_ = parent.clone();
|
|
|
|
let parent_thread = Builder::new()
|
|
|
|
.name("exit".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
// Unlock the accounts in the parent
|
|
|
|
{
|
|
|
|
let (ref parent_record_locks, _) = *parent_.record_locks.lock().unwrap();
|
|
|
|
parent_record_locks.lock().unwrap().clear();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Function will block until the parent_thread unlocks the parent's record lock
|
|
|
|
let now = Instant::now();
|
2019-04-17 13:45:33 -07:00
|
|
|
assert_eq!(
|
|
|
|
Accounts::lock_account(
|
2019-05-07 15:51:35 -07:00
|
|
|
(
|
|
|
|
&mut child.account_locks.lock().unwrap(),
|
|
|
|
&mut child.record_locks.lock().unwrap().1
|
|
|
|
),
|
2019-04-17 13:45:33 -07:00
|
|
|
&vec![locked_pubkey],
|
|
|
|
&mut ErrorCounters::default()
|
|
|
|
),
|
2019-05-07 15:51:35 -07:00
|
|
|
Ok(())
|
2019-04-17 13:45:33 -07:00
|
|
|
);
|
2019-05-07 15:51:35 -07:00
|
|
|
// Make sure that the function blocked
|
|
|
|
assert!(now.elapsed().as_secs() > 1);
|
|
|
|
parent_thread.join().unwrap();
|
2019-04-17 13:45:33 -07:00
|
|
|
|
|
|
|
{
|
2019-05-07 15:51:35 -07:00
|
|
|
// Check the lock was successfully obtained
|
|
|
|
let child_account_locks = &mut child.account_locks.lock().unwrap();
|
|
|
|
let parent_record_locks = child.record_locks.lock().unwrap();
|
|
|
|
assert_eq!(child_account_locks.len(), 1);
|
|
|
|
|
|
|
|
// Make sure child removed the parent's record locks after the parent had
|
|
|
|
// released all its locks
|
|
|
|
assert!(parent_record_locks.1.is_empty());
|
|
|
|
|
|
|
|
// After all the checks pass, clear the account we just locked from the
|
|
|
|
// set of locks
|
|
|
|
child_account_locks.clear();
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
// Make sure calling new_from_parent() on the child bank also cleans up the copy of old locked
|
|
|
|
// parent accounts, in case the child doesn't call lock_account() after a parent has
|
|
|
|
// released their account locks
|
2019-04-17 13:45:33 -07:00
|
|
|
{
|
2019-05-07 15:51:35 -07:00
|
|
|
// Mock an empty set of parent record accounts in the child bank
|
|
|
|
let (_, ref mut parent_record_locks) = *child.record_locks.lock().unwrap();
|
|
|
|
parent_record_locks.push(Arc::new(Mutex::new(HashSet::new())));
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call new_from_parent, make sure the empty parent locked_accounts is purged
|
|
|
|
let child2 = Accounts::new_from_parent(&child);
|
|
|
|
{
|
2019-05-07 15:51:35 -07:00
|
|
|
let (_, ref mut parent_record_locks) = *child.record_locks.lock().unwrap();
|
|
|
|
assert!(parent_record_locks.is_empty());
|
|
|
|
let (_, ref mut parent_record_locks2) = *child2.record_locks.lock().unwrap();
|
|
|
|
assert!(parent_record_locks2.is_empty());
|
2019-04-17 13:45:33 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|