2019-11-04 21:14:55 -08:00
|
|
|
use crate::{
|
|
|
|
block_error::BlockError,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore::Blockstore,
|
2020-01-14 17:15:26 -08:00
|
|
|
blockstore_db::BlockstoreError,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore_meta::SlotMeta,
|
2020-01-14 17:15:26 -08:00
|
|
|
entry::{create_ticks, Entry, EntrySlice, EntryVerificationStatus, VerifyRecyclers},
|
2019-11-04 21:14:55 -08:00
|
|
|
leader_schedule_cache::LeaderScheduleCache,
|
|
|
|
};
|
2020-08-07 11:21:35 -07:00
|
|
|
use crossbeam_channel::Sender;
|
2019-11-04 21:14:55 -08:00
|
|
|
use itertools::Itertools;
|
2019-10-20 08:54:38 -07:00
|
|
|
use log::*;
|
2019-11-04 21:14:55 -08:00
|
|
|
use rand::{seq::SliceRandom, thread_rng};
|
|
|
|
use rayon::{prelude::*, ThreadPool};
|
2020-01-14 17:15:26 -08:00
|
|
|
use solana_measure::{measure::Measure, thread_mem_usage};
|
2020-02-14 11:11:55 -08:00
|
|
|
use solana_metrics::{datapoint_error, inc_new_counter_debug};
|
2019-09-12 11:39:39 -07:00
|
|
|
use solana_rayon_threadlimit::get_thread_count;
|
2019-11-20 15:43:10 -08:00
|
|
|
use solana_runtime::{
|
2020-09-24 07:36:22 -07:00
|
|
|
bank::{
|
2020-11-29 11:21:55 -08:00
|
|
|
Bank, InnerInstructionsList, TransactionBalancesSet, TransactionExecutionResult,
|
|
|
|
TransactionLogMessages, TransactionResults,
|
2020-09-24 07:36:22 -07:00
|
|
|
},
|
2020-06-17 08:27:03 -07:00
|
|
|
bank_forks::BankForks,
|
2020-08-07 11:21:35 -07:00
|
|
|
bank_utils,
|
2020-08-20 21:56:25 -07:00
|
|
|
commitment::VOTE_THRESHOLD_SIZE,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_batch::TransactionBatch,
|
2020-07-21 13:06:49 -07:00
|
|
|
transaction_utils::OrderedIterator,
|
2020-11-30 09:18:33 -08:00
|
|
|
vote_account::ArcVoteAccount,
|
2020-08-07 11:21:35 -07:00
|
|
|
vote_sender_types::ReplayVoteSender,
|
2019-11-20 15:43:10 -08:00
|
|
|
};
|
2019-11-04 21:14:55 -08:00
|
|
|
use solana_sdk::{
|
2020-04-14 00:34:41 -07:00
|
|
|
clock::{Slot, MAX_PROCESSING_AGE},
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config::GenesisConfig,
|
2019-11-04 21:14:55 -08:00
|
|
|
hash::Hash,
|
2020-03-22 11:10:04 -07:00
|
|
|
pubkey::Pubkey,
|
2020-07-21 13:06:49 -07:00
|
|
|
signature::{Keypair, Signature},
|
2019-11-04 21:14:55 -08:00
|
|
|
timing::duration_as_ms,
|
2020-01-14 17:15:26 -08:00
|
|
|
transaction::{Result, Transaction, TransactionError},
|
2019-11-04 21:14:55 -08:00
|
|
|
};
|
2020-12-10 19:25:07 -08:00
|
|
|
use solana_transaction_status::token_balances::{
|
|
|
|
collect_token_balances, TransactionTokenBalancesSet,
|
|
|
|
};
|
|
|
|
|
2019-11-04 21:14:55 -08:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
2020-09-23 18:46:42 -07:00
|
|
|
collections::{HashMap, HashSet},
|
2019-12-05 18:41:29 -08:00
|
|
|
path::PathBuf,
|
2019-11-04 21:14:55 -08:00
|
|
|
result,
|
2019-11-21 13:23:40 -08:00
|
|
|
sync::Arc,
|
2019-11-04 21:14:55 -08:00
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2019-12-02 14:42:05 -08:00
|
|
|
use thiserror::Error;
|
2019-05-29 17:16:36 -07:00
|
|
|
|
2020-01-23 15:09:32 -08:00
|
|
|
pub type BlockstoreProcessorResult =
|
2020-05-06 08:24:59 -07:00
|
|
|
result::Result<(BankForks, LeaderScheduleCache), BlockstoreProcessorError>;
|
2020-01-23 15:09:32 -08:00
|
|
|
|
2019-05-29 17:16:36 -07:00
|
|
|
thread_local!(static PAR_THREAD_POOL: RefCell<ThreadPool> = RefCell::new(rayon::ThreadPoolBuilder::new()
|
2019-09-12 11:39:39 -07:00
|
|
|
.num_threads(get_thread_count())
|
2020-01-20 20:08:19 -08:00
|
|
|
.thread_name(|ix| format!("blockstore_processor_{}", ix))
|
2019-05-29 17:16:36 -07:00
|
|
|
.build()
|
2019-09-12 11:39:39 -07:00
|
|
|
.unwrap())
|
|
|
|
);
|
2019-05-29 17:16:36 -07:00
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
fn first_err(results: &[Result<()>]) -> Result<()> {
|
|
|
|
for r in results {
|
2019-05-23 17:35:15 -07:00
|
|
|
if r.is_err() {
|
|
|
|
return r.clone();
|
|
|
|
}
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:06:49 -07:00
|
|
|
// Includes transaction signature for unit-testing
|
|
|
|
fn get_first_error(
|
|
|
|
batch: &TransactionBatch,
|
|
|
|
fee_collection_results: Vec<Result<()>>,
|
|
|
|
) -> Option<(Result<()>, Signature)> {
|
|
|
|
let mut first_err = None;
|
2020-08-07 11:21:35 -07:00
|
|
|
for (result, (_, transaction)) in fee_collection_results.iter().zip(OrderedIterator::new(
|
2020-07-21 13:06:49 -07:00
|
|
|
batch.transactions(),
|
|
|
|
batch.iteration_order(),
|
|
|
|
)) {
|
|
|
|
if let Err(ref err) = result {
|
|
|
|
if first_err.is_none() {
|
|
|
|
first_err = Some((result.clone(), transaction.signatures[0]));
|
|
|
|
}
|
|
|
|
warn!(
|
|
|
|
"Unexpected validator error: {:?}, transaction: {:?}",
|
|
|
|
err, transaction
|
|
|
|
);
|
|
|
|
datapoint_error!(
|
|
|
|
"validator_process_entry_error",
|
|
|
|
(
|
|
|
|
"error",
|
|
|
|
format!("error: {:?}, transaction: {:?}", err, transaction),
|
|
|
|
String
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
first_err
|
|
|
|
}
|
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
fn execute_batch(
|
|
|
|
batch: &TransactionBatch,
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2019-11-20 15:43:10 -08:00
|
|
|
) -> Result<()> {
|
2020-12-10 19:25:07 -08:00
|
|
|
let record_token_balances = transaction_status_sender.is_some();
|
|
|
|
|
|
|
|
let mut mint_decimals: HashMap<Pubkey, u8> = HashMap::new();
|
|
|
|
|
|
|
|
let pre_token_balances = if record_token_balances {
|
|
|
|
collect_token_balances(&bank, &batch, &mut mint_decimals)
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2020-10-08 12:06:15 -07:00
|
|
|
let (tx_results, balances, inner_instructions, transaction_logs) =
|
2020-09-24 07:36:22 -07:00
|
|
|
batch.bank().load_execute_and_commit_transactions(
|
|
|
|
batch,
|
|
|
|
MAX_PROCESSING_AGE,
|
|
|
|
transaction_status_sender.is_some(),
|
|
|
|
transaction_status_sender.is_some(),
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_status_sender.is_some(),
|
2020-09-24 07:36:22 -07:00
|
|
|
);
|
2019-09-19 10:06:08 -07:00
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
bank_utils::find_and_send_votes(batch.transactions(), &tx_results, replay_vote_sender);
|
|
|
|
|
|
|
|
let TransactionResults {
|
|
|
|
fee_collection_results,
|
2020-11-29 11:21:55 -08:00
|
|
|
execution_results,
|
2020-08-07 11:21:35 -07:00
|
|
|
..
|
|
|
|
} = tx_results;
|
2020-07-29 23:17:40 -07:00
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
if let Some(sender) = transaction_status_sender {
|
2020-12-10 19:25:07 -08:00
|
|
|
let post_token_balances = if record_token_balances {
|
|
|
|
collect_token_balances(&bank, &batch, &mut mint_decimals)
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
let token_balances =
|
|
|
|
TransactionTokenBalancesSet::new(pre_token_balances, post_token_balances);
|
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
send_transaction_status_batch(
|
|
|
|
bank.clone(),
|
|
|
|
batch.transactions(),
|
2020-07-21 13:06:49 -07:00
|
|
|
batch.iteration_order_vec(),
|
2020-11-29 11:21:55 -08:00
|
|
|
execution_results,
|
2019-12-18 09:56:29 -08:00
|
|
|
balances,
|
2020-12-10 19:25:07 -08:00
|
|
|
token_balances,
|
2020-09-24 07:36:22 -07:00
|
|
|
inner_instructions,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_logs,
|
2019-11-20 15:43:10 -08:00
|
|
|
sender,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:06:49 -07:00
|
|
|
let first_err = get_first_error(batch, fee_collection_results);
|
|
|
|
first_err.map(|(result, _)| result).unwrap_or(Ok(()))
|
2019-09-19 10:06:08 -07:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
fn execute_batches(
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
batches: &[TransactionBatch],
|
|
|
|
entry_callback: Option<&ProcessCallback>,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2019-10-08 14:58:49 -07:00
|
|
|
) -> Result<()> {
|
2019-09-19 10:06:08 -07:00
|
|
|
inc_new_counter_debug!("bank-par_execute_entries-count", batches.len());
|
2019-05-29 17:16:36 -07:00
|
|
|
let results: Vec<Result<()>> = PAR_THREAD_POOL.with(|thread_pool| {
|
|
|
|
thread_pool.borrow().install(|| {
|
2019-09-19 10:06:08 -07:00
|
|
|
batches
|
2019-05-29 17:16:36 -07:00
|
|
|
.into_par_iter()
|
2019-11-20 15:43:10 -08:00
|
|
|
.map_with(transaction_status_sender, |sender, batch| {
|
2020-08-07 11:21:35 -07:00
|
|
|
let result = execute_batch(batch, bank, sender.clone(), replay_vote_sender);
|
2019-10-08 14:58:49 -07:00
|
|
|
if let Some(entry_callback) = entry_callback {
|
|
|
|
entry_callback(bank);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
})
|
2019-05-29 17:16:36 -07:00
|
|
|
.collect()
|
2019-02-16 13:17:37 -08:00
|
|
|
})
|
2019-05-29 17:16:36 -07:00
|
|
|
});
|
2019-02-19 15:41:40 -08:00
|
|
|
|
2019-02-21 13:37:08 -08:00
|
|
|
first_err(&results)
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
|
2019-03-12 16:46:41 -07:00
|
|
|
/// Process an ordered list of entries in parallel
|
2019-02-16 13:17:37 -08:00
|
|
|
/// 1. In order lock accounts for each entry while the lock succeeds, up to a Tick entry
|
|
|
|
/// 2. Process the locked group in parallel
|
|
|
|
/// 3. Register the `Tick` if it's available
|
|
|
|
/// 4. Update the leader scheduler, goto 1
|
2019-11-20 15:43:10 -08:00
|
|
|
pub fn process_entries(
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
entries: &[Entry],
|
|
|
|
randomize: bool,
|
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2019-11-20 15:43:10 -08:00
|
|
|
) -> Result<()> {
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries_with_callback(
|
|
|
|
bank,
|
|
|
|
entries,
|
|
|
|
randomize,
|
|
|
|
None,
|
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2020-07-29 23:17:40 -07:00
|
|
|
)
|
2019-10-08 14:58:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn process_entries_with_callback(
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
entries: &[Entry],
|
|
|
|
randomize: bool,
|
|
|
|
entry_callback: Option<&ProcessCallback>,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2019-10-08 14:58:49 -07:00
|
|
|
) -> Result<()> {
|
2019-02-16 13:17:37 -08:00
|
|
|
// accumulator for entries that can be processed in parallel
|
2019-09-19 10:06:08 -07:00
|
|
|
let mut batches = vec![];
|
2019-10-23 12:11:04 -07:00
|
|
|
let mut tick_hashes = vec![];
|
2019-02-16 13:17:37 -08:00
|
|
|
for entry in entries {
|
|
|
|
if entry.is_tick() {
|
2019-11-08 11:29:41 -08:00
|
|
|
// If it's a tick, save it for later
|
2019-10-23 12:11:04 -07:00
|
|
|
tick_hashes.push(entry.hash);
|
2019-11-08 11:29:41 -08:00
|
|
|
if bank.is_block_boundary(bank.tick_height() + tick_hashes.len() as u64) {
|
|
|
|
// If it's a tick that will cause a new blockhash to be created,
|
|
|
|
// execute the group and register the tick
|
2019-11-20 15:43:10 -08:00
|
|
|
execute_batches(
|
|
|
|
bank,
|
|
|
|
&batches,
|
|
|
|
entry_callback,
|
|
|
|
transaction_status_sender.clone(),
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2019-11-20 15:43:10 -08:00
|
|
|
)?;
|
2019-11-08 11:29:41 -08:00
|
|
|
batches.clear();
|
|
|
|
for hash in &tick_hashes {
|
|
|
|
bank.register_tick(hash);
|
|
|
|
}
|
|
|
|
tick_hashes.clear();
|
|
|
|
}
|
2019-02-16 13:17:37 -08:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-23 17:35:15 -07:00
|
|
|
// else loop on processing the entry
|
|
|
|
loop {
|
2019-09-19 10:06:08 -07:00
|
|
|
let iteration_order = if randomize {
|
|
|
|
let mut iteration_order: Vec<usize> = (0..entry.transactions.len()).collect();
|
|
|
|
iteration_order.shuffle(&mut thread_rng());
|
|
|
|
Some(iteration_order)
|
2019-08-28 08:38:32 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-05-23 17:35:15 -07:00
|
|
|
// try to lock the accounts
|
2019-09-19 10:06:08 -07:00
|
|
|
let batch = bank.prepare_batch(&entry.transactions, iteration_order);
|
2019-05-23 17:35:15 -07:00
|
|
|
|
2019-09-19 10:06:08 -07:00
|
|
|
let first_lock_err = first_err(batch.lock_results());
|
2019-05-23 17:35:15 -07:00
|
|
|
|
|
|
|
// if locking worked
|
|
|
|
if first_lock_err.is_ok() {
|
2019-09-19 10:06:08 -07:00
|
|
|
batches.push(batch);
|
2019-05-23 17:35:15 -07:00
|
|
|
// done with this entry
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// else we failed to lock, 2 possible reasons
|
2019-09-19 10:06:08 -07:00
|
|
|
if batches.is_empty() {
|
2019-05-23 17:35:15 -07:00
|
|
|
// An entry has account lock conflicts with *itself*, which should not happen
|
|
|
|
// if generated by a properly functioning leader
|
2020-02-14 11:11:55 -08:00
|
|
|
datapoint_error!(
|
2019-05-10 08:33:58 -07:00
|
|
|
"validator_process_entry_error",
|
|
|
|
(
|
|
|
|
"error",
|
|
|
|
format!(
|
|
|
|
"Lock accounts error, entry conflicts with itself, txs: {:?}",
|
|
|
|
entry.transactions
|
|
|
|
),
|
|
|
|
String
|
|
|
|
)
|
2019-05-07 15:51:35 -07:00
|
|
|
);
|
2019-05-23 17:35:15 -07:00
|
|
|
// bail
|
2019-05-07 15:51:35 -07:00
|
|
|
first_lock_err?;
|
2019-05-23 17:35:15 -07:00
|
|
|
} else {
|
|
|
|
// else we have an entry that conflicts with a prior entry
|
|
|
|
// execute the current queue and try to process this entry again
|
2019-11-20 15:43:10 -08:00
|
|
|
execute_batches(
|
|
|
|
bank,
|
|
|
|
&batches,
|
|
|
|
entry_callback,
|
|
|
|
transaction_status_sender.clone(),
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2019-11-20 15:43:10 -08:00
|
|
|
)?;
|
2019-09-19 10:06:08 -07:00
|
|
|
batches.clear();
|
2019-05-07 15:51:35 -07:00
|
|
|
}
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 23:17:40 -07:00
|
|
|
execute_batches(
|
|
|
|
bank,
|
|
|
|
&batches,
|
|
|
|
entry_callback,
|
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2020-07-29 23:17:40 -07:00
|
|
|
)?;
|
2019-10-23 12:11:04 -07:00
|
|
|
for hash in tick_hashes {
|
|
|
|
bank.register_tick(&hash);
|
|
|
|
}
|
2019-02-21 13:37:08 -08:00
|
|
|
Ok(())
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
#[derive(Error, Debug)]
|
2020-01-13 13:13:52 -08:00
|
|
|
pub enum BlockstoreProcessorError {
|
2019-12-02 14:42:05 -08:00
|
|
|
#[error("failed to load entries")]
|
2020-01-14 17:15:26 -08:00
|
|
|
FailedToLoadEntries(#[from] BlockstoreError),
|
2019-12-02 14:42:05 -08:00
|
|
|
|
|
|
|
#[error("failed to load meta")]
|
2019-10-31 13:38:50 -07:00
|
|
|
FailedToLoadMeta,
|
|
|
|
|
2019-12-02 14:42:05 -08:00
|
|
|
#[error("invalid block")]
|
|
|
|
InvalidBlock(#[from] BlockError),
|
|
|
|
|
|
|
|
#[error("invalid transaction")]
|
2020-01-14 17:15:26 -08:00
|
|
|
InvalidTransaction(#[from] TransactionError),
|
2019-12-09 14:49:19 -08:00
|
|
|
|
|
|
|
#[error("no valid forks found")]
|
|
|
|
NoValidForksFound,
|
2020-01-24 17:27:04 -08:00
|
|
|
|
|
|
|
#[error("invalid hard fork")]
|
|
|
|
InvalidHardFork(Slot),
|
2020-09-11 09:48:06 -07:00
|
|
|
|
|
|
|
#[error("root bank with mismatched capitalization at {0}")]
|
|
|
|
RootBankWithMismatchedCapitalization(Slot),
|
2019-03-13 12:47:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
/// Callback for accessing bank state while processing the blockstore
|
2020-06-08 17:38:14 -07:00
|
|
|
pub type ProcessCallback = Arc<dyn Fn(&Bank) + Sync + Send>;
|
2019-10-08 14:58:49 -07:00
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
#[derive(Default, Clone)]
|
2019-10-08 14:58:49 -07:00
|
|
|
pub struct ProcessOptions {
|
2020-12-07 00:49:55 -08:00
|
|
|
pub bpf_jit: bool,
|
2019-11-04 21:14:55 -08:00
|
|
|
pub poh_verify: bool,
|
2019-10-08 14:58:49 -07:00
|
|
|
pub full_leader_cache: bool,
|
|
|
|
pub dev_halt_at_slot: Option<Slot>,
|
|
|
|
pub entry_callback: Option<ProcessCallback>,
|
|
|
|
pub override_num_threads: Option<usize>,
|
2020-01-24 17:27:04 -08:00
|
|
|
pub new_hard_forks: Option<Vec<Slot>>,
|
2020-03-22 11:10:04 -07:00
|
|
|
pub frozen_accounts: Vec<Pubkey>,
|
2020-09-23 18:46:42 -07:00
|
|
|
pub debug_keys: Option<Arc<HashSet<Pubkey>>>,
|
2020-08-25 09:49:15 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
pub fn process_blockstore(
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config: &GenesisConfig,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2019-12-05 18:41:29 -08:00
|
|
|
account_paths: Vec<PathBuf>,
|
2019-10-08 14:58:49 -07:00
|
|
|
opts: ProcessOptions,
|
2020-01-23 15:09:32 -08:00
|
|
|
) -> BlockstoreProcessorResult {
|
2019-10-08 14:58:49 -07:00
|
|
|
if let Some(num_threads) = opts.override_num_threads {
|
|
|
|
PAR_THREAD_POOL.with(|pool| {
|
|
|
|
*pool.borrow_mut() = rayon::ThreadPoolBuilder::new()
|
|
|
|
.num_threads(num_threads)
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2019-02-20 15:42:35 -08:00
|
|
|
// Setup bank for slot 0
|
2020-09-24 12:23:09 -07:00
|
|
|
let bank0 = Bank::new_with_paths(
|
2020-08-25 09:49:15 -07:00
|
|
|
&genesis_config,
|
|
|
|
account_paths,
|
|
|
|
&opts.frozen_accounts,
|
2020-09-23 18:46:42 -07:00
|
|
|
opts.debug_keys.clone(),
|
2020-12-15 20:29:09 -08:00
|
|
|
Some(&crate::builtins::get(opts.bpf_jit)),
|
2020-09-24 12:23:09 -07:00
|
|
|
);
|
|
|
|
let bank0 = Arc::new(bank0);
|
2019-12-23 09:43:07 -08:00
|
|
|
info!("processing ledger for slot 0...");
|
2020-01-14 17:15:26 -08:00
|
|
|
let recyclers = VerifyRecyclers::default();
|
2020-12-13 17:26:34 -08:00
|
|
|
process_bank_0(&bank0, blockstore, &opts, &recyclers);
|
2020-09-24 12:23:09 -07:00
|
|
|
do_process_blockstore_from_root(blockstore, bank0, &opts, &recyclers, None)
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Process blockstore from a known root bank
|
2020-09-24 12:23:09 -07:00
|
|
|
pub(crate) fn process_blockstore_from_root(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2020-09-24 12:23:09 -07:00
|
|
|
bank: Bank,
|
2019-10-08 14:58:49 -07:00
|
|
|
opts: &ProcessOptions,
|
2020-01-14 17:15:26 -08:00
|
|
|
recyclers: &VerifyRecyclers,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-25 09:49:15 -07:00
|
|
|
) -> BlockstoreProcessorResult {
|
|
|
|
do_process_blockstore_from_root(
|
|
|
|
blockstore,
|
2020-09-24 12:23:09 -07:00
|
|
|
Arc::new(bank),
|
2020-08-25 09:49:15 -07:00
|
|
|
opts,
|
|
|
|
recyclers,
|
|
|
|
transaction_status_sender,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_process_blockstore_from_root(
|
|
|
|
blockstore: &Blockstore,
|
2020-09-24 12:23:09 -07:00
|
|
|
bank: Arc<Bank>,
|
2020-08-25 09:49:15 -07:00
|
|
|
opts: &ProcessOptions,
|
|
|
|
recyclers: &VerifyRecyclers,
|
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-01-23 15:09:32 -08:00
|
|
|
) -> BlockstoreProcessorResult {
|
2020-03-25 21:41:50 -07:00
|
|
|
info!("processing ledger from slot {}...", bank.slot());
|
2020-01-01 11:19:20 -08:00
|
|
|
let allocated = thread_mem_usage::Allocatedp::default();
|
|
|
|
let initial_allocation = allocated.get();
|
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
// Starting slot must be a root, and thus has no parents
|
|
|
|
assert!(bank.parent().is_none());
|
|
|
|
let start_slot = bank.slot();
|
|
|
|
let now = Instant::now();
|
2020-05-05 19:45:41 -07:00
|
|
|
let mut root = start_slot;
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-01-24 17:27:04 -08:00
|
|
|
if let Some(ref new_hard_forks) = opts.new_hard_forks {
|
|
|
|
let hard_forks = bank.hard_forks();
|
|
|
|
|
|
|
|
for hard_fork_slot in new_hard_forks.iter() {
|
2020-07-05 13:51:43 -07:00
|
|
|
if *hard_fork_slot > start_slot {
|
|
|
|
hard_forks.write().unwrap().register(*hard_fork_slot);
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Hard fork at {} ignored, --hard-fork option can be removed.",
|
|
|
|
hard_fork_slot
|
2020-01-24 17:27:04 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 21:32:44 -07:00
|
|
|
// ensure start_slot is rooted for correct replay
|
|
|
|
if blockstore.is_primary_access() {
|
|
|
|
blockstore
|
|
|
|
.set_roots(&[start_slot])
|
|
|
|
.expect("Couldn't set root slot on startup");
|
|
|
|
} else if !blockstore.is_root(start_slot) {
|
|
|
|
panic!("starting slot isn't root and can't update due to being secondary blockstore access: {}", start_slot);
|
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-03-25 21:41:50 -07:00
|
|
|
if let Ok(metas) = blockstore.slot_meta_iterator(start_slot) {
|
|
|
|
if let Some((slot, _meta)) = metas.last() {
|
|
|
|
info!("ledger holds data through slot {}", slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Iterate and replay slots from blockstore starting from `start_slot`
|
2020-05-06 08:24:59 -07:00
|
|
|
let (initial_forks, leader_schedule_cache) = {
|
|
|
|
if let Some(meta) = blockstore
|
|
|
|
.meta(start_slot)
|
|
|
|
.unwrap_or_else(|_| panic!("Failed to get meta for slot {}", start_slot))
|
|
|
|
{
|
2019-08-13 17:20:14 -07:00
|
|
|
let epoch_schedule = bank.epoch_schedule();
|
|
|
|
let mut leader_schedule_cache = LeaderScheduleCache::new(*epoch_schedule, &bank);
|
2019-10-08 14:58:49 -07:00
|
|
|
if opts.full_leader_cache {
|
|
|
|
leader_schedule_cache.set_max_schedules(std::usize::MAX);
|
|
|
|
}
|
2020-05-06 08:24:59 -07:00
|
|
|
let initial_forks = load_frozen_forks(
|
2019-08-13 17:20:14 -07:00
|
|
|
&bank,
|
|
|
|
&meta,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore,
|
2019-08-13 17:20:14 -07:00
|
|
|
&mut leader_schedule_cache,
|
2020-05-05 19:45:41 -07:00
|
|
|
&mut root,
|
2019-10-08 14:58:49 -07:00
|
|
|
opts,
|
2020-01-14 17:15:26 -08:00
|
|
|
recyclers,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender,
|
2019-08-13 17:20:14 -07:00
|
|
|
)?;
|
2020-05-06 08:24:59 -07:00
|
|
|
(initial_forks, leader_schedule_cache)
|
2019-08-13 17:20:14 -07:00
|
|
|
} else {
|
|
|
|
// If there's no meta for the input `start_slot`, then we started from a snapshot
|
2020-01-13 13:13:52 -08:00
|
|
|
// and there's no point in processing the rest of blockstore and implies blockstore
|
2019-08-13 17:20:14 -07:00
|
|
|
// should be empty past this point.
|
|
|
|
let leader_schedule_cache = LeaderScheduleCache::new_from_bank(&bank);
|
2020-05-06 08:24:59 -07:00
|
|
|
(vec![bank], leader_schedule_cache)
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
if initial_forks.is_empty() {
|
|
|
|
return Err(BlockstoreProcessorError::NoValidForksFound);
|
|
|
|
}
|
|
|
|
let bank_forks = BankForks::new_from_banks(&initial_forks, root);
|
2019-08-13 17:20:14 -07:00
|
|
|
|
|
|
|
info!(
|
2020-05-06 08:24:59 -07:00
|
|
|
"ledger processed in {}ms. {} MB allocated. root={}, {} fork{} at {}, with {} frozen bank{}",
|
2019-08-13 17:20:14 -07:00
|
|
|
duration_as_ms(&now.elapsed()),
|
2020-01-01 11:19:20 -08:00
|
|
|
allocated.since(initial_allocation) / 1_000_000,
|
2020-05-06 08:24:59 -07:00
|
|
|
bank_forks.root(),
|
|
|
|
initial_forks.len(),
|
|
|
|
if initial_forks.len() > 1 { "s" } else { "" },
|
|
|
|
initial_forks
|
2019-11-04 21:14:55 -08:00
|
|
|
.iter()
|
2020-05-06 08:24:59 -07:00
|
|
|
.map(|b| b.slot().to_string())
|
2020-01-23 15:09:32 -08:00
|
|
|
.join(", "),
|
|
|
|
bank_forks.frozen_banks().len(),
|
|
|
|
if bank_forks.frozen_banks().len() > 1 {
|
|
|
|
"s"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
2019-08-13 17:20:14 -07:00
|
|
|
);
|
2020-01-23 15:09:32 -08:00
|
|
|
assert!(bank_forks.active_banks().is_empty());
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-09-11 09:48:06 -07:00
|
|
|
// We might be promptly restarted after bad capitalization was detected while creating newer snapshot.
|
|
|
|
// In that case, we're most likely restored from the last good snapshot and replayed up to this root.
|
|
|
|
// So again check here for the bad capitalization to avoid to continue until the next snapshot creation.
|
|
|
|
if !bank_forks.root_bank().calculate_and_verify_capitalization() {
|
|
|
|
return Err(BlockstoreProcessorError::RootBankWithMismatchedCapitalization(root));
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
Ok((bank_forks, leader_schedule_cache))
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
/// Verify that a segment of entries has the correct number of ticks and hashes
|
|
|
|
pub fn verify_ticks(
|
2019-10-08 14:58:49 -07:00
|
|
|
bank: &Arc<Bank>,
|
2019-08-13 17:20:14 -07:00
|
|
|
entries: &[Entry],
|
2020-01-14 17:15:26 -08:00
|
|
|
slot_full: bool,
|
|
|
|
tick_hash_count: &mut u64,
|
|
|
|
) -> std::result::Result<(), BlockError> {
|
|
|
|
let next_bank_tick_height = bank.tick_height() + entries.tick_count();
|
|
|
|
let max_bank_tick_height = bank.max_tick_height();
|
2020-08-20 21:56:25 -07:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
if next_bank_tick_height > max_bank_tick_height {
|
|
|
|
warn!("Too many entry ticks found in slot: {}", bank.slot());
|
|
|
|
return Err(BlockError::InvalidTickCount);
|
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
if next_bank_tick_height < max_bank_tick_height && slot_full {
|
|
|
|
warn!("Too few entry ticks found in slot: {}", bank.slot());
|
|
|
|
return Err(BlockError::InvalidTickCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
if next_bank_tick_height == max_bank_tick_height {
|
|
|
|
let has_trailing_entry = entries.last().map(|e| !e.is_tick()).unwrap_or_default();
|
|
|
|
if has_trailing_entry {
|
2019-10-31 13:38:50 -07:00
|
|
|
warn!("Slot: {} did not end with a tick entry", bank.slot());
|
2020-01-14 17:15:26 -08:00
|
|
|
return Err(BlockError::TrailingEntry);
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
if !slot_full {
|
|
|
|
warn!("Slot: {} was not marked full", bank.slot());
|
|
|
|
return Err(BlockError::InvalidLastTick);
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
2020-01-14 17:15:26 -08:00
|
|
|
}
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
let hashes_per_tick = bank.hashes_per_tick().unwrap_or(0);
|
|
|
|
if !entries.verify_tick_hash_count(tick_hash_count, hashes_per_tick) {
|
|
|
|
warn!(
|
|
|
|
"Tick with invalid number of hashes found in slot: {}",
|
|
|
|
bank.slot()
|
|
|
|
);
|
|
|
|
return Err(BlockError::InvalidTickHashCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_full_slot(
|
|
|
|
blockstore: &Blockstore,
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
opts: &ProcessOptions,
|
|
|
|
recyclers: &VerifyRecyclers,
|
2020-03-23 12:19:11 -07:00
|
|
|
progress: &mut ConfirmationProgress,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2020-01-14 17:15:26 -08:00
|
|
|
) -> result::Result<(), BlockstoreProcessorError> {
|
|
|
|
let mut timing = ConfirmationTiming::default();
|
|
|
|
let skip_verification = !opts.poh_verify;
|
|
|
|
confirm_slot(
|
|
|
|
blockstore,
|
|
|
|
bank,
|
|
|
|
&mut timing,
|
2020-03-23 12:19:11 -07:00
|
|
|
progress,
|
2020-01-14 17:15:26 -08:00
|
|
|
skip_verification,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2020-01-14 17:15:26 -08:00
|
|
|
opts.entry_callback.as_ref(),
|
|
|
|
recyclers,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if !bank.is_complete() {
|
|
|
|
Err(BlockstoreProcessorError::InvalidBlock(
|
|
|
|
BlockError::Incomplete,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConfirmationTiming {
|
|
|
|
pub started: Instant,
|
|
|
|
pub replay_elapsed: u64,
|
2020-06-16 14:00:29 -07:00
|
|
|
pub poh_verify_elapsed: u64,
|
|
|
|
pub transaction_verify_elapsed: u64,
|
2020-01-14 17:15:26 -08:00
|
|
|
pub fetch_elapsed: u64,
|
|
|
|
pub fetch_fail_elapsed: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ConfirmationTiming {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
started: Instant::now(),
|
|
|
|
replay_elapsed: 0,
|
2020-06-16 14:00:29 -07:00
|
|
|
poh_verify_elapsed: 0,
|
|
|
|
transaction_verify_elapsed: 0,
|
2020-01-14 17:15:26 -08:00
|
|
|
fetch_elapsed: 0,
|
|
|
|
fetch_fail_elapsed: 0,
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
2020-01-14 17:15:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ConfirmationProgress {
|
|
|
|
pub last_entry: Hash,
|
|
|
|
pub tick_hash_count: u64,
|
|
|
|
pub num_shreds: u64,
|
|
|
|
pub num_entries: usize,
|
|
|
|
pub num_txs: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfirmationProgress {
|
|
|
|
pub fn new(last_entry: Hash) -> Self {
|
|
|
|
Self {
|
|
|
|
last_entry,
|
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
pub fn confirm_slot(
|
|
|
|
blockstore: &Blockstore,
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
timing: &mut ConfirmationTiming,
|
|
|
|
progress: &mut ConfirmationProgress,
|
|
|
|
skip_verification: bool,
|
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2020-01-14 17:15:26 -08:00
|
|
|
entry_callback: Option<&ProcessCallback>,
|
|
|
|
recyclers: &VerifyRecyclers,
|
|
|
|
) -> result::Result<(), BlockstoreProcessorError> {
|
|
|
|
let slot = bank.slot();
|
|
|
|
|
|
|
|
let (entries, num_shreds, slot_full) = {
|
|
|
|
let mut load_elapsed = Measure::start("load_elapsed");
|
|
|
|
let load_result = blockstore
|
2020-04-09 20:10:51 -07:00
|
|
|
.get_slot_entries_with_shred_info(slot, progress.num_shreds, false)
|
2020-01-14 17:15:26 -08:00
|
|
|
.map_err(BlockstoreProcessorError::FailedToLoadEntries);
|
|
|
|
load_elapsed.stop();
|
|
|
|
if load_result.is_err() {
|
|
|
|
timing.fetch_fail_elapsed += load_elapsed.as_us();
|
|
|
|
} else {
|
|
|
|
timing.fetch_elapsed += load_elapsed.as_us();
|
|
|
|
}
|
|
|
|
load_result
|
|
|
|
}?;
|
|
|
|
|
|
|
|
let num_entries = entries.len();
|
|
|
|
let num_txs = entries.iter().map(|e| e.transactions.len()).sum::<usize>();
|
|
|
|
trace!(
|
|
|
|
"Fetched entries for slot {}, num_entries: {}, num_shreds: {}, num_txs: {}, slot_full: {}",
|
|
|
|
slot,
|
|
|
|
num_entries,
|
|
|
|
num_shreds,
|
|
|
|
num_txs,
|
|
|
|
slot_full,
|
|
|
|
);
|
|
|
|
|
|
|
|
if !skip_verification {
|
|
|
|
let tick_hash_count = &mut progress.tick_hash_count;
|
|
|
|
verify_ticks(bank, &entries, slot_full, tick_hash_count).map_err(|err| {
|
2019-10-08 14:58:49 -07:00
|
|
|
warn!(
|
2020-01-14 17:15:26 -08:00
|
|
|
"{:#?}, slot: {}, entry len: {}, tick_height: {}, last entry: {}, last_blockhash: {}, shred_index: {}, slot_full: {}",
|
|
|
|
err,
|
|
|
|
slot,
|
|
|
|
num_entries,
|
|
|
|
bank.tick_height(),
|
|
|
|
progress.last_entry,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
num_shreds,
|
|
|
|
slot_full,
|
2019-10-08 14:58:49 -07:00
|
|
|
);
|
2020-01-14 17:15:26 -08:00
|
|
|
err
|
2019-11-20 15:43:10 -08:00
|
|
|
})?;
|
2020-01-14 17:15:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let verifier = if !skip_verification {
|
|
|
|
datapoint_debug!("verify-batch-size", ("size", num_entries as i64, i64));
|
2020-09-15 18:23:21 -07:00
|
|
|
let entry_state = entries.start_verify(
|
|
|
|
&progress.last_entry,
|
|
|
|
recyclers.clone(),
|
2020-09-21 22:36:23 -07:00
|
|
|
bank.secp256k1_program_enabled(),
|
2020-09-15 18:23:21 -07:00
|
|
|
);
|
2020-01-14 17:15:26 -08:00
|
|
|
if entry_state.status() == EntryVerificationStatus::Failure {
|
|
|
|
warn!("Ledger proof of history failed at slot: {}", slot);
|
|
|
|
return Err(BlockError::InvalidEntryHash.into());
|
|
|
|
}
|
|
|
|
Some(entry_state)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
let mut replay_elapsed = Measure::start("replay_elapsed");
|
|
|
|
let process_result = process_entries_with_callback(
|
|
|
|
bank,
|
|
|
|
&entries,
|
|
|
|
true,
|
|
|
|
entry_callback,
|
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender,
|
2020-01-14 17:15:26 -08:00
|
|
|
)
|
|
|
|
.map_err(BlockstoreProcessorError::from);
|
|
|
|
replay_elapsed.stop();
|
|
|
|
timing.replay_elapsed += replay_elapsed.as_us();
|
|
|
|
|
|
|
|
if let Some(mut verifier) = verifier {
|
2020-06-16 14:00:29 -07:00
|
|
|
let verified = verifier.finish_verify(&entries);
|
|
|
|
timing.poh_verify_elapsed += verifier.poh_duration_us();
|
|
|
|
timing.transaction_verify_elapsed += verifier.transaction_duration_us();
|
|
|
|
if !verified {
|
2020-01-14 17:15:26 -08:00
|
|
|
warn!("Ledger proof of history failed at slot: {}", bank.slot());
|
|
|
|
return Err(BlockError::InvalidEntryHash.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process_result?;
|
|
|
|
|
|
|
|
progress.num_shreds += num_shreds;
|
|
|
|
progress.num_entries += num_entries;
|
|
|
|
progress.num_txs += num_txs;
|
|
|
|
if let Some(last_entry) = entries.last() {
|
|
|
|
progress.last_entry = last_entry.hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special handling required for processing the entries in slot 0
|
|
|
|
fn process_bank_0(
|
2019-10-08 14:58:49 -07:00
|
|
|
bank0: &Arc<Bank>,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2019-10-08 14:58:49 -07:00
|
|
|
opts: &ProcessOptions,
|
2020-01-14 17:15:26 -08:00
|
|
|
recyclers: &VerifyRecyclers,
|
2020-12-13 17:26:34 -08:00
|
|
|
) {
|
2019-08-13 17:20:14 -07:00
|
|
|
assert_eq!(bank0.slot(), 0);
|
2020-03-23 12:19:11 -07:00
|
|
|
let mut progress = ConfirmationProgress::new(bank0.last_blockhash());
|
2020-07-29 23:17:40 -07:00
|
|
|
confirm_full_slot(
|
|
|
|
blockstore,
|
|
|
|
bank0,
|
|
|
|
opts,
|
|
|
|
recyclers,
|
|
|
|
&mut progress,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("processing for bank 0 must succeed");
|
2019-08-13 17:20:14 -07:00
|
|
|
bank0.freeze();
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
// Given a bank, add its children to the pending slots queue if those children slots are
|
2019-08-13 17:20:14 -07:00
|
|
|
// complete
|
|
|
|
fn process_next_slots(
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
meta: &SlotMeta,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2019-08-13 17:20:14 -07:00
|
|
|
leader_schedule_cache: &LeaderScheduleCache,
|
2020-01-10 12:16:44 -08:00
|
|
|
pending_slots: &mut Vec<(SlotMeta, Arc<Bank>, Hash)>,
|
2020-05-06 08:24:59 -07:00
|
|
|
initial_forks: &mut HashMap<Slot, Arc<Bank>>,
|
2020-01-13 13:13:52 -08:00
|
|
|
) -> result::Result<(), BlockstoreProcessorError> {
|
2020-01-10 12:16:44 -08:00
|
|
|
if let Some(parent) = bank.parent() {
|
2020-05-06 08:24:59 -07:00
|
|
|
initial_forks.remove(&parent.slot());
|
2020-01-10 12:16:44 -08:00
|
|
|
}
|
2020-05-06 08:24:59 -07:00
|
|
|
initial_forks.insert(bank.slot(), bank.clone());
|
2020-01-10 12:16:44 -08:00
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
if meta.next_slots.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a fork point if there are multiple children, create a new child bank for each fork
|
|
|
|
for next_slot in &meta.next_slots {
|
2020-01-13 13:13:52 -08:00
|
|
|
let next_meta = blockstore
|
2019-08-13 17:20:14 -07:00
|
|
|
.meta(*next_slot)
|
2019-02-20 15:42:35 -08:00
|
|
|
.map_err(|err| {
|
2019-08-13 17:20:14 -07:00
|
|
|
warn!("Failed to load meta for slot {}: {:?}", next_slot, err);
|
2020-01-13 13:13:52 -08:00
|
|
|
BlockstoreProcessorError::FailedToLoadMeta
|
2019-02-20 15:42:35 -08:00
|
|
|
})?
|
|
|
|
.unwrap();
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Only process full slots in blockstore_processor, replay_stage
|
2019-08-13 17:20:14 -07:00
|
|
|
// handles any partials
|
|
|
|
if next_meta.is_full() {
|
2020-01-01 11:19:20 -08:00
|
|
|
let allocated = thread_mem_usage::Allocatedp::default();
|
|
|
|
let initial_allocation = allocated.get();
|
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
let next_bank = Arc::new(Bank::new_from_parent(
|
|
|
|
&bank,
|
|
|
|
&leader_schedule_cache
|
|
|
|
.slot_leader_at(*next_slot, Some(&bank))
|
|
|
|
.unwrap(),
|
|
|
|
*next_slot,
|
|
|
|
));
|
2020-01-01 11:19:20 -08:00
|
|
|
trace!(
|
|
|
|
"New bank for slot {}, parent slot is {}. {} bytes allocated",
|
|
|
|
next_slot,
|
|
|
|
bank.slot(),
|
|
|
|
allocated.since(initial_allocation)
|
|
|
|
);
|
2020-01-10 12:16:44 -08:00
|
|
|
pending_slots.push((next_meta, next_bank, bank.last_blockhash()));
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-20 19:04:18 -07:00
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
// Reverse sort by slot, so the next slot to be processed can be popped
|
2020-01-10 12:16:44 -08:00
|
|
|
pending_slots.sort_by(|a, b| b.1.slot().cmp(&a.1.slot()));
|
2019-08-13 17:20:14 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-19 02:39:44 -07:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Iterate through blockstore processing slots starting from the root slot pointed to by the
|
2020-05-06 08:24:59 -07:00
|
|
|
// given `meta` and return a vector of frozen bank forks
|
|
|
|
fn load_frozen_forks(
|
2019-08-13 17:20:14 -07:00
|
|
|
root_bank: &Arc<Bank>,
|
|
|
|
root_meta: &SlotMeta,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2019-08-13 17:20:14 -07:00
|
|
|
leader_schedule_cache: &mut LeaderScheduleCache,
|
2020-05-05 19:45:41 -07:00
|
|
|
root: &mut Slot,
|
2019-10-08 14:58:49 -07:00
|
|
|
opts: &ProcessOptions,
|
2020-01-14 17:15:26 -08:00
|
|
|
recyclers: &VerifyRecyclers,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-05-06 08:24:59 -07:00
|
|
|
) -> result::Result<Vec<Arc<Bank>>, BlockstoreProcessorError> {
|
|
|
|
let mut initial_forks = HashMap::new();
|
2020-08-20 21:56:25 -07:00
|
|
|
let mut all_banks = HashMap::new();
|
2019-04-20 20:17:57 -07:00
|
|
|
let mut last_status_report = Instant::now();
|
2020-09-08 20:05:10 -07:00
|
|
|
let mut last_free = Instant::now();
|
2019-08-13 17:20:14 -07:00
|
|
|
let mut pending_slots = vec![];
|
2020-01-01 11:31:18 -08:00
|
|
|
let mut last_root_slot = root_bank.slot();
|
2020-03-23 12:19:11 -07:00
|
|
|
let mut slots_elapsed = 0;
|
|
|
|
let mut txs = 0;
|
2020-08-20 21:56:25 -07:00
|
|
|
let blockstore_max_root = blockstore.max_root();
|
|
|
|
let max_root = std::cmp::max(root_bank.slot(), blockstore_max_root);
|
|
|
|
info!(
|
|
|
|
"load_frozen_forks() latest root from blockstore: {}, max_root: {}",
|
|
|
|
blockstore_max_root, max_root,
|
|
|
|
);
|
2019-08-13 17:20:14 -07:00
|
|
|
process_next_slots(
|
|
|
|
root_bank,
|
|
|
|
root_meta,
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore,
|
2019-08-13 17:20:14 -07:00
|
|
|
leader_schedule_cache,
|
|
|
|
&mut pending_slots,
|
2020-05-06 08:24:59 -07:00
|
|
|
&mut initial_forks,
|
2019-08-13 17:20:14 -07:00
|
|
|
)?;
|
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
let dev_halt_at_slot = opts.dev_halt_at_slot.unwrap_or(std::u64::MAX);
|
2020-12-04 21:14:59 -08:00
|
|
|
if root_bank.slot() != dev_halt_at_slot {
|
|
|
|
while !pending_slots.is_empty() {
|
|
|
|
let (meta, bank, last_entry_hash) = pending_slots.pop().unwrap();
|
|
|
|
let slot = bank.slot();
|
|
|
|
if last_status_report.elapsed() > Duration::from_secs(2) {
|
|
|
|
let secs = last_status_report.elapsed().as_secs() as f32;
|
|
|
|
last_status_report = Instant::now();
|
|
|
|
info!(
|
|
|
|
"processing ledger: slot={}, last root slot={} slots={} slots/s={:?} txs/s={}",
|
|
|
|
slot,
|
|
|
|
last_root_slot,
|
|
|
|
slots_elapsed,
|
|
|
|
slots_elapsed as f32 / secs,
|
|
|
|
txs as f32 / secs,
|
|
|
|
);
|
|
|
|
slots_elapsed = 0;
|
|
|
|
txs = 0;
|
|
|
|
}
|
2019-04-20 20:17:57 -07:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
let allocated = thread_mem_usage::Allocatedp::default();
|
|
|
|
let initial_allocation = allocated.get();
|
2020-01-01 11:19:20 -08:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
let mut progress = ConfirmationProgress::new(last_entry_hash);
|
2020-08-07 11:21:35 -07:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
if process_single_slot(
|
|
|
|
blockstore,
|
|
|
|
&bank,
|
|
|
|
opts,
|
|
|
|
recyclers,
|
|
|
|
&mut progress,
|
|
|
|
transaction_status_sender.clone(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
continue;
|
2020-08-20 21:56:25 -07:00
|
|
|
}
|
2020-12-04 21:14:59 -08:00
|
|
|
txs += progress.num_txs;
|
|
|
|
|
|
|
|
// Block must be frozen by this point, otherwise `process_single_slot` would
|
|
|
|
// have errored above
|
|
|
|
assert!(bank.is_frozen());
|
|
|
|
all_banks.insert(bank.slot(), bank.clone());
|
|
|
|
|
|
|
|
// If we've reached the last known root in blockstore, start looking
|
|
|
|
// for newer cluster confirmed roots
|
|
|
|
let new_root_bank = {
|
|
|
|
if *root == max_root {
|
|
|
|
supermajority_root_from_vote_accounts(
|
|
|
|
bank.slot(),
|
|
|
|
bank.total_epoch_stake(),
|
|
|
|
bank.vote_accounts(),
|
|
|
|
).and_then(|supermajority_root| {
|
|
|
|
if supermajority_root > *root {
|
|
|
|
// If there's a cluster confirmed root greater than our last
|
|
|
|
// replayed root, then beccause the cluster confirmed root should
|
|
|
|
// be descended from our last root, it must exist in `all_banks`
|
|
|
|
let cluster_root_bank = all_banks.get(&supermajority_root).unwrap();
|
|
|
|
|
|
|
|
// cluster root must be a descendant of our root, otherwise something
|
|
|
|
// is drastically wrong
|
|
|
|
assert!(cluster_root_bank.ancestors.contains_key(root));
|
|
|
|
info!("blockstore processor found new cluster confirmed root: {}, observed in bank: {}", cluster_root_bank.slot(), bank.slot());
|
|
|
|
Some(cluster_root_bank)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if blockstore.is_root(slot) {
|
|
|
|
Some(&bank)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(new_root_bank) = new_root_bank {
|
|
|
|
*root = new_root_bank.slot();
|
|
|
|
last_root_slot = new_root_bank.slot();
|
|
|
|
leader_schedule_cache.set_root(&new_root_bank);
|
|
|
|
new_root_bank.squash();
|
2020-08-20 21:56:25 -07:00
|
|
|
|
2020-12-21 21:33:37 -08:00
|
|
|
if last_free.elapsed() > Duration::from_secs(10) {
|
2020-12-04 21:14:59 -08:00
|
|
|
// This could take few secs; so update last_free later
|
|
|
|
new_root_bank.exhaustively_free_unused_resource();
|
|
|
|
last_free = Instant::now();
|
|
|
|
}
|
2020-08-20 21:56:25 -07:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
// Filter out all non descendants of the new root
|
|
|
|
pending_slots
|
|
|
|
.retain(|(_, pending_bank, _)| pending_bank.ancestors.contains_key(root));
|
|
|
|
initial_forks.retain(|_, fork_tip_bank| fork_tip_bank.ancestors.contains_key(root));
|
|
|
|
all_banks.retain(|_, bank| bank.ancestors.contains_key(root));
|
2020-09-08 20:05:10 -07:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
slots_elapsed += 1;
|
2020-08-20 21:56:25 -07:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
trace!(
|
|
|
|
"Bank for {}slot {} is complete. {} bytes allocated",
|
|
|
|
if last_root_slot == slot { "root " } else { "" },
|
|
|
|
slot,
|
|
|
|
allocated.since(initial_allocation)
|
|
|
|
);
|
2019-02-28 12:09:19 -08:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
process_next_slots(
|
|
|
|
&bank,
|
|
|
|
&meta,
|
|
|
|
blockstore,
|
|
|
|
leader_schedule_cache,
|
|
|
|
&mut pending_slots,
|
|
|
|
&mut initial_forks,
|
|
|
|
)?;
|
2020-01-01 11:19:20 -08:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
if slot >= dev_halt_at_slot {
|
|
|
|
break;
|
|
|
|
}
|
2020-01-23 01:49:32 -08:00
|
|
|
}
|
2019-02-20 15:42:35 -08:00
|
|
|
}
|
2019-02-19 18:31:56 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
Ok(initial_forks.values().cloned().collect::<Vec<_>>())
|
2019-02-16 02:26:21 -08:00
|
|
|
}
|
|
|
|
|
2020-09-17 21:33:08 -07:00
|
|
|
// `roots` is sorted largest to smallest by root slot
|
|
|
|
fn supermajority_root(roots: &[(Slot, u64)], total_epoch_stake: u64) -> Option<Slot> {
|
|
|
|
if roots.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:56:25 -07:00
|
|
|
// Find latest root
|
|
|
|
let mut total = 0;
|
2020-09-17 21:33:08 -07:00
|
|
|
let mut prev_root = roots[0].0;
|
|
|
|
for (root, stake) in roots.iter() {
|
|
|
|
assert!(*root <= prev_root);
|
2020-08-20 21:56:25 -07:00
|
|
|
total += stake;
|
|
|
|
if total as f64 / total_epoch_stake as f64 > VOTE_THRESHOLD_SIZE {
|
|
|
|
return Some(*root);
|
|
|
|
}
|
2020-09-17 21:33:08 -07:00
|
|
|
prev_root = *root;
|
2020-08-20 21:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-09-17 21:33:08 -07:00
|
|
|
fn supermajority_root_from_vote_accounts<I>(
|
|
|
|
bank_slot: Slot,
|
|
|
|
total_epoch_stake: u64,
|
2020-11-30 09:18:33 -08:00
|
|
|
vote_accounts: I,
|
2020-09-17 21:33:08 -07:00
|
|
|
) -> Option<Slot>
|
|
|
|
where
|
2020-11-30 09:18:33 -08:00
|
|
|
I: IntoIterator<Item = (Pubkey, (u64, ArcVoteAccount))>,
|
2020-09-17 21:33:08 -07:00
|
|
|
{
|
2020-11-30 09:18:33 -08:00
|
|
|
let mut roots_stakes: Vec<(Slot, u64)> = vote_accounts
|
|
|
|
.into_iter()
|
2020-08-20 21:56:25 -07:00
|
|
|
.filter_map(|(key, (stake, account))| {
|
|
|
|
if stake == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:18:33 -08:00
|
|
|
match account.vote_state().as_ref() {
|
|
|
|
Err(_) => {
|
|
|
|
warn!(
|
|
|
|
"Unable to get vote_state from account {} in bank: {}",
|
|
|
|
key, bank_slot
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Ok(vote_state) => vote_state.root_slot.map(|root_slot| (root_slot, stake)),
|
2020-08-20 21:56:25 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-09-17 21:33:08 -07:00
|
|
|
// Sort from greatest to smallest slot
|
|
|
|
roots_stakes.sort_unstable_by(|a, b| a.0.cmp(&b.0).reverse());
|
2020-08-20 21:56:25 -07:00
|
|
|
|
|
|
|
// Find latest root
|
2020-09-17 21:33:08 -07:00
|
|
|
supermajority_root(&roots_stakes, total_epoch_stake)
|
2020-08-20 21:56:25 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
// Processes and replays the contents of a single slot, returns Error
|
|
|
|
// if failed to play the slot
|
|
|
|
fn process_single_slot(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore: &Blockstore,
|
2020-01-10 12:16:44 -08:00
|
|
|
bank: &Arc<Bank>,
|
|
|
|
opts: &ProcessOptions,
|
2020-01-14 17:15:26 -08:00
|
|
|
recyclers: &VerifyRecyclers,
|
2020-03-23 12:19:11 -07:00
|
|
|
progress: &mut ConfirmationProgress,
|
2020-07-14 20:14:48 -07:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
replay_vote_sender: Option<&ReplayVoteSender>,
|
2020-01-13 13:13:52 -08:00
|
|
|
) -> result::Result<(), BlockstoreProcessorError> {
|
2020-01-14 17:15:26 -08:00
|
|
|
// Mark corrupt slots as dead so validators don't replay this slot and
|
|
|
|
// see DuplicateSignature errors later in ReplayStage
|
2020-08-07 11:21:35 -07:00
|
|
|
confirm_full_slot(blockstore, bank, opts, recyclers, progress, transaction_status_sender, replay_vote_sender).map_err(|err| {
|
2020-01-14 17:15:26 -08:00
|
|
|
let slot = bank.slot();
|
2020-01-10 12:16:44 -08:00
|
|
|
warn!("slot {} failed to verify: {}", slot, err);
|
2020-06-02 21:32:44 -07:00
|
|
|
if blockstore.is_primary_access() {
|
|
|
|
blockstore
|
|
|
|
.set_dead_slot(slot)
|
|
|
|
.expect("Failed to mark slot as dead in blockstore");
|
|
|
|
} else if !blockstore.is_dead(slot) {
|
|
|
|
panic!("Failed slot isn't dead and can't update due to being secondary blockstore access: {}", slot);
|
|
|
|
}
|
2020-01-10 12:16:44 -08:00
|
|
|
err
|
|
|
|
})?;
|
|
|
|
|
|
|
|
bank.freeze(); // all banks handled by this routine are created from complete slots
|
2020-08-20 21:56:25 -07:00
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
pub struct TransactionStatusBatch {
|
|
|
|
pub bank: Arc<Bank>,
|
|
|
|
pub transactions: Vec<Transaction>,
|
2020-07-21 13:06:49 -07:00
|
|
|
pub iteration_order: Option<Vec<usize>>,
|
2020-11-29 11:21:55 -08:00
|
|
|
pub statuses: Vec<TransactionExecutionResult>,
|
2019-12-18 09:56:29 -08:00
|
|
|
pub balances: TransactionBalancesSet,
|
2020-12-10 19:25:07 -08:00
|
|
|
pub token_balances: TransactionTokenBalancesSet,
|
2020-09-24 07:36:22 -07:00
|
|
|
pub inner_instructions: Vec<Option<InnerInstructionsList>>,
|
2020-10-08 12:06:15 -07:00
|
|
|
pub transaction_logs: Vec<TransactionLogMessages>,
|
2019-11-20 15:43:10 -08:00
|
|
|
}
|
2020-09-24 07:36:22 -07:00
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
pub type TransactionStatusSender = Sender<TransactionStatusBatch>;
|
|
|
|
|
|
|
|
pub fn send_transaction_status_batch(
|
|
|
|
bank: Arc<Bank>,
|
|
|
|
transactions: &[Transaction],
|
2020-07-21 13:06:49 -07:00
|
|
|
iteration_order: Option<Vec<usize>>,
|
2020-11-29 11:21:55 -08:00
|
|
|
statuses: Vec<TransactionExecutionResult>,
|
2019-12-18 09:56:29 -08:00
|
|
|
balances: TransactionBalancesSet,
|
2020-12-10 19:25:07 -08:00
|
|
|
token_balances: TransactionTokenBalancesSet,
|
2020-09-24 07:36:22 -07:00
|
|
|
inner_instructions: Vec<Option<InnerInstructionsList>>,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_logs: Vec<TransactionLogMessages>,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: TransactionStatusSender,
|
|
|
|
) {
|
|
|
|
let slot = bank.slot();
|
|
|
|
if let Err(e) = transaction_status_sender.send(TransactionStatusBatch {
|
|
|
|
bank,
|
|
|
|
transactions: transactions.to_vec(),
|
2020-07-21 13:06:49 -07:00
|
|
|
iteration_order,
|
2019-11-20 15:43:10 -08:00
|
|
|
statuses,
|
2019-12-18 09:56:29 -08:00
|
|
|
balances,
|
2020-12-10 19:25:07 -08:00
|
|
|
token_balances,
|
2020-09-24 07:36:22 -07:00
|
|
|
inner_instructions,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_logs,
|
2019-11-20 15:43:10 -08:00
|
|
|
}) {
|
|
|
|
trace!(
|
|
|
|
"Slot {} transaction_status send batch failed: {:?}",
|
|
|
|
slot,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:15:07 -07:00
|
|
|
// used for tests only
|
2020-01-13 13:13:52 -08:00
|
|
|
pub fn fill_blockstore_slot_with_ticks(
|
|
|
|
blockstore: &Blockstore,
|
2019-10-20 08:54:38 -07:00
|
|
|
ticks_per_slot: u64,
|
|
|
|
slot: u64,
|
|
|
|
parent_slot: u64,
|
|
|
|
last_entry_hash: Hash,
|
|
|
|
) -> Hash {
|
2019-10-31 14:15:07 -07:00
|
|
|
// Only slot 0 can be equal to the parent_slot
|
|
|
|
assert!(slot.saturating_sub(1) >= parent_slot);
|
|
|
|
let num_slots = (slot - parent_slot).max(1);
|
2019-10-31 13:38:50 -07:00
|
|
|
let entries = create_ticks(num_slots * ticks_per_slot, 0, last_entry_hash);
|
2019-10-20 08:54:38 -07:00
|
|
|
let last_entry_hash = entries.last().unwrap().hash;
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore
|
2019-10-20 08:54:38 -07:00
|
|
|
.write_entries(
|
|
|
|
slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot,
|
|
|
|
Some(parent_slot),
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-10-20 08:54:38 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
last_entry_hash
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:26:21 -08:00
|
|
|
#[cfg(test)]
|
2019-05-20 23:09:00 -07:00
|
|
|
pub mod tests {
|
2019-02-16 02:26:21 -08:00
|
|
|
use super::*;
|
2019-11-20 15:43:10 -08:00
|
|
|
use crate::{
|
|
|
|
entry::{create_ticks, next_entry, next_entry_mut},
|
|
|
|
genesis_utils::{
|
|
|
|
create_genesis_config, create_genesis_config_with_leader, GenesisConfigInfo,
|
|
|
|
},
|
2019-05-22 20:39:00 -07:00
|
|
|
};
|
2020-07-29 23:17:40 -07:00
|
|
|
use crossbeam_channel::unbounded;
|
2019-10-20 08:54:38 -07:00
|
|
|
use matches::assert_matches;
|
2019-07-16 22:04:40 -07:00
|
|
|
use rand::{thread_rng, Rng};
|
2020-07-29 23:17:40 -07:00
|
|
|
use solana_runtime::genesis_utils::{
|
2020-08-20 21:56:25 -07:00
|
|
|
self, create_genesis_config_with_vote_accounts, ValidatorVoteKeypairs,
|
2020-07-29 23:17:40 -07:00
|
|
|
};
|
2019-10-08 22:34:26 -07:00
|
|
|
use solana_sdk::{
|
2020-11-30 09:18:33 -08:00
|
|
|
account::Account,
|
2019-10-08 22:34:26 -07:00
|
|
|
epoch_schedule::EpochSchedule,
|
|
|
|
hash::Hash,
|
|
|
|
pubkey::Pubkey,
|
2020-02-20 13:28:55 -08:00
|
|
|
signature::{Keypair, Signer},
|
2020-08-06 15:04:43 -07:00
|
|
|
system_instruction::SystemError,
|
2019-10-08 22:34:26 -07:00
|
|
|
system_transaction,
|
|
|
|
transaction::{Transaction, TransactionError},
|
|
|
|
};
|
2020-09-17 21:33:08 -07:00
|
|
|
use solana_vote_program::{
|
|
|
|
self,
|
2020-11-30 09:18:33 -08:00
|
|
|
vote_state::{VoteState, VoteStateVersions, MAX_LOCKOUT_HISTORY},
|
2020-09-17 21:33:08 -07:00
|
|
|
vote_transaction,
|
|
|
|
};
|
2020-07-29 23:17:40 -07:00
|
|
|
use std::{collections::BTreeSet, sync::RwLock};
|
2020-08-20 21:56:25 -07:00
|
|
|
use trees::tr;
|
2019-02-16 02:26:21 -08:00
|
|
|
|
2019-10-31 13:38:50 -07:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_missing_hashes() {
|
2019-10-31 13:38:50 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let hashes_per_tick = 2;
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
mut genesis_config, ..
|
|
|
|
} = create_genesis_config(10_000);
|
|
|
|
genesis_config.poh_config.hashes_per_tick = Some(hashes_per_tick);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-10-31 13:38:50 -07:00
|
|
|
|
|
|
|
let parent_slot = 0;
|
|
|
|
let slot = 1;
|
|
|
|
let entries = create_ticks(ticks_per_slot, hashes_per_tick - 1, blockhash);
|
2019-12-09 14:49:19 -08:00
|
|
|
assert_matches!(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.write_entries(
|
2019-10-31 13:38:50 -07:00
|
|
|
slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot,
|
|
|
|
Some(parent_slot),
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-12-09 14:49:19 -08:00
|
|
|
),
|
|
|
|
Ok(_)
|
|
|
|
);
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2020-01-10 12:16:44 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2020-01-10 12:16:44 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]);
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_invalid_slot_tick_count() {
|
2019-10-31 13:38:50 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-10-31 13:38:50 -07:00
|
|
|
|
|
|
|
// Create a new ledger with slot 0 full of ticks
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-10-31 13:38:50 -07:00
|
|
|
|
|
|
|
// Write slot 1 with one tick missing
|
|
|
|
let parent_slot = 0;
|
|
|
|
let slot = 1;
|
|
|
|
let entries = create_ticks(ticks_per_slot - 1, 0, blockhash);
|
2019-12-09 14:49:19 -08:00
|
|
|
assert_matches!(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.write_entries(
|
2019-10-31 13:38:50 -07:00
|
|
|
slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot,
|
|
|
|
Some(parent_slot),
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-12-09 14:49:19 -08:00
|
|
|
),
|
|
|
|
Ok(_)
|
|
|
|
);
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
// Should return slot 0, the last slot on the fork that is valid
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2020-01-10 12:16:44 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2020-01-10 12:16:44 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]);
|
2019-12-09 14:49:19 -08:00
|
|
|
|
|
|
|
// Write slot 2 fully
|
|
|
|
let _last_slot2_entry_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 2, 0, blockhash);
|
2019-12-09 14:49:19 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2019-12-09 14:49:19 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2019-12-09 14:49:19 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// One valid fork, one bad fork. process_blockstore() should only return the valid fork
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0, 2]);
|
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 2);
|
|
|
|
assert_eq!(bank_forks.root(), 0);
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_slot_with_trailing_entry() {
|
2019-10-31 13:38:50 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
2019-10-31 13:38:50 -07:00
|
|
|
mint_keypair,
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config,
|
2019-10-31 13:38:50 -07:00
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-10-31 13:38:50 -07:00
|
|
|
|
|
|
|
let mut entries = create_ticks(ticks_per_slot, 0, blockhash);
|
|
|
|
let trailing_entry = {
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 1, blockhash);
|
|
|
|
next_entry(&blockhash, 1, vec![tx])
|
|
|
|
};
|
|
|
|
entries.push(trailing_entry);
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Tricks blockstore into writing the trailing entry by lying that there is one more tick
|
2019-10-31 13:38:50 -07:00
|
|
|
// per slot.
|
|
|
|
let parent_slot = 0;
|
|
|
|
let slot = 1;
|
2019-12-09 14:49:19 -08:00
|
|
|
assert_matches!(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.write_entries(
|
2019-10-31 13:38:50 -07:00
|
|
|
slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot + 1,
|
|
|
|
Some(parent_slot),
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-12-09 14:49:19 -08:00
|
|
|
),
|
|
|
|
Ok(_)
|
|
|
|
);
|
2019-10-31 13:38:50 -07:00
|
|
|
|
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-31 13:38:50 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]);
|
2019-10-31 13:38:50 -07:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:18:24 -08:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_incomplete_slot() {
|
2019-02-26 09:18:24 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-02-26 09:18:24 -08:00
|
|
|
|
|
|
|
/*
|
2020-01-13 13:13:52 -08:00
|
|
|
Build a blockstore in the ledger with the following fork structure:
|
2019-02-26 09:18:24 -08:00
|
|
|
|
2019-03-04 19:22:23 -08:00
|
|
|
slot 0 (all ticks)
|
2019-02-26 09:18:24 -08:00
|
|
|
|
|
2019-03-04 19:22:23 -08:00
|
|
|
slot 1 (all ticks but one)
|
2019-02-26 16:35:00 -08:00
|
|
|
|
|
2019-03-04 19:22:23 -08:00
|
|
|
slot 2 (all ticks)
|
2019-02-26 09:18:24 -08:00
|
|
|
|
2019-02-26 16:35:00 -08:00
|
|
|
where slot 1 is incomplete (missing 1 tick at the end)
|
2019-02-26 09:18:24 -08:00
|
|
|
*/
|
|
|
|
|
2019-02-26 16:35:00 -08:00
|
|
|
// Create a new ledger with slot 0 full of ticks
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, mut blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-02-26 16:35:00 -08:00
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-02-26 09:18:24 -08:00
|
|
|
|
2019-02-26 16:35:00 -08:00
|
|
|
// Write slot 1
|
|
|
|
// slot 1, points at slot 0. Missing one tick
|
|
|
|
{
|
|
|
|
let parent_slot = 0;
|
|
|
|
let slot = 1;
|
2019-10-31 13:38:50 -07:00
|
|
|
let mut entries = create_ticks(ticks_per_slot, 0, blockhash);
|
2019-03-02 10:25:16 -08:00
|
|
|
blockhash = entries.last().unwrap().hash;
|
2019-02-26 16:35:00 -08:00
|
|
|
|
2019-03-04 19:22:23 -08:00
|
|
|
// throw away last one
|
2019-02-26 16:35:00 -08:00
|
|
|
entries.pop();
|
|
|
|
|
2019-12-09 14:49:19 -08:00
|
|
|
assert_matches!(
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.write_entries(
|
2019-09-03 21:32:51 -07:00
|
|
|
slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot,
|
|
|
|
Some(parent_slot),
|
|
|
|
false,
|
|
|
|
&Arc::new(Keypair::new()),
|
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-12-09 14:49:19 -08:00
|
|
|
),
|
|
|
|
Ok(_)
|
|
|
|
);
|
2019-02-26 16:35:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// slot 2, points at slot 1
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 2, 1, blockhash);
|
2019-02-26 09:18:24 -08:00
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-05-15 09:35:43 -07:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-02-26 09:18:24 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]); // slot 1 isn't "full", we stop at slot zero
|
2020-01-10 12:16:44 -08:00
|
|
|
|
2020-01-14 17:15:26 -08:00
|
|
|
/* Add a complete slot such that the store looks like:
|
2020-01-10 12:16:44 -08:00
|
|
|
|
|
|
|
slot 0 (all ticks)
|
|
|
|
/ \
|
|
|
|
slot 1 (all ticks but one) slot 3 (all ticks)
|
|
|
|
|
|
|
|
|
slot 2 (all ticks)
|
|
|
|
*/
|
|
|
|
let opts = ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 3, 0, blockhash);
|
2020-01-10 12:16:44 -08:00
|
|
|
// Slot 0 should not show up in the ending bank_forks_info
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2020-01-10 12:16:44 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
// slot 1 isn't "full", we stop at slot zero
|
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0, 3]);
|
2019-02-26 09:18:24 -08:00
|
|
|
}
|
|
|
|
|
2019-04-29 12:29:14 -07:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_two_forks_and_squash() {
|
2019-04-29 12:29:14 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
// Create a new ledger with slot 0 full of ticks
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-04-29 12:29:14 -07:00
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
|
|
|
let mut last_entry_hash = blockhash;
|
|
|
|
|
|
|
|
/*
|
2020-01-13 13:13:52 -08:00
|
|
|
Build a blockstore in the ledger with the following fork structure:
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
slot 0
|
|
|
|
|
|
|
|
|
slot 1
|
|
|
|
/ \
|
|
|
|
slot 2 |
|
|
|
|
/ |
|
|
|
|
slot 3 |
|
|
|
|
|
|
|
|
|
slot 4 <-- set_root(true)
|
|
|
|
|
|
|
|
*/
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
// Fork 1, ending at slot 3
|
|
|
|
let last_slot1_entry_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 1, 0, last_entry_hash);
|
|
|
|
last_entry_hash = fill_blockstore_slot_with_ticks(
|
|
|
|
&blockstore,
|
|
|
|
ticks_per_slot,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
last_slot1_entry_hash,
|
|
|
|
);
|
2019-04-29 12:29:14 -07:00
|
|
|
let last_fork1_entry_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 3, 2, last_entry_hash);
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
// Fork 2, ending at slot 4
|
2020-01-13 13:13:52 -08:00
|
|
|
let last_fork2_entry_hash = fill_blockstore_slot_with_ticks(
|
|
|
|
&blockstore,
|
|
|
|
ticks_per_slot,
|
|
|
|
4,
|
|
|
|
1,
|
|
|
|
last_slot1_entry_hash,
|
|
|
|
);
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
info!("last_fork1_entry.hash: {:?}", last_fork1_entry_hash);
|
|
|
|
info!("last_fork2_entry.hash: {:?}", last_fork2_entry_hash);
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.set_roots(&[0, 1, 4]).unwrap();
|
2019-04-29 12:29:14 -07:00
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-04-29 12:29:14 -07:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
// One fork, other one is ignored b/c not a descendant of the root
|
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![4]);
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
assert!(&bank_forks[4]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
2020-05-15 09:35:43 -07:00
|
|
|
.next()
|
|
|
|
.is_none());
|
2019-04-29 12:29:14 -07:00
|
|
|
|
|
|
|
// Ensure bank_forks holds the right banks
|
2020-05-06 08:24:59 -07:00
|
|
|
verify_fork_infos(&bank_forks);
|
2019-05-07 23:34:10 -07:00
|
|
|
|
|
|
|
assert_eq!(bank_forks.root(), 4);
|
2019-04-29 12:29:14 -07:00
|
|
|
}
|
|
|
|
|
2019-02-19 19:40:23 -08:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_two_forks() {
|
2019-02-19 19:40:23 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-02-19 19:40:23 -08:00
|
|
|
|
|
|
|
// Create a new ledger with slot 0 full of ticks
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-02-19 19:40:23 -08:00
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
2019-03-02 10:25:16 -08:00
|
|
|
let mut last_entry_hash = blockhash;
|
2019-02-19 19:40:23 -08:00
|
|
|
|
|
|
|
/*
|
2020-01-13 13:13:52 -08:00
|
|
|
Build a blockstore in the ledger with the following fork structure:
|
2019-02-19 19:40:23 -08:00
|
|
|
|
|
|
|
slot 0
|
|
|
|
|
|
2019-04-15 13:12:28 -07:00
|
|
|
slot 1 <-- set_root(true)
|
2019-02-19 19:40:23 -08:00
|
|
|
/ \
|
|
|
|
slot 2 |
|
|
|
|
/ |
|
|
|
|
slot 3 |
|
|
|
|
|
|
|
|
|
slot 4
|
|
|
|
|
|
|
|
*/
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-02-19 19:40:23 -08:00
|
|
|
|
|
|
|
// Fork 1, ending at slot 3
|
2019-03-01 08:57:42 -08:00
|
|
|
let last_slot1_entry_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 1, 0, last_entry_hash);
|
|
|
|
last_entry_hash = fill_blockstore_slot_with_ticks(
|
|
|
|
&blockstore,
|
|
|
|
ticks_per_slot,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
last_slot1_entry_hash,
|
|
|
|
);
|
2019-03-01 08:57:42 -08:00
|
|
|
let last_fork1_entry_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 3, 2, last_entry_hash);
|
2019-02-19 19:40:23 -08:00
|
|
|
|
|
|
|
// Fork 2, ending at slot 4
|
2020-01-13 13:13:52 -08:00
|
|
|
let last_fork2_entry_hash = fill_blockstore_slot_with_ticks(
|
|
|
|
&blockstore,
|
|
|
|
ticks_per_slot,
|
|
|
|
4,
|
|
|
|
1,
|
|
|
|
last_slot1_entry_hash,
|
|
|
|
);
|
2019-02-19 19:40:23 -08:00
|
|
|
|
2019-03-01 08:57:42 -08:00
|
|
|
info!("last_fork1_entry.hash: {:?}", last_fork1_entry_hash);
|
|
|
|
info!("last_fork2_entry.hash: {:?}", last_fork2_entry_hash);
|
2019-02-19 19:40:23 -08:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.set_roots(&[0, 1]).unwrap();
|
2019-04-15 13:12:28 -07:00
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-02-19 19:40:23 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![1, 2, 3, 4]);
|
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 4);
|
|
|
|
assert_eq!(bank_forks.root(), 1);
|
|
|
|
|
2019-04-15 13:12:28 -07:00
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[3]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[2, 1]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[4]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[1]
|
|
|
|
);
|
2019-02-20 15:42:35 -08:00
|
|
|
|
2019-05-07 23:34:10 -07:00
|
|
|
assert_eq!(bank_forks.root(), 1);
|
|
|
|
|
2019-04-15 13:12:28 -07:00
|
|
|
// Ensure bank_forks holds the right banks
|
2020-05-06 08:24:59 -07:00
|
|
|
verify_fork_infos(&bank_forks);
|
2019-02-19 19:40:23 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 12:38:26 -08:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_dead_slot() {
|
2019-11-12 12:38:26 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
slot 0
|
|
|
|
|
|
|
|
|
slot 1
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
slot 2 (dead) \
|
|
|
|
\
|
|
|
|
slot 3
|
|
|
|
*/
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-11-12 12:38:26 -08:00
|
|
|
let slot1_blockhash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 1, 0, blockhash);
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 2, 1, slot1_blockhash);
|
|
|
|
blockstore.set_dead_slot(2).unwrap();
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 3, 1, slot1_blockhash);
|
2019-11-12 12:38:26 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2019-12-05 18:41:29 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2019-12-05 18:41:29 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-12 12:38:26 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0, 1, 3]);
|
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 3);
|
2019-11-12 12:38:26 -08:00
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[3]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[1, 0]
|
|
|
|
);
|
2020-05-06 08:24:59 -07:00
|
|
|
verify_fork_infos(&bank_forks);
|
2019-11-12 12:38:26 -08:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_with_dead_child() {
|
2020-01-10 12:16:44 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
slot 0
|
|
|
|
|
|
|
|
|
slot 1
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
slot 2 \
|
|
|
|
/ \
|
|
|
|
slot 4 (dead) slot 3
|
|
|
|
*/
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2020-01-10 12:16:44 -08:00
|
|
|
let slot1_blockhash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 1, 0, blockhash);
|
2020-01-10 12:16:44 -08:00
|
|
|
let slot2_blockhash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 2, 1, slot1_blockhash);
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 4, 2, slot2_blockhash);
|
|
|
|
blockstore.set_dead_slot(4).unwrap();
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 3, 1, slot1_blockhash);
|
2020-01-10 12:16:44 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2020-01-10 12:16:44 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2020-01-10 12:16:44 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Should see the parent of the dead child
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0, 1, 2, 3]);
|
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 3);
|
|
|
|
|
2020-01-10 12:16:44 -08:00
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[3]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[1, 0]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[2]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[1, 0]
|
|
|
|
);
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 3);
|
|
|
|
verify_fork_infos(&bank_forks);
|
2020-01-10 12:16:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_root_with_all_dead_children() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
slot 0
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
slot 1 (dead) slot 2 (dead)
|
|
|
|
*/
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 1, 0, blockhash);
|
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, 2, 0, blockhash);
|
|
|
|
blockstore.set_dead_slot(1).unwrap();
|
|
|
|
blockstore.set_dead_slot(2).unwrap();
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) = process_blockstore(
|
2020-01-10 12:16:44 -08:00
|
|
|
&genesis_config,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2020-01-10 12:16:44 -08:00
|
|
|
Vec::new(),
|
|
|
|
ProcessOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Should see only the parent of the dead children
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]);
|
|
|
|
verify_fork_infos(&bank_forks);
|
2020-01-10 12:16:44 -08:00
|
|
|
}
|
|
|
|
|
2019-05-20 19:04:18 -07:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_epoch_boundary_root() {
|
2019-05-20 19:04:18 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot;
|
2019-05-20 19:04:18 -07:00
|
|
|
|
|
|
|
// Create a new ledger with slot 0 full of ticks
|
2019-11-08 20:56:57 -08:00
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-05-20 19:04:18 -07:00
|
|
|
let mut last_entry_hash = blockhash;
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-05-20 19:04:18 -07:00
|
|
|
|
2020-08-20 21:56:25 -07:00
|
|
|
// Let `last_slot` be the number of slots in the first two epochs
|
2019-12-05 18:41:29 -08:00
|
|
|
let epoch_schedule = get_epoch_schedule(&genesis_config, Vec::new());
|
2019-05-20 19:04:18 -07:00
|
|
|
let last_slot = epoch_schedule.get_last_slot_in_epoch(1);
|
|
|
|
|
2020-08-20 21:56:25 -07:00
|
|
|
// Create a single chain of slots with all indexes in the range [0, v + 1]
|
2019-05-20 19:04:18 -07:00
|
|
|
for i in 1..=last_slot + 1 {
|
2020-01-13 13:13:52 -08:00
|
|
|
last_entry_hash = fill_blockstore_slot_with_ticks(
|
|
|
|
&blockstore,
|
2019-05-20 19:04:18 -07:00
|
|
|
ticks_per_slot,
|
|
|
|
i,
|
|
|
|
i - 1,
|
|
|
|
last_entry_hash,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a root on the last slot of the last confirmed epoch
|
2019-05-29 09:43:22 -07:00
|
|
|
let rooted_slots: Vec<_> = (0..=last_slot).collect();
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.set_roots(&rooted_slots).unwrap();
|
2019-05-20 19:04:18 -07:00
|
|
|
|
2020-06-17 20:54:52 -07:00
|
|
|
// Set a root on the next slot of the confirmed epoch
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.set_roots(&[last_slot + 1]).unwrap();
|
2019-05-20 19:04:18 -07:00
|
|
|
|
|
|
|
// Check that we can properly restart the ledger / leader scheduler doesn't fail
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-05-20 19:04:18 -07:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
// There is one fork, head is last_slot + 1
|
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![last_slot + 1]);
|
2019-05-20 19:04:18 -07:00
|
|
|
|
|
|
|
// The latest root should have purged all its parents
|
|
|
|
assert!(&bank_forks[last_slot + 1]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
2020-05-15 09:35:43 -07:00
|
|
|
.next()
|
|
|
|
.is_none());
|
2019-05-20 19:04:18 -07:00
|
|
|
}
|
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
#[test]
|
|
|
|
fn test_first_err() {
|
|
|
|
assert_eq!(first_err(&[Ok(())]), Ok(()));
|
|
|
|
assert_eq!(
|
2019-03-13 12:58:44 -07:00
|
|
|
first_err(&[Ok(()), Err(TransactionError::DuplicateSignature)]),
|
|
|
|
Err(TransactionError::DuplicateSignature)
|
2019-02-16 13:17:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
first_err(&[
|
|
|
|
Ok(()),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::DuplicateSignature),
|
|
|
|
Err(TransactionError::AccountInUse)
|
2019-02-16 13:17:37 -08:00
|
|
|
]),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::DuplicateSignature)
|
2019-02-16 13:17:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
first_err(&[
|
|
|
|
Ok(()),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountInUse),
|
|
|
|
Err(TransactionError::DuplicateSignature)
|
2019-02-16 13:17:37 -08:00
|
|
|
]),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountInUse)
|
2019-02-16 13:17:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
first_err(&[
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountInUse),
|
2019-02-16 13:17:37 -08:00
|
|
|
Ok(()),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::DuplicateSignature)
|
2019-02-16 13:17:37 -08:00
|
|
|
]),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountInUse)
|
2019-02-16 13:17:37 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_empty_entry_is_registered() {
|
2019-03-01 14:52:27 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(2);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
let keypair = Keypair::new();
|
2019-11-08 20:56:57 -08:00
|
|
|
let slot_entries = create_ticks(genesis_config.ticks_per_slot, 1, genesis_config.hash());
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-01 14:52:27 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair.pubkey(),
|
2019-03-01 14:52:27 -08:00
|
|
|
1,
|
|
|
|
slot_entries.last().unwrap().hash,
|
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
|
|
|
|
// First, ensure the TX is rejected because of the unregistered last ID
|
|
|
|
assert_eq!(
|
|
|
|
bank.process_transaction(&tx),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::BlockhashNotFound)
|
2019-02-16 13:17:37 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Now ensure the TX is accepted despite pointing to the ID of an empty entry.
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &slot_entries, true, None, None).unwrap();
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:42:35 -08:00
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_simple() {
|
2019-03-01 14:52:27 -08:00
|
|
|
solana_logger::setup();
|
2020-10-19 12:12:08 -07:00
|
|
|
let leader_pubkey = solana_sdk::pubkey::new_rand();
|
2019-05-07 11:16:22 -07:00
|
|
|
let mint = 100;
|
2019-10-31 13:38:50 -07:00
|
|
|
let hashes_per_tick = 10;
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
mut genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config_with_leader(mint, &leader_pubkey, 50);
|
|
|
|
genesis_config.poh_config.hashes_per_tick = Some(hashes_per_tick);
|
|
|
|
let (ledger_path, mut last_entry_hash) = create_new_tmp_ledger!(&genesis_config);
|
2019-02-20 15:42:35 -08:00
|
|
|
debug!("ledger_path: {:?}", ledger_path);
|
2019-02-16 02:26:21 -08:00
|
|
|
|
2019-05-07 11:16:22 -07:00
|
|
|
let deducted_from_mint = 3;
|
2019-02-20 15:42:35 -08:00
|
|
|
let mut entries = vec![];
|
2019-11-08 20:56:57 -08:00
|
|
|
let blockhash = genesis_config.hash();
|
2019-05-07 11:16:22 -07:00
|
|
|
for _ in 0..deducted_from_mint {
|
2019-02-16 02:26:21 -08:00
|
|
|
// Transfer one token from the mint to a random account
|
|
|
|
let keypair = Keypair::new();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 1, blockhash);
|
2019-10-31 13:38:50 -07:00
|
|
|
let entry = next_entry_mut(&mut last_entry_hash, 1, vec![tx]);
|
2019-02-16 02:26:21 -08:00
|
|
|
entries.push(entry);
|
|
|
|
|
|
|
|
// Add a second Transaction that will produce a
|
2019-03-18 09:05:03 -07:00
|
|
|
// InstructionError<0, ResultWithNegativeLamports> error when processed
|
2019-02-16 02:26:21 -08:00
|
|
|
let keypair2 = Keypair::new();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::transfer(&mint_keypair, &keypair2.pubkey(), 101, blockhash);
|
2019-10-31 13:38:50 -07:00
|
|
|
let entry = next_entry_mut(&mut last_entry_hash, 1, vec![tx]);
|
2019-02-16 02:26:21 -08:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
2019-10-31 13:38:50 -07:00
|
|
|
let remaining_hashes = hashes_per_tick - entries.len() as u64;
|
|
|
|
let tick_entry = next_entry_mut(&mut last_entry_hash, remaining_hashes, vec![]);
|
|
|
|
entries.push(tick_entry);
|
|
|
|
|
2019-02-26 16:35:00 -08:00
|
|
|
// Fill up the rest of slot 1 with ticks
|
2019-10-31 13:38:50 -07:00
|
|
|
entries.extend(create_ticks(
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.ticks_per_slot - 1,
|
|
|
|
genesis_config.poh_config.hashes_per_tick.unwrap(),
|
2019-10-31 13:38:50 -07:00
|
|
|
last_entry_hash,
|
|
|
|
));
|
2019-10-08 00:42:51 -07:00
|
|
|
let last_blockhash = entries.last().unwrap().hash;
|
2019-10-31 13:38:50 -07:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
|
|
|
blockstore
|
2019-09-03 21:32:51 -07:00
|
|
|
.write_entries(
|
2019-08-28 22:34:47 -07:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.ticks_per_slot,
|
2019-08-28 22:34:47 -07:00
|
|
|
None,
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
2019-10-08 00:42:51 -07:00
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-08-28 22:34:47 -07:00
|
|
|
)
|
2019-03-14 15:18:37 -07:00
|
|
|
.unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-02-20 15:42:35 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0, 1]);
|
2019-05-07 23:34:10 -07:00
|
|
|
assert_eq!(bank_forks.root(), 0);
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 1);
|
2019-02-20 15:42:35 -08:00
|
|
|
|
2019-02-28 10:57:58 -08:00
|
|
|
let bank = bank_forks[1].clone();
|
2019-05-07 11:16:22 -07:00
|
|
|
assert_eq!(
|
|
|
|
bank.get_balance(&mint_keypair.pubkey()),
|
|
|
|
mint - deducted_from_mint
|
|
|
|
);
|
2019-11-08 20:56:57 -08:00
|
|
|
assert_eq!(bank.tick_height(), 2 * genesis_config.ticks_per_slot);
|
2019-10-08 00:42:51 -07:00
|
|
|
assert_eq!(bank.last_blockhash(), last_blockhash);
|
2019-02-16 02:26:21 -08:00
|
|
|
}
|
2019-02-16 13:17:37 -08:00
|
|
|
|
2019-02-28 12:09:19 -08:00
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_with_one_tick_per_slot() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
mut genesis_config, ..
|
|
|
|
} = create_genesis_config(123);
|
|
|
|
genesis_config.ticks_per_slot = 1;
|
|
|
|
let (ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-02-28 12:09:19 -08:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-02-28 12:09:19 -08:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![0]);
|
2019-02-28 12:09:19 -08:00
|
|
|
let bank = bank_forks[0].clone();
|
2019-10-21 07:51:02 -07:00
|
|
|
assert_eq!(bank.tick_height(), 1);
|
2019-02-28 12:09:19 -08:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_options_override_threads() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(123);
|
|
|
|
let (ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-10-08 14:58:49 -07:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
|
|
|
override_num_threads: Some(1),
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
PAR_THREAD_POOL.with(|pool| {
|
|
|
|
assert_eq!(pool.borrow().current_num_threads(), 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_options_full_leader_cache() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(123);
|
|
|
|
let (ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2019-10-08 14:58:49 -07:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
|
|
|
full_leader_cache: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-05-06 08:24:59 -07:00
|
|
|
let (_bank_forks, leader_schedule) =
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(leader_schedule.max_schedules(), std::usize::MAX);
|
2019-10-08 14:58:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_options_entry_callback() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-10-08 14:58:49 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(100);
|
|
|
|
let (ledger_path, last_entry_hash) = create_new_tmp_ledger!(&genesis_config);
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore =
|
|
|
|
Blockstore::open(&ledger_path).expect("Expected to successfully open database ledger");
|
2019-11-08 20:56:57 -08:00
|
|
|
let blockhash = genesis_config.hash();
|
2019-10-08 14:58:49 -07:00
|
|
|
let keypairs = [Keypair::new(), Keypair::new(), Keypair::new()];
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &keypairs[0].pubkey(), 1, blockhash);
|
2019-10-08 14:58:49 -07:00
|
|
|
let entry_1 = next_entry(&last_entry_hash, 1, vec![tx]);
|
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &keypairs[1].pubkey(), 1, blockhash);
|
2019-10-08 14:58:49 -07:00
|
|
|
let entry_2 = next_entry(&entry_1.hash, 1, vec![tx]);
|
|
|
|
|
|
|
|
let mut entries = vec![entry_1, entry_2];
|
2019-10-31 13:38:50 -07:00
|
|
|
entries.extend(create_ticks(
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.ticks_per_slot,
|
2019-10-31 13:38:50 -07:00
|
|
|
0,
|
|
|
|
last_entry_hash,
|
|
|
|
));
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore
|
2019-10-08 14:58:49 -07:00
|
|
|
.write_entries(
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.ticks_per_slot,
|
2019-10-08 14:58:49 -07:00
|
|
|
None,
|
|
|
|
true,
|
|
|
|
&Arc::new(Keypair::new()),
|
2019-10-08 17:38:05 -07:00
|
|
|
entries,
|
2019-11-18 18:05:02 -08:00
|
|
|
0,
|
2019-10-08 14:58:49 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let callback_counter: Arc<RwLock<usize>> = Arc::default();
|
|
|
|
let entry_callback = {
|
|
|
|
let counter = callback_counter.clone();
|
|
|
|
let pubkeys: Vec<Pubkey> = keypairs.iter().map(|k| k.pubkey()).collect();
|
|
|
|
Arc::new(move |bank: &Bank| {
|
|
|
|
let mut counter = counter.write().unwrap();
|
|
|
|
assert_eq!(bank.get_balance(&pubkeys[*counter]), 1);
|
|
|
|
assert_eq!(bank.get_balance(&pubkeys[*counter + 1]), 0);
|
|
|
|
*counter += 1;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let opts = ProcessOptions {
|
|
|
|
override_num_threads: Some(1),
|
|
|
|
entry_callback: Some(entry_callback),
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-01-13 13:13:52 -08:00
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
2019-10-08 14:58:49 -07:00
|
|
|
assert_eq!(*callback_counter.write().unwrap(), 2);
|
|
|
|
}
|
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
#[test]
|
2019-03-12 16:46:41 -07:00
|
|
|
fn test_process_entries_tick() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
|
|
|
|
// ensure bank can process a tick
|
2019-03-01 14:52:27 -08:00
|
|
|
assert_eq!(bank.tick_height(), 0);
|
2019-11-08 20:56:57 -08:00
|
|
|
let tick = next_entry(&genesis_config.hash(), 1, vec![]);
|
2020-07-29 23:17:40 -07:00
|
|
|
assert_eq!(process_entries(&bank, &[tick], true, None, None), Ok(()));
|
2019-03-01 14:52:27 -08:00
|
|
|
assert_eq!(bank.tick_height(), 1);
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-12 16:46:41 -07:00
|
|
|
fn test_process_entries_2_entries_collision() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = bank.last_blockhash();
|
2019-02-16 13:17:37 -08:00
|
|
|
|
|
|
|
// ensure bank can process 2 entries that have a common account and no tick is registered
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair1.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
2,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-03-02 10:25:16 -08:00
|
|
|
let entry_1 = next_entry(&blockhash, 1, vec![tx]);
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair2.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
2,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-03-01 08:57:42 -08:00
|
|
|
let entry_2 = next_entry(&entry_1.hash, 1, vec![tx]);
|
2019-11-20 15:43:10 -08:00
|
|
|
assert_eq!(
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &[entry_1, entry_2], true, None, None),
|
2019-11-20 15:43:10 -08:00
|
|
|
Ok(())
|
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.get_balance(&keypair1.pubkey()), 2);
|
|
|
|
assert_eq!(bank.get_balance(&keypair2.pubkey()), 2);
|
2019-03-02 10:25:16 -08:00
|
|
|
assert_eq!(bank.last_blockhash(), blockhash);
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-12 16:46:41 -07:00
|
|
|
fn test_process_entries_2_txes_collision() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
|
|
|
|
// fund: put 4 in each of 1 and 2
|
2019-03-27 04:59:30 -07:00
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair1.pubkey()), Ok(_));
|
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair2.pubkey()), Ok(_));
|
2019-02-16 13:17:37 -08:00
|
|
|
|
|
|
|
// construct an Entry whose 2nd transaction would cause a lock conflict with previous entry
|
|
|
|
let entry_1_to_mint = next_entry(
|
2019-03-02 10:25:16 -08:00
|
|
|
&bank.last_blockhash(),
|
2019-02-16 13:17:37 -08:00
|
|
|
1,
|
2019-10-23 22:01:22 -07:00
|
|
|
vec![system_transaction::transfer(
|
2019-02-16 13:17:37 -08:00
|
|
|
&keypair1,
|
2019-03-09 19:28:43 -08:00
|
|
|
&mint_keypair.pubkey(),
|
2019-02-16 13:17:37 -08:00
|
|
|
1,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-02-16 13:17:37 -08:00
|
|
|
)],
|
|
|
|
);
|
|
|
|
|
|
|
|
let entry_2_to_3_mint_to_1 = next_entry(
|
2019-03-01 08:57:42 -08:00
|
|
|
&entry_1_to_mint.hash,
|
2019-02-16 13:17:37 -08:00
|
|
|
1,
|
|
|
|
vec![
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&keypair2,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair3.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
2,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
), // should be fine
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-02-16 13:17:37 -08:00
|
|
|
&keypair1,
|
2019-03-09 19:28:43 -08:00
|
|
|
&mint_keypair.pubkey(),
|
2019-02-16 13:17:37 -08:00
|
|
|
2,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-02-16 13:17:37 -08:00
|
|
|
), // will collide
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-20 15:43:10 -08:00
|
|
|
process_entries(
|
|
|
|
&bank,
|
|
|
|
&[entry_1_to_mint, entry_2_to_3_mint_to_1],
|
|
|
|
false,
|
2020-07-29 23:17:40 -07:00
|
|
|
None,
|
|
|
|
None,
|
2019-11-20 15:43:10 -08:00
|
|
|
),
|
2019-02-16 13:17:37 -08:00
|
|
|
Ok(())
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(bank.get_balance(&keypair1.pubkey()), 1);
|
|
|
|
assert_eq!(bank.get_balance(&keypair2.pubkey()), 2);
|
|
|
|
assert_eq!(bank.get_balance(&keypair3.pubkey()), 2);
|
|
|
|
}
|
|
|
|
|
2019-04-02 03:55:42 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_entries_2_txes_collision_and_error() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-04-02 03:55:42 -07:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
let keypair4 = Keypair::new();
|
|
|
|
|
|
|
|
// fund: put 4 in each of 1 and 2
|
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair1.pubkey()), Ok(_));
|
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair2.pubkey()), Ok(_));
|
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair4.pubkey()), Ok(_));
|
|
|
|
|
|
|
|
// construct an Entry whose 2nd transaction would cause a lock conflict with previous entry
|
|
|
|
let entry_1_to_mint = next_entry(
|
|
|
|
&bank.last_blockhash(),
|
|
|
|
1,
|
|
|
|
vec![
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-04-02 03:55:42 -07:00
|
|
|
&keypair1,
|
|
|
|
&mint_keypair.pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
),
|
2019-04-03 08:45:57 -07:00
|
|
|
system_transaction::transfer(
|
2019-04-02 03:55:42 -07:00
|
|
|
&keypair4,
|
|
|
|
&keypair4.pubkey(),
|
|
|
|
1,
|
|
|
|
Hash::default(), // Should cause a transaction failure with BlockhashNotFound
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
let entry_2_to_3_mint_to_1 = next_entry(
|
|
|
|
&entry_1_to_mint.hash,
|
|
|
|
1,
|
|
|
|
vec![
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-04-02 03:55:42 -07:00
|
|
|
&keypair2,
|
|
|
|
&keypair3.pubkey(),
|
|
|
|
2,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
), // should be fine
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-04-02 03:55:42 -07:00
|
|
|
&keypair1,
|
|
|
|
&mint_keypair.pubkey(),
|
|
|
|
2,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
), // will collide
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(process_entries(
|
|
|
|
&bank,
|
2019-08-28 08:38:32 -07:00
|
|
|
&[entry_1_to_mint.clone(), entry_2_to_3_mint_to_1.clone()],
|
2019-10-08 14:58:49 -07:00
|
|
|
false,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-07-29 23:17:40 -07:00
|
|
|
None,
|
2019-04-02 03:55:42 -07:00
|
|
|
)
|
|
|
|
.is_err());
|
|
|
|
|
|
|
|
// First transaction in first entry succeeded, so keypair1 lost 1 lamport
|
|
|
|
assert_eq!(bank.get_balance(&keypair1.pubkey()), 3);
|
|
|
|
assert_eq!(bank.get_balance(&keypair2.pubkey()), 4);
|
|
|
|
|
|
|
|
// Check all accounts are unlocked
|
|
|
|
let txs1 = &entry_1_to_mint.transactions[..];
|
|
|
|
let txs2 = &entry_2_to_3_mint_to_1.transactions[..];
|
2019-09-19 10:06:08 -07:00
|
|
|
let batch1 = bank.prepare_batch(txs1, None);
|
|
|
|
for result in batch1.lock_results() {
|
2019-04-02 03:55:42 -07:00
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
// txs1 and txs2 have accounts that conflict, so we must drop txs1 first
|
2019-09-19 10:06:08 -07:00
|
|
|
drop(batch1);
|
|
|
|
let batch2 = bank.prepare_batch(txs2, None);
|
|
|
|
for result in batch2.lock_results() {
|
2019-04-02 03:55:42 -07:00
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 17:35:15 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_entries_2nd_entry_collision_with_self_and_error() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-23 17:35:15 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-05-23 17:35:15 -07:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
|
|
|
|
// fund: put some money in each of 1 and 2
|
|
|
|
assert_matches!(bank.transfer(5, &mint_keypair, &keypair1.pubkey()), Ok(_));
|
|
|
|
assert_matches!(bank.transfer(4, &mint_keypair, &keypair2.pubkey()), Ok(_));
|
|
|
|
|
|
|
|
// 3 entries: first has a transfer, 2nd has a conflict with 1st, 3rd has a conflict with itself
|
|
|
|
let entry_1_to_mint = next_entry(
|
|
|
|
&bank.last_blockhash(),
|
|
|
|
1,
|
|
|
|
vec![system_transaction::transfer(
|
|
|
|
&keypair1,
|
|
|
|
&mint_keypair.pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
)],
|
|
|
|
);
|
|
|
|
// should now be:
|
|
|
|
// keypair1=4
|
|
|
|
// keypair2=4
|
|
|
|
// keypair3=0
|
|
|
|
|
|
|
|
let entry_2_to_3_and_1_to_mint = next_entry(
|
|
|
|
&entry_1_to_mint.hash,
|
|
|
|
1,
|
|
|
|
vec![
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(
|
2019-05-23 17:35:15 -07:00
|
|
|
&keypair2,
|
|
|
|
&keypair3.pubkey(),
|
|
|
|
2,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
), // should be fine
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypair1,
|
|
|
|
&mint_keypair.pubkey(),
|
|
|
|
2,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
), // will collide with predecessor
|
|
|
|
],
|
|
|
|
);
|
|
|
|
// should now be:
|
|
|
|
// keypair1=2
|
|
|
|
// keypair2=2
|
|
|
|
// keypair3=2
|
|
|
|
|
|
|
|
let entry_conflict_itself = next_entry(
|
|
|
|
&entry_2_to_3_and_1_to_mint.hash,
|
|
|
|
1,
|
|
|
|
vec![
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypair1,
|
|
|
|
&keypair3.pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
),
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypair1,
|
|
|
|
&keypair2.pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
), // should be fine
|
|
|
|
],
|
|
|
|
);
|
|
|
|
// would now be:
|
|
|
|
// keypair1=0
|
|
|
|
// keypair2=3
|
|
|
|
// keypair3=3
|
|
|
|
|
|
|
|
assert!(process_entries(
|
|
|
|
&bank,
|
|
|
|
&[
|
2020-05-15 09:35:43 -07:00
|
|
|
entry_1_to_mint,
|
|
|
|
entry_2_to_3_and_1_to_mint,
|
|
|
|
entry_conflict_itself,
|
2019-08-28 08:38:32 -07:00
|
|
|
],
|
2019-10-08 14:58:49 -07:00
|
|
|
false,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-07-29 23:17:40 -07:00
|
|
|
None,
|
2019-05-23 17:35:15 -07:00
|
|
|
)
|
|
|
|
.is_err());
|
|
|
|
|
|
|
|
// last entry should have been aborted before par_execute_entries
|
|
|
|
assert_eq!(bank.get_balance(&keypair1.pubkey()), 2);
|
|
|
|
assert_eq!(bank.get_balance(&keypair2.pubkey()), 2);
|
|
|
|
assert_eq!(bank.get_balance(&keypair3.pubkey()), 2);
|
|
|
|
}
|
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
#[test]
|
2019-03-12 16:46:41 -07:00
|
|
|
fn test_process_entries_2_entries_par() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
let keypair4 = Keypair::new();
|
|
|
|
|
|
|
|
//load accounts
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair1.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
1,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair2.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
1,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
|
|
|
|
|
|
|
// ensure bank can process 2 entries that do not have a common account and no tick is registered
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = bank.last_blockhash();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::transfer(&keypair1, &keypair3.pubkey(), 1, bank.last_blockhash());
|
2019-03-02 10:25:16 -08:00
|
|
|
let entry_1 = next_entry(&blockhash, 1, vec![tx]);
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::transfer(&keypair2, &keypair4.pubkey(), 1, bank.last_blockhash());
|
2019-03-01 08:57:42 -08:00
|
|
|
let entry_2 = next_entry(&entry_1.hash, 1, vec![tx]);
|
2019-11-20 15:43:10 -08:00
|
|
|
assert_eq!(
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &[entry_1, entry_2], true, None, None),
|
2019-11-20 15:43:10 -08:00
|
|
|
Ok(())
|
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.get_balance(&keypair3.pubkey()), 1);
|
|
|
|
assert_eq!(bank.get_balance(&keypair4.pubkey()), 1);
|
2019-03-02 10:25:16 -08:00
|
|
|
assert_eq!(bank.last_blockhash(), blockhash);
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:11:29 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_entry_tx_random_execution_with_error() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-09-17 15:11:29 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1_000_000_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-09-17 15:11:29 -07:00
|
|
|
|
|
|
|
const NUM_TRANSFERS_PER_ENTRY: usize = 8;
|
|
|
|
const NUM_TRANSFERS: usize = NUM_TRANSFERS_PER_ENTRY * 32;
|
|
|
|
// large enough to scramble locks and results
|
|
|
|
|
|
|
|
let keypairs: Vec<_> = (0..NUM_TRANSFERS * 2).map(|_| Keypair::new()).collect();
|
|
|
|
|
|
|
|
// give everybody one lamport
|
|
|
|
for keypair in &keypairs {
|
|
|
|
bank.transfer(1, &mint_keypair, &keypair.pubkey())
|
|
|
|
.expect("funding failed");
|
|
|
|
}
|
|
|
|
let mut hash = bank.last_blockhash();
|
|
|
|
|
2019-11-08 02:27:35 -08:00
|
|
|
let present_account_key = Keypair::new();
|
|
|
|
let present_account = Account::new(1, 10, &Pubkey::default());
|
|
|
|
bank.store_account(&present_account_key.pubkey(), &present_account);
|
|
|
|
|
2019-09-17 15:11:29 -07:00
|
|
|
let entries: Vec<_> = (0..NUM_TRANSFERS)
|
|
|
|
.step_by(NUM_TRANSFERS_PER_ENTRY)
|
|
|
|
.map(|i| {
|
|
|
|
let mut transactions = (0..NUM_TRANSFERS_PER_ENTRY)
|
|
|
|
.map(|j| {
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypairs[i + j],
|
|
|
|
&keypairs[i + j + NUM_TRANSFERS].pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
transactions.push(system_transaction::create_account(
|
|
|
|
&mint_keypair,
|
2019-11-08 02:27:35 -08:00
|
|
|
&present_account_key, // puts a TX error in results
|
2019-09-17 15:11:29 -07:00
|
|
|
bank.last_blockhash(),
|
|
|
|
1,
|
|
|
|
0,
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2019-09-17 15:11:29 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
next_entry_mut(&mut hash, 0, transactions)
|
|
|
|
})
|
|
|
|
.collect();
|
2020-07-29 23:17:40 -07:00
|
|
|
assert_eq!(process_entries(&bank, &entries, true, None, None), Ok(()));
|
2019-09-17 15:11:29 -07:00
|
|
|
}
|
|
|
|
|
2019-08-28 08:38:32 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_entry_tx_random_execution_no_error() {
|
|
|
|
// entropy multiplier should be big enough to provide sufficient entropy
|
|
|
|
// but small enough to not take too much time while executing the test.
|
|
|
|
let entropy_multiplier: usize = 25;
|
|
|
|
let initial_lamports = 100;
|
|
|
|
|
|
|
|
// number of accounts need to be in multiple of 4 for correct
|
|
|
|
// execution of the test.
|
|
|
|
let num_accounts = entropy_multiplier * 4;
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-08-28 08:38:32 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config((num_accounts + 1) as u64 * initial_lamports);
|
2019-08-28 08:38:32 -07:00
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-08-28 08:38:32 -07:00
|
|
|
|
|
|
|
let mut keypairs: Vec<Keypair> = vec![];
|
|
|
|
|
|
|
|
for _ in 0..num_accounts {
|
|
|
|
let keypair = Keypair::new();
|
2019-10-23 22:01:22 -07:00
|
|
|
let create_account_tx = system_transaction::transfer(
|
2019-08-28 08:38:32 -07:00
|
|
|
&mint_keypair,
|
|
|
|
&keypair.pubkey(),
|
|
|
|
0,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
);
|
|
|
|
assert_eq!(bank.process_transaction(&create_account_tx), Ok(()));
|
|
|
|
assert_matches!(
|
|
|
|
bank.transfer(initial_lamports, &mint_keypair, &keypair.pubkey()),
|
|
|
|
Ok(_)
|
|
|
|
);
|
|
|
|
keypairs.push(keypair);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tx_vector: Vec<Transaction> = vec![];
|
|
|
|
|
|
|
|
for i in (0..num_accounts).step_by(4) {
|
|
|
|
tx_vector.append(&mut vec![
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypairs[i + 1],
|
|
|
|
&keypairs[i].pubkey(),
|
|
|
|
initial_lamports,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
),
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypairs[i + 3],
|
|
|
|
&keypairs[i + 2].pubkey(),
|
|
|
|
initial_lamports,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer lamports to each other
|
|
|
|
let entry = next_entry(&bank.last_blockhash(), 1, tx_vector);
|
2020-07-29 23:17:40 -07:00
|
|
|
assert_eq!(process_entries(&bank, &[entry], true, None, None), Ok(()));
|
2019-08-28 08:38:32 -07:00
|
|
|
bank.squash();
|
|
|
|
|
|
|
|
// Even number keypair should have balance of 2 * initial_lamports and
|
|
|
|
// odd number keypair should have balance of 0, which proves
|
|
|
|
// that even in case of random order of execution, overall state remains
|
|
|
|
// consistent.
|
2020-05-15 09:35:43 -07:00
|
|
|
for (i, keypair) in keypairs.iter().enumerate() {
|
2019-08-28 08:38:32 -07:00
|
|
|
if i % 2 == 0 {
|
2020-05-15 09:35:43 -07:00
|
|
|
assert_eq!(bank.get_balance(&keypair.pubkey()), 2 * initial_lamports);
|
2019-08-28 08:38:32 -07:00
|
|
|
} else {
|
2020-05-15 09:35:43 -07:00
|
|
|
assert_eq!(bank.get_balance(&keypair.pubkey()), 0);
|
2019-08-28 08:38:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
#[test]
|
2019-03-12 16:46:41 -07:00
|
|
|
fn test_process_entries_2_entries_tick() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-02-16 13:17:37 -08:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
let keypair4 = Keypair::new();
|
|
|
|
|
|
|
|
//load accounts
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair1.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
1,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(
|
2019-03-02 10:20:10 -08:00
|
|
|
&mint_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&keypair2.pubkey(),
|
2019-03-02 10:20:10 -08:00
|
|
|
1,
|
2019-03-02 10:25:16 -08:00
|
|
|
bank.last_blockhash(),
|
2019-03-02 10:20:10 -08:00
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
while blockhash == bank.last_blockhash() {
|
2019-03-01 14:52:27 -08:00
|
|
|
bank.register_tick(&Hash::default());
|
|
|
|
}
|
2019-02-16 13:17:37 -08:00
|
|
|
|
|
|
|
// ensure bank can process 2 entries that do not have a common account and tick is registered
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&keypair2, &keypair3.pubkey(), 1, blockhash);
|
2019-03-02 10:25:16 -08:00
|
|
|
let entry_1 = next_entry(&blockhash, 1, vec![tx]);
|
2019-03-01 08:57:42 -08:00
|
|
|
let tick = next_entry(&entry_1.hash, 1, vec![]);
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::transfer(&keypair1, &keypair4.pubkey(), 1, bank.last_blockhash());
|
2019-03-01 08:57:42 -08:00
|
|
|
let entry_2 = next_entry(&tick.hash, 1, vec![tx]);
|
2019-02-21 13:37:08 -08:00
|
|
|
assert_eq!(
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &[entry_1, tick, entry_2.clone()], true, None, None),
|
2019-02-21 13:37:08 -08:00
|
|
|
Ok(())
|
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
assert_eq!(bank.get_balance(&keypair3.pubkey()), 1);
|
|
|
|
assert_eq!(bank.get_balance(&keypair4.pubkey()), 1);
|
2019-03-01 14:52:27 -08:00
|
|
|
|
2019-02-16 13:17:37 -08:00
|
|
|
// ensure that an error is returned for an empty account (keypair2)
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::transfer(&keypair2, &keypair3.pubkey(), 1, bank.last_blockhash());
|
2019-03-01 08:57:42 -08:00
|
|
|
let entry_3 = next_entry(&entry_2.hash, 1, vec![tx]);
|
2019-02-21 13:37:08 -08:00
|
|
|
assert_eq!(
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &[entry_3], true, None, None),
|
2019-03-13 12:58:44 -07:00
|
|
|
Err(TransactionError::AccountNotFound)
|
2019-02-21 13:37:08 -08:00
|
|
|
);
|
2019-02-16 13:17:37 -08:00
|
|
|
}
|
2019-04-11 11:51:34 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_update_transaction_statuses() {
|
|
|
|
// Make sure instruction errors still update the signature cache
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(11_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-04-11 11:51:34 -07:00
|
|
|
bank.transfer(1_000, &mint_keypair, &pubkey).unwrap();
|
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1_000);
|
|
|
|
assert_eq!(
|
|
|
|
bank.transfer(10_001, &mint_keypair, &pubkey),
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
2020-08-06 15:04:43 -07:00
|
|
|
SystemError::ResultWithNegativeLamports.into(),
|
2019-04-11 11:51:34 -07:00
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bank.transfer(10_001, &mint_keypair, &pubkey),
|
|
|
|
Err(TransactionError::DuplicateSignature)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure other errors don't update the signature cache
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &pubkey, 1000, Hash::default());
|
2019-04-11 11:51:34 -07:00
|
|
|
let signature = tx.signatures[0];
|
|
|
|
|
|
|
|
// Should fail with blockhash not found
|
|
|
|
assert_eq!(
|
|
|
|
bank.process_transaction(&tx).map(|_| signature),
|
|
|
|
Err(TransactionError::BlockhashNotFound)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Should fail again with blockhash not found
|
|
|
|
assert_eq!(
|
|
|
|
bank.process_transaction(&tx).map(|_| signature),
|
|
|
|
Err(TransactionError::BlockhashNotFound)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_update_transaction_statuses_fail() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(11_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
2019-04-11 11:51:34 -07:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
2019-10-23 22:01:22 -07:00
|
|
|
let success_tx = system_transaction::transfer(
|
2019-04-11 11:51:34 -07:00
|
|
|
&mint_keypair,
|
|
|
|
&keypair1.pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
);
|
2019-10-23 22:01:22 -07:00
|
|
|
let fail_tx = system_transaction::transfer(
|
2019-04-11 11:51:34 -07:00
|
|
|
&mint_keypair,
|
|
|
|
&keypair2.pubkey(),
|
|
|
|
2,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let entry_1_to_mint = next_entry(
|
|
|
|
&bank.last_blockhash(),
|
|
|
|
1,
|
|
|
|
vec![
|
|
|
|
success_tx,
|
|
|
|
fail_tx.clone(), // will collide
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &[entry_1_to_mint], false, None, None),
|
2019-04-11 11:51:34 -07:00
|
|
|
Err(TransactionError::AccountInUse)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Should not see duplicate signature error
|
|
|
|
assert_eq!(bank.process_transaction(&fail_tx), Ok(()));
|
|
|
|
}
|
2019-05-01 09:27:13 -07:00
|
|
|
|
2020-12-04 21:14:59 -08:00
|
|
|
#[test]
|
|
|
|
fn test_halt_at_slot_starting_snapshot_root() {
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(123);
|
|
|
|
|
|
|
|
// Create roots at slots 0, 1
|
|
|
|
let forks = tr(0) / tr(1);
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
|
|
|
blockstore.add_tree(
|
|
|
|
forks,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
genesis_config.ticks_per_slot,
|
|
|
|
genesis_config.hash(),
|
|
|
|
);
|
|
|
|
blockstore.set_roots(&[0, 1]).unwrap();
|
|
|
|
|
|
|
|
// Specify halting at slot 0
|
|
|
|
let opts = ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
dev_halt_at_slot: Some(0),
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
|
|
|
let (bank_forks, _leader_schedule) =
|
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
|
|
|
|
|
|
|
// Should be able to fetch slot 0 because we specified halting at slot 0, even
|
|
|
|
// if there is a greater root at slot 1.
|
|
|
|
assert!(bank_forks.get(0).is_some());
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
#[test]
|
2020-01-13 13:13:52 -08:00
|
|
|
fn test_process_blockstore_from_root() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
mut genesis_config, ..
|
|
|
|
} = create_genesis_config(123);
|
2019-08-13 17:20:14 -07:00
|
|
|
|
|
|
|
let ticks_per_slot = 1;
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.ticks_per_slot = ticks_per_slot;
|
|
|
|
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
2019-08-13 17:20:14 -07:00
|
|
|
|
|
|
|
/*
|
2020-01-13 13:13:52 -08:00
|
|
|
Build a blockstore in the ledger with the following fork structure:
|
2019-08-13 17:20:14 -07:00
|
|
|
|
|
|
|
slot 0 (all ticks)
|
|
|
|
|
|
|
|
|
slot 1 (all ticks)
|
|
|
|
|
|
|
|
|
slot 2 (all ticks)
|
|
|
|
|
|
|
|
|
slot 3 (all ticks) -> root
|
|
|
|
|
|
|
|
|
slot 4 (all ticks)
|
|
|
|
|
|
|
|
|
slot 5 (all ticks) -> root
|
|
|
|
|
|
|
|
|
slot 6 (all ticks)
|
|
|
|
*/
|
|
|
|
|
|
|
|
let mut last_hash = blockhash;
|
|
|
|
for i in 0..6 {
|
|
|
|
last_hash =
|
2020-01-13 13:13:52 -08:00
|
|
|
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, i + 1, i, last_hash);
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.set_roots(&[3, 5]).unwrap();
|
2019-08-13 17:20:14 -07:00
|
|
|
|
|
|
|
// Set up bank1
|
2020-09-24 12:23:09 -07:00
|
|
|
let bank0 = Arc::new(Bank::new(&genesis_config));
|
2019-10-08 14:58:49 -07:00
|
|
|
let opts = ProcessOptions {
|
2019-11-04 21:14:55 -08:00
|
|
|
poh_verify: true,
|
2019-10-08 14:58:49 -07:00
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
2020-01-14 17:15:26 -08:00
|
|
|
let recyclers = VerifyRecyclers::default();
|
2020-12-13 17:26:34 -08:00
|
|
|
process_bank_0(&bank0, &blockstore, &opts, &recyclers);
|
2019-08-13 17:20:14 -07:00
|
|
|
let bank1 = Arc::new(Bank::new_from_parent(&bank0, &Pubkey::default(), 1));
|
2020-01-14 17:15:26 -08:00
|
|
|
confirm_full_slot(
|
|
|
|
&blockstore,
|
|
|
|
&bank1,
|
|
|
|
&opts,
|
|
|
|
&recyclers,
|
2020-03-23 12:19:11 -07:00
|
|
|
&mut ConfirmationProgress::new(bank0.last_blockhash()),
|
2020-07-14 20:14:48 -07:00
|
|
|
None,
|
2020-07-29 23:17:40 -07:00
|
|
|
None,
|
2020-01-14 17:15:26 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-13 09:40:51 -08:00
|
|
|
bank1.squash();
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
// Test process_blockstore_from_root() from slot 1 onwards
|
2020-09-24 12:23:09 -07:00
|
|
|
let (bank_forks, _leader_schedule) =
|
|
|
|
do_process_blockstore_from_root(&blockstore, bank1, &opts, &recyclers, None).unwrap();
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
assert_eq!(frozen_bank_slots(&bank_forks), vec![5, 6]);
|
|
|
|
assert_eq!(bank_forks.working_bank().slot(), 6);
|
2019-08-13 17:20:14 -07:00
|
|
|
assert_eq!(bank_forks.root(), 5);
|
|
|
|
|
|
|
|
// Verify the parents of the head of the fork
|
|
|
|
assert_eq!(
|
|
|
|
&bank_forks[6]
|
|
|
|
.parents()
|
|
|
|
.iter()
|
|
|
|
.map(|bank| bank.slot())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&[5]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that bank forks has the correct banks
|
2020-05-06 08:24:59 -07:00
|
|
|
verify_fork_infos(&bank_forks);
|
2019-08-13 17:20:14 -07:00
|
|
|
}
|
|
|
|
|
2019-05-01 09:27:13 -07:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn test_process_entries_stress() {
|
|
|
|
// this test throws lots of rayon threads at process_entries()
|
|
|
|
// finds bugs in very low-layer stuff
|
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(1_000_000_000);
|
|
|
|
let mut bank = Arc::new(Bank::new(&genesis_config));
|
2019-05-01 09:27:13 -07:00
|
|
|
|
2019-09-17 15:11:29 -07:00
|
|
|
const NUM_TRANSFERS_PER_ENTRY: usize = 8;
|
|
|
|
const NUM_TRANSFERS: usize = NUM_TRANSFERS_PER_ENTRY * 32;
|
|
|
|
|
2019-05-01 09:27:13 -07:00
|
|
|
let keypairs: Vec<_> = (0..NUM_TRANSFERS * 2).map(|_| Keypair::new()).collect();
|
|
|
|
|
|
|
|
// give everybody one lamport
|
|
|
|
for keypair in &keypairs {
|
|
|
|
bank.transfer(1, &mint_keypair, &keypair.pubkey())
|
|
|
|
.expect("funding failed");
|
|
|
|
}
|
|
|
|
|
2019-11-08 02:27:35 -08:00
|
|
|
let present_account_key = Keypair::new();
|
|
|
|
let present_account = Account::new(1, 10, &Pubkey::default());
|
|
|
|
bank.store_account(&present_account_key.pubkey(), &present_account);
|
|
|
|
|
2019-05-01 09:27:13 -07:00
|
|
|
let mut i = 0;
|
|
|
|
let mut hash = bank.last_blockhash();
|
2019-07-16 22:04:40 -07:00
|
|
|
let mut root: Option<Arc<Bank>> = None;
|
2019-05-01 09:27:13 -07:00
|
|
|
loop {
|
|
|
|
let entries: Vec<_> = (0..NUM_TRANSFERS)
|
2019-09-17 15:11:29 -07:00
|
|
|
.step_by(NUM_TRANSFERS_PER_ENTRY)
|
2019-05-01 09:27:13 -07:00
|
|
|
.map(|i| {
|
2019-09-17 15:11:29 -07:00
|
|
|
next_entry_mut(&mut hash, 0, {
|
|
|
|
let mut transactions = (i..i + NUM_TRANSFERS_PER_ENTRY)
|
|
|
|
.map(|i| {
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypairs[i],
|
|
|
|
&keypairs[i + NUM_TRANSFERS].pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
transactions.push(system_transaction::create_account(
|
|
|
|
&mint_keypair,
|
2019-11-08 02:27:35 -08:00
|
|
|
&present_account_key, // puts a TX error in results
|
2019-05-01 09:27:13 -07:00
|
|
|
bank.last_blockhash(),
|
2019-09-17 15:11:29 -07:00
|
|
|
100,
|
|
|
|
100,
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2019-09-17 15:11:29 -07:00
|
|
|
));
|
|
|
|
transactions
|
|
|
|
})
|
2019-05-01 09:27:13 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
info!("paying iteration {}", i);
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &entries, true, None, None).expect("paying failed");
|
2019-05-01 09:27:13 -07:00
|
|
|
|
|
|
|
let entries: Vec<_> = (0..NUM_TRANSFERS)
|
2019-09-17 15:11:29 -07:00
|
|
|
.step_by(NUM_TRANSFERS_PER_ENTRY)
|
2019-05-01 09:27:13 -07:00
|
|
|
.map(|i| {
|
|
|
|
next_entry_mut(
|
|
|
|
&mut hash,
|
|
|
|
0,
|
2019-09-17 15:11:29 -07:00
|
|
|
(i..i + NUM_TRANSFERS_PER_ENTRY)
|
|
|
|
.map(|i| {
|
|
|
|
system_transaction::transfer(
|
|
|
|
&keypairs[i + NUM_TRANSFERS],
|
|
|
|
&keypairs[i].pubkey(),
|
|
|
|
1,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
2019-05-01 09:27:13 -07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
info!("refunding iteration {}", i);
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries(&bank, &entries, true, None, None).expect("refunding failed");
|
2019-05-01 09:27:13 -07:00
|
|
|
|
|
|
|
// advance to next block
|
|
|
|
process_entries(
|
|
|
|
&bank,
|
|
|
|
&(0..bank.ticks_per_slot())
|
|
|
|
.map(|_| next_entry_mut(&mut hash, 1, vec![]))
|
|
|
|
.collect::<Vec<_>>(),
|
2019-08-28 08:38:32 -07:00
|
|
|
true,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-07-29 23:17:40 -07:00
|
|
|
None,
|
2019-05-01 09:27:13 -07:00
|
|
|
)
|
|
|
|
.expect("process ticks failed");
|
|
|
|
|
2019-07-16 22:04:40 -07:00
|
|
|
if i % 16 == 0 {
|
2020-05-15 09:35:43 -07:00
|
|
|
if let Some(old_root) = root {
|
|
|
|
old_root.squash();
|
|
|
|
}
|
2019-10-08 14:58:49 -07:00
|
|
|
root = Some(bank.clone());
|
2019-07-16 22:04:40 -07:00
|
|
|
}
|
2019-05-01 09:27:13 -07:00
|
|
|
i += 1;
|
2019-07-08 18:11:58 -07:00
|
|
|
|
2019-10-08 14:58:49 -07:00
|
|
|
bank = Arc::new(Bank::new_from_parent(
|
|
|
|
&bank,
|
2019-07-16 22:04:40 -07:00
|
|
|
&Pubkey::default(),
|
2019-10-08 14:58:49 -07:00
|
|
|
bank.slot() + thread_rng().gen_range(1, 3),
|
|
|
|
));
|
2019-05-01 09:27:13 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 11:29:41 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_ledger_ticks_ordering() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-11-08 11:29:41 -08:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(100);
|
|
|
|
let bank0 = Arc::new(Bank::new(&genesis_config));
|
|
|
|
let genesis_hash = genesis_config.hash();
|
2019-11-08 11:29:41 -08:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
|
|
|
|
// Simulate a slot of virtual ticks, creates a new blockhash
|
2019-11-08 20:56:57 -08:00
|
|
|
let mut entries = create_ticks(genesis_config.ticks_per_slot, 1, genesis_hash);
|
2019-11-08 11:29:41 -08:00
|
|
|
|
|
|
|
// The new blockhash is going to be the hash of the last tick in the block
|
|
|
|
let new_blockhash = entries.last().unwrap().hash;
|
|
|
|
// Create an transaction that references the new blockhash, should still
|
|
|
|
// be able to find the blockhash if we process transactions all in the same
|
|
|
|
// batch
|
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 1, new_blockhash);
|
|
|
|
let entry = next_entry(&new_blockhash, 1, vec![tx]);
|
|
|
|
entries.push(entry);
|
|
|
|
|
2020-07-29 23:17:40 -07:00
|
|
|
process_entries_with_callback(&bank0, &entries, true, None, None, None).unwrap();
|
2019-11-08 11:29:41 -08:00
|
|
|
assert_eq!(bank0.get_balance(&keypair.pubkey()), 1)
|
|
|
|
}
|
2019-05-20 19:04:18 -07:00
|
|
|
|
|
|
|
fn get_epoch_schedule(
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config: &GenesisConfig,
|
2019-12-05 18:41:29 -08:00
|
|
|
account_paths: Vec<PathBuf>,
|
2019-05-20 19:04:18 -07:00
|
|
|
) -> EpochSchedule {
|
2020-09-24 12:23:09 -07:00
|
|
|
let bank = Bank::new_with_paths(&genesis_config, account_paths, &[], None, None);
|
2020-05-15 09:35:43 -07:00
|
|
|
*bank.epoch_schedule()
|
2019-05-20 19:04:18 -07:00
|
|
|
}
|
2019-08-13 17:20:14 -07:00
|
|
|
|
2020-05-06 08:24:59 -07:00
|
|
|
fn frozen_bank_slots(bank_forks: &BankForks) -> Vec<Slot> {
|
|
|
|
let mut slots: Vec<_> = bank_forks.frozen_banks().keys().cloned().collect();
|
2020-12-13 17:26:34 -08:00
|
|
|
slots.sort_unstable();
|
2020-05-06 08:24:59 -07:00
|
|
|
slots
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:20:14 -07:00
|
|
|
// Check that `bank_forks` contains all the ancestors and banks for each fork identified in
|
|
|
|
// `bank_forks_info`
|
2020-05-06 08:24:59 -07:00
|
|
|
fn verify_fork_infos(bank_forks: &BankForks) {
|
|
|
|
for slot in frozen_bank_slots(bank_forks) {
|
|
|
|
let head_bank = &bank_forks[slot];
|
2019-08-13 17:20:14 -07:00
|
|
|
let mut parents = head_bank.parents();
|
|
|
|
parents.push(head_bank.clone());
|
|
|
|
|
|
|
|
// Ensure the tip of each fork and all its parents are in the given bank_forks
|
|
|
|
for parent in parents {
|
|
|
|
let parent_bank = &bank_forks[parent.slot()];
|
|
|
|
assert_eq!(parent_bank.slot(), parent.slot());
|
|
|
|
assert!(parent_bank.is_frozen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 13:06:49 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_first_error() {
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(1_000_000_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_config));
|
|
|
|
|
|
|
|
let present_account_key = Keypair::new();
|
|
|
|
let present_account = Account::new(1, 10, &Pubkey::default());
|
|
|
|
bank.store_account(&present_account_key.pubkey(), &present_account);
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
|
|
|
|
// Create array of two transactions which throw different errors
|
2020-10-19 12:23:14 -07:00
|
|
|
let account_not_found_tx = system_transaction::transfer(
|
|
|
|
&keypair,
|
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
42,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
);
|
2020-07-21 13:06:49 -07:00
|
|
|
let account_not_found_sig = account_not_found_tx.signatures[0];
|
|
|
|
let mut account_loaded_twice = system_transaction::transfer(
|
|
|
|
&mint_keypair,
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2020-07-21 13:06:49 -07:00
|
|
|
42,
|
|
|
|
bank.last_blockhash(),
|
|
|
|
);
|
|
|
|
account_loaded_twice.message.account_keys[1] = mint_keypair.pubkey();
|
|
|
|
let transactions = [account_loaded_twice, account_not_found_tx];
|
|
|
|
|
|
|
|
// Use inverted iteration_order
|
|
|
|
let iteration_order: Vec<usize> = vec![1, 0];
|
|
|
|
|
|
|
|
let batch = bank.prepare_batch(&transactions, Some(iteration_order));
|
|
|
|
let (
|
|
|
|
TransactionResults {
|
|
|
|
fee_collection_results,
|
2020-08-07 11:21:35 -07:00
|
|
|
..
|
2020-07-21 13:06:49 -07:00
|
|
|
},
|
|
|
|
_balances,
|
2020-09-24 07:36:22 -07:00
|
|
|
_inner_instructions,
|
2020-10-08 12:06:15 -07:00
|
|
|
_log_messages,
|
2020-09-24 07:36:22 -07:00
|
|
|
) = batch.bank().load_execute_and_commit_transactions(
|
|
|
|
&batch,
|
|
|
|
MAX_PROCESSING_AGE,
|
|
|
|
false,
|
|
|
|
false,
|
2020-10-08 12:06:15 -07:00
|
|
|
false,
|
2020-09-24 07:36:22 -07:00
|
|
|
);
|
2020-07-21 13:06:49 -07:00
|
|
|
let (err, signature) = get_first_error(&batch, fee_collection_results).unwrap();
|
|
|
|
// First error found should be for the 2nd transaction, due to iteration_order
|
|
|
|
assert_eq!(err.unwrap_err(), TransactionError::AccountNotFound);
|
|
|
|
assert_eq!(signature, account_not_found_sig);
|
|
|
|
}
|
2020-07-29 23:17:40 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_replay_vote_sender() {
|
|
|
|
let validator_keypairs: Vec<_> =
|
|
|
|
(0..10).map(|_| ValidatorVoteKeypairs::new_rand()).collect();
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
voting_keypair: _,
|
|
|
|
..
|
|
|
|
} = create_genesis_config_with_vote_accounts(
|
|
|
|
1_000_000_000,
|
|
|
|
&validator_keypairs,
|
|
|
|
vec![100; validator_keypairs.len()],
|
|
|
|
);
|
|
|
|
let bank0 = Arc::new(Bank::new(&genesis_config));
|
|
|
|
bank0.freeze();
|
|
|
|
|
2020-10-19 12:23:14 -07:00
|
|
|
let bank1 = Arc::new(Bank::new_from_parent(
|
|
|
|
&bank0,
|
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
1,
|
|
|
|
));
|
2020-07-29 23:17:40 -07:00
|
|
|
|
|
|
|
// The new blockhash is going to be the hash of the last tick in the block
|
|
|
|
let bank_1_blockhash = bank1.last_blockhash();
|
|
|
|
|
|
|
|
// Create an transaction that references the new blockhash, should still
|
|
|
|
// be able to find the blockhash if we process transactions all in the same
|
|
|
|
// batch
|
|
|
|
let mut expected_successful_voter_pubkeys = BTreeSet::new();
|
|
|
|
let vote_txs: Vec<_> = validator_keypairs
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, validator_keypairs)| {
|
|
|
|
if i % 3 == 0 {
|
|
|
|
// These votes are correct
|
|
|
|
expected_successful_voter_pubkeys
|
|
|
|
.insert(validator_keypairs.vote_keypair.pubkey());
|
|
|
|
vote_transaction::new_vote_transaction(
|
|
|
|
vec![0],
|
|
|
|
bank0.hash(),
|
|
|
|
bank_1_blockhash,
|
|
|
|
&validator_keypairs.node_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
} else if i % 3 == 1 {
|
|
|
|
// These have the wrong authorized voter
|
|
|
|
vote_transaction::new_vote_transaction(
|
|
|
|
vec![0],
|
|
|
|
bank0.hash(),
|
|
|
|
bank_1_blockhash,
|
|
|
|
&validator_keypairs.node_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
&Keypair::new(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// These have an invalid vote for non-existent bank 2
|
|
|
|
vote_transaction::new_vote_transaction(
|
|
|
|
vec![bank1.slot() + 1],
|
|
|
|
bank0.hash(),
|
|
|
|
bank_1_blockhash,
|
|
|
|
&validator_keypairs.node_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let entry = next_entry(&bank_1_blockhash, 1, vote_txs);
|
2020-08-07 11:21:35 -07:00
|
|
|
let (replay_vote_sender, replay_vote_receiver) = unbounded();
|
|
|
|
let _ = process_entries(&bank1, &[entry], true, None, Some(&replay_vote_sender));
|
|
|
|
let successes: BTreeSet<Pubkey> = replay_vote_receiver
|
2020-07-29 23:17:40 -07:00
|
|
|
.try_iter()
|
|
|
|
.map(|(vote_pubkey, _, _)| vote_pubkey)
|
|
|
|
.collect();
|
|
|
|
assert_eq!(successes, expected_successful_voter_pubkeys);
|
|
|
|
}
|
2020-08-20 21:56:25 -07:00
|
|
|
|
|
|
|
fn make_slot_with_vote_tx(
|
|
|
|
blockstore: &Blockstore,
|
|
|
|
ticks_per_slot: u64,
|
|
|
|
tx_landed_slot: Slot,
|
|
|
|
parent_slot: Slot,
|
|
|
|
parent_blockhash: &Hash,
|
|
|
|
vote_tx: Transaction,
|
|
|
|
slot_leader_keypair: &Arc<Keypair>,
|
|
|
|
) {
|
|
|
|
// Add votes to `last_slot` so that `root` will be confirmed
|
|
|
|
let vote_entry = next_entry(&parent_blockhash, 1, vec![vote_tx]);
|
|
|
|
let mut entries = create_ticks(ticks_per_slot, 0, vote_entry.hash);
|
|
|
|
entries.insert(0, vote_entry);
|
|
|
|
blockstore
|
|
|
|
.write_entries(
|
|
|
|
tx_landed_slot,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticks_per_slot,
|
|
|
|
Some(parent_slot),
|
|
|
|
true,
|
|
|
|
&slot_leader_keypair,
|
|
|
|
entries,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_test_process_blockstore_with_supermajority_root(blockstore_root: Option<Slot>) {
|
|
|
|
solana_logger::setup();
|
|
|
|
/*
|
|
|
|
Build fork structure:
|
|
|
|
slot 0
|
|
|
|
|
|
|
|
|
slot 1 <- (blockstore root)
|
|
|
|
/ \
|
|
|
|
slot 2 |
|
|
|
|
| |
|
|
|
|
slot 4 |
|
|
|
|
slot 5
|
|
|
|
|
|
|
|
|
`expected_root_slot`
|
|
|
|
/ \
|
|
|
|
... minor fork
|
|
|
|
/
|
|
|
|
`last_slot`
|
|
|
|
*/
|
|
|
|
let starting_fork_slot = 5;
|
|
|
|
let mut main_fork = tr(starting_fork_slot);
|
|
|
|
let mut main_fork_ref = main_fork.root_mut();
|
|
|
|
|
|
|
|
// Make enough slots to make a root slot > blockstore_root
|
|
|
|
let expected_root_slot = starting_fork_slot + blockstore_root.unwrap_or(0);
|
|
|
|
let last_main_fork_slot = expected_root_slot + MAX_LOCKOUT_HISTORY as u64 + 1;
|
|
|
|
|
|
|
|
// Make `minor_fork`
|
|
|
|
let last_minor_fork_slot = last_main_fork_slot + 1;
|
|
|
|
let minor_fork = tr(last_minor_fork_slot);
|
|
|
|
|
|
|
|
// Make 'main_fork`
|
|
|
|
for slot in starting_fork_slot + 1..last_main_fork_slot {
|
|
|
|
if slot - 1 == expected_root_slot {
|
|
|
|
main_fork_ref.push_front(minor_fork.clone());
|
|
|
|
}
|
|
|
|
main_fork_ref.push_front(tr(slot));
|
|
|
|
main_fork_ref = main_fork_ref.first_mut().unwrap();
|
|
|
|
}
|
|
|
|
let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / main_fork);
|
|
|
|
let validator_keypairs = ValidatorVoteKeypairs::new_rand();
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } =
|
|
|
|
genesis_utils::create_genesis_config_with_vote_accounts(
|
|
|
|
10_000,
|
|
|
|
&[&validator_keypairs],
|
|
|
|
vec![100],
|
|
|
|
);
|
|
|
|
let ticks_per_slot = genesis_config.ticks_per_slot();
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
|
|
|
blockstore.add_tree(forks, false, true, ticks_per_slot, genesis_config.hash());
|
|
|
|
|
|
|
|
if let Some(blockstore_root) = blockstore_root {
|
|
|
|
blockstore.set_roots(&[blockstore_root]).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let opts = ProcessOptions {
|
|
|
|
poh_verify: true,
|
|
|
|
..ProcessOptions::default()
|
|
|
|
};
|
|
|
|
let (bank_forks, _leader_schedule) =
|
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts.clone()).unwrap();
|
|
|
|
|
|
|
|
let last_vote_bank_hash = bank_forks.get(last_main_fork_slot - 1).unwrap().hash();
|
|
|
|
let last_vote_blockhash = bank_forks
|
|
|
|
.get(last_main_fork_slot - 1)
|
|
|
|
.unwrap()
|
|
|
|
.last_blockhash();
|
|
|
|
let slots: Vec<_> = (expected_root_slot..last_main_fork_slot).collect();
|
|
|
|
let vote_tx = vote_transaction::new_vote_transaction(
|
|
|
|
slots,
|
|
|
|
last_vote_bank_hash,
|
|
|
|
last_vote_blockhash,
|
|
|
|
&validator_keypairs.node_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
&validator_keypairs.vote_keypair,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add votes to `last_slot` so that `root` will be confirmed
|
|
|
|
let leader_keypair = Arc::new(validator_keypairs.node_keypair);
|
|
|
|
make_slot_with_vote_tx(
|
|
|
|
&blockstore,
|
|
|
|
ticks_per_slot,
|
|
|
|
last_main_fork_slot,
|
|
|
|
last_main_fork_slot - 1,
|
|
|
|
&last_vote_blockhash,
|
|
|
|
vote_tx,
|
|
|
|
&leader_keypair,
|
|
|
|
);
|
|
|
|
|
|
|
|
let (bank_forks, _leader_schedule) =
|
|
|
|
process_blockstore(&genesis_config, &blockstore, Vec::new(), opts).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(bank_forks.root(), expected_root_slot);
|
|
|
|
assert_eq!(
|
|
|
|
bank_forks.frozen_banks().len() as u64,
|
|
|
|
last_minor_fork_slot - expected_root_slot + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Minor fork at `last_main_fork_slot + 1` was above the `expected_root_slot`
|
|
|
|
// so should not have been purged
|
|
|
|
//
|
|
|
|
// Fork at slot 2 was purged because it was below the `expected_root_slot`
|
|
|
|
for slot in 0..=last_minor_fork_slot {
|
|
|
|
if slot >= expected_root_slot {
|
|
|
|
let bank = bank_forks.get(slot).unwrap();
|
|
|
|
assert_eq!(bank.slot(), slot);
|
|
|
|
assert!(bank.is_frozen());
|
|
|
|
} else {
|
|
|
|
assert!(bank_forks.get(slot).is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_blockstore_with_supermajority_root() {
|
|
|
|
run_test_process_blockstore_with_supermajority_root(None);
|
|
|
|
run_test_process_blockstore_with_supermajority_root(Some(1))
|
|
|
|
}
|
2020-08-25 09:49:15 -07:00
|
|
|
|
2020-09-17 21:33:08 -07:00
|
|
|
#[test]
|
2020-12-13 17:26:34 -08:00
|
|
|
#[allow(clippy::field_reassign_with_default)]
|
2020-09-17 21:33:08 -07:00
|
|
|
fn test_supermajority_root_from_vote_accounts() {
|
|
|
|
let convert_to_vote_accounts =
|
2020-11-30 09:18:33 -08:00
|
|
|
|roots_stakes: Vec<(Slot, u64)>| -> Vec<(Pubkey, (u64, ArcVoteAccount))> {
|
2020-09-17 21:33:08 -07:00
|
|
|
roots_stakes
|
|
|
|
.into_iter()
|
|
|
|
.map(|(root, stake)| {
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
vote_state.root_slot = Some(root);
|
|
|
|
let mut vote_account =
|
|
|
|
Account::new(1, VoteState::size_of(), &solana_vote_program::id());
|
2020-12-21 15:19:04 -08:00
|
|
|
let versioned = VoteStateVersions::new_current(vote_state);
|
2020-09-17 21:33:08 -07:00
|
|
|
VoteState::serialize(&versioned, &mut vote_account.data).unwrap();
|
2020-11-30 09:18:33 -08:00
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
(stake, ArcVoteAccount::from(vote_account)),
|
|
|
|
)
|
2020-09-17 21:33:08 -07:00
|
|
|
})
|
|
|
|
.collect_vec()
|
|
|
|
};
|
|
|
|
|
|
|
|
let total_stake = 10;
|
|
|
|
let slot = 100;
|
|
|
|
|
|
|
|
// Supermajority root should be None
|
|
|
|
assert!(
|
|
|
|
supermajority_root_from_vote_accounts(slot, total_stake, std::iter::empty()).is_none()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Supermajority root should be None
|
|
|
|
let roots_stakes = vec![(8, 1), (3, 1), (4, 1), (8, 1)];
|
|
|
|
let accounts = convert_to_vote_accounts(roots_stakes);
|
|
|
|
assert!(
|
|
|
|
supermajority_root_from_vote_accounts(slot, total_stake, accounts.into_iter())
|
|
|
|
.is_none()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Supermajority root should be 4, has 7/10 of the stake
|
|
|
|
let roots_stakes = vec![(8, 1), (3, 1), (4, 1), (8, 5)];
|
|
|
|
let accounts = convert_to_vote_accounts(roots_stakes);
|
|
|
|
assert_eq!(
|
|
|
|
supermajority_root_from_vote_accounts(slot, total_stake, accounts.into_iter()).unwrap(),
|
|
|
|
4
|
|
|
|
);
|
|
|
|
|
|
|
|
// Supermajority root should be 8, it has 7/10 of the stake
|
|
|
|
let roots_stakes = vec![(8, 1), (3, 1), (4, 1), (8, 6)];
|
|
|
|
let accounts = convert_to_vote_accounts(roots_stakes);
|
|
|
|
assert_eq!(
|
|
|
|
supermajority_root_from_vote_accounts(slot, total_stake, accounts.into_iter()).unwrap(),
|
|
|
|
8
|
|
|
|
);
|
|
|
|
}
|
2019-02-16 02:26:21 -08:00
|
|
|
}
|