2019-08-23 14:04:53 -07:00
|
|
|
use crate::accounts_db::{AccountInfo, AccountStorage, AccountsDB, AppendVecId, ErrorCounters};
|
2019-10-23 22:01:22 -07:00
|
|
|
use crate::accounts_index::AccountsIndex;
|
2019-04-16 13:32:22 -07:00
|
|
|
use crate::append_vec::StoredAccount;
|
2019-06-10 22:18:32 -07:00
|
|
|
use crate::blockhash_queue::BlockhashQueue;
|
2019-04-02 08:35:38 -07:00
|
|
|
use crate::message_processor::has_duplicates;
|
2019-08-23 14:04:53 -07:00
|
|
|
use crate::rent_collector::RentCollector;
|
2019-02-27 20:57:01 -08:00
|
|
|
use log::*;
|
2019-07-16 16:58:30 -07:00
|
|
|
use rayon::slice::ParallelSliceMut;
|
2019-05-17 07:00:06 -07:00
|
|
|
use solana_metrics::inc_new_counter_error;
|
2019-06-27 14:25:10 -07:00
|
|
|
use solana_sdk::account::Account;
|
2019-09-20 13:21:12 -07:00
|
|
|
use solana_sdk::bank_hash::BankHash;
|
2019-10-23 22:01:22 -07:00
|
|
|
use solana_sdk::clock::Slot;
|
2019-06-27 14:25:10 -07:00
|
|
|
use solana_sdk::message::Message;
|
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-05-24 13:06:55 -07:00
|
|
|
use solana_sdk::system_program;
|
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-30 21:31:35 -07:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-08-05 22:53:19 -07:00
|
|
|
use std::io::{BufReader, Error as IOError, Read};
|
|
|
|
use std::path::Path;
|
2019-10-16 13:31:21 -07:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2019-10-23 22:01:22 -07:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2019-08-28 08:38:32 -07:00
|
|
|
use crate::transaction_utils::OrderedIterator;
|
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct CreditOnlyLock {
|
|
|
|
credits: AtomicU64,
|
|
|
|
lock_count: Mutex<u64>,
|
|
|
|
}
|
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
/// This structure handles synchronization for db
|
2019-06-05 21:51:44 -07:00
|
|
|
#[derive(Default, Debug)]
|
2018-12-17 12:41:23 -08:00
|
|
|
pub struct Accounts {
|
2019-10-23 22:01:22 -07:00
|
|
|
/// my slot
|
|
|
|
pub slot: Slot,
|
|
|
|
|
2019-04-15 17:15:50 -07:00
|
|
|
/// Single global AccountsDB
|
|
|
|
pub accounts_db: Arc<AccountsDB>,
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
/// set of credit-debit accounts which are currently in the pipeline
|
2019-06-10 22:05:46 -07:00
|
|
|
account_locks: Mutex<HashSet<Pubkey>>,
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2019-07-11 18:46:49 -07:00
|
|
|
/// Set of credit-only accounts which are currently in the pipeline, caching account balance
|
2019-10-16 13:31:21 -07:00
|
|
|
/// and number of locks. On commit_credits(), we do a take() on the option so that the hashmap
|
2019-07-11 18:46:49 -07:00
|
|
|
/// is no longer available to be written to.
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks: Arc<RwLock<Option<HashMap<Pubkey, CreditOnlyLock>>>>,
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
// for the load instructions
|
|
|
|
pub type TransactionAccounts = Vec<Account>;
|
|
|
|
pub type TransactionCredits = Vec<u64>;
|
|
|
|
pub type TransactionRents = Vec<u64>;
|
|
|
|
pub type TransactionLoaders = Vec<Vec<(Pubkey, Account)>>;
|
|
|
|
|
2019-09-08 11:13:59 -07:00
|
|
|
pub type TransactionLoadResult = (
|
|
|
|
TransactionAccounts,
|
|
|
|
TransactionLoaders,
|
|
|
|
TransactionCredits,
|
|
|
|
TransactionRents,
|
|
|
|
);
|
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
impl Accounts {
|
2019-07-25 22:59:28 -07:00
|
|
|
pub fn new(paths: Option<String>) -> Self {
|
|
|
|
let accounts_db = Arc::new(AccountsDB::new(paths));
|
2019-07-23 13:47:48 -07:00
|
|
|
|
2019-04-16 13:32:22 -07:00
|
|
|
Accounts {
|
2019-10-23 22:01:22 -07:00
|
|
|
slot: 0,
|
2019-04-16 13:32:22 -07:00
|
|
|
accounts_db,
|
2019-05-07 15:51:35 -07:00
|
|
|
account_locks: Mutex::new(HashSet::new()),
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks: Arc::new(RwLock::new(Some(HashMap::new()))),
|
2018-12-24 16:11:20 -08:00
|
|
|
}
|
2019-02-28 17:34:37 -08:00
|
|
|
}
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn new_from_parent(parent: &Accounts, slot: Slot, parent_slot: Slot) -> Self {
|
2019-04-16 13:32:22 -07:00
|
|
|
let accounts_db = parent.accounts_db.clone();
|
2019-09-20 13:21:12 -07:00
|
|
|
accounts_db.set_hash(slot, parent_slot);
|
2019-04-16 13:32:22 -07:00
|
|
|
Accounts {
|
2019-10-23 22:01:22 -07:00
|
|
|
slot,
|
2019-04-16 13:32:22 -07:00
|
|
|
accounts_db,
|
2019-05-07 15:51:35 -07:00
|
|
|
account_locks: Mutex::new(HashSet::new()),
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks: Arc::new(RwLock::new(Some(HashMap::new()))),
|
2019-04-16 13:32:22 -07:00
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2018-12-24 16:11:20 -08:00
|
|
|
|
2019-08-05 22:53:19 -07:00
|
|
|
pub fn accounts_from_stream<R: Read, P: AsRef<Path>>(
|
2019-06-05 21:51:44 -07:00
|
|
|
&self,
|
|
|
|
stream: &mut BufReader<R>,
|
2019-08-05 22:53:19 -07:00
|
|
|
local_paths: String,
|
|
|
|
append_vecs_path: P,
|
|
|
|
) -> std::result::Result<(), IOError> {
|
|
|
|
self.accounts_db
|
|
|
|
.accounts_from_stream(stream, local_paths, append_vecs_path)
|
2019-06-05 21:51:44 -07:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:11:20 -08:00
|
|
|
fn load_tx_accounts(
|
2019-10-23 22:01:22 -07:00
|
|
|
&self,
|
2019-04-23 09:56:36 -07:00
|
|
|
storage: &AccountStorage,
|
2019-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2019-04-15 17:15:50 -07:00
|
|
|
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,
|
2019-08-23 14:04:53 -07:00
|
|
|
rent_collector: &RentCollector,
|
2019-10-23 22:01:22 -07:00
|
|
|
) -> Result<(TransactionAccounts, TransactionRents)> {
|
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-10-30 21:55:17 -07:00
|
|
|
let mut accounts: TransactionAccounts = Vec::with_capacity(message.account_keys.len());
|
|
|
|
let mut rents: TransactionRents = Vec::with_capacity(message.account_keys.len());
|
2019-10-14 16:16:44 -07:00
|
|
|
for key in message
|
2019-08-23 14:04:53 -07:00
|
|
|
.account_keys
|
|
|
|
.iter()
|
2019-10-23 22:01:22 -07:00
|
|
|
.filter(|key| !message.program_ids().contains(key))
|
2019-08-23 14:04:53 -07:00
|
|
|
{
|
|
|
|
let (account, rent) = AccountsDB::load(storage, ancestors, accounts_index, key)
|
2019-10-16 13:31:21 -07:00
|
|
|
.and_then(|(account, _)| rent_collector.update(account))
|
2019-08-23 14:04:53 -07:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
accounts.push(account);
|
|
|
|
rents.push(rent);
|
2019-01-29 16:33:28 -08:00
|
|
|
}
|
2019-08-23 14:04:53 -07:00
|
|
|
|
|
|
|
if accounts.is_empty() || 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-08-23 14:04:53 -07:00
|
|
|
} else if accounts[0].owner != system_program::id() {
|
2019-05-24 13:06:55 -07:00
|
|
|
error_counters.invalid_account_for_fee += 1;
|
|
|
|
Err(TransactionError::InvalidAccountForFee)
|
2019-08-23 14:04:53 -07:00
|
|
|
} else if 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-08-23 14:04:53 -07:00
|
|
|
accounts[0].lamports -= fee;
|
2019-10-23 22:01:22 -07:00
|
|
|
Ok((accounts, rents))
|
2019-01-29 16:33:28 -08:00
|
|
|
}
|
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-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2019-04-15 17:15:50 -07:00
|
|
|
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-05-16 10:32:27 -07:00
|
|
|
return Err(TransactionError::ProgramAccountNotFound);
|
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-09-24 04:09:53 -07:00
|
|
|
let program_owner = program.owner;
|
2019-04-15 17:15:50 -07:00
|
|
|
accounts.insert(0, (program_id, program));
|
2019-09-24 04:09:53 -07:00
|
|
|
program_id = program_owner;
|
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-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2019-04-15 17:15:50 -07:00
|
|
|
accounts_index: &AccountsIndex<AccountInfo>,
|
2019-01-08 09:20:25 -08:00
|
|
|
tx: &Transaction,
|
|
|
|
error_counters: &mut ErrorCounters,
|
2019-08-23 14:04:53 -07:00
|
|
|
) -> Result<TransactionLoaders> {
|
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-07-01 17:34:22 -07:00
|
|
|
if message.account_keys.len() <= ix.program_id_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-07-01 17:34:22 -07:00
|
|
|
let program_id = message.account_keys[ix.program_id_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-06-05 11:43:41 -07:00
|
|
|
pub fn load_accounts(
|
2018-12-24 16:11:20 -08:00
|
|
|
&self,
|
2019-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2018-12-17 12:41:23 -08:00
|
|
|
txs: &[Transaction],
|
2019-08-28 08:38:32 -07:00
|
|
|
txs_iteration_order: Option<&[usize]>,
|
2019-01-06 22:06:55 -08:00
|
|
|
lock_results: Vec<Result<()>>,
|
2019-06-10 22:18:32 -07:00
|
|
|
hash_queue: &BlockhashQueue,
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
2019-08-23 14:04:53 -07:00
|
|
|
rent_collector: &RentCollector,
|
2019-09-08 11:13:59 -07:00
|
|
|
) -> Vec<Result<TransactionLoadResult>> {
|
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();
|
2019-08-28 08:38:32 -07:00
|
|
|
OrderedIterator::new(txs, txs_iteration_order)
|
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-06-10 22:18:32 -07:00
|
|
|
let fee_calculator = hash_queue
|
|
|
|
.get_fee_calculator(&tx.message().recent_blockhash)
|
|
|
|
.ok_or(TransactionError::BlockhashNotFound)?;
|
|
|
|
|
2019-03-29 15:11:21 -07:00
|
|
|
let fee = fee_calculator.calculate_fee(tx.message());
|
2019-10-23 22:01:22 -07:00
|
|
|
let (accounts, rents) = self.load_tx_accounts(
|
2019-04-15 17:15:50 -07:00
|
|
|
&storage,
|
|
|
|
ancestors,
|
|
|
|
&accounts_index,
|
|
|
|
tx,
|
|
|
|
fee,
|
|
|
|
error_counters,
|
2019-08-23 14:04:53 -07:00
|
|
|
rent_collector,
|
2019-04-15 17:15:50 -07:00
|
|
|
)?;
|
|
|
|
let loaders = Self::load_loaders(
|
|
|
|
&storage,
|
|
|
|
ancestors,
|
|
|
|
&accounts_index,
|
|
|
|
tx,
|
|
|
|
error_counters,
|
|
|
|
)?;
|
2019-10-23 22:01:22 -07:00
|
|
|
let credits = vec![0; accounts.len()];
|
2019-08-23 14:04:53 -07:00
|
|
|
Ok((accounts, loaders, credits, rents))
|
2019-01-06 22:06:55 -08:00
|
|
|
}
|
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,
|
2019-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2019-05-06 07:31:50 -07:00
|
|
|
pubkey: &Pubkey,
|
2019-10-23 22:01:22 -07:00
|
|
|
) -> Option<(Account, Slot)> {
|
|
|
|
let (mut account, slot) = self
|
|
|
|
.accounts_db
|
2019-04-15 17:15:50 -07:00
|
|
|
.load_slow(ancestors, pubkey)
|
2019-10-23 22:01:22 -07:00
|
|
|
.unwrap_or((Account::default(), self.slot));
|
|
|
|
|
|
|
|
account.lamports += self.credit_only_pending_credits(pubkey);
|
|
|
|
|
|
|
|
if account.lamports > 0 {
|
|
|
|
Some((account, slot))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2018-12-24 16:11:20 -08:00
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
/// scans underlying accounts_db for this delta (slot) with a map function
|
2019-07-10 21:22:58 -07:00
|
|
|
/// from StoredAccount to B
|
2019-10-23 22:01:22 -07:00
|
|
|
/// returns only the latest/current version of B for this slot
|
|
|
|
fn scan_slot<F, B>(&self, slot: Slot, func: F) -> Vec<B>
|
2019-07-10 21:22:58 -07:00
|
|
|
where
|
2019-10-02 18:33:01 -07:00
|
|
|
F: Fn(&StoredAccount) -> Option<B> + Send + Sync,
|
2019-07-10 21:22:58 -07:00
|
|
|
B: Send + Default,
|
|
|
|
{
|
|
|
|
let accumulator: Vec<Vec<(Pubkey, u64, B)>> = self.accounts_db.scan_account_storage(
|
2019-10-23 22:01:22 -07:00
|
|
|
slot,
|
2019-05-30 21:31:35 -07:00
|
|
|
|stored_account: &StoredAccount,
|
|
|
|
_id: AppendVecId,
|
2019-07-10 21:22:58 -07:00
|
|
|
accum: &mut Vec<(Pubkey, u64, B)>| {
|
|
|
|
if let Some(val) = func(stored_account) {
|
|
|
|
accum.push((
|
2019-04-16 08:50:05 -07:00
|
|
|
stored_account.meta.pubkey,
|
2019-07-10 21:22:58 -07:00
|
|
|
std::u64::MAX - stored_account.meta.write_version,
|
|
|
|
val,
|
|
|
|
));
|
2019-04-15 17:15:50 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-07-10 21:22:58 -07:00
|
|
|
|
|
|
|
let mut versions: Vec<(Pubkey, u64, B)> = accumulator.into_iter().flat_map(|x| x).collect();
|
2019-07-16 16:58:30 -07:00
|
|
|
self.accounts_db.thread_pool.install(|| {
|
|
|
|
versions.par_sort_by_key(|s| (s.0, s.1));
|
|
|
|
});
|
2019-04-16 08:50:05 -07:00
|
|
|
versions.dedup_by_key(|s| s.0);
|
2019-07-10 21:22:58 -07:00
|
|
|
versions
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_pubkey, _version, val)| val)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn load_by_program_slot(&self, slot: Slot, program_id: &Pubkey) -> Vec<(Pubkey, Account)> {
|
|
|
|
self.scan_slot(slot, |stored_account| {
|
2019-09-23 15:20:45 -07:00
|
|
|
if stored_account.account_meta.owner == *program_id {
|
2019-07-10 21:22:58 -07:00
|
|
|
Some((stored_account.meta.pubkey, stored_account.clone_account()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2019-03-07 08:51:56 -08:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn verify_hash_internal_state(&self, slot: Slot, ancestors: &HashMap<Slot, usize>) -> bool {
|
|
|
|
self.accounts_db.verify_hash_internal_state(slot, ancestors)
|
2019-09-20 13:21:12 -07:00
|
|
|
}
|
|
|
|
|
2019-07-11 11:58:28 -07:00
|
|
|
pub fn load_by_program(
|
|
|
|
&self,
|
2019-10-23 22:01:22 -07:00
|
|
|
ancestors: &HashMap<Slot, usize>,
|
2019-07-11 11:58:28 -07:00
|
|
|
program_id: &Pubkey,
|
|
|
|
) -> Vec<(Pubkey, Account)> {
|
|
|
|
self.accounts_db.scan_accounts(
|
|
|
|
ancestors,
|
|
|
|
|collector: &mut Vec<(Pubkey, Account)>, option| {
|
|
|
|
if let Some(data) = option
|
|
|
|
.filter(|(_, account, _)| account.owner == *program_id && account.lamports != 0)
|
2019-10-23 22:01:22 -07:00
|
|
|
.map(|(pubkey, account, _slot)| (*pubkey, account))
|
2019-07-11 11:58:28 -07:00
|
|
|
{
|
|
|
|
collector.push(data)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:58:11 -08:00
|
|
|
/// Slow because lock is held for 1 operation instead of many
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn store_slow(&self, slot: Slot, pubkey: &Pubkey, account: &Account) {
|
|
|
|
self.accounts_db.store(slot, &[(pubkey, account)]);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2019-01-04 16:04:31 -08:00
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
fn take_credit_only(&self) -> Result<HashMap<Pubkey, CreditOnlyLock>> {
|
|
|
|
let mut w_credit_only_locks = self.credit_only_locks.write().unwrap();
|
|
|
|
w_credit_only_locks
|
|
|
|
.take()
|
2019-07-11 18:46:49 -07:00
|
|
|
.ok_or(TransactionError::AccountInUse)
|
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
fn is_locked_credit_only(&self, key: &Pubkey) -> bool {
|
|
|
|
self.credit_only_locks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |locks| {
|
|
|
|
locks
|
|
|
|
.get(key)
|
|
|
|
.map_or(false, |lock| *lock.lock_count.lock().unwrap() > 0)
|
|
|
|
})
|
2019-07-11 18:46:49 -07:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
fn credit_only_pending_credits(&self, key: &Pubkey) -> u64 {
|
|
|
|
self.credit_only_locks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.map_or(0, |locks| {
|
|
|
|
locks
|
|
|
|
.get(key)
|
|
|
|
.map_or(0, |lock| lock.credits.load(Ordering::Relaxed))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unlock_credit_only(&self, key: &Pubkey) {
|
|
|
|
self.credit_only_locks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.map(|locks| {
|
|
|
|
locks
|
|
|
|
.get(key)
|
|
|
|
.map(|lock| *lock.lock_count.lock().unwrap() -= 1)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lock_credit_only(&self, key: &Pubkey) -> bool {
|
|
|
|
self.credit_only_locks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |locks| {
|
|
|
|
locks.get(key).map_or(false, |lock| {
|
|
|
|
*lock.lock_count.lock().unwrap() += 1;
|
|
|
|
true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_credit_only(&self, key: &Pubkey, lock: CreditOnlyLock) -> bool {
|
|
|
|
self.credit_only_locks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.as_mut()
|
|
|
|
.map_or(false, |locks| {
|
|
|
|
assert!(locks.get(key).is_none());
|
|
|
|
locks.insert(*key, lock);
|
|
|
|
true
|
|
|
|
})
|
2019-07-11 18:46:49 -07:00
|
|
|
}
|
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
fn lock_account(
|
2019-10-23 22:01:22 -07:00
|
|
|
&self,
|
2019-06-10 22:05:46 -07:00
|
|
|
locks: &mut HashSet<Pubkey>,
|
2019-06-27 14:25:10 -07:00
|
|
|
message: &Message,
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Result<()> {
|
2019-06-27 14:25:10 -07:00
|
|
|
let (credit_debit_keys, credit_only_keys) = message.get_account_keys_by_lock_type();
|
|
|
|
|
|
|
|
for k in credit_debit_keys.iter() {
|
2019-10-23 22:01:22 -07:00
|
|
|
if locks.contains(k) || self.is_locked_credit_only(k) {
|
2019-06-27 14:25:10 -07:00
|
|
|
error_counters.account_in_use += 1;
|
2019-09-16 21:45:16 -07:00
|
|
|
debug!("CD Account in use: {:?}", k);
|
2019-06-27 14:25:10 -07:00
|
|
|
return Err(TransactionError::AccountInUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k in credit_only_keys.iter() {
|
2019-06-10 22:05:46 -07:00
|
|
|
if locks.contains(k) {
|
2018-12-17 12:41:23 -08:00
|
|
|
error_counters.account_in_use += 1;
|
2019-09-16 21:45:16 -07:00
|
|
|
debug!("CO Account in use: {:?}", k);
|
2019-03-13 12:58:44 -07:00
|
|
|
return Err(TransactionError::AccountInUse);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
for k in credit_debit_keys {
|
|
|
|
locks.insert(*k);
|
|
|
|
}
|
2019-10-23 22:01:22 -07:00
|
|
|
|
|
|
|
let credit_only_writes: Vec<&&Pubkey> = credit_only_keys
|
|
|
|
.iter()
|
|
|
|
.filter(|k| !self.lock_credit_only(k))
|
|
|
|
.collect();
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
for k in credit_only_writes.iter() {
|
2019-10-23 22:01:22 -07:00
|
|
|
self.insert_credit_only(
|
|
|
|
*k,
|
2019-06-27 14:25:10 -07:00
|
|
|
CreditOnlyLock {
|
|
|
|
credits: AtomicU64::new(0),
|
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
|
2018-12-17 12:41:23 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
fn unlock_account(&self, tx: &Transaction, result: &Result<()>, locks: &mut HashSet<Pubkey>) {
|
2019-06-27 14:25:10 -07:00
|
|
|
let (credit_debit_keys, credit_only_keys) = &tx.message().get_account_keys_by_lock_type();
|
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-06-27 14:25:10 -07:00
|
|
|
for k in credit_debit_keys {
|
2019-04-15 17:15:50 -07:00
|
|
|
locks.remove(k);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
for k in credit_only_keys {
|
2019-10-23 22:01:22 -07:00
|
|
|
self.unlock_credit_only(k);
|
2019-06-27 14:25:10 -07:00
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 16:04:31 -08:00
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn hash_internal_state(&self, slot_id: Slot) -> Option<BankHash> {
|
|
|
|
let slot_hashes = self.accounts_db.slot_hashes.read().unwrap();
|
|
|
|
let slot_hash = slot_hashes.get(&slot_id)?;
|
|
|
|
if slot_hash.0 {
|
|
|
|
Some(slot_hash.1)
|
2019-04-16 13:32:22 -07:00
|
|
|
} else {
|
2019-09-20 13:21:12 -07:00
|
|
|
None
|
2019-04-16 13:32:22 -07:00
|
|
|
}
|
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-08-28 08:38:32 -07:00
|
|
|
pub fn lock_accounts(
|
|
|
|
&self,
|
|
|
|
txs: &[Transaction],
|
|
|
|
txs_iteration_order: Option<&[usize]>,
|
|
|
|
) -> Vec<Result<()>> {
|
2018-12-17 12:41:23 -08:00
|
|
|
let mut error_counters = ErrorCounters::default();
|
2019-08-28 08:38:32 -07:00
|
|
|
let rv = OrderedIterator::new(txs, txs_iteration_order)
|
2019-02-20 21:31:24 -08:00
|
|
|
.map(|tx| {
|
2019-06-10 22:05:46 -07:00
|
|
|
let message = &tx.message();
|
2019-10-23 22:01:22 -07:00
|
|
|
self.lock_account(
|
2019-06-10 22:05:46 -07:00
|
|
|
&mut self.account_locks.lock().unwrap(),
|
2019-06-27 14:25:10 -07:00
|
|
|
&message,
|
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 {
|
2019-05-17 07:00:06 -07:00
|
|
|
inc_new_counter_error!(
|
2018-12-17 12:41:23 -08:00
|
|
|
"bank-process_transactions-account_in_use",
|
2019-05-17 07:00:06 -07:00
|
|
|
error_counters.account_in_use,
|
|
|
|
0,
|
|
|
|
100
|
2018-12-17 12:41:23 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Once accounts are unlocked, new transactions that modify that state can enter the pipeline
|
2019-09-16 21:45:16 -07:00
|
|
|
pub fn unlock_accounts(
|
|
|
|
&self,
|
|
|
|
txs: &[Transaction],
|
|
|
|
txs_iteration_order: Option<&[usize]>,
|
|
|
|
results: &[Result<()>],
|
|
|
|
) {
|
2019-06-10 22:05:46 -07:00
|
|
|
let mut account_locks = self.account_locks.lock().unwrap();
|
2018-12-17 12:41:23 -08:00
|
|
|
debug!("bank unlock accounts");
|
2019-09-16 21:45:16 -07:00
|
|
|
|
|
|
|
OrderedIterator::new(txs, txs_iteration_order)
|
|
|
|
.zip(results.iter())
|
2019-10-23 22:01:22 -07:00
|
|
|
.for_each(|(tx, result)| self.unlock_account(tx, result, &mut account_locks));
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn has_accounts(&self, slot: Slot) -> bool {
|
|
|
|
self.accounts_db.has_accounts(slot)
|
2019-02-20 12:17:32 -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,
|
2019-10-23 22:01:22 -07:00
|
|
|
slot: Slot,
|
2018-12-17 12:41:23 -08:00
|
|
|
txs: &[Transaction],
|
2019-08-28 08:38:32 -07:00
|
|
|
txs_iteration_order: Option<&[usize]>,
|
2018-12-17 12:41:23 -08:00
|
|
|
res: &[Result<()>],
|
2019-09-08 11:13:59 -07:00
|
|
|
loaded: &mut [Result<TransactionLoadResult>],
|
2019-02-19 17:11:43 -08:00
|
|
|
) {
|
2019-08-28 08:38:32 -07:00
|
|
|
let accounts_to_store =
|
|
|
|
self.collect_accounts_to_store(txs, txs_iteration_order, res, loaded);
|
2019-10-23 22:01:22 -07:00
|
|
|
self.accounts_db.store(slot, &accounts_to_store);
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
/// Purge a slot if it is not a root
|
|
|
|
/// Root slots cannot be purged
|
|
|
|
pub fn purge_slot(&self, slot: Slot) {
|
|
|
|
self.accounts_db.purge_slot(slot);
|
2018-12-24 16:11:20 -08:00
|
|
|
}
|
2019-10-23 22:01:22 -07:00
|
|
|
/// Add a slot to root. Root slots cannot be purged
|
|
|
|
pub fn add_root(&self, slot: Slot) {
|
|
|
|
self.accounts_db.add_root(slot)
|
2019-03-21 17:36:10 -07:00
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
/// Commit remaining credit-only changes, regardless of reference count
|
2019-07-11 18:46:49 -07:00
|
|
|
///
|
2019-10-23 22:01:22 -07:00
|
|
|
/// We do a take() on `self.credit_only_locks` so that the hashmap is no longer
|
2019-07-11 18:46:49 -07:00
|
|
|
/// available to be written to. This prevents any transactions from reinserting into the hashmap.
|
|
|
|
/// Then there are then only 2 cases for interleaving with commit_credits and lock_accounts.
|
|
|
|
/// Either:
|
|
|
|
// 1) Any transactions that tries to lock after commit_credits will find the HashMap is None
|
|
|
|
// so will fail the lock
|
|
|
|
// 2) Any transaction that grabs a lock and then commit_credits clears the HashMap will find
|
|
|
|
// the HashMap is None on unlock_accounts, and will perform a no-op.
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn commit_credits(&self, ancestors: &HashMap<Slot, usize>, slot: Slot) {
|
2019-07-11 18:46:49 -07:00
|
|
|
// Clear the credit only hashmap so that no further transactions can modify it
|
2019-10-23 22:01:22 -07:00
|
|
|
let credit_only_locks = self
|
|
|
|
.take_credit_only()
|
2019-10-16 13:31:21 -07:00
|
|
|
.expect("Credit only locks didn't exist in commit_credits");
|
2019-10-23 22:01:22 -07:00
|
|
|
self.store_credit_only_credits(credit_only_locks, ancestors, slot);
|
2019-07-11 18:46:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Used only for tests to store credit-only accounts after every transaction
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn commit_credits_unsafe(&self, ancestors: &HashMap<Slot, usize>, slot: Slot) {
|
2019-07-11 18:46:49 -07:00
|
|
|
// Clear the credit only hashmap so that no further transactions can modify it
|
2019-10-23 22:01:22 -07:00
|
|
|
let mut credit_only_locks = self.credit_only_locks.write().unwrap();
|
|
|
|
let credit_only_locks = credit_only_locks
|
|
|
|
.as_mut()
|
|
|
|
.expect("Credit only locks didn't exist in commit_credits");
|
|
|
|
|
|
|
|
self.store_credit_only_credits(credit_only_locks.drain(), ancestors, slot);
|
2019-07-11 18:46:49 -07:00
|
|
|
}
|
|
|
|
|
2019-10-16 13:31:21 -07:00
|
|
|
fn store_credit_only_credits<I>(
|
2019-07-11 18:46:49 -07:00
|
|
|
&self,
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks: I,
|
|
|
|
ancestors: &HashMap<Slot, usize>,
|
|
|
|
slot: Slot,
|
2019-10-16 13:31:21 -07:00
|
|
|
) where
|
2019-07-11 18:46:49 -07:00
|
|
|
I: IntoIterator<Item = (Pubkey, CreditOnlyLock)>,
|
|
|
|
{
|
2019-10-23 22:01:22 -07:00
|
|
|
for (pubkey, lock) in credit_only_locks {
|
2019-10-16 13:31:21 -07:00
|
|
|
let lock_count = *lock.lock_count.lock().unwrap();
|
|
|
|
if lock_count != 0 {
|
|
|
|
warn!(
|
|
|
|
"dropping credit-only lock on {}, still has {} locks",
|
|
|
|
pubkey, lock_count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let credit = lock.credits.load(Ordering::Relaxed);
|
|
|
|
if credit > 0 {
|
2019-10-23 22:01:22 -07:00
|
|
|
let (mut account, _slot) = self
|
|
|
|
.accounts_db
|
2019-10-16 13:31:21 -07:00
|
|
|
.load_slow(ancestors, &pubkey)
|
|
|
|
.unwrap_or_default();
|
2019-06-27 14:25:10 -07:00
|
|
|
account.lamports += credit;
|
2019-10-23 22:01:22 -07:00
|
|
|
self.store_slow(slot, &pubkey, &account);
|
2019-06-27 14:25:10 -07:00
|
|
|
}
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
}
|
|
|
|
|
2019-08-19 13:00:37 -07:00
|
|
|
fn collect_accounts_to_store<'a>(
|
2019-06-27 14:25:10 -07:00
|
|
|
&self,
|
|
|
|
txs: &'a [Transaction],
|
2019-08-28 08:38:32 -07:00
|
|
|
txs_iteration_order: Option<&'a [usize]>,
|
2019-06-27 14:25:10 -07:00
|
|
|
res: &'a [Result<()>],
|
2019-09-08 11:13:59 -07:00
|
|
|
loaded: &'a mut [Result<TransactionLoadResult>],
|
2019-08-19 13:00:37 -07:00
|
|
|
) -> Vec<(&'a Pubkey, &'a Account)> {
|
2019-10-30 21:55:17 -07:00
|
|
|
let mut accounts = Vec::with_capacity(loaded.len());
|
2019-08-28 08:38:32 -07:00
|
|
|
for (i, (raccs, tx)) in loaded
|
|
|
|
.iter_mut()
|
|
|
|
.zip(OrderedIterator::new(txs, txs_iteration_order))
|
|
|
|
.enumerate()
|
|
|
|
{
|
2019-06-27 14:25:10 -07:00
|
|
|
if res[i].is_err() || raccs.is_err() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:38:32 -07:00
|
|
|
let message = &tx.message();
|
2019-06-27 14:25:10 -07:00
|
|
|
let acc = raccs.as_mut().unwrap();
|
|
|
|
for (((i, key), account), credit) in message
|
|
|
|
.account_keys
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.zip(acc.0.iter())
|
|
|
|
.zip(acc.2.iter())
|
|
|
|
{
|
2019-08-19 13:00:37 -07:00
|
|
|
if message.is_debitable(i) {
|
|
|
|
accounts.push((key, account));
|
2019-10-16 13:31:21 -07:00
|
|
|
}
|
|
|
|
if *credit > 0 {
|
|
|
|
// Increment credit-only account balance Atomic
|
2019-10-23 22:01:22 -07:00
|
|
|
self.credit_only_locks
|
2019-10-16 13:31:21 -07:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
2019-08-19 13:00:37 -07:00
|
|
|
.expect("Collect accounts should only be called before a commit, and credit only account locks should exist before a commit")
|
|
|
|
.get(key)
|
2019-10-16 13:31:21 -07:00
|
|
|
.unwrap()
|
2019-08-19 13:00:37 -07:00
|
|
|
.credits
|
|
|
|
.fetch_add(*credit, Ordering::Relaxed);
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
accounts
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
pub fn create_test_accounts(accounts: &Accounts, pubkeys: &mut Vec<Pubkey>, num: usize, slot: u64) {
|
2019-07-19 09:32:29 -07:00
|
|
|
for t in 0..num {
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let account = Account::new((t + 1) as u64, 0, &Account::default().owner);
|
2019-10-23 22:01:22 -07:00
|
|
|
accounts.store_slow(slot, &pubkey, &account);
|
2019-07-19 09:32:29 -07:00
|
|
|
pubkeys.push(pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::*;
|
2019-08-05 22:53:19 -07:00
|
|
|
use crate::accounts_db::tests::copy_append_vecs;
|
2019-08-13 16:05:37 -07:00
|
|
|
use crate::accounts_db::{get_temp_accounts_paths, AccountsDBSerialize};
|
|
|
|
use bincode::serialize_into;
|
2019-05-30 21:31:35 -07:00
|
|
|
use rand::{thread_rng, Rng};
|
2019-01-08 09:20:25 -08:00
|
|
|
use solana_sdk::account::Account;
|
2019-06-10 22:18:32 -07:00
|
|
|
use solana_sdk::fee_calculator::FeeCalculator;
|
2019-01-08 09:20:25 -08:00
|
|
|
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-07-12 16:38:15 -07:00
|
|
|
use solana_sdk::sysvar;
|
2019-01-08 09:20:25 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2019-05-30 21:31:35 -07:00
|
|
|
use std::io::Cursor;
|
2019-06-27 14:25:10 -07:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::{thread, time};
|
2019-08-05 22:53:19 -07:00
|
|
|
use tempfile::TempDir;
|
2019-01-08 09:20:25 -08:00
|
|
|
|
2019-10-16 13:31: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,
|
2019-09-08 11:13:59 -07:00
|
|
|
) -> Vec<Result<TransactionLoadResult>> {
|
2019-06-10 22:18:32 -07:00
|
|
|
let mut hash_queue = BlockhashQueue::new(100);
|
|
|
|
hash_queue.register_hash(&tx.message().recent_blockhash, &fee_calculator);
|
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();
|
2019-10-16 13:31:21 -07:00
|
|
|
let rent_collector = RentCollector::default();
|
2019-08-23 14:04:53 -07:00
|
|
|
let res = accounts.load_accounts(
|
|
|
|
&ancestors,
|
|
|
|
&[tx],
|
2019-08-28 08:38:32 -07:00
|
|
|
None,
|
2019-08-23 14:04:53 -07:00
|
|
|
vec![Ok(())],
|
|
|
|
&hash_queue,
|
|
|
|
error_counters,
|
|
|
|
&rent_collector,
|
|
|
|
);
|
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,
|
2019-09-08 11:13:59 -07:00
|
|
|
) -> Vec<Result<TransactionLoadResult>> {
|
2019-03-29 15:11:21 -07:00
|
|
|
let fee_calculator = FeeCalculator::default();
|
2019-10-16 13:31:21 -07:00
|
|
|
load_accounts_with_fee(tx, ka, &fee_calculator, error_counters)
|
2019-03-29 15:11:21 -07:00
|
|
|
}
|
|
|
|
|
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-05-23 15:19:53 -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::<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-05-22 15:23:16 -07:00
|
|
|
assert_eq!(
|
|
|
|
loaded_accounts[0],
|
|
|
|
Err(TransactionError::ProgramAccountNotFound)
|
|
|
|
);
|
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-10-08 22:34:26 -07:00
|
|
|
let fee_calculator = FeeCalculator::new(10, 0);
|
2019-03-29 15:11:21 -07:00
|
|
|
assert_eq!(fee_calculator.calculate_fee(tx.message()), 10);
|
|
|
|
|
2019-10-16 13:31:21 -07:00
|
|
|
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!(
|
2019-06-27 14:25:10 -07:00
|
|
|
loaded_accounts[0].clone(),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::InsufficientFundsForFee)
|
2019-05-24 13:06:55 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_load_accounts_invalid_account_for_fee() {
|
|
|
|
let mut accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let key0 = keypair.pubkey();
|
|
|
|
|
|
|
|
let account = Account::new(1, 1, &Pubkey::new_rand()); // <-- owner is not the system program
|
|
|
|
accounts.push((key0, account));
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(1, &(), vec![0])];
|
|
|
|
let tx = Transaction::new_with_compiled_instructions(
|
|
|
|
&[&keypair],
|
|
|
|
&[],
|
|
|
|
Hash::default(),
|
|
|
|
vec![native_loader::id()],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
|
|
|
|
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);
|
|
|
|
|
|
|
|
assert_eq!(error_counters.invalid_account_for_fee, 1);
|
|
|
|
assert_eq!(loaded_accounts.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
loaded_accounts[0],
|
|
|
|
Err(TransactionError::InvalidAccountForFee)
|
2019-03-13 12:58:44 -07:00
|
|
|
);
|
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-08-23 14:04:53 -07:00
|
|
|
let mut account = Account::new(1, 1, &Pubkey::default());
|
|
|
|
account.rent_epoch = 1;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key0, account));
|
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
let mut account = Account::new(2, 1, &Pubkey::default());
|
|
|
|
account.rent_epoch = 1;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key1, account));
|
|
|
|
|
2019-05-22 15:23:16 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), 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] {
|
2019-08-23 14:04:53 -07:00
|
|
|
Ok((
|
|
|
|
transaction_accounts,
|
|
|
|
transaction_loaders,
|
|
|
|
transaction_credits,
|
|
|
|
_transaction_rents,
|
|
|
|
)) => {
|
|
|
|
assert_eq!(transaction_accounts.len(), 2);
|
|
|
|
assert_eq!(transaction_accounts[0], accounts[0].1);
|
|
|
|
assert_eq!(transaction_loaders.len(), 1);
|
|
|
|
assert_eq!(transaction_loaders[0].len(), 0);
|
|
|
|
assert_eq!(transaction_credits.len(), 2);
|
|
|
|
assert_eq!(transaction_credits, &vec![0, 0]);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
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-05-22 15:23:16 -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![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-05-24 11:04:05 -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![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]);
|
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
let mut account = Account::new(1, 1, &Pubkey::default());
|
|
|
|
account.rent_epoch = 1;
|
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-08-23 14:04:53 -07:00
|
|
|
account.rent_epoch = 1;
|
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-08-23 14:04:53 -07:00
|
|
|
account.rent_epoch = 1;
|
2019-02-14 09:57:54 -08:00
|
|
|
account.owner = key1;
|
2019-01-08 09:20:25 -08:00
|
|
|
accounts.push((key2, account));
|
|
|
|
|
|
|
|
let instructions = vec![
|
2019-03-15 08:47:25 -07:00
|
|
|
CompiledInstruction::new(1, &(), vec![0]),
|
2019-05-22 15:23:16 -07:00
|
|
|
CompiledInstruction::new(2, &(), 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] {
|
2019-08-23 14:04:53 -07:00
|
|
|
Ok((
|
|
|
|
transaction_accounts,
|
|
|
|
transaction_loaders,
|
|
|
|
transaction_credits,
|
|
|
|
_transaction_rents,
|
|
|
|
)) => {
|
|
|
|
assert_eq!(transaction_accounts.len(), 1);
|
|
|
|
assert_eq!(transaction_accounts[0], accounts[0].1);
|
|
|
|
assert_eq!(transaction_loaders.len(), 2);
|
|
|
|
assert_eq!(transaction_loaders[0].len(), 1);
|
|
|
|
assert_eq!(transaction_loaders[1].len(), 2);
|
|
|
|
assert_eq!(transaction_credits.len(), 1);
|
|
|
|
assert_eq!(transaction_credits, &vec![0]);
|
|
|
|
for loaders in transaction_loaders.iter() {
|
2019-06-10 19:50:02 -07:00
|
|
|
for (i, accounts_subset) in loaders.iter().enumerate() {
|
2019-01-08 09:20:25 -08:00
|
|
|
// +1 to skip first not loader account
|
2019-09-24 04:09:53 -07:00
|
|
|
assert_eq!(*accounts_subset, accounts[i + 1]);
|
2019-01-08 09:20:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
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-10-23 22:01:22 -07:00
|
|
|
fn test_load_by_program_slot() {
|
2019-04-16 13:32:22 -07:00
|
|
|
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-10-23 22:01:22 -07:00
|
|
|
let loaded = accounts.load_by_program_slot(0, &Pubkey::new(&[2; 32]));
|
2019-04-16 13:32:22 -07:00
|
|
|
assert_eq!(loaded.len(), 2);
|
2019-10-23 22:01:22 -07:00
|
|
|
let loaded = accounts.load_by_program_slot(0, &Pubkey::new(&[3; 32]));
|
2019-04-16 13:32:22 -07:00
|
|
|
assert_eq!(loaded, vec![(pubkey2, account2)]);
|
2019-10-23 22:01:22 -07:00
|
|
|
let loaded = accounts.load_by_program_slot(0, &Pubkey::new(&[4; 32]));
|
2019-04-16 13:32:22 -07:00
|
|
|
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-05-16 10:32:27 -07:00
|
|
|
Err(TransactionError::ProgramAccountNotFound)
|
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-07-12 16:38:15 -07:00
|
|
|
accounts.store_slow(0, &Pubkey::default(), &Account::new(1, 0, &sysvar::id()));
|
2019-05-31 12:26:45 -07:00
|
|
|
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-05-30 21:31:35 -07:00
|
|
|
fn check_accounts(accounts: &Accounts, pubkeys: &Vec<Pubkey>, num: usize) {
|
|
|
|
for _ in 1..num {
|
|
|
|
let idx = thread_rng().gen_range(0, num - 1);
|
|
|
|
let ancestors = vec![(0, 0)].into_iter().collect();
|
2019-07-23 13:47:48 -07:00
|
|
|
let account = accounts.load_slow(&ancestors, &pubkeys[idx]);
|
|
|
|
let account1 = Some((
|
|
|
|
Account::new((idx + 1) as u64, 0, &Account::default().owner),
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(account, account1);
|
2019-05-30 21:31:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accounts_serialize() {
|
|
|
|
solana_logger::setup();
|
2019-08-05 22:53:19 -07:00
|
|
|
let (_accounts_dir, paths) = get_temp_accounts_paths(4).unwrap();
|
|
|
|
let accounts = Accounts::new(Some(paths));
|
2019-05-30 21:31:35 -07:00
|
|
|
|
|
|
|
let mut pubkeys: Vec<Pubkey> = vec![];
|
2019-08-13 16:05:37 -07:00
|
|
|
create_test_accounts(&accounts, &mut pubkeys, 100, 0);
|
2019-05-30 21:31:35 -07:00
|
|
|
check_accounts(&accounts, &pubkeys, 100);
|
|
|
|
accounts.add_root(0);
|
|
|
|
|
2019-08-13 16:05:37 -07:00
|
|
|
let mut writer = Cursor::new(vec![]);
|
|
|
|
serialize_into(
|
|
|
|
&mut writer,
|
|
|
|
&AccountsDBSerialize::new(&*accounts.accounts_db, 0),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-05-30 21:31:35 -07:00
|
|
|
|
2019-08-05 22:53:19 -07:00
|
|
|
let copied_accounts = TempDir::new().unwrap();
|
|
|
|
|
|
|
|
// Simulate obtaining a copy of the AppendVecs from a tarball
|
|
|
|
copy_append_vecs(&accounts.accounts_db, copied_accounts.path()).unwrap();
|
|
|
|
|
2019-08-13 16:05:37 -07:00
|
|
|
let buf = writer.into_inner();
|
2019-06-05 21:51:44 -07:00
|
|
|
let mut reader = BufReader::new(&buf[..]);
|
2019-08-05 22:53:19 -07:00
|
|
|
let (_accounts_dir, daccounts_paths) = get_temp_accounts_paths(2).unwrap();
|
|
|
|
let daccounts = Accounts::new(Some(daccounts_paths.clone()));
|
|
|
|
assert!(daccounts
|
|
|
|
.accounts_from_stream(&mut reader, daccounts_paths, copied_accounts.path())
|
|
|
|
.is_ok());
|
2019-05-30 21:31:35 -07:00
|
|
|
check_accounts(&daccounts, &pubkeys, 100);
|
|
|
|
assert_eq!(
|
|
|
|
accounts.hash_internal_state(0),
|
|
|
|
daccounts.hash_internal_state(0)
|
|
|
|
);
|
|
|
|
}
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
#[test]
|
|
|
|
fn test_accounts_locks() {
|
|
|
|
let keypair0 = Keypair::new();
|
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
|
|
|
|
let account0 = Account::new(1, 0, &Pubkey::default());
|
|
|
|
let account1 = Account::new(2, 0, &Pubkey::default());
|
|
|
|
let account2 = Account::new(3, 0, &Pubkey::default());
|
|
|
|
let account3 = Account::new(4, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
let accounts = Accounts::new(None);
|
|
|
|
accounts.store_slow(0, &keypair0.pubkey(), &account0);
|
|
|
|
accounts.store_slow(0, &keypair1.pubkey(), &account1);
|
|
|
|
accounts.store_slow(0, &keypair2.pubkey(), &account2);
|
|
|
|
accounts.store_slow(0, &keypair3.pubkey(), &account3);
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair0.pubkey(), keypair1.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let tx = Transaction::new(&[&keypair0], message, Hash::default());
|
2019-08-28 08:38:32 -07:00
|
|
|
let results0 = accounts.lock_accounts(&[tx.clone()], None);
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
assert!(results0[0].is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
*accounts
|
2019-10-23 22:01:22 -07:00
|
|
|
.credit_only_locks
|
2019-06-27 14:25:10 -07:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-07-11 18:46:49 -07:00
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
2019-06-27 14:25:10 -07:00
|
|
|
.get(&keypair1.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.lock_count
|
|
|
|
.lock()
|
|
|
|
.unwrap(),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair2.pubkey(), keypair1.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let tx0 = Transaction::new(&[&keypair2], message, Hash::default());
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair1.pubkey(), keypair3.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let tx1 = Transaction::new(&[&keypair1], message, Hash::default());
|
|
|
|
let txs = vec![tx0, tx1];
|
2019-08-28 08:38:32 -07:00
|
|
|
let results1 = accounts.lock_accounts(&txs, None);
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
assert!(results1[0].is_ok()); // Credit-only account (keypair1) can be referenced multiple times
|
|
|
|
assert!(results1[1].is_err()); // Credit-only account (keypair1) cannot also be locked as credit-debit
|
|
|
|
assert_eq!(
|
|
|
|
*accounts
|
2019-10-23 22:01:22 -07:00
|
|
|
.credit_only_locks
|
2019-06-27 14:25:10 -07:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-07-11 18:46:49 -07:00
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
2019-06-27 14:25:10 -07:00
|
|
|
.get(&keypair1.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.lock_count
|
|
|
|
.lock()
|
|
|
|
.unwrap(),
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
2019-09-16 21:45:16 -07:00
|
|
|
accounts.unlock_accounts(&[tx], None, &results0);
|
|
|
|
accounts.unlock_accounts(&txs, None, &results1);
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair1.pubkey(), keypair3.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let tx = Transaction::new(&[&keypair1], message, Hash::default());
|
2019-08-28 08:38:32 -07:00
|
|
|
let results2 = accounts.lock_accounts(&[tx], None);
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
assert!(results2[0].is_ok()); // Now keypair1 account can be locked as credit-debit
|
|
|
|
|
|
|
|
// Check that credit-only credits are still cached in accounts struct
|
2019-10-23 22:01:22 -07:00
|
|
|
let credit_only_locks = accounts.credit_only_locks.read().unwrap();
|
|
|
|
let credit_only_locks = credit_only_locks.as_ref().unwrap();
|
|
|
|
let keypair1_lock = credit_only_locks.get(&keypair1.pubkey());
|
2019-06-27 14:25:10 -07:00
|
|
|
assert!(keypair1_lock.is_some());
|
|
|
|
assert_eq!(*keypair1_lock.unwrap().lock_count.lock().unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accounts_locks_multithreaded() {
|
|
|
|
let counter = Arc::new(AtomicU64::new(0));
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
let keypair0 = Keypair::new();
|
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
|
|
|
|
let account0 = Account::new(1, 0, &Pubkey::default());
|
|
|
|
let account1 = Account::new(2, 0, &Pubkey::default());
|
|
|
|
let account2 = Account::new(3, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
let accounts = Accounts::new(None);
|
|
|
|
accounts.store_slow(0, &keypair0.pubkey(), &account0);
|
|
|
|
accounts.store_slow(0, &keypair1.pubkey(), &account1);
|
|
|
|
accounts.store_slow(0, &keypair2.pubkey(), &account2);
|
|
|
|
|
|
|
|
let accounts_arc = Arc::new(accounts);
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let credit_only_message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair0.pubkey(), keypair1.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let credit_only_tx = Transaction::new(&[&keypair0], credit_only_message, Hash::default());
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let credit_debit_message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair1.pubkey(), keypair2.pubkey(), native_loader::id()],
|
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let credit_debit_tx = Transaction::new(&[&keypair1], credit_debit_message, Hash::default());
|
|
|
|
|
|
|
|
let counter_clone = counter.clone();
|
|
|
|
let accounts_clone = accounts_arc.clone();
|
|
|
|
let exit_clone = exit.clone();
|
|
|
|
thread::spawn(move || {
|
|
|
|
let counter_clone = counter_clone.clone();
|
|
|
|
let exit_clone = exit_clone.clone();
|
|
|
|
loop {
|
|
|
|
let txs = vec![credit_debit_tx.clone()];
|
2019-08-28 08:38:32 -07:00
|
|
|
let results = accounts_clone.clone().lock_accounts(&txs, None);
|
2019-06-27 14:25:10 -07:00
|
|
|
for result in results.iter() {
|
|
|
|
if result.is_ok() {
|
|
|
|
counter_clone.clone().fetch_add(1, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 21:45:16 -07:00
|
|
|
accounts_clone.unlock_accounts(&txs, None, &results);
|
2019-06-27 14:25:10 -07:00
|
|
|
if exit_clone.clone().load(Ordering::Relaxed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let counter_clone = counter.clone();
|
|
|
|
for _ in 0..5 {
|
|
|
|
let txs = vec![credit_only_tx.clone()];
|
2019-08-28 08:38:32 -07:00
|
|
|
let results = accounts_arc.clone().lock_accounts(&txs, None);
|
2019-06-27 14:25:10 -07:00
|
|
|
if results[0].is_ok() {
|
|
|
|
let counter_value = counter_clone.clone().load(Ordering::SeqCst);
|
|
|
|
thread::sleep(time::Duration::from_millis(50));
|
|
|
|
assert_eq!(counter_value, counter_clone.clone().load(Ordering::SeqCst));
|
|
|
|
}
|
2019-09-16 21:45:16 -07:00
|
|
|
accounts_arc.unlock_accounts(&txs, None, &results);
|
2019-06-27 14:25:10 -07:00
|
|
|
thread::sleep(time::Duration::from_millis(50));
|
|
|
|
}
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-16 13:31:21 -07:00
|
|
|
fn test_commit_credits() {
|
2019-06-27 14:25:10 -07:00
|
|
|
let pubkey0 = Pubkey::new_rand();
|
|
|
|
let pubkey1 = Pubkey::new_rand();
|
|
|
|
let pubkey2 = Pubkey::new_rand();
|
|
|
|
|
2019-10-16 13:31:21 -07:00
|
|
|
let account0 = Account::new(1, 0, &Pubkey::default());
|
|
|
|
let account1 = Account::new(2, 0, &Pubkey::default());
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
let accounts = Accounts::new(None);
|
|
|
|
accounts.store_slow(0, &pubkey0, &account0);
|
|
|
|
accounts.store_slow(0, &pubkey1, &account1);
|
|
|
|
|
2019-07-11 18:46:49 -07:00
|
|
|
{
|
2019-10-23 22:01:22 -07:00
|
|
|
let mut credit_only_locks = accounts.credit_only_locks.write().unwrap();
|
|
|
|
let credit_only_locks = credit_only_locks.as_mut().unwrap();
|
|
|
|
credit_only_locks.insert(
|
2019-07-11 18:46:49 -07:00
|
|
|
pubkey0,
|
|
|
|
CreditOnlyLock {
|
2019-10-16 13:31:21 -07:00
|
|
|
credits: AtomicU64::new(0),
|
2019-07-11 18:46:49 -07:00
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks.insert(
|
2019-07-11 18:46:49 -07:00
|
|
|
pubkey1,
|
|
|
|
CreditOnlyLock {
|
|
|
|
credits: AtomicU64::new(5),
|
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
2019-10-23 22:01:22 -07:00
|
|
|
credit_only_locks.insert(
|
2019-07-11 18:46:49 -07:00
|
|
|
pubkey2,
|
|
|
|
CreditOnlyLock {
|
|
|
|
credits: AtomicU64::new(10),
|
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-06-27 14:25:10 -07:00
|
|
|
|
|
|
|
let ancestors = vec![(0, 0)].into_iter().collect();
|
2019-10-23 22:01:22 -07:00
|
|
|
accounts.commit_credits(&ancestors, 0);
|
2019-06-27 14:25:10 -07:00
|
|
|
|
2019-10-16 13:31:21 -07:00
|
|
|
// No change when CreditOnlyLock credits are 0
|
2019-06-27 14:25:10 -07:00
|
|
|
assert_eq!(
|
|
|
|
accounts.load_slow(&ancestors, &pubkey0).unwrap().0.lamports,
|
|
|
|
1
|
|
|
|
);
|
2019-10-16 13:31:21 -07:00
|
|
|
// New balance should equal previous balance plus CreditOnlyLock credits
|
2019-06-27 14:25:10 -07:00
|
|
|
assert_eq!(
|
|
|
|
accounts.load_slow(&ancestors, &pubkey1).unwrap().0.lamports,
|
2019-10-16 13:31:21 -07:00
|
|
|
7
|
2019-06-27 14:25:10 -07:00
|
|
|
);
|
2019-10-16 13:31:21 -07:00
|
|
|
// New account should be created
|
2019-06-27 14:25:10 -07:00
|
|
|
assert_eq!(
|
|
|
|
accounts.load_slow(&ancestors, &pubkey2).unwrap().0.lamports,
|
|
|
|
10
|
|
|
|
);
|
|
|
|
// Account locks should be cleared
|
2019-10-23 22:01:22 -07:00
|
|
|
assert!(accounts.credit_only_locks.read().unwrap().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_credit_only_pending_credits() {
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let account = Account::new(1, 0, &Pubkey::default());
|
|
|
|
|
|
|
|
let accounts = Accounts::new(None);
|
|
|
|
accounts.store_slow(0, &pubkey, &account);
|
|
|
|
|
|
|
|
accounts.insert_credit_only(
|
|
|
|
&pubkey,
|
|
|
|
CreditOnlyLock {
|
|
|
|
credits: AtomicU64::new(10),
|
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let ancestors = vec![(0, 0)].into_iter().collect();
|
|
|
|
assert_eq!(
|
|
|
|
accounts.load_slow(&ancestors, &pubkey).unwrap().0.lamports,
|
|
|
|
11
|
|
|
|
);
|
2019-07-11 18:46:49 -07:00
|
|
|
assert_eq!(
|
|
|
|
accounts
|
2019-10-23 22:01:22 -07:00
|
|
|
.accounts_db
|
|
|
|
.load_slow(&ancestors, &pubkey)
|
2019-07-11 18:46:49 -07:00
|
|
|
.unwrap()
|
2019-10-23 22:01:22 -07:00
|
|
|
.0
|
|
|
|
.lamports,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
accounts.commit_credits(&ancestors, 0);
|
|
|
|
assert_eq!(
|
|
|
|
accounts
|
|
|
|
.accounts_db
|
|
|
|
.load_slow(&ancestors, &pubkey)
|
2019-07-11 18:46:49 -07:00
|
|
|
.unwrap()
|
2019-10-23 22:01:22 -07:00
|
|
|
.0
|
|
|
|
.lamports,
|
|
|
|
11
|
2019-07-11 18:46:49 -07:00
|
|
|
);
|
2019-06-27 14:25:10 -07:00
|
|
|
}
|
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
#[test]
|
2019-08-19 13:00:37 -07:00
|
|
|
fn test_collect_accounts_to_store() {
|
2019-06-10 19:50:02 -07:00
|
|
|
let keypair0 = Keypair::new();
|
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair0.pubkey(), pubkey, native_loader::id()],
|
2019-06-10 19:50:02 -07:00
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
2019-06-27 14:25:10 -07:00
|
|
|
let tx0 = Transaction::new(&[&keypair0], message, Hash::default());
|
|
|
|
|
|
|
|
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
|
|
|
let message = Message::new_with_compiled_instructions(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
vec![keypair1.pubkey(), pubkey, native_loader::id()],
|
2019-06-10 19:50:02 -07:00
|
|
|
Hash::default(),
|
|
|
|
instructions,
|
|
|
|
);
|
2019-06-27 14:25:10 -07:00
|
|
|
let tx1 = Transaction::new(&[&keypair1], message, Hash::default());
|
2019-06-10 19:50:02 -07:00
|
|
|
let txs = vec![tx0, tx1];
|
|
|
|
|
|
|
|
let loaders = vec![Ok(()), Ok(())];
|
|
|
|
|
|
|
|
let account0 = Account::new(1, 0, &Pubkey::default());
|
|
|
|
let account1 = Account::new(2, 0, &Pubkey::default());
|
|
|
|
let account2 = Account::new(3, 0, &Pubkey::default());
|
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
let transaction_accounts0 = vec![account0, account2.clone()];
|
|
|
|
let transaction_loaders0 = vec![];
|
|
|
|
let transaction_credits0 = vec![0, 2];
|
|
|
|
let transaction_rents0 = vec![0, 0];
|
2019-06-10 19:50:02 -07:00
|
|
|
let loaded0 = Ok((
|
2019-08-23 14:04:53 -07:00
|
|
|
transaction_accounts0,
|
|
|
|
transaction_loaders0,
|
|
|
|
transaction_credits0,
|
|
|
|
transaction_rents0,
|
2019-06-10 19:50:02 -07:00
|
|
|
));
|
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
let transaction_accounts1 = vec![account1, account2.clone()];
|
|
|
|
let transaction_loaders1 = vec![];
|
|
|
|
let transaction_credits1 = vec![0, 3];
|
|
|
|
let transaction_rents1 = vec![0, 0];
|
2019-06-10 19:50:02 -07:00
|
|
|
let loaded1 = Ok((
|
2019-08-23 14:04:53 -07:00
|
|
|
transaction_accounts1,
|
|
|
|
transaction_loaders1,
|
|
|
|
transaction_credits1,
|
|
|
|
transaction_rents1,
|
2019-06-10 19:50:02 -07:00
|
|
|
));
|
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
let mut loaded = vec![loaded0, loaded1];
|
|
|
|
|
|
|
|
let accounts = Accounts::new(None);
|
2019-07-11 18:46:49 -07:00
|
|
|
{
|
2019-10-23 22:01:22 -07:00
|
|
|
let mut credit_only_locks = accounts.credit_only_locks.write().unwrap();
|
2019-07-11 18:46:49 -07:00
|
|
|
let credit_only_locks = credit_only_locks.as_mut().unwrap();
|
|
|
|
credit_only_locks.insert(
|
|
|
|
pubkey,
|
|
|
|
CreditOnlyLock {
|
|
|
|
credits: AtomicU64::new(0),
|
|
|
|
lock_count: Mutex::new(1),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-08-28 08:38:32 -07:00
|
|
|
let collected_accounts =
|
|
|
|
accounts.collect_accounts_to_store(&txs, None, &loaders, &mut loaded);
|
2019-08-19 13:00:37 -07:00
|
|
|
assert_eq!(collected_accounts.len(), 2);
|
|
|
|
assert!(collected_accounts
|
|
|
|
.iter()
|
|
|
|
.find(|(pubkey, _account)| *pubkey == &keypair0.pubkey())
|
|
|
|
.is_some());
|
|
|
|
assert!(collected_accounts
|
|
|
|
.iter()
|
|
|
|
.find(|(pubkey, _account)| *pubkey == &keypair1.pubkey())
|
|
|
|
.is_some());
|
|
|
|
|
2019-06-27 14:25:10 -07:00
|
|
|
// Ensure credit_only_lock reflects credits from both accounts: 2 + 3 = 5
|
2019-10-23 22:01:22 -07:00
|
|
|
let credit_only_locks = accounts.credit_only_locks.read().unwrap();
|
2019-07-11 18:46:49 -07:00
|
|
|
let credit_only_locks = credit_only_locks.as_ref().unwrap();
|
2019-06-27 14:25:10 -07:00
|
|
|
assert_eq!(
|
|
|
|
credit_only_locks
|
|
|
|
.get(&pubkey)
|
|
|
|
.unwrap()
|
|
|
|
.credits
|
|
|
|
.load(Ordering::Relaxed),
|
|
|
|
5
|
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
2018-12-17 12:41:23 -08:00
|
|
|
}
|