2018-08-10 21:13:34 -07:00
|
|
|
//! The `bank` module tracks client accounts and the progress of smart
|
2018-06-06 08:49:22 -07:00
|
|
|
//! contracts. It offers a high-level API that signs transactions
|
|
|
|
//! on behalf of the caller, and a low-level API for when they have
|
2018-03-29 11:20:54 -07:00
|
|
|
//! already been signed and verified.
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-09-17 15:38:10 -07:00
|
|
|
use bincode::deserialize;
|
2018-09-17 13:36:31 -07:00
|
|
|
use bincode::serialize;
|
2018-09-20 19:17:37 -07:00
|
|
|
use budget_program::BudgetState;
|
2018-07-16 18:33:50 -07:00
|
|
|
use counter::Counter;
|
2018-09-26 19:23:59 -07:00
|
|
|
use dynamic_program::DynamicProgram;
|
2018-05-10 13:01:42 -07:00
|
|
|
use entry::Entry;
|
2018-09-07 12:55:30 -07:00
|
|
|
use hash::{hash, Hash};
|
2018-06-29 11:58:29 -07:00
|
|
|
use itertools::Itertools;
|
|
|
|
use ledger::Block;
|
2018-08-06 11:35:45 -07:00
|
|
|
use log::Level;
|
2018-03-26 21:03:26 -07:00
|
|
|
use mint::Mint;
|
2018-09-17 15:38:10 -07:00
|
|
|
use payment_plan::Payment;
|
2018-10-04 13:15:54 -07:00
|
|
|
use poh_recorder::PohRecorder;
|
|
|
|
use signature::Keypair;
|
|
|
|
use signature::Signature;
|
2018-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::account::{Account, KeyedAccount};
|
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-08-13 08:55:13 -07:00
|
|
|
use std;
|
2018-10-04 13:15:54 -07:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
|
2018-03-02 09:16:39 -08:00
|
|
|
use std::result;
|
2018-06-22 10:45:22 -07:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-10-04 13:15:54 -07:00
|
|
|
use std::sync::{Mutex, RwLock};
|
2018-06-07 19:35:38 -07:00
|
|
|
use std::time::Instant;
|
2018-09-21 15:32:15 -07:00
|
|
|
use storage_program::StorageProgram;
|
2018-09-19 17:25:57 -07:00
|
|
|
use system_program::SystemProgram;
|
2018-09-26 09:07:53 -07:00
|
|
|
use system_transaction::SystemTransaction;
|
2018-09-27 11:51:08 -07:00
|
|
|
use tictactoe_dashboard_program::TicTacToeDashboardProgram;
|
2018-09-24 13:53:02 -07:00
|
|
|
use tictactoe_program::TicTacToeProgram;
|
2018-08-01 11:01:22 -07:00
|
|
|
use timing::{duration_as_us, timestamp};
|
2018-09-26 09:07:53 -07:00
|
|
|
use transaction::Transaction;
|
2018-08-09 12:31:34 -07:00
|
|
|
use window::WINDOW_SIZE;
|
2018-03-02 09:16:39 -08:00
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// The number of most recent `last_id` values that the bank will track the signatures
|
|
|
|
/// of. Once the bank discards a `last_id`, it will reject any transactions that use
|
|
|
|
/// that `last_id` in a transaction. Lowering this value reduces memory consumption,
|
|
|
|
/// but requires clients to update its `last_id` more frequently. Raising the value
|
|
|
|
/// lengthens the time a client must wait to be certain a missing transaction will
|
|
|
|
/// not be processed by the network.
|
2018-10-04 13:15:54 -07:00
|
|
|
pub const MAX_ENTRY_IDS: usize = 1024 * 32;
|
2018-04-05 09:26:43 -07:00
|
|
|
|
2018-06-29 11:58:29 -07:00
|
|
|
pub const VERIFY_BLOCK_SIZE: usize = 16;
|
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// Reasons a transaction might be rejected.
|
2018-09-07 20:18:36 -07:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2018-05-14 14:33:11 -07:00
|
|
|
pub enum BankError {
|
2018-10-04 13:15:54 -07:00
|
|
|
/// This Pubkey is being processed in another transaction
|
|
|
|
AccountInUse,
|
|
|
|
|
2018-08-09 08:13:57 -07:00
|
|
|
/// Attempt to debit from `Pubkey`, but no found no record of a prior credit.
|
2018-09-27 05:01:20 -07:00
|
|
|
AccountNotFound,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
/// The from `Pubkey` does not have sufficient balance to pay the fee to schedule the transaction
|
2018-09-27 05:01:20 -07:00
|
|
|
InsufficientFundsForFee,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
|
|
|
/// The bank has seen `Signature` before. This can occur under normal operation
|
|
|
|
/// when a UDP packet is duplicated, as a user error from a client not updating
|
|
|
|
/// its `last_id`, or as a double-spend attack.
|
2018-09-27 05:01:20 -07:00
|
|
|
DuplicateSignature,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
|
|
|
/// The bank has not seen the given `last_id` or the transaction is too old and
|
|
|
|
/// the `last_id` has been discarded.
|
2018-09-27 05:01:20 -07:00
|
|
|
LastIdNotFound,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
2018-09-26 14:57:34 -07:00
|
|
|
/// The bank has not seen a transaction with the given `Signature` or the transaction is
|
|
|
|
/// too old and has been discarded.
|
|
|
|
SignatureNotFound,
|
|
|
|
|
2018-06-29 11:58:29 -07:00
|
|
|
/// Proof of History verification failed.
|
|
|
|
LedgerVerificationFailed,
|
2018-09-07 20:18:36 -07:00
|
|
|
/// Contract's transaction token balance does not equal the balance after the transaction
|
2018-09-28 16:16:35 -07:00
|
|
|
UnbalancedTransaction(u8),
|
2018-09-17 13:36:31 -07:00
|
|
|
/// Contract's transactions resulted in an account with a negative balance
|
|
|
|
/// The difference from InsufficientFundsForFee is that the transaction was executed by the
|
|
|
|
/// contract
|
2018-09-28 16:16:35 -07:00
|
|
|
ResultWithNegativeTokens(u8),
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
/// Contract id is unknown
|
2018-09-28 16:16:35 -07:00
|
|
|
UnknownContractId(u8),
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
/// Contract modified an accounts contract id
|
2018-09-28 16:16:35 -07:00
|
|
|
ModifiedContractId(u8),
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
/// Contract spent the tokens of an account that doesn't belong to it
|
2018-09-28 16:16:35 -07:00
|
|
|
ExternalAccountTokenSpend(u8),
|
2018-09-21 13:21:01 -07:00
|
|
|
|
|
|
|
/// The program returned an error
|
2018-09-28 16:16:35 -07:00
|
|
|
ProgramRuntimeError(u8),
|
2018-10-04 13:15:54 -07:00
|
|
|
|
|
|
|
/// Recoding into PoH failed
|
|
|
|
RecordFailure,
|
2018-03-02 09:16:39 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
pub type Result<T> = result::Result<T, BankError>;
|
2018-09-27 13:49:50 -07:00
|
|
|
type SignatureStatusMap = HashMap<Signature, Result<()>>;
|
2018-09-17 13:36:31 -07:00
|
|
|
|
2018-09-07 20:18:36 -07:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ErrorCounters {
|
|
|
|
account_not_found_validator: usize,
|
|
|
|
account_not_found_leader: usize,
|
2018-10-04 13:15:54 -07:00
|
|
|
account_in_use: usize,
|
2018-09-07 20:18:36 -07:00
|
|
|
}
|
2018-06-06 08:49:22 -07:00
|
|
|
/// The state of all accounts and contracts after processing its entries.
|
2018-05-14 14:33:11 -07:00
|
|
|
pub struct Bank {
|
2018-06-06 08:49:22 -07:00
|
|
|
/// A map of account public keys to the balance in that account.
|
2018-08-10 21:13:34 -07:00
|
|
|
accounts: RwLock<HashMap<Pubkey, Account>>,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
/// set of accounts which are currently in the pipeline
|
|
|
|
account_locks: Mutex<HashSet<Pubkey>>,
|
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// A FIFO queue of `last_id` items, where each item is a set of signatures
|
2018-06-22 10:45:22 -07:00
|
|
|
/// that have been processed using that `last_id`. Rejected `last_id`
|
|
|
|
/// values are so old that the `last_id` has been pulled out of the queue.
|
2018-06-22 10:44:31 -07:00
|
|
|
last_ids: RwLock<VecDeque<Hash>>,
|
|
|
|
|
2018-08-01 11:01:22 -07:00
|
|
|
/// Mapping of hashes to signature sets along with timestamp. The bank uses this data to
|
2018-06-22 10:45:22 -07:00
|
|
|
/// reject transactions with signatures its seen before
|
2018-09-27 13:49:50 -07:00
|
|
|
last_ids_sigs: RwLock<HashMap<Hash, (SignatureStatusMap, u64)>>,
|
2018-06-06 08:49:22 -07:00
|
|
|
|
|
|
|
/// The number of transactions the bank has processed without error since the
|
|
|
|
/// start of the ledger.
|
2018-05-14 05:49:48 -07:00
|
|
|
transaction_count: AtomicUsize,
|
2018-08-10 15:18:44 -07:00
|
|
|
|
|
|
|
/// This bool allows us to submit metrics that are specific for leaders or validators
|
|
|
|
/// It is set to `true` by fullnode before creating the bank.
|
|
|
|
pub is_leader: bool,
|
2018-08-13 08:55:13 -07:00
|
|
|
|
|
|
|
// The latest finality time for the network
|
|
|
|
finality_time: AtomicUsize,
|
2018-09-23 22:13:44 -07:00
|
|
|
|
|
|
|
// loaded contracts hashed by program_id
|
|
|
|
loaded_contracts: RwLock<HashMap<Pubkey, DynamicProgram>>,
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:58:29 -07:00
|
|
|
impl Default for Bank {
|
|
|
|
fn default() -> Self {
|
|
|
|
Bank {
|
2018-08-10 21:13:34 -07:00
|
|
|
accounts: RwLock::new(HashMap::new()),
|
2018-10-04 13:15:54 -07:00
|
|
|
account_locks: Mutex::new(HashSet::new()),
|
2018-04-04 16:03:18 -07:00
|
|
|
last_ids: RwLock::new(VecDeque::new()),
|
2018-06-22 10:44:31 -07:00
|
|
|
last_ids_sigs: RwLock::new(HashMap::new()),
|
2018-05-14 05:49:48 -07:00
|
|
|
transaction_count: AtomicUsize::new(0),
|
2018-08-10 15:18:44 -07:00
|
|
|
is_leader: true,
|
2018-08-13 08:55:13 -07:00
|
|
|
finality_time: AtomicUsize::new(std::usize::MAX),
|
2018-09-23 22:13:44 -07:00
|
|
|
loaded_contracts: RwLock::new(HashMap::new()),
|
2018-06-29 11:58:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bank {
|
2018-08-10 15:18:44 -07:00
|
|
|
/// Create a default Bank
|
|
|
|
pub fn new_default(is_leader: bool) -> Self {
|
|
|
|
let mut bank = Bank::default();
|
|
|
|
bank.is_leader = is_leader;
|
|
|
|
bank
|
|
|
|
}
|
2018-06-29 11:58:29 -07:00
|
|
|
/// Create an Bank using a deposit.
|
|
|
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
|
|
|
let bank = Self::default();
|
2018-09-07 20:18:36 -07:00
|
|
|
{
|
|
|
|
let mut accounts = bank.accounts.write().unwrap();
|
|
|
|
let account = accounts.entry(deposit.to).or_insert_with(Account::default);
|
|
|
|
Self::apply_payment(deposit, account);
|
|
|
|
}
|
2018-05-29 09:04:37 -07:00
|
|
|
bank
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
/// Create an Bank with only a Mint. Typically used by unit tests.
|
2018-04-02 13:41:07 -07:00
|
|
|
pub fn new(mint: &Mint) -> Self {
|
2018-04-02 12:51:44 -07:00
|
|
|
let deposit = Payment {
|
|
|
|
to: mint.pubkey(),
|
|
|
|
tokens: mint.tokens,
|
|
|
|
};
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Self::new_from_deposit(&deposit);
|
|
|
|
bank.register_entry_id(&mint.last_id());
|
|
|
|
bank
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 20:18:36 -07:00
|
|
|
/// Commit funds to the given account
|
|
|
|
fn apply_payment(payment: &Payment, account: &mut Account) {
|
|
|
|
trace!("apply payments {}", payment.tokens);
|
|
|
|
account.tokens += payment.tokens;
|
2018-06-07 19:35:38 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// Return the last entry ID registered.
|
2018-05-03 12:24:35 -07:00
|
|
|
pub fn last_id(&self) -> Hash {
|
2018-05-09 18:16:37 -07:00
|
|
|
let last_ids = self.last_ids.read().expect("'last_ids' read lock");
|
2018-06-28 11:58:33 -07:00
|
|
|
let last_item = last_ids
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.expect("get last item from 'last_ids' list");
|
2018-06-22 10:44:31 -07:00
|
|
|
*last_item
|
2018-05-03 12:24:35 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// Store the given signature. The bank will reject any transaction with the same signature.
|
2018-09-27 13:49:50 -07:00
|
|
|
fn reserve_signature(signatures: &mut SignatureStatusMap, signature: &Signature) -> Result<()> {
|
2018-09-21 09:50:07 -07:00
|
|
|
if let Some(_result) = signatures.get(signature) {
|
2018-09-27 05:01:20 -07:00
|
|
|
return Err(BankError::DuplicateSignature);
|
2018-06-22 10:44:31 -07:00
|
|
|
}
|
2018-09-21 09:50:07 -07:00
|
|
|
signatures.insert(*signature, Ok(()));
|
2018-05-29 19:48:33 -07:00
|
|
|
Ok(())
|
2018-04-04 16:03:18 -07:00
|
|
|
}
|
|
|
|
|
2018-07-10 14:19:42 -07:00
|
|
|
/// Forget all signatures. Useful for benchmarking.
|
|
|
|
pub fn clear_signatures(&self) {
|
|
|
|
for (_, sigs) in self.last_ids_sigs.write().unwrap().iter_mut() {
|
2018-08-01 11:01:22 -07:00
|
|
|
sigs.0.clear();
|
2018-07-10 14:19:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
fn reserve_signature_with_last_id(
|
|
|
|
last_ids_sigs: &mut HashMap<Hash, (SignatureStatusMap, u64)>,
|
|
|
|
last_id: &Hash,
|
|
|
|
sig: &Signature,
|
|
|
|
) -> Result<()> {
|
|
|
|
if let Some(entry) = last_ids_sigs.get_mut(last_id) {
|
|
|
|
return Self::reserve_signature(&mut entry.0, sig);
|
2018-04-04 16:03:18 -07:00
|
|
|
}
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::LastIdNotFound)
|
2018-04-05 08:53:58 -07:00
|
|
|
}
|
2018-07-31 16:10:02 -07:00
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
fn reserve_signature_with_last_id_test(&self, sig: &Signature, last_id: &Hash) -> Result<()> {
|
|
|
|
let mut last_ids_sigs = self.last_ids_sigs.write().unwrap();
|
|
|
|
Self::reserve_signature_with_last_id(&mut last_ids_sigs, last_id, sig)
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:07:29 -07:00
|
|
|
fn update_signature_status(
|
2018-09-27 13:49:50 -07:00
|
|
|
signatures: &mut SignatureStatusMap,
|
2018-09-21 15:07:29 -07:00
|
|
|
signature: &Signature,
|
|
|
|
result: &Result<()>,
|
|
|
|
) {
|
|
|
|
let entry = signatures.entry(*signature).or_insert(Ok(()));
|
|
|
|
*entry = result.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_signature_status_with_last_id(
|
2018-10-04 13:15:54 -07:00
|
|
|
last_ids_sigs: &mut HashMap<Hash, (SignatureStatusMap, u64)>,
|
2018-09-21 15:07:29 -07:00
|
|
|
signature: &Signature,
|
|
|
|
result: &Result<()>,
|
|
|
|
last_id: &Hash,
|
|
|
|
) {
|
2018-10-04 13:15:54 -07:00
|
|
|
if let Some(entry) = last_ids_sigs.get_mut(last_id) {
|
2018-09-21 15:07:29 -07:00
|
|
|
Self::update_signature_status(&mut entry.0, signature, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_transaction_statuses(&self, txs: &[Transaction], res: &[Result<()>]) {
|
2018-10-04 13:15:54 -07:00
|
|
|
let mut last_ids = self.last_ids_sigs.write().unwrap();
|
2018-09-21 15:07:29 -07:00
|
|
|
for (i, tx) in txs.iter().enumerate() {
|
2018-10-04 13:15:54 -07:00
|
|
|
Self::update_signature_status_with_last_id(
|
|
|
|
&mut last_ids,
|
|
|
|
&tx.signature,
|
|
|
|
&res[i],
|
|
|
|
&tx.last_id,
|
|
|
|
);
|
2018-09-21 15:07:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 22:07:53 -07:00
|
|
|
/// Look through the last_ids and find all the valid ids
|
|
|
|
/// This is batched to avoid holding the lock for a significant amount of time
|
2018-08-01 11:01:22 -07:00
|
|
|
///
|
|
|
|
/// Return a vec of tuple of (valid index, timestamp)
|
|
|
|
/// index is into the passed ids slice to avoid copying hashes
|
|
|
|
pub fn count_valid_ids(&self, ids: &[Hash]) -> Vec<(usize, u64)> {
|
2018-07-31 22:07:53 -07:00
|
|
|
let last_ids = self.last_ids_sigs.read().unwrap();
|
2018-08-01 11:01:22 -07:00
|
|
|
let mut ret = Vec::new();
|
|
|
|
for (i, id) in ids.iter().enumerate() {
|
|
|
|
if let Some(entry) = last_ids.get(id) {
|
|
|
|
ret.push((i, entry.1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
2018-07-31 22:07:53 -07:00
|
|
|
}
|
2018-04-05 08:53:58 -07:00
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
/// Tell the bank which Entry IDs exist on the ledger. This function
|
2018-04-05 08:53:58 -07:00
|
|
|
/// assumes subsequent calls correspond to later entries, and will boot
|
|
|
|
/// the oldest ones once its internal cache is full. Once boot, the
|
2018-05-14 14:33:11 -07:00
|
|
|
/// bank will reject transactions using that `last_id`.
|
2018-04-05 08:53:58 -07:00
|
|
|
pub fn register_entry_id(&self, last_id: &Hash) {
|
2018-08-03 11:27:44 -07:00
|
|
|
let mut last_ids = self
|
|
|
|
.last_ids
|
2018-05-11 11:38:52 -07:00
|
|
|
.write()
|
|
|
|
.expect("'last_ids' write lock in register_entry_id");
|
2018-08-03 11:27:44 -07:00
|
|
|
let mut last_ids_sigs = self
|
|
|
|
.last_ids_sigs
|
2018-06-22 10:44:31 -07:00
|
|
|
.write()
|
|
|
|
.expect("last_ids_sigs write lock");
|
2018-04-05 09:26:43 -07:00
|
|
|
if last_ids.len() >= MAX_ENTRY_IDS {
|
2018-06-22 10:44:31 -07:00
|
|
|
let id = last_ids.pop_front().unwrap();
|
|
|
|
last_ids_sigs.remove(&id);
|
2018-04-05 09:26:43 -07:00
|
|
|
}
|
2018-09-21 09:50:07 -07:00
|
|
|
last_ids_sigs.insert(*last_id, (HashMap::new(), timestamp()));
|
2018-06-22 10:44:31 -07:00
|
|
|
last_ids.push_back(*last_id);
|
2018-04-02 08:36:22 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
/// Process a Transaction. This is used for unit tests and simply calls the vector Bank::process_transactions method.
|
2018-07-10 14:19:42 -07:00
|
|
|
pub fn process_transaction(&self, tx: &Transaction) -> Result<()> {
|
2018-10-04 13:15:54 -07:00
|
|
|
let txs = vec![tx.clone()];
|
|
|
|
match self.process_transactions(&txs)[0] {
|
2018-09-07 20:18:36 -07:00
|
|
|
Err(ref e) => {
|
|
|
|
info!("process_transaction error: {:?}", e);
|
|
|
|
Err((*e).clone())
|
|
|
|
}
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 13:15:54 -07:00
|
|
|
fn lock_account(
|
|
|
|
account_locks: &mut HashSet<Pubkey>,
|
|
|
|
keys: &[Pubkey],
|
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Copy all the accounts
|
|
|
|
for k in keys {
|
|
|
|
if account_locks.contains(k) {
|
|
|
|
error_counters.account_in_use += 1;
|
|
|
|
return Err(BankError::AccountInUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k in keys {
|
|
|
|
account_locks.insert(*k);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unlock_account(tx: &Transaction, result: &Result<()>, account_locks: &mut HashSet<Pubkey>) {
|
|
|
|
match result {
|
|
|
|
Err(BankError::AccountInUse) => (),
|
|
|
|
_ => for k in &tx.account_keys {
|
|
|
|
account_locks.remove(k);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-09-07 20:18:36 -07:00
|
|
|
|
|
|
|
fn load_account(
|
|
|
|
&self,
|
|
|
|
tx: &Transaction,
|
|
|
|
accounts: &HashMap<Pubkey, Account>,
|
2018-10-04 13:15:54 -07:00
|
|
|
last_ids_sigs: &mut HashMap<Hash, (SignatureStatusMap, u64)>,
|
2018-09-07 20:18:36 -07:00
|
|
|
error_counters: &mut ErrorCounters,
|
|
|
|
) -> Result<Vec<Account>> {
|
|
|
|
// Copy all the accounts
|
2018-09-28 16:16:35 -07:00
|
|
|
if accounts.get(&tx.account_keys[0]).is_none() {
|
2018-09-07 20:18:36 -07:00
|
|
|
if !self.is_leader {
|
|
|
|
error_counters.account_not_found_validator += 1;
|
|
|
|
} else {
|
|
|
|
error_counters.account_not_found_leader += 1;
|
|
|
|
}
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::AccountNotFound)
|
2018-09-28 16:16:35 -07:00
|
|
|
} else if accounts.get(&tx.account_keys[0]).unwrap().tokens < tx.fee {
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::InsufficientFundsForFee)
|
2018-09-07 20:18:36 -07:00
|
|
|
} else {
|
|
|
|
let mut called_accounts: Vec<Account> = tx
|
2018-09-28 16:16:35 -07:00
|
|
|
.account_keys
|
2018-09-07 20:18:36 -07:00
|
|
|
.iter()
|
2018-09-14 11:40:05 -07:00
|
|
|
.map(|key| accounts.get(key).cloned().unwrap_or_default())
|
2018-09-07 20:18:36 -07:00
|
|
|
.collect();
|
|
|
|
// There is no way to predict what contract will execute without an error
|
|
|
|
// If a fee can pay for execution then the contract will be scheduled
|
2018-10-04 13:15:54 -07:00
|
|
|
let err =
|
|
|
|
Self::reserve_signature_with_last_id(last_ids_sigs, &tx.last_id, &tx.signature);
|
|
|
|
err?;
|
2018-09-07 20:18:36 -07:00
|
|
|
called_accounts[0].tokens -= tx.fee;
|
|
|
|
Ok(called_accounts)
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
/// This function will prevent multiple threads from modifying the same account state at the
|
|
|
|
/// same time
|
|
|
|
#[must_use]
|
|
|
|
fn lock_accounts(&self, txs: &[Transaction]) -> Vec<Result<()>> {
|
|
|
|
let mut account_locks = self.account_locks.lock().unwrap();
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
|
|
|
let rv = txs
|
|
|
|
.iter()
|
|
|
|
.map(|tx| Self::lock_account(&mut account_locks, &tx.account_keys, &mut error_counters))
|
|
|
|
.collect();
|
|
|
|
inc_new_counter_info!(
|
|
|
|
"bank-process_transactions-account_in_use",
|
|
|
|
error_counters.account_in_use
|
|
|
|
);
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Once accounts are unlocked, new transactions that modify that state can enter the pipeline
|
|
|
|
fn unlock_accounts(&self, txs: &[Transaction], results: &[Result<()>]) {
|
|
|
|
debug!("bank unlock accounts");
|
|
|
|
let mut account_locks = self.account_locks.lock().unwrap();
|
|
|
|
txs.iter()
|
|
|
|
.zip(results.iter())
|
|
|
|
.for_each(|(tx, result)| Self::unlock_account(tx, result, &mut account_locks));
|
|
|
|
}
|
|
|
|
|
2018-09-07 20:18:36 -07:00
|
|
|
fn load_accounts(
|
|
|
|
&self,
|
2018-09-14 11:40:05 -07:00
|
|
|
txs: &[Transaction],
|
2018-10-04 13:15:54 -07:00
|
|
|
results: Vec<Result<()>>,
|
2018-09-07 20:18:36 -07:00
|
|
|
error_counters: &mut ErrorCounters,
|
2018-10-04 13:15:54 -07:00
|
|
|
) -> Vec<(Result<Vec<Account>>)> {
|
|
|
|
let accounts = self.accounts.read().unwrap();
|
|
|
|
let mut last_sigs = self.last_ids_sigs.write().unwrap();
|
2018-09-07 20:18:36 -07:00
|
|
|
txs.iter()
|
2018-10-04 13:15:54 -07:00
|
|
|
.zip(results.into_iter())
|
|
|
|
.map(|etx| match etx {
|
|
|
|
(tx, Ok(())) => self.load_account(tx, &accounts, &mut last_sigs, error_counters),
|
|
|
|
(_, Err(e)) => Err(e),
|
|
|
|
}).collect()
|
2018-09-07 20:18:36 -07:00
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
pub fn verify_transaction(
|
2018-10-04 13:36:15 -07:00
|
|
|
instruction_index: usize,
|
2018-09-28 16:16:35 -07:00
|
|
|
tx_program_id: &Pubkey,
|
2018-09-19 17:25:57 -07:00
|
|
|
pre_program_id: &Pubkey,
|
2018-09-17 13:36:31 -07:00
|
|
|
pre_tokens: i64,
|
|
|
|
account: &Account,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Verify the transaction
|
2018-09-19 17:25:57 -07:00
|
|
|
// make sure that program_id is still the same or this was just assigned by the system call contract
|
|
|
|
if !((*pre_program_id == account.program_id)
|
2018-09-28 16:16:35 -07:00
|
|
|
|| (SystemProgram::check_id(&tx_program_id)
|
2018-09-19 17:25:57 -07:00
|
|
|
&& SystemProgram::check_id(&pre_program_id)))
|
2018-09-17 13:36:31 -07:00
|
|
|
{
|
|
|
|
//TODO, this maybe redundant bpf should be able to guarantee this property
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::ModifiedContractId(instruction_index as u8));
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
// For accounts unassigned to the contract, the individual balance of each accounts cannot decrease.
|
2018-09-28 16:16:35 -07:00
|
|
|
if *tx_program_id != account.program_id && pre_tokens > account.tokens {
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::ExternalAccountTokenSpend(
|
|
|
|
instruction_index as u8,
|
|
|
|
));
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
if account.tokens < 0 {
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::ResultWithNegativeTokens(instruction_index as u8));
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
fn loaded_contract(
|
|
|
|
&self,
|
|
|
|
tx_program_id: &Pubkey,
|
|
|
|
tx: &Transaction,
|
|
|
|
program_index: usize,
|
|
|
|
accounts: &mut [&mut Account],
|
|
|
|
) -> bool {
|
2018-09-23 22:13:44 -07:00
|
|
|
let loaded_contracts = self.loaded_contracts.write().unwrap();
|
2018-09-28 16:16:35 -07:00
|
|
|
match loaded_contracts.get(&tx_program_id) {
|
2018-09-23 22:13:44 -07:00
|
|
|
Some(dc) => {
|
2018-09-28 16:16:35 -07:00
|
|
|
let mut infos: Vec<_> = (&tx.account_keys)
|
2018-09-23 22:13:44 -07:00
|
|
|
.into_iter()
|
|
|
|
.zip(accounts)
|
|
|
|
.map(|(key, account)| KeyedAccount { key, account })
|
|
|
|
.collect();
|
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
dc.call(&mut infos, tx.userdata(program_index));
|
2018-09-23 22:13:44 -07:00
|
|
|
true
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
/// Execute a function with a subset of accounts as writable references.
|
|
|
|
/// Since the subset can point to the same references, in any order there is no way
|
|
|
|
/// for the borrow checker to track them with regards to the original set.
|
|
|
|
fn with_subset<F, A>(accounts: &mut [Account], ixes: &[u8], func: F) -> A
|
|
|
|
where
|
|
|
|
F: Fn(&mut [&mut Account]) -> A,
|
|
|
|
{
|
|
|
|
let mut subset: Vec<&mut Account> = ixes
|
|
|
|
.iter()
|
|
|
|
.map(|ix| {
|
|
|
|
let ptr = &mut accounts[*ix as usize] as *mut Account;
|
|
|
|
// lifetime of this unsafe is only within the scope of the closure
|
|
|
|
// there is no way to reorder them without breaking borrow checker rules
|
|
|
|
unsafe { &mut *ptr }
|
|
|
|
}).collect();
|
|
|
|
func(&mut subset)
|
|
|
|
}
|
|
|
|
/// Execute an instruction
|
|
|
|
/// This method calls the instruction's program entry pont method and verifies that the result of
|
|
|
|
/// the call does not violate the bank's accounting rules.
|
2018-09-17 19:37:59 -07:00
|
|
|
/// The accounts are committed back to the bank only if this function returns Ok(_).
|
2018-09-28 16:16:35 -07:00
|
|
|
fn execute_instruction(
|
|
|
|
&self,
|
|
|
|
tx: &Transaction,
|
2018-10-04 13:36:15 -07:00
|
|
|
instruction_index: usize,
|
2018-09-28 16:16:35 -07:00
|
|
|
program_accounts: &mut [&mut Account],
|
|
|
|
) -> Result<()> {
|
2018-10-04 13:36:15 -07:00
|
|
|
let tx_program_id = tx.program_id(instruction_index);
|
2018-09-28 16:16:35 -07:00
|
|
|
// TODO: the runtime should be checking read/write access to memory
|
|
|
|
// we are trusting the hard coded contracts not to clobber or allocate
|
|
|
|
let pre_total: i64 = program_accounts.iter().map(|a| a.tokens).sum();
|
|
|
|
let pre_data: Vec<_> = program_accounts
|
2018-09-17 13:36:31 -07:00
|
|
|
.iter_mut()
|
2018-09-19 17:25:57 -07:00
|
|
|
.map(|a| (a.program_id, a.tokens))
|
2018-09-17 13:36:31 -07:00
|
|
|
.collect();
|
2018-09-07 20:18:36 -07:00
|
|
|
|
|
|
|
// Call the contract method
|
|
|
|
// It's up to the contract to implement its own rules on moving funds
|
2018-09-28 16:16:35 -07:00
|
|
|
if SystemProgram::check_id(&tx_program_id) {
|
|
|
|
SystemProgram::process_transaction(
|
|
|
|
&tx,
|
2018-10-04 13:36:15 -07:00
|
|
|
instruction_index,
|
2018-09-28 16:16:35 -07:00
|
|
|
program_accounts,
|
|
|
|
&self.loaded_contracts,
|
|
|
|
)
|
|
|
|
} else if BudgetState::check_id(&tx_program_id) {
|
2018-10-04 13:36:15 -07:00
|
|
|
if BudgetState::process_transaction(&tx, instruction_index, program_accounts).is_err() {
|
|
|
|
return Err(BankError::ProgramRuntimeError(instruction_index as u8));
|
2018-09-21 13:21:01 -07:00
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
} else if StorageProgram::check_id(&tx_program_id) {
|
2018-10-04 13:36:15 -07:00
|
|
|
if StorageProgram::process_transaction(&tx, instruction_index, program_accounts)
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return Err(BankError::ProgramRuntimeError(instruction_index as u8));
|
2018-09-26 13:50:30 -07:00
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
} else if TicTacToeProgram::check_id(&tx_program_id) {
|
2018-10-04 13:36:15 -07:00
|
|
|
if TicTacToeProgram::process_transaction(&tx, instruction_index, program_accounts)
|
|
|
|
.is_err()
|
2018-09-28 16:16:35 -07:00
|
|
|
{
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::ProgramRuntimeError(instruction_index as u8));
|
2018-09-24 13:53:02 -07:00
|
|
|
}
|
2018-09-27 11:51:08 -07:00
|
|
|
} else if TicTacToeDashboardProgram::check_id(&tx_program_id) {
|
2018-10-04 13:36:15 -07:00
|
|
|
if TicTacToeDashboardProgram::process_transaction(
|
|
|
|
&tx,
|
|
|
|
instruction_index,
|
|
|
|
program_accounts,
|
|
|
|
).is_err()
|
2018-09-27 11:51:08 -07:00
|
|
|
{
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::ProgramRuntimeError(instruction_index as u8));
|
2018-09-27 11:51:08 -07:00
|
|
|
}
|
2018-10-04 13:36:15 -07:00
|
|
|
} else if self.loaded_contract(tx_program_id, tx, instruction_index, program_accounts) {
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
2018-10-04 13:36:15 -07:00
|
|
|
return Err(BankError::UnknownContractId(instruction_index as u8));
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-09-07 20:18:36 -07:00
|
|
|
// Verify the transaction
|
2018-09-28 16:16:35 -07:00
|
|
|
for ((pre_program_id, pre_tokens), post_account) in
|
|
|
|
pre_data.iter().zip(program_accounts.iter())
|
|
|
|
{
|
|
|
|
Self::verify_transaction(
|
2018-10-04 13:36:15 -07:00
|
|
|
instruction_index,
|
2018-09-28 16:16:35 -07:00
|
|
|
&tx_program_id,
|
|
|
|
pre_program_id,
|
|
|
|
*pre_tokens,
|
|
|
|
post_account,
|
|
|
|
)?;
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
// The total sum of all the tokens in all the pages cannot change.
|
2018-09-28 16:16:35 -07:00
|
|
|
let post_total: i64 = program_accounts.iter().map(|a| a.tokens).sum();
|
2018-09-07 20:18:36 -07:00
|
|
|
if pre_total != post_total {
|
2018-10-04 13:36:15 -07:00
|
|
|
Err(BankError::UnbalancedTransaction(instruction_index as u8))
|
2018-09-07 20:18:36 -07:00
|
|
|
} else {
|
2018-09-21 14:48:10 -07:00
|
|
|
Ok(())
|
2018-09-07 20:18:36 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
/// Execute a transaction.
|
|
|
|
/// This method calls each instruction in the transaction over the set of loaded Accounts
|
|
|
|
/// The accounts are committed back to the bank only if every instruction succeeds
|
|
|
|
fn execute_transaction(&self, tx: &Transaction, tx_accounts: &mut [Account]) -> Result<()> {
|
2018-10-04 13:36:15 -07:00
|
|
|
for (instruction_index, prog) in tx.instructions.iter().enumerate() {
|
2018-09-28 16:16:35 -07:00
|
|
|
Self::with_subset(tx_accounts, &prog.accounts, |program_accounts| {
|
2018-10-04 13:36:15 -07:00
|
|
|
self.execute_instruction(tx, instruction_index, program_accounts)
|
2018-09-28 16:16:35 -07:00
|
|
|
})?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-07 20:18:36 -07:00
|
|
|
|
|
|
|
pub fn store_accounts(
|
2018-10-04 13:15:54 -07:00
|
|
|
&self,
|
2018-09-21 14:48:10 -07:00
|
|
|
txs: &[Transaction],
|
|
|
|
res: &[Result<()>],
|
2018-09-14 11:40:05 -07:00
|
|
|
loaded: &[Result<Vec<Account>>],
|
2018-09-07 20:18:36 -07:00
|
|
|
) {
|
2018-10-04 13:15:54 -07:00
|
|
|
let mut accounts = self.accounts.write().unwrap();
|
2018-09-21 14:48:10 -07:00
|
|
|
for (i, racc) in loaded.iter().enumerate() {
|
|
|
|
if res[i].is_err() || racc.is_err() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tx = &txs[i];
|
|
|
|
let acc = racc.as_ref().unwrap();
|
2018-09-28 16:16:35 -07:00
|
|
|
for (key, account) in tx.account_keys.iter().zip(acc.iter()) {
|
2018-09-21 14:48:10 -07:00
|
|
|
//purge if 0
|
|
|
|
if account.tokens == 0 {
|
|
|
|
accounts.remove(&key);
|
|
|
|
} else {
|
|
|
|
*accounts.entry(*key).or_insert_with(Account::default) = account.clone();
|
|
|
|
assert_eq!(accounts.get(key).unwrap().tokens, account.tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
pub fn process_and_record_transactions(
|
|
|
|
&self,
|
|
|
|
txs: &[Transaction],
|
|
|
|
poh: &PohRecorder,
|
|
|
|
) -> Result<()> {
|
|
|
|
let now = Instant::now();
|
|
|
|
// Once accounts are locked, other threads cannot encode transactions that will modify the
|
|
|
|
// same account state
|
|
|
|
let locked_accounts = self.lock_accounts(txs);
|
|
|
|
let lock_time = now.elapsed();
|
|
|
|
let now = Instant::now();
|
|
|
|
let results = self.execute_and_commit_transactions(txs, locked_accounts);
|
|
|
|
let process_time = now.elapsed();
|
|
|
|
let now = Instant::now();
|
|
|
|
self.record_transactions(txs, &results, poh)?;
|
|
|
|
let record_time = now.elapsed();
|
|
|
|
let now = Instant::now();
|
|
|
|
// Once the accounts are unlocked new transactions can enter the pipeline to process them
|
|
|
|
self.unlock_accounts(&txs, &results);
|
|
|
|
let unlock_time = now.elapsed();
|
|
|
|
debug!(
|
|
|
|
"lock: {}us process: {}us record: {}us unlock: {}us txs_len={}",
|
|
|
|
duration_as_us(&lock_time),
|
|
|
|
duration_as_us(&process_time),
|
|
|
|
duration_as_us(&record_time),
|
|
|
|
duration_as_us(&unlock_time),
|
|
|
|
txs.len(),
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn record_transactions(
|
|
|
|
&self,
|
|
|
|
txs: &[Transaction],
|
|
|
|
results: &[Result<()>],
|
|
|
|
poh: &PohRecorder,
|
|
|
|
) -> Result<()> {
|
|
|
|
let processed_transactions: Vec<_> = results
|
|
|
|
.iter()
|
|
|
|
.zip(txs.iter())
|
|
|
|
.filter_map(|(r, x)| match r {
|
|
|
|
Ok(_) => Some(x.clone()),
|
|
|
|
Err(ref e) => {
|
|
|
|
debug!("process transaction failed {:?}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
// unlock all the accounts with errors which are filtered by the above `filter_map`
|
|
|
|
if !processed_transactions.is_empty() {
|
|
|
|
let hash = Transaction::hash(&processed_transactions);
|
|
|
|
debug!("processed ok: {} {}", processed_transactions.len(), hash);
|
|
|
|
// record and unlock will unlock all the successfull transactions
|
|
|
|
poh.record(hash, processed_transactions).map_err(|e| {
|
|
|
|
warn!("record failure: {:?}", e);
|
|
|
|
BankError::RecordFailure
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-22 10:45:22 -07:00
|
|
|
/// Process a batch of transactions.
|
2018-06-22 22:36:03 -07:00
|
|
|
#[must_use]
|
2018-10-04 13:15:54 -07:00
|
|
|
pub fn execute_and_commit_transactions(
|
|
|
|
&self,
|
|
|
|
txs: &[Transaction],
|
|
|
|
locked_accounts: Vec<Result<()>>,
|
|
|
|
) -> Vec<Result<()>> {
|
2018-09-18 11:31:00 -07:00
|
|
|
debug!("processing transactions: {}", txs.len());
|
2018-09-07 20:18:36 -07:00
|
|
|
let mut error_counters = ErrorCounters::default();
|
2018-06-07 19:35:38 -07:00
|
|
|
let now = Instant::now();
|
2018-10-04 13:15:54 -07:00
|
|
|
let mut loaded_accounts = self.load_accounts(txs, locked_accounts, &mut error_counters);
|
2018-09-07 20:18:36 -07:00
|
|
|
let load_elapsed = now.elapsed();
|
2018-06-22 10:44:31 -07:00
|
|
|
let now = Instant::now();
|
2018-10-04 13:15:54 -07:00
|
|
|
let executed: Vec<Result<()>> = loaded_accounts
|
2018-09-07 20:18:36 -07:00
|
|
|
.iter_mut()
|
2018-09-21 14:48:10 -07:00
|
|
|
.zip(txs.iter())
|
2018-09-07 20:18:36 -07:00
|
|
|
.map(|(acc, tx)| match acc {
|
|
|
|
Err(e) => Err(e.clone()),
|
2018-09-23 22:13:44 -07:00
|
|
|
Ok(ref mut accounts) => self.execute_transaction(tx, accounts),
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect();
|
2018-09-07 20:18:36 -07:00
|
|
|
let execution_elapsed = now.elapsed();
|
|
|
|
let now = Instant::now();
|
2018-10-04 13:15:54 -07:00
|
|
|
self.store_accounts(txs, &executed, &loaded_accounts);
|
|
|
|
// once committed there is no way to unroll
|
2018-09-07 20:18:36 -07:00
|
|
|
let write_elapsed = now.elapsed();
|
2018-06-22 10:45:22 -07:00
|
|
|
debug!(
|
2018-10-04 13:15:54 -07:00
|
|
|
"load: {}us execute: {}us store: {}us txs_len={}",
|
2018-09-07 20:18:36 -07:00
|
|
|
duration_as_us(&load_elapsed),
|
|
|
|
duration_as_us(&execution_elapsed),
|
|
|
|
duration_as_us(&write_elapsed),
|
2018-10-04 13:15:54 -07:00
|
|
|
txs.len(),
|
2018-06-22 10:45:22 -07:00
|
|
|
);
|
2018-10-04 13:15:54 -07:00
|
|
|
self.update_transaction_statuses(txs, &executed);
|
2018-06-22 12:11:27 -07:00
|
|
|
let mut tx_count = 0;
|
2018-07-19 15:38:35 -07:00
|
|
|
let mut err_count = 0;
|
2018-10-04 13:15:54 -07:00
|
|
|
for r in &executed {
|
2018-06-07 19:35:38 -07:00
|
|
|
if r.is_ok() {
|
2018-06-22 12:11:27 -07:00
|
|
|
tx_count += 1;
|
2018-06-22 10:44:31 -07:00
|
|
|
} else {
|
2018-07-19 15:38:35 -07:00
|
|
|
if err_count == 0 {
|
2018-09-18 11:31:00 -07:00
|
|
|
debug!("tx error: {:?}", r);
|
2018-07-19 15:38:35 -07:00
|
|
|
}
|
|
|
|
err_count += 1;
|
2018-06-07 19:35:38 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-19 15:38:35 -07:00
|
|
|
if err_count > 0 {
|
|
|
|
info!("{} errors of {} txs", err_count, err_count + tx_count);
|
2018-08-20 14:56:01 -07:00
|
|
|
if !self.is_leader {
|
2018-08-23 09:42:59 -07:00
|
|
|
inc_new_counter_info!("bank-process_transactions_err-validator", err_count);
|
|
|
|
inc_new_counter_info!(
|
|
|
|
"bank-appy_debits-account_not_found-validator",
|
2018-09-07 20:18:36 -07:00
|
|
|
error_counters.account_not_found_validator
|
2018-08-23 09:42:59 -07:00
|
|
|
);
|
2018-08-20 14:56:01 -07:00
|
|
|
} else {
|
2018-08-23 09:42:59 -07:00
|
|
|
inc_new_counter_info!("bank-process_transactions_err-leader", err_count);
|
2018-09-07 20:18:36 -07:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"bank-appy_debits-account_not_found-leader",
|
|
|
|
error_counters.account_not_found_leader
|
|
|
|
);
|
2018-08-20 14:56:01 -07:00
|
|
|
}
|
2018-10-04 13:15:54 -07:00
|
|
|
inc_new_counter_info!("bank-process_transactions-error_count", err_count);
|
2018-07-19 15:38:35 -07:00
|
|
|
}
|
2018-10-04 13:15:54 -07:00
|
|
|
|
2018-06-22 10:45:22 -07:00
|
|
|
self.transaction_count
|
2018-06-22 12:11:27 -07:00
|
|
|
.fetch_add(tx_count, Ordering::Relaxed);
|
2018-10-04 13:15:54 -07:00
|
|
|
inc_new_counter_info!("bank-process_transactions-txs", tx_count);
|
|
|
|
executed
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn process_transactions(&self, txs: &[Transaction]) -> Vec<Result<()>> {
|
|
|
|
let locked_accounts = self.lock_accounts(txs);
|
|
|
|
let results = self.execute_and_commit_transactions(txs, locked_accounts);
|
|
|
|
self.unlock_accounts(txs, &results);
|
|
|
|
results
|
2018-04-05 21:13:54 -07:00
|
|
|
}
|
|
|
|
|
2018-09-24 12:26:47 -07:00
|
|
|
pub fn process_entry(&self, entry: &Entry) -> Result<()> {
|
2018-07-11 10:30:07 -07:00
|
|
|
if !entry.transactions.is_empty() {
|
2018-09-21 14:48:10 -07:00
|
|
|
for result in self.process_transactions(&entry.transactions) {
|
2018-07-11 10:30:07 -07:00
|
|
|
result?;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 21:01:13 -07:00
|
|
|
self.register_entry_id(&entry.id);
|
2018-07-11 10:30:07 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process an ordered list of entries, populating a circular buffer "tail"
|
|
|
|
/// as we go.
|
|
|
|
fn process_entries_tail(
|
2018-07-10 11:22:45 -07:00
|
|
|
&self,
|
|
|
|
entries: Vec<Entry>,
|
2018-07-10 11:28:12 -07:00
|
|
|
tail: &mut Vec<Entry>,
|
2018-07-10 15:58:08 -07:00
|
|
|
tail_idx: &mut usize,
|
2018-07-10 11:22:45 -07:00
|
|
|
) -> Result<u64> {
|
|
|
|
let mut entry_count = 0;
|
|
|
|
|
|
|
|
for entry in entries {
|
2018-07-10 15:58:08 -07:00
|
|
|
if tail.len() > *tail_idx {
|
|
|
|
tail[*tail_idx] = entry.clone();
|
2018-07-10 12:23:55 -07:00
|
|
|
} else {
|
|
|
|
tail.push(entry.clone());
|
|
|
|
}
|
2018-07-10 15:58:08 -07:00
|
|
|
*tail_idx = (*tail_idx + 1) % WINDOW_SIZE as usize;
|
2018-07-10 11:22:45 -07:00
|
|
|
|
2018-07-11 10:30:07 -07:00
|
|
|
entry_count += 1;
|
2018-09-24 12:26:47 -07:00
|
|
|
self.process_entry(&entry)?;
|
2018-07-10 11:22:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(entry_count)
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:49:22 -07:00
|
|
|
/// Process an ordered list of entries.
|
2018-09-24 12:26:47 -07:00
|
|
|
pub fn process_entries(&self, entries: &[Entry]) -> Result<()> {
|
2018-05-10 13:01:42 -07:00
|
|
|
for entry in entries {
|
2018-09-24 12:26:47 -07:00
|
|
|
self.process_entry(&entry)?;
|
2018-05-10 13:01:42 -07:00
|
|
|
}
|
2018-09-03 02:48:11 -07:00
|
|
|
Ok(())
|
2018-05-10 13:01:42 -07:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:58:29 -07:00
|
|
|
/// Append entry blocks to the ledger, verifying them along the way.
|
2018-07-11 10:30:07 -07:00
|
|
|
fn process_blocks<I>(
|
2018-07-10 11:22:45 -07:00
|
|
|
&self,
|
2018-08-07 11:09:33 -07:00
|
|
|
start_hash: Hash,
|
2018-07-10 11:22:45 -07:00
|
|
|
entries: I,
|
2018-07-10 11:28:12 -07:00
|
|
|
tail: &mut Vec<Entry>,
|
2018-07-10 15:58:08 -07:00
|
|
|
tail_idx: &mut usize,
|
2018-07-10 11:22:45 -07:00
|
|
|
) -> Result<u64>
|
2018-06-29 11:58:29 -07:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Entry>,
|
|
|
|
{
|
|
|
|
// Ledger verification needs to be parallelized, but we can't pull the whole
|
|
|
|
// thing into memory. We therefore chunk it.
|
2018-08-06 10:09:43 -07:00
|
|
|
let mut entry_count = *tail_idx as u64;
|
2018-08-07 11:09:33 -07:00
|
|
|
let mut id = start_hash;
|
2018-06-29 11:58:29 -07:00
|
|
|
for block in &entries.into_iter().chunks(VERIFY_BLOCK_SIZE) {
|
|
|
|
let block: Vec<_> = block.collect();
|
2018-08-06 23:16:20 -07:00
|
|
|
if !block.verify(&id) {
|
2018-07-31 16:10:02 -07:00
|
|
|
warn!("Ledger proof of history failed at entry: {}", entry_count);
|
2018-06-29 11:58:29 -07:00
|
|
|
return Err(BankError::LedgerVerificationFailed);
|
|
|
|
}
|
2018-08-06 23:16:20 -07:00
|
|
|
id = block.last().unwrap().id;
|
2018-07-10 15:58:08 -07:00
|
|
|
entry_count += self.process_entries_tail(block, tail, tail_idx)?;
|
2018-06-29 11:58:29 -07:00
|
|
|
}
|
|
|
|
Ok(entry_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a full ledger.
|
2018-07-02 10:07:32 -07:00
|
|
|
pub fn process_ledger<I>(&self, entries: I) -> Result<(u64, Vec<Entry>)>
|
2018-06-29 11:58:29 -07:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Entry>,
|
|
|
|
{
|
|
|
|
let mut entries = entries.into_iter();
|
|
|
|
|
|
|
|
// The first item in the ledger is required to be an entry with zero num_hashes,
|
|
|
|
// which implies its id can be used as the ledger's seed.
|
|
|
|
let entry0 = entries.next().expect("invalid ledger: empty");
|
|
|
|
|
|
|
|
// The second item in the ledger is a special transaction where the to and from
|
|
|
|
// fields are the same. That entry should be treated as a deposit, not a
|
|
|
|
// transfer to oneself.
|
|
|
|
let entry1 = entries
|
|
|
|
.next()
|
|
|
|
.expect("invalid ledger: need at least 2 entries");
|
2018-07-02 10:07:32 -07:00
|
|
|
{
|
|
|
|
let tx = &entry1.transactions[0];
|
2018-09-28 16:16:35 -07:00
|
|
|
assert!(SystemProgram::check_id(tx.program_id(0)), "Invalid ledger");
|
|
|
|
let instruction: SystemProgram = deserialize(tx.userdata(0)).unwrap();
|
2018-09-19 17:25:57 -07:00
|
|
|
let deposit = if let SystemProgram::Move { tokens } = instruction {
|
2018-09-17 15:38:10 -07:00
|
|
|
Some(tokens)
|
2018-07-02 10:07:32 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}.expect("invalid ledger, needs to start with a contract");
|
2018-09-07 20:18:36 -07:00
|
|
|
{
|
|
|
|
let mut accounts = self.accounts.write().unwrap();
|
2018-09-28 16:16:35 -07:00
|
|
|
let account = accounts
|
|
|
|
.entry(tx.account_keys[0])
|
|
|
|
.or_insert_with(Account::default);
|
2018-09-17 15:38:10 -07:00
|
|
|
account.tokens += deposit;
|
|
|
|
trace!("applied genesis payment {:?} => {:?}", deposit, account);
|
2018-09-07 20:18:36 -07:00
|
|
|
}
|
2018-07-02 10:07:32 -07:00
|
|
|
}
|
2018-06-29 11:58:29 -07:00
|
|
|
self.register_entry_id(&entry0.id);
|
|
|
|
self.register_entry_id(&entry1.id);
|
2018-08-07 11:09:33 -07:00
|
|
|
let entry1_id = entry1.id;
|
2018-06-29 11:58:29 -07:00
|
|
|
|
2018-07-10 11:28:12 -07:00
|
|
|
let mut tail = Vec::with_capacity(WINDOW_SIZE as usize);
|
2018-07-10 12:23:55 -07:00
|
|
|
tail.push(entry0);
|
|
|
|
tail.push(entry1);
|
2018-07-10 11:22:45 -07:00
|
|
|
let mut tail_idx = 2;
|
2018-08-07 11:09:33 -07:00
|
|
|
let entry_count = self.process_blocks(entry1_id, entries, &mut tail, &mut tail_idx)?;
|
2018-07-02 10:07:32 -07:00
|
|
|
|
2018-07-11 10:30:07 -07:00
|
|
|
// check f we need to rotate tail
|
2018-07-27 11:27:52 -07:00
|
|
|
if tail.len() == WINDOW_SIZE as usize {
|
|
|
|
tail.rotate_left(tail_idx)
|
|
|
|
}
|
2018-07-10 12:23:55 -07:00
|
|
|
|
2018-07-02 10:07:32 -07:00
|
|
|
Ok((entry_count, tail))
|
2018-06-29 11:58:29 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create, sign, and process a Transaction from `keypair` to `to` of
|
|
|
|
/// `n` tokens where `last_id` is the last Entry ID observed by the client.
|
2018-02-23 13:08:19 -08:00
|
|
|
pub fn transfer(
|
2018-04-04 11:33:00 -07:00
|
|
|
&self,
|
2018-03-05 16:29:32 -08:00
|
|
|
n: i64,
|
2018-08-09 07:56:04 -07:00
|
|
|
keypair: &Keypair,
|
2018-08-09 08:13:57 -07:00
|
|
|
to: Pubkey,
|
2018-03-20 22:15:44 -07:00
|
|
|
last_id: Hash,
|
2018-03-02 09:16:39 -08:00
|
|
|
) -> Result<Signature> {
|
2018-09-26 08:51:51 -07:00
|
|
|
let tx = Transaction::system_new(keypair, to, n, last_id);
|
2018-08-09 08:26:21 -07:00
|
|
|
let signature = tx.signature;
|
|
|
|
self.process_transaction(&tx).map(|_| signature)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
pub fn read_balance(account: &Account) -> i64 {
|
2018-09-19 17:25:57 -07:00
|
|
|
if SystemProgram::check_id(&account.program_id) {
|
|
|
|
SystemProgram::get_balance(account)
|
2018-09-20 19:17:37 -07:00
|
|
|
} else if BudgetState::check_id(&account.program_id) {
|
|
|
|
BudgetState::get_balance(account)
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
|
|
|
account.tokens
|
|
|
|
}
|
2018-03-07 20:55:49 -08:00
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
/// Each contract would need to be able to introspect its own state
|
2018-09-24 13:53:02 -07:00
|
|
|
/// this is hard coded to the budget contract language
|
2018-08-09 08:13:57 -07:00
|
|
|
pub fn get_balance(&self, pubkey: &Pubkey) -> i64 {
|
2018-09-07 20:18:36 -07:00
|
|
|
self.get_account(pubkey)
|
2018-09-17 13:36:31 -07:00
|
|
|
.map(|x| Self::read_balance(&x))
|
2018-09-07 20:18:36 -07:00
|
|
|
.unwrap_or(0)
|
2018-08-10 21:13:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_account(&self, pubkey: &Pubkey) -> Option<Account> {
|
|
|
|
let accounts = self
|
|
|
|
.accounts
|
2018-05-11 11:38:52 -07:00
|
|
|
.read()
|
2018-08-10 21:13:34 -07:00
|
|
|
.expect("'accounts' read lock in get_balance");
|
|
|
|
accounts.get(pubkey).cloned()
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-05-14 05:49:48 -07:00
|
|
|
|
|
|
|
pub fn transaction_count(&self) -> usize {
|
|
|
|
self.transaction_count.load(Ordering::Relaxed)
|
|
|
|
}
|
2018-06-28 11:58:33 -07:00
|
|
|
|
2018-09-26 14:57:34 -07:00
|
|
|
pub fn get_signature_status(&self, signature: &Signature) -> Result<()> {
|
|
|
|
let last_ids_sigs = self.last_ids_sigs.read().unwrap();
|
|
|
|
for (_hash, (signatures, _)) in last_ids_sigs.iter() {
|
|
|
|
if let Some(res) = signatures.get(signature) {
|
|
|
|
return res.clone();
|
2018-06-28 11:58:33 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 14:57:34 -07:00
|
|
|
Err(BankError::SignatureNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_signature(&self, signature: &Signature) -> bool {
|
|
|
|
self.get_signature_status(signature) != Err(BankError::SignatureNotFound)
|
2018-06-28 11:58:33 -07:00
|
|
|
}
|
2018-08-13 08:55:13 -07:00
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
pub fn get_signature(&self, last_id: &Hash, signature: &Signature) -> Option<Result<()>> {
|
|
|
|
self.last_ids_sigs
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(last_id)
|
|
|
|
.and_then(|sigs| sigs.0.get(signature).cloned())
|
|
|
|
}
|
|
|
|
|
2018-09-07 12:55:30 -07:00
|
|
|
/// Hash the `accounts` HashMap. This represents a validator's interpretation
|
|
|
|
/// of the ledger up to the `last_id`, to be sent back to the leader when voting.
|
|
|
|
pub fn hash_internal_state(&self) -> Hash {
|
|
|
|
let mut ordered_accounts = BTreeMap::new();
|
|
|
|
for (pubkey, account) in self.accounts.read().unwrap().iter() {
|
|
|
|
ordered_accounts.insert(*pubkey, account.clone());
|
|
|
|
}
|
|
|
|
hash(&serialize(&ordered_accounts).unwrap())
|
|
|
|
}
|
|
|
|
|
2018-08-13 08:55:13 -07:00
|
|
|
pub fn finality(&self) -> usize {
|
|
|
|
self.finality_time.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_finality(&self, finality: usize) {
|
|
|
|
self.finality_time.store(finality, Ordering::Relaxed);
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-04-05 09:26:43 -07:00
|
|
|
use bincode::serialize;
|
2018-06-14 16:32:39 -07:00
|
|
|
use entry::next_entry;
|
2018-07-02 10:07:32 -07:00
|
|
|
use entry::Entry;
|
2018-07-01 23:09:41 -07:00
|
|
|
use entry_writer::{self, EntryWriter};
|
2018-04-05 21:39:07 -07:00
|
|
|
use hash::hash;
|
2018-08-07 11:09:33 -07:00
|
|
|
use ledger;
|
2018-09-17 13:36:31 -07:00
|
|
|
use logger;
|
2018-10-04 13:15:54 -07:00
|
|
|
use signature::Keypair;
|
2018-09-07 12:55:30 -07:00
|
|
|
use signature::{GenKeys, KeypairUtil};
|
2018-08-13 08:55:13 -07:00
|
|
|
use std;
|
2018-07-03 15:19:57 -07:00
|
|
|
use std::io::{BufReader, Cursor, Seek, SeekFrom};
|
2018-10-04 13:15:54 -07:00
|
|
|
use system_transaction::SystemTransaction;
|
2018-09-28 16:16:35 -07:00
|
|
|
use transaction::Instruction;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-09-07 20:18:36 -07:00
|
|
|
#[test]
|
|
|
|
fn test_bank_new() {
|
|
|
|
let mint = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 10_000);
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:19 -08:00
|
|
|
#[test]
|
2018-06-06 08:49:22 -07:00
|
|
|
fn test_two_payments_to_one_party() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(10_000);
|
2018-08-09 07:56:04 -07:00
|
|
|
let pubkey = Keypair::new().pubkey();
|
2018-05-14 14:39:34 -07:00
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
assert_eq!(bank.last_id(), mint.last_id());
|
2018-05-03 12:24:35 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1_000);
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1_500);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 2);
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-02-27 10:28:10 -08:00
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
#[test]
|
2018-10-04 13:15:54 -07:00
|
|
|
fn test_one_source_two_tx_one_batch() {
|
2018-09-28 16:16:35 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let key1 = Keypair::new().pubkey();
|
|
|
|
let key2 = Keypair::new().pubkey();
|
|
|
|
let bank = Bank::new(&mint);
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(bank.last_id(), mint.last_id());
|
|
|
|
|
|
|
|
let t1 = Transaction::system_move(&mint.keypair(), key1, 1, mint.last_id(), 0);
|
|
|
|
let t2 = Transaction::system_move(&mint.keypair(), key2, 1, mint.last_id(), 0);
|
|
|
|
let res = bank.process_transactions(&vec![t1.clone(), t2.clone()]);
|
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
assert_eq!(res[0], Ok(()));
|
|
|
|
assert_eq!(res[1], Err(BankError::AccountInUse));
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 0);
|
|
|
|
assert_eq!(bank.get_balance(&key1), 1);
|
|
|
|
assert_eq!(bank.get_balance(&key2), 0);
|
|
|
|
assert_eq!(bank.get_signature(&t1.last_id, &t1.signature), Some(Ok(())));
|
|
|
|
// TODO: Transactions that fail to pay a fee could be dropped silently
|
|
|
|
assert_eq!(
|
|
|
|
bank.get_signature(&t2.last_id, &t2.signature),
|
|
|
|
Some(Err(BankError::AccountInUse))
|
|
|
|
);
|
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
|
2018-10-04 13:15:54 -07:00
|
|
|
#[test]
|
|
|
|
fn test_one_tx_two_out_atomic_fail() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let key1 = Keypair::new().pubkey();
|
|
|
|
let key2 = Keypair::new().pubkey();
|
|
|
|
let bank = Bank::new(&mint);
|
2018-09-28 16:16:35 -07:00
|
|
|
let spend = SystemProgram::Move { tokens: 1 };
|
|
|
|
let instructions = vec![
|
|
|
|
Instruction {
|
2018-10-04 13:36:15 -07:00
|
|
|
program_ids_index: 0,
|
2018-09-28 16:16:35 -07:00
|
|
|
userdata: serialize(&spend).unwrap(),
|
|
|
|
accounts: vec![0, 1],
|
|
|
|
},
|
|
|
|
Instruction {
|
2018-10-04 13:36:15 -07:00
|
|
|
program_ids_index: 0,
|
2018-09-28 16:16:35 -07:00
|
|
|
userdata: serialize(&spend).unwrap(),
|
|
|
|
accounts: vec![0, 2],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let t1 = Transaction::new_with_instructions(
|
|
|
|
&mint.keypair(),
|
|
|
|
&[key1, key2],
|
|
|
|
mint.last_id(),
|
|
|
|
0,
|
|
|
|
vec![SystemProgram::id()],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let res = bank.process_transactions(&vec![t1.clone()]);
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
assert_eq!(res[0], Err(BankError::ResultWithNegativeTokens(1)));
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 1);
|
|
|
|
assert_eq!(bank.get_balance(&key1), 0);
|
|
|
|
assert_eq!(bank.get_balance(&key2), 0);
|
|
|
|
assert_eq!(
|
|
|
|
bank.get_signature(&t1.last_id, &t1.signature),
|
|
|
|
Some(Err(BankError::ResultWithNegativeTokens(1)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_one_tx_two_out_atomic_pass() {
|
|
|
|
let mint = Mint::new(2);
|
|
|
|
let key1 = Keypair::new().pubkey();
|
|
|
|
let key2 = Keypair::new().pubkey();
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
|
|
|
|
let spend = SystemProgram::Move { tokens: 1 };
|
|
|
|
let instructions = vec![
|
|
|
|
Instruction {
|
2018-10-04 13:36:15 -07:00
|
|
|
program_ids_index: 0,
|
2018-09-28 16:16:35 -07:00
|
|
|
userdata: serialize(&spend).unwrap(),
|
|
|
|
accounts: vec![0, 1],
|
|
|
|
},
|
|
|
|
Instruction {
|
2018-10-04 13:36:15 -07:00
|
|
|
program_ids_index: 0,
|
2018-09-28 16:16:35 -07:00
|
|
|
userdata: serialize(&spend).unwrap(),
|
|
|
|
accounts: vec![0, 2],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let t1 = Transaction::new_with_instructions(
|
|
|
|
&mint.keypair(),
|
|
|
|
&[key1, key2],
|
|
|
|
mint.last_id(),
|
|
|
|
0,
|
|
|
|
vec![SystemProgram::id()],
|
|
|
|
instructions,
|
|
|
|
);
|
|
|
|
let res = bank.process_transactions(&vec![t1.clone()]);
|
|
|
|
assert_eq!(res.len(), 1);
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(res[0], Ok(()));
|
2018-09-28 16:16:35 -07:00
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 0);
|
|
|
|
assert_eq!(bank.get_balance(&key1), 1);
|
|
|
|
assert_eq!(bank.get_balance(&key2), 1);
|
|
|
|
assert_eq!(bank.get_signature(&t1.last_id, &t1.signature), Some(Ok(())));
|
|
|
|
}
|
|
|
|
|
2018-05-29 19:35:14 -07:00
|
|
|
#[test]
|
2018-06-06 08:49:22 -07:00
|
|
|
fn test_negative_tokens() {
|
2018-09-17 13:36:31 -07:00
|
|
|
logger::setup();
|
2018-05-29 19:35:14 -07:00
|
|
|
let mint = Mint::new(1);
|
2018-08-09 07:56:04 -07:00
|
|
|
let pubkey = Keypair::new().pubkey();
|
2018-05-29 19:35:14 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-09-17 13:36:31 -07:00
|
|
|
let res = bank.transfer(-1, &mint.keypair(), pubkey, mint.last_id());
|
|
|
|
println!("{:?}", bank.get_account(&pubkey));
|
2018-09-28 16:16:35 -07:00
|
|
|
assert_matches!(res, Err(BankError::ResultWithNegativeTokens(0)));
|
2018-05-29 19:35:14 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 0);
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:18:45 -07:00
|
|
|
// TODO: This test demonstrates that fees are not paid when a program fails.
|
2018-09-07 20:18:36 -07:00
|
|
|
// See github issue 1157 (https://github.com/solana-labs/solana/issues/1157)
|
|
|
|
#[test]
|
|
|
|
fn test_detect_failed_duplicate_transactions_issue_1157() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-09-17 13:36:31 -07:00
|
|
|
let dest = Keypair::new();
|
2018-09-07 20:18:36 -07:00
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
// source with 0 contract context
|
2018-09-26 15:18:45 -07:00
|
|
|
let tx = Transaction::system_create(
|
|
|
|
&mint.keypair(),
|
|
|
|
dest.pubkey(),
|
|
|
|
mint.last_id(),
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
Pubkey::default(),
|
|
|
|
1,
|
|
|
|
);
|
2018-09-07 20:18:36 -07:00
|
|
|
let signature = tx.signature;
|
|
|
|
assert!(!bank.has_signature(&signature));
|
2018-09-17 13:36:31 -07:00
|
|
|
let res = bank.process_transaction(&tx);
|
2018-09-26 15:18:45 -07:00
|
|
|
|
|
|
|
// Result failed, but signature is registered
|
2018-10-04 13:15:54 -07:00
|
|
|
assert!(res.is_err());
|
2018-09-07 20:18:36 -07:00
|
|
|
assert!(bank.has_signature(&signature));
|
2018-09-26 15:18:45 -07:00
|
|
|
assert_matches!(
|
|
|
|
bank.get_signature_status(&signature),
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BankError::ResultWithNegativeTokens(0))
|
2018-09-26 15:18:45 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// The tokens didn't move, but the from address paid the transaction fee.
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(bank.get_balance(&dest.pubkey()), 0);
|
2018-09-26 15:18:45 -07:00
|
|
|
|
|
|
|
// BUG: This should be the original balance minus the transaction fee.
|
|
|
|
//assert_eq!(bank.get_balance(&mint.pubkey()), 0);
|
2018-09-07 20:18:36 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:00:08 -07:00
|
|
|
#[test]
|
|
|
|
fn test_account_not_found() {
|
|
|
|
let mint = Mint::new(1);
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 07:56:04 -07:00
|
|
|
let keypair = Keypair::new();
|
2018-04-05 20:00:08 -07:00
|
|
|
assert_eq!(
|
2018-05-23 11:26:32 -07:00
|
|
|
bank.transfer(1, &keypair, mint.pubkey(), mint.last_id()),
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::AccountNotFound)
|
2018-04-05 20:00:08 -07:00
|
|
|
);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 0);
|
2018-04-05 20:00:08 -07:00
|
|
|
}
|
|
|
|
|
2018-02-27 10:28:10 -08:00
|
|
|
#[test]
|
2018-06-06 08:49:22 -07:00
|
|
|
fn test_insufficient_funds() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(11_000);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 07:56:04 -07:00
|
|
|
let pubkey = Keypair::new().pubkey();
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-09-07 20:18:36 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1_000);
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_matches!(
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(10_001, &mint.keypair(), pubkey, mint.last_id()),
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BankError::ResultWithNegativeTokens(0))
|
2018-03-02 09:16:39 -08:00
|
|
|
);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-03-01 11:23:27 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint_pubkey = mint.keypair().pubkey();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&mint_pubkey), 10_000);
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1_000);
|
2018-02-27 10:28:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_to_newb() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 07:56:04 -07:00
|
|
|
let pubkey = Keypair::new().pubkey();
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 500);
|
2018-02-27 10:28:10 -08:00
|
|
|
}
|
2018-03-08 07:58:01 -08:00
|
|
|
|
2018-04-02 08:36:22 -07:00
|
|
|
#[test]
|
2018-05-25 14:51:41 -07:00
|
|
|
fn test_duplicate_transaction_signature() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 08:26:21 -07:00
|
|
|
let signature = Signature::default();
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(
|
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id()),
|
|
|
|
Ok(())
|
2018-05-29 19:48:33 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id()),
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::DuplicateSignature)
|
2018-05-29 19:48:33 -07:00
|
|
|
);
|
2018-04-02 08:36:22 -07:00
|
|
|
}
|
2018-04-05 09:26:43 -07:00
|
|
|
|
2018-04-26 04:22:11 -07:00
|
|
|
#[test]
|
2018-09-07 20:18:36 -07:00
|
|
|
fn test_clear_signatures() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 08:26:21 -07:00
|
|
|
let signature = Signature::default();
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id())
|
2018-05-29 19:48:33 -07:00
|
|
|
.unwrap();
|
2018-09-07 20:18:36 -07:00
|
|
|
bank.clear_signatures();
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(
|
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id()),
|
|
|
|
Ok(())
|
2018-05-29 19:48:33 -07:00
|
|
|
);
|
2018-04-26 04:22:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:18:45 -07:00
|
|
|
#[test]
|
|
|
|
fn test_get_signature_status() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let signature = Signature::default();
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id())
|
2018-09-26 15:18:45 -07:00
|
|
|
.expect("reserve signature");
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(bank.get_signature_status(&signature), Ok(()));
|
2018-09-26 15:18:45 -07:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:58:33 -07:00
|
|
|
#[test]
|
2018-06-29 09:30:09 -07:00
|
|
|
fn test_has_signature() {
|
2018-06-28 11:58:33 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 08:26:21 -07:00
|
|
|
let signature = Signature::default();
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id())
|
2018-06-28 11:58:33 -07:00
|
|
|
.expect("reserve signature");
|
2018-08-09 08:26:21 -07:00
|
|
|
assert!(bank.has_signature(&signature));
|
2018-06-28 11:58:33 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:26:43 -07:00
|
|
|
#[test]
|
2018-06-06 08:49:22 -07:00
|
|
|
fn test_reject_old_last_id() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 08:26:21 -07:00
|
|
|
let signature = Signature::default();
|
2018-04-05 09:26:43 -07:00
|
|
|
for i in 0..MAX_ENTRY_IDS {
|
|
|
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.register_entry_id(&last_id);
|
2018-04-05 09:26:43 -07:00
|
|
|
}
|
|
|
|
// Assert we're no longer able to use the oldest entry ID.
|
2018-05-29 19:48:33 -07:00
|
|
|
assert_eq!(
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.reserve_signature_with_last_id_test(&signature, &mint.last_id()),
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::LastIdNotFound)
|
2018-05-29 19:48:33 -07:00
|
|
|
);
|
2018-04-05 09:26:43 -07:00
|
|
|
}
|
2018-07-31 16:10:02 -07:00
|
|
|
|
2018-07-31 22:07:53 -07:00
|
|
|
#[test]
|
|
|
|
fn test_count_valid_ids() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let ids: Vec<_> = (0..MAX_ENTRY_IDS)
|
|
|
|
.map(|i| {
|
|
|
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
|
|
|
bank.register_entry_id(&last_id);
|
|
|
|
last_id
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect();
|
2018-08-01 11:01:22 -07:00
|
|
|
assert_eq!(bank.count_valid_ids(&[]).len(), 0);
|
|
|
|
assert_eq!(bank.count_valid_ids(&[mint.last_id()]).len(), 0);
|
|
|
|
for (i, id) in bank.count_valid_ids(&ids).iter().enumerate() {
|
|
|
|
assert_eq!(id.0, i);
|
|
|
|
}
|
2018-07-31 22:07:53 -07:00
|
|
|
}
|
2018-07-31 16:10:02 -07:00
|
|
|
|
2018-04-12 19:53:34 -07:00
|
|
|
#[test]
|
|
|
|
fn test_debits_before_credits() {
|
|
|
|
let mint = Mint::new(2);
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 07:56:04 -07:00
|
|
|
let keypair = Keypair::new();
|
2018-09-26 08:51:51 -07:00
|
|
|
let tx0 = Transaction::system_new(&mint.keypair(), keypair.pubkey(), 2, mint.last_id());
|
|
|
|
let tx1 = Transaction::system_new(&keypair, mint.pubkey(), 1, mint.last_id());
|
2018-05-29 09:12:27 -07:00
|
|
|
let txs = vec![tx0, tx1];
|
2018-09-21 14:48:10 -07:00
|
|
|
let results = bank.process_transactions(&txs);
|
2018-05-14 05:49:48 -07:00
|
|
|
assert!(results[1].is_err());
|
|
|
|
|
|
|
|
// Assert bad transactions aren't counted.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-04-12 19:53:34 -07:00
|
|
|
}
|
2018-06-14 16:32:39 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_empty_entry_is_registered() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-08-09 07:56:04 -07:00
|
|
|
let keypair = Keypair::new();
|
2018-06-14 16:32:39 -07:00
|
|
|
let entry = next_entry(&mint.last_id(), 1, vec![]);
|
2018-09-26 08:51:51 -07:00
|
|
|
let tx = Transaction::system_new(&mint.keypair(), keypair.pubkey(), 1, entry.id);
|
2018-06-14 16:32:39 -07:00
|
|
|
|
|
|
|
// First, ensure the TX is rejected because of the unregistered last ID
|
|
|
|
assert_eq!(
|
|
|
|
bank.process_transaction(&tx),
|
2018-09-27 05:01:20 -07:00
|
|
|
Err(BankError::LastIdNotFound)
|
2018-06-14 16:32:39 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// Now ensure the TX is accepted despite pointing to the ID of an empty entry.
|
2018-09-24 12:26:47 -07:00
|
|
|
bank.process_entries(&[entry]).unwrap();
|
2018-10-04 13:15:54 -07:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
2018-06-14 16:32:39 -07:00
|
|
|
}
|
2018-07-01 21:06:40 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_genesis() {
|
|
|
|
let mint = Mint::new(1);
|
|
|
|
let genesis = mint.create_entries();
|
|
|
|
let bank = Bank::default();
|
|
|
|
bank.process_ledger(genesis).unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 1);
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 12:55:30 -07:00
|
|
|
fn create_sample_block_with_next_entries_using_keypairs(
|
|
|
|
mint: &Mint,
|
|
|
|
keypairs: &[Keypair],
|
|
|
|
) -> impl Iterator<Item = Entry> {
|
2018-10-04 13:15:54 -07:00
|
|
|
let mut hash = mint.last_id();
|
|
|
|
let mut entries: Vec<Entry> = vec![];
|
|
|
|
for k in keypairs {
|
|
|
|
let txs = vec![Transaction::system_new(
|
|
|
|
&mint.keypair(),
|
|
|
|
k.pubkey(),
|
|
|
|
1,
|
|
|
|
hash,
|
|
|
|
)];
|
|
|
|
let mut e = ledger::next_entries(&hash, 0, txs);
|
|
|
|
entries.append(&mut e);
|
|
|
|
hash = entries.last().unwrap().id;
|
|
|
|
}
|
2018-09-07 12:55:30 -07:00
|
|
|
entries.into_iter()
|
|
|
|
}
|
|
|
|
|
2018-07-02 10:07:32 -07:00
|
|
|
fn create_sample_block(mint: &Mint, length: usize) -> impl Iterator<Item = Entry> {
|
|
|
|
let mut entries = Vec::with_capacity(length);
|
|
|
|
let mut hash = mint.last_id();
|
2018-08-09 07:31:00 -07:00
|
|
|
let mut num_hashes = 0;
|
2018-07-02 10:07:32 -07:00
|
|
|
for _ in 0..length {
|
2018-08-09 07:56:04 -07:00
|
|
|
let keypair = Keypair::new();
|
2018-09-17 13:36:31 -07:00
|
|
|
let tx = Transaction::system_new(&mint.keypair(), keypair.pubkey(), 1, hash);
|
2018-09-21 21:01:13 -07:00
|
|
|
let entry = Entry::new_mut(&mut hash, &mut num_hashes, vec![tx]);
|
2018-07-02 10:07:32 -07:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
entries.into_iter()
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
2018-08-07 11:09:33 -07:00
|
|
|
|
2018-08-09 08:13:57 -07:00
|
|
|
fn create_sample_ledger(length: usize) -> (impl Iterator<Item = Entry>, Pubkey) {
|
2018-07-02 10:07:32 -07:00
|
|
|
let mint = Mint::new(1 + length as i64);
|
2018-07-01 21:06:40 -07:00
|
|
|
let genesis = mint.create_entries();
|
2018-07-02 10:07:32 -07:00
|
|
|
let block = create_sample_block(&mint, length);
|
2018-07-01 21:06:40 -07:00
|
|
|
(genesis.into_iter().chain(block), mint.pubkey())
|
|
|
|
}
|
|
|
|
|
2018-09-07 12:55:30 -07:00
|
|
|
fn create_sample_ledger_with_mint_and_keypairs(
|
|
|
|
mint: &Mint,
|
|
|
|
keypairs: &[Keypair],
|
|
|
|
) -> impl Iterator<Item = Entry> {
|
|
|
|
let genesis = mint.create_entries();
|
|
|
|
let block = create_sample_block_with_next_entries_using_keypairs(mint, keypairs);
|
|
|
|
genesis.into_iter().chain(block)
|
|
|
|
}
|
|
|
|
|
2018-07-01 21:06:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_ledger() {
|
2018-07-02 10:07:32 -07:00
|
|
|
let (ledger, pubkey) = create_sample_ledger(1);
|
|
|
|
let (ledger, dup) = ledger.tee();
|
2018-07-01 21:06:40 -07:00
|
|
|
let bank = Bank::default();
|
2018-07-02 10:07:32 -07:00
|
|
|
let (ledger_height, tail) = bank.process_ledger(ledger).unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
2018-07-02 10:07:32 -07:00
|
|
|
assert_eq!(ledger_height, 3);
|
|
|
|
assert_eq!(tail.len(), 3);
|
|
|
|
assert_eq!(tail, dup.collect_vec());
|
|
|
|
let last_entry = &tail[tail.len() - 1];
|
|
|
|
assert_eq!(bank.last_id(), last_entry.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_around_window_size() {
|
2018-07-11 09:24:21 -07:00
|
|
|
// TODO: put me back in when Criterion is up
|
2018-07-11 09:12:26 -07:00
|
|
|
// for _ in 0..10 {
|
2018-07-11 09:24:21 -07:00
|
|
|
// let (ledger, _) = create_sample_ledger(WINDOW_SIZE as usize);
|
2018-07-11 09:12:26 -07:00
|
|
|
// let bank = Bank::default();
|
|
|
|
// let (_, _) = bank.process_ledger(ledger).unwrap();
|
|
|
|
// }
|
2018-07-10 11:22:45 -07:00
|
|
|
|
2018-07-10 12:23:55 -07:00
|
|
|
let window_size = WINDOW_SIZE as usize;
|
2018-07-10 15:58:08 -07:00
|
|
|
for entry_count in window_size - 3..window_size + 2 {
|
2018-07-10 12:23:55 -07:00
|
|
|
let (ledger, pubkey) = create_sample_ledger(entry_count);
|
|
|
|
let bank = Bank::default();
|
|
|
|
let (ledger_height, tail) = bank.process_ledger(ledger).unwrap();
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
|
|
|
assert_eq!(ledger_height, entry_count as u64 + 2);
|
|
|
|
assert!(tail.len() <= window_size);
|
|
|
|
let last_entry = &tail[tail.len() - 1];
|
|
|
|
assert_eq!(bank.last_id(), last_entry.id);
|
|
|
|
}
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the given entries to a file and then return a file iterator to them.
|
|
|
|
fn to_file_iter(entries: impl Iterator<Item = Entry>) -> impl Iterator<Item = Entry> {
|
2018-07-01 23:56:21 -07:00
|
|
|
let mut file = Cursor::new(vec![]);
|
2018-07-02 00:04:19 -07:00
|
|
|
EntryWriter::write_entries(&mut file, entries).unwrap();
|
2018-07-01 21:06:40 -07:00
|
|
|
file.seek(SeekFrom::Start(0)).unwrap();
|
2018-07-01 23:09:41 -07:00
|
|
|
|
2018-07-01 21:06:40 -07:00
|
|
|
let reader = BufReader::new(file);
|
2018-07-03 15:19:57 -07:00
|
|
|
entry_writer::read_entries(reader).map(|x| x.unwrap())
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_from_file() {
|
2018-07-02 10:07:32 -07:00
|
|
|
let (ledger, pubkey) = create_sample_ledger(1);
|
2018-07-01 21:06:40 -07:00
|
|
|
let ledger = to_file_iter(ledger);
|
|
|
|
|
|
|
|
let bank = Bank::default();
|
|
|
|
bank.process_ledger(ledger).unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_from_files() {
|
|
|
|
let mint = Mint::new(2);
|
|
|
|
let genesis = to_file_iter(mint.create_entries().into_iter());
|
2018-07-02 10:07:32 -07:00
|
|
|
let block = to_file_iter(create_sample_block(&mint, 1));
|
2018-07-01 21:06:40 -07:00
|
|
|
|
|
|
|
let bank = Bank::default();
|
|
|
|
bank.process_ledger(genesis.chain(block)).unwrap();
|
2018-07-02 16:31:10 -07:00
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), 1);
|
2018-07-01 21:06:40 -07:00
|
|
|
}
|
2018-07-11 09:12:26 -07:00
|
|
|
|
2018-08-10 15:18:44 -07:00
|
|
|
#[test]
|
|
|
|
fn test_new_default() {
|
|
|
|
let def_bank = Bank::default();
|
|
|
|
assert!(def_bank.is_leader);
|
|
|
|
let leader_bank = Bank::new_default(true);
|
|
|
|
assert!(leader_bank.is_leader);
|
|
|
|
let validator_bank = Bank::new_default(false);
|
|
|
|
assert!(!validator_bank.is_leader);
|
|
|
|
}
|
2018-08-13 08:55:13 -07:00
|
|
|
#[test]
|
2018-09-07 12:55:30 -07:00
|
|
|
fn test_hash_internal_state() {
|
|
|
|
let mint = Mint::new(2_000);
|
|
|
|
let seed = [0u8; 32];
|
|
|
|
let mut rnd = GenKeys::new(seed);
|
|
|
|
let keypairs = rnd.gen_n_keypairs(5);
|
|
|
|
let ledger0 = create_sample_ledger_with_mint_and_keypairs(&mint, &keypairs);
|
|
|
|
let ledger1 = create_sample_ledger_with_mint_and_keypairs(&mint, &keypairs);
|
|
|
|
|
|
|
|
let bank0 = Bank::default();
|
|
|
|
bank0.process_ledger(ledger0).unwrap();
|
|
|
|
let bank1 = Bank::default();
|
|
|
|
bank1.process_ledger(ledger1).unwrap();
|
|
|
|
|
|
|
|
let initial_state = bank0.hash_internal_state();
|
|
|
|
|
|
|
|
assert_eq!(bank1.hash_internal_state(), initial_state);
|
|
|
|
|
|
|
|
let pubkey = keypairs[0].pubkey();
|
|
|
|
bank0
|
|
|
|
.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
|
|
|
.unwrap();
|
|
|
|
assert_ne!(bank0.hash_internal_state(), initial_state);
|
|
|
|
bank1
|
|
|
|
.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(bank0.hash_internal_state(), bank1.hash_internal_state());
|
|
|
|
}
|
|
|
|
#[test]
|
2018-08-13 08:55:13 -07:00
|
|
|
fn test_finality() {
|
|
|
|
let def_bank = Bank::default();
|
|
|
|
assert_eq!(def_bank.finality(), std::usize::MAX);
|
|
|
|
def_bank.set_finality(90);
|
|
|
|
assert_eq!(def_bank.finality(), 90);
|
|
|
|
}
|
2018-10-04 13:15:54 -07:00
|
|
|
#[test]
|
|
|
|
fn test_interleaving_locks() {
|
|
|
|
let mint = Mint::new(3);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let alice = Keypair::new();
|
|
|
|
let bob = Keypair::new();
|
|
|
|
|
|
|
|
let tx1 = Transaction::system_new(&mint.keypair(), alice.pubkey(), 1, mint.last_id());
|
|
|
|
let pay_alice = vec![tx1];
|
|
|
|
|
|
|
|
let locked_alice = bank.lock_accounts(&pay_alice);
|
|
|
|
let results_alice = bank.execute_and_commit_transactions(&pay_alice, locked_alice);
|
|
|
|
assert_eq!(results_alice[0], Ok(()));
|
|
|
|
|
|
|
|
// try executing an interleaved transfer twice
|
|
|
|
assert_eq!(
|
|
|
|
bank.transfer(1, &mint.keypair(), bob.pubkey(), mint.last_id()),
|
|
|
|
Err(BankError::AccountInUse)
|
|
|
|
);
|
|
|
|
// the second time shoudl fail as well
|
|
|
|
// this verifies that `unlock_accounts` doesn't unlock `AccountInUse` accounts
|
|
|
|
assert_eq!(
|
|
|
|
bank.transfer(1, &mint.keypair(), bob.pubkey(), mint.last_id()),
|
|
|
|
Err(BankError::AccountInUse)
|
|
|
|
);
|
|
|
|
|
|
|
|
bank.unlock_accounts(&pay_alice, &results_alice);
|
|
|
|
|
|
|
|
assert_matches!(
|
|
|
|
bank.transfer(2, &mint.keypair(), bob.pubkey(), mint.last_id()),
|
|
|
|
Ok(_)
|
|
|
|
);
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|