2018-06-06 08:58:49 -07:00
|
|
|
//! The `banking_stage` processes Transaction messages. It is intended to be used
|
|
|
|
//! to contruct a software pipeline. The stage uses all available CPU cores and
|
|
|
|
//! can do its processing in parallel with signature verification on the GPU.
|
2018-05-14 16:31:13 -07:00
|
|
|
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::entry::Entry;
|
2019-02-17 19:22:29 -08:00
|
|
|
use crate::leader_confirmation_service::LeaderConfirmationService;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet::Packets;
|
2019-02-13 19:12:14 -08:00
|
|
|
use crate::packet::SharedPackets;
|
2019-02-19 16:17:36 -08:00
|
|
|
use crate::poh_recorder::{PohRecorder, PohRecorderError, WorkingBank};
|
2019-02-09 09:25:11 -08:00
|
|
|
use crate::poh_service::{PohService, PohServiceConfig};
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::result::{Error, Result};
|
|
|
|
use crate::service::Service;
|
|
|
|
use crate::sigverify_stage::VerifiedPackets;
|
2018-05-14 16:31:13 -07:00
|
|
|
use bincode::deserialize;
|
2018-08-06 11:35:45 -07:00
|
|
|
use log::Level;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_metrics::counter::Counter;
|
|
|
|
use solana_runtime::bank::{self, Bank, BankError};
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-12-08 16:54:42 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_sdk::timing::{self, duration_as_us, MAX_ENTRY_IDS};
|
2018-11-29 16:18:47 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2019-02-17 19:15:34 -08:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2019-02-19 16:17:36 -08:00
|
|
|
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError};
|
2018-09-26 05:52:13 -07:00
|
|
|
use std::sync::{Arc, Mutex};
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, Builder, JoinHandle};
|
2018-05-14 16:31:13 -07:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
2018-12-21 13:55:45 -08:00
|
|
|
use sys_info;
|
2018-05-14 16:31:13 -07:00
|
|
|
|
2019-02-13 19:12:14 -08:00
|
|
|
pub type UnprocessedPackets = Vec<(SharedPackets, usize)>; // `usize` is the index of the first unprocessed packet in `SharedPackets`
|
2018-10-18 22:57:48 -07:00
|
|
|
|
2018-09-26 05:52:13 -07:00
|
|
|
// number of threads is 1 until mt bank is ready
|
2018-12-21 13:55:45 -08:00
|
|
|
pub const NUM_THREADS: u32 = 10;
|
2018-09-26 05:52:13 -07:00
|
|
|
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Stores the stage's thread handle and output receiver.
|
2018-05-14 16:31:13 -07:00
|
|
|
pub struct BankingStage {
|
2019-02-13 19:12:14 -08:00
|
|
|
bank_thread_hdls: Vec<JoinHandle<UnprocessedPackets>>,
|
2018-10-25 14:56:21 -07:00
|
|
|
poh_service: PohService,
|
2019-02-17 19:22:29 -08:00
|
|
|
leader_confirmation_service: LeaderConfirmationService,
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 16:31:13 -07:00
|
|
|
impl BankingStage {
|
2018-07-05 14:41:53 -07:00
|
|
|
/// Create the stage using `bank`. Exit when `verified_receiver` is dropped.
|
2018-12-07 19:01:28 -08:00
|
|
|
#[allow(clippy::new_ret_no_self)]
|
2018-05-14 16:31:13 -07:00
|
|
|
pub fn new(
|
2018-09-27 13:49:50 -07:00
|
|
|
bank: &Arc<Bank>,
|
|
|
|
verified_receiver: Receiver<VerifiedPackets>,
|
2019-02-09 09:25:11 -08:00
|
|
|
config: PohServiceConfig,
|
2018-10-12 00:39:10 -07:00
|
|
|
last_entry_id: &Hash,
|
2019-02-05 08:03:52 -08:00
|
|
|
max_tick_height: u64,
|
2018-12-08 16:54:42 -08:00
|
|
|
leader_id: Pubkey,
|
2018-09-21 21:01:13 -07:00
|
|
|
) -> (Self, Receiver<Vec<Entry>>) {
|
|
|
|
let (entry_sender, entry_receiver) = channel();
|
2018-09-26 05:52:13 -07:00
|
|
|
let shared_verified_receiver = Arc::new(Mutex::new(verified_receiver));
|
2019-02-19 16:17:36 -08:00
|
|
|
let working_bank = WorkingBank {
|
|
|
|
bank: bank.clone(),
|
|
|
|
sender: entry_sender,
|
|
|
|
min_tick_height: bank.tick_height(),
|
|
|
|
max_tick_height,
|
|
|
|
};
|
|
|
|
|
|
|
|
let poh_recorder = PohRecorder::new(bank.tick_height(), *last_entry_id);
|
2018-10-25 14:56:21 -07:00
|
|
|
|
2018-09-26 05:52:13 -07:00
|
|
|
// Single thread to generate entries from many banks.
|
|
|
|
// This thread talks to poh_service and broadcasts the entries once they have been recorded.
|
|
|
|
// Once an entry has been recorded, its last_id is registered with the bank.
|
2019-02-17 19:15:34 -08:00
|
|
|
let poh_exit = Arc::new(AtomicBool::new(false));
|
2019-02-19 16:17:36 -08:00
|
|
|
|
|
|
|
let (poh_service, leader_sender) =
|
|
|
|
PohService::new(poh_recorder.clone(), config, poh_exit.clone());
|
|
|
|
|
|
|
|
leader_sender
|
|
|
|
.send(working_bank.clone())
|
|
|
|
.expect("failed to send leader to poh_service");
|
2018-09-26 05:52:13 -07:00
|
|
|
|
2018-12-20 15:47:48 -08:00
|
|
|
// Single thread to compute confirmation
|
2019-02-21 01:43:57 -08:00
|
|
|
let leader_confirmation_service =
|
|
|
|
LeaderConfirmationService::new(bank.clone(), leader_id, poh_exit.clone());
|
2018-11-02 15:49:14 -07:00
|
|
|
|
2018-09-26 05:52:13 -07:00
|
|
|
// Many banks that process transactions in parallel.
|
2019-02-13 19:12:14 -08:00
|
|
|
let bank_thread_hdls: Vec<JoinHandle<UnprocessedPackets>> = (0..Self::num_threads())
|
2018-09-26 05:52:13 -07:00
|
|
|
.map(|_| {
|
|
|
|
let thread_bank = bank.clone();
|
|
|
|
let thread_verified_receiver = shared_verified_receiver.clone();
|
2018-10-25 14:56:21 -07:00
|
|
|
let thread_poh_recorder = poh_recorder.clone();
|
2019-02-19 16:17:36 -08:00
|
|
|
let thread_leader = working_bank.clone();
|
2018-09-26 05:52:13 -07:00
|
|
|
Builder::new()
|
|
|
|
.name("solana-banking-stage-tx".to_string())
|
|
|
|
.spawn(move || {
|
2019-02-13 19:12:14 -08:00
|
|
|
let mut unprocessed_packets: UnprocessedPackets = vec![];
|
|
|
|
loop {
|
|
|
|
match Self::process_packets(
|
2018-09-26 05:52:13 -07:00
|
|
|
&thread_bank,
|
|
|
|
&thread_verified_receiver,
|
2018-10-25 14:56:21 -07:00
|
|
|
&thread_poh_recorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
&thread_leader,
|
2018-09-26 05:52:13 -07:00
|
|
|
) {
|
2019-02-13 19:12:14 -08:00
|
|
|
Err(Error::RecvTimeoutError(RecvTimeoutError::Timeout)) => (),
|
|
|
|
Ok(more_unprocessed_packets) => {
|
|
|
|
unprocessed_packets.extend(more_unprocessed_packets);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
debug!("solana-banking-stage-tx: exit due to {:?}", err);
|
|
|
|
break;
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 20:20:40 -08:00
|
|
|
}
|
2019-02-13 19:12:14 -08:00
|
|
|
unprocessed_packets
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.collect();
|
2018-10-18 22:57:48 -07:00
|
|
|
(
|
2018-12-07 19:01:28 -08:00
|
|
|
Self {
|
2018-10-18 22:57:48 -07:00
|
|
|
bank_thread_hdls,
|
2018-10-25 14:56:21 -07:00
|
|
|
poh_service,
|
2019-02-17 19:22:29 -08:00
|
|
|
leader_confirmation_service,
|
2018-10-18 22:57:48 -07:00
|
|
|
},
|
|
|
|
entry_receiver,
|
|
|
|
)
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
2018-12-21 13:55:45 -08:00
|
|
|
pub fn num_threads() -> u32 {
|
|
|
|
sys_info::cpu_num().unwrap_or(NUM_THREADS)
|
|
|
|
}
|
|
|
|
|
2019-02-08 10:12:43 -08:00
|
|
|
/// Convert the transactions from a blob of binary data to a vector of transactions
|
|
|
|
fn deserialize_transactions(p: &Packets) -> Vec<Option<Transaction>> {
|
2018-05-14 16:31:13 -07:00
|
|
|
p.packets
|
2018-10-31 22:12:15 -07:00
|
|
|
.iter()
|
2019-02-08 10:12:43 -08:00
|
|
|
.map(|x| deserialize(&x.data[0..x.meta.size]).ok())
|
2018-12-07 19:01:28 -08:00
|
|
|
.collect()
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:02:21 -08:00
|
|
|
fn record_transactions(
|
|
|
|
txs: &[Transaction],
|
|
|
|
results: &[bank::Result<()>],
|
|
|
|
poh: &PohRecorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
working_bank: &WorkingBank,
|
2019-02-17 14:12:06 -08:00
|
|
|
) -> Result<()> {
|
2019-02-16 14:02:21 -08:00
|
|
|
let processed_transactions: Vec<_> = results
|
|
|
|
.iter()
|
|
|
|
.zip(txs.iter())
|
|
|
|
.filter_map(|(r, x)| match r {
|
|
|
|
Ok(_) => Some(x.clone()),
|
|
|
|
Err(BankError::ProgramError(index, err)) => {
|
|
|
|
info!("program error {:?}, {:?}", index, err);
|
|
|
|
Some(x.clone())
|
|
|
|
}
|
|
|
|
Err(ref e) => {
|
|
|
|
debug!("process transaction failed {:?}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
debug!("processed: {} ", processed_transactions.len());
|
|
|
|
// unlock all the accounts with errors which are filtered by the above `filter_map`
|
|
|
|
if !processed_transactions.is_empty() {
|
|
|
|
let hash = Transaction::hash(&processed_transactions);
|
|
|
|
// record and unlock will unlock all the successfull transactions
|
2019-02-19 16:17:36 -08:00
|
|
|
poh.record(hash, processed_transactions, working_bank)?;
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_and_record_transactions(
|
|
|
|
bank: &Bank,
|
|
|
|
txs: &[Transaction],
|
|
|
|
poh: &PohRecorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
working_bank: &WorkingBank,
|
2019-02-17 14:12:06 -08:00
|
|
|
) -> Result<()> {
|
2019-02-16 14:02:21 -08:00
|
|
|
let now = Instant::now();
|
|
|
|
// Once accounts are locked, other threads cannot encode transactions that will modify the
|
|
|
|
// same account state
|
|
|
|
let lock_results = bank.lock_accounts(txs);
|
|
|
|
let lock_time = now.elapsed();
|
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
// Use a shorter maximum age when adding transactions into the pipeline. This will reduce
|
|
|
|
// the likelihood of any single thread getting starved and processing old ids.
|
|
|
|
// TODO: Banking stage threads should be prioritized to complete faster then this queue
|
|
|
|
// expires.
|
|
|
|
let (loaded_accounts, results) =
|
|
|
|
bank.load_and_execute_transactions(txs, lock_results, MAX_ENTRY_IDS as usize / 2);
|
|
|
|
let load_execute_time = now.elapsed();
|
|
|
|
|
|
|
|
let record_time = {
|
|
|
|
let now = Instant::now();
|
2019-02-19 16:17:36 -08:00
|
|
|
Self::record_transactions(txs, &results, poh, working_bank)?;
|
2019-02-16 14:02:21 -08:00
|
|
|
now.elapsed()
|
|
|
|
};
|
|
|
|
|
|
|
|
let commit_time = {
|
|
|
|
let now = Instant::now();
|
|
|
|
bank.commit_transactions(txs, &loaded_accounts, &results);
|
|
|
|
now.elapsed()
|
|
|
|
};
|
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
// Once the accounts are new transactions can enter the pipeline to process them
|
|
|
|
bank.unlock_accounts(&txs, &results);
|
|
|
|
let unlock_time = now.elapsed();
|
|
|
|
debug!(
|
|
|
|
"lock: {}us load_execute: {}us record: {}us commit: {}us unlock: {}us txs_len: {}",
|
|
|
|
duration_as_us(&lock_time),
|
|
|
|
duration_as_us(&load_execute_time),
|
|
|
|
duration_as_us(&record_time),
|
|
|
|
duration_as_us(&commit_time),
|
|
|
|
duration_as_us(&unlock_time),
|
|
|
|
txs.len(),
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:12:14 -08:00
|
|
|
/// Sends transactions to the bank.
|
|
|
|
///
|
|
|
|
/// Returns the number of transactions successfully processed by the bank, which may be less
|
|
|
|
/// than the total number if max PoH height was reached and the bank halted
|
2018-09-21 21:01:13 -07:00
|
|
|
fn process_transactions(
|
|
|
|
bank: &Arc<Bank>,
|
2018-09-27 13:49:50 -07:00
|
|
|
transactions: &[Transaction],
|
2018-09-26 05:52:13 -07:00
|
|
|
poh: &PohRecorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
working_bank: &WorkingBank,
|
2019-02-13 19:12:14 -08:00
|
|
|
) -> Result<(usize)> {
|
2018-09-21 21:01:13 -07:00
|
|
|
let mut chunk_start = 0;
|
|
|
|
while chunk_start != transactions.len() {
|
2018-09-24 12:26:47 -07:00
|
|
|
let chunk_end = chunk_start + Entry::num_will_fit(&transactions[chunk_start..]);
|
2018-09-21 21:01:13 -07:00
|
|
|
|
2019-02-16 14:02:21 -08:00
|
|
|
let result = Self::process_and_record_transactions(
|
|
|
|
bank,
|
|
|
|
&transactions[chunk_start..chunk_end],
|
|
|
|
poh,
|
2019-02-19 16:17:36 -08:00
|
|
|
working_bank,
|
2019-02-16 14:02:21 -08:00
|
|
|
);
|
2019-02-17 14:12:06 -08:00
|
|
|
if let Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached)) = result {
|
2019-02-13 19:12:14 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result?;
|
2018-09-26 05:52:13 -07:00
|
|
|
chunk_start = chunk_end;
|
2018-09-21 21:01:13 -07:00
|
|
|
}
|
2019-02-13 19:12:14 -08:00
|
|
|
Ok(chunk_start)
|
2018-09-21 21:01:13 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 20:20:40 -08:00
|
|
|
/// Process the incoming packets
|
2018-07-10 19:33:16 -07:00
|
|
|
pub fn process_packets(
|
2018-07-11 20:10:25 -07:00
|
|
|
bank: &Arc<Bank>,
|
2018-09-27 13:49:50 -07:00
|
|
|
verified_receiver: &Arc<Mutex<Receiver<VerifiedPackets>>>,
|
2018-09-26 05:52:13 -07:00
|
|
|
poh: &PohRecorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
working_bank: &WorkingBank,
|
2019-02-13 19:12:14 -08:00
|
|
|
) -> Result<UnprocessedPackets> {
|
2018-05-14 16:31:13 -07:00
|
|
|
let recv_start = Instant::now();
|
2018-09-26 05:52:13 -07:00
|
|
|
let mms = verified_receiver
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.recv_timeout(Duration::from_millis(100))?;
|
2018-05-14 16:31:13 -07:00
|
|
|
let mut reqs_len = 0;
|
|
|
|
let mms_len = mms.len();
|
|
|
|
info!(
|
|
|
|
"@{:?} process start stalled for: {:?}ms batches: {}",
|
|
|
|
timing::timestamp(),
|
|
|
|
timing::duration_as_ms(&recv_start.elapsed()),
|
|
|
|
mms.len(),
|
|
|
|
);
|
2018-09-24 17:13:49 -07:00
|
|
|
inc_new_counter_info!("banking_stage-entries_received", mms_len);
|
2018-05-30 21:24:21 -07:00
|
|
|
let count = mms.iter().map(|x| x.1.len()).sum();
|
2018-05-14 16:31:13 -07:00
|
|
|
let proc_start = Instant::now();
|
2018-10-23 14:44:14 -07:00
|
|
|
let mut new_tx_count = 0;
|
2019-02-13 19:12:14 -08:00
|
|
|
|
|
|
|
let mut unprocessed_packets = vec![];
|
|
|
|
let mut bank_shutdown = false;
|
2018-05-14 16:31:13 -07:00
|
|
|
for (msgs, vers) in mms {
|
2019-02-13 19:12:14 -08:00
|
|
|
if bank_shutdown {
|
|
|
|
unprocessed_packets.push((msgs, 0));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
let transactions = Self::deserialize_transactions(&msgs.read().unwrap());
|
2018-05-25 14:51:41 -07:00
|
|
|
reqs_len += transactions.len();
|
2018-09-21 21:01:13 -07:00
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
debug!("transactions received {}", transactions.len());
|
2019-02-13 19:12:14 -08:00
|
|
|
let (verified_transactions, verified_transaction_index): (Vec<_>, Vec<_>) =
|
|
|
|
transactions
|
|
|
|
.into_iter()
|
|
|
|
.zip(vers)
|
|
|
|
.zip(0..)
|
|
|
|
.filter_map(|((tx, ver), index)| match tx {
|
|
|
|
None => None,
|
|
|
|
Some(tx) => {
|
|
|
|
if tx.verify_refs() && ver != 0 {
|
|
|
|
Some((tx, index))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-11-02 14:32:05 -07:00
|
|
|
}
|
2019-02-13 19:12:14 -08:00
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
|
|
|
|
debug!("verified transactions {}", verified_transactions.len());
|
|
|
|
|
2019-02-19 16:22:33 -08:00
|
|
|
let processed =
|
2019-02-19 16:17:36 -08:00
|
|
|
Self::process_transactions(bank, &verified_transactions, poh, working_bank)?;
|
2019-02-13 19:12:14 -08:00
|
|
|
if processed < verified_transactions.len() {
|
|
|
|
bank_shutdown = true;
|
|
|
|
// Collect any unprocessed transactions in this batch for forwarding
|
|
|
|
unprocessed_packets.push((msgs, verified_transaction_index[processed]));
|
|
|
|
}
|
|
|
|
new_tx_count += processed;
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
2018-09-21 21:01:13 -07:00
|
|
|
|
2018-09-24 17:13:49 -07:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-time_ms",
|
2018-09-26 05:52:13 -07:00
|
|
|
timing::duration_as_ms(&proc_start.elapsed()) as usize
|
2018-09-24 17:13:49 -07:00
|
|
|
);
|
2018-05-14 16:31:13 -07:00
|
|
|
let total_time_s = timing::duration_as_s(&proc_start.elapsed());
|
|
|
|
let total_time_ms = timing::duration_as_ms(&proc_start.elapsed());
|
|
|
|
info!(
|
2018-05-25 14:51:41 -07:00
|
|
|
"@{:?} done processing transaction batches: {} time: {:?}ms reqs: {} reqs/s: {}",
|
2018-05-14 16:31:13 -07:00
|
|
|
timing::timestamp(),
|
|
|
|
mms_len,
|
|
|
|
total_time_ms,
|
|
|
|
reqs_len,
|
|
|
|
(reqs_len as f32) / (total_time_s)
|
|
|
|
);
|
2018-08-06 11:35:45 -07:00
|
|
|
inc_new_counter_info!("banking_stage-process_packets", count);
|
2018-10-23 14:44:14 -07:00
|
|
|
inc_new_counter_info!("banking_stage-process_transactions", new_tx_count);
|
2019-02-13 19:12:14 -08:00
|
|
|
|
|
|
|
Ok(unprocessed_packets)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join_and_collect_unprocessed_packets(&mut self) -> UnprocessedPackets {
|
|
|
|
let mut unprocessed_packets: UnprocessedPackets = vec![];
|
|
|
|
for bank_thread_hdl in self.bank_thread_hdls.drain(..) {
|
|
|
|
match bank_thread_hdl.join() {
|
|
|
|
Ok(more_unprocessed_packets) => {
|
|
|
|
unprocessed_packets.extend(more_unprocessed_packets)
|
|
|
|
}
|
|
|
|
err => warn!("bank_thread_hdl join failed: {:?}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unprocessed_packets
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:14:08 -07:00
|
|
|
impl Service for BankingStage {
|
2019-02-13 19:12:14 -08:00
|
|
|
type JoinReturnType = ();
|
2018-10-18 22:57:48 -07:00
|
|
|
|
2019-02-13 19:12:14 -08:00
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-10-18 22:57:48 -07:00
|
|
|
for bank_thread_hdl in self.bank_thread_hdls {
|
2019-02-13 19:12:14 -08:00
|
|
|
bank_thread_hdl.join()?;
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
2019-02-17 19:22:29 -08:00
|
|
|
self.leader_confirmation_service.join()?;
|
2019-02-13 19:12:14 -08:00
|
|
|
let _ = self.poh_service.join()?;
|
|
|
|
Ok(())
|
2018-07-03 21:14:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-01-09 14:33:44 -08:00
|
|
|
use crate::entry::EntrySlice;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet::to_packets;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_sdk::genesis_block::GenesisBlock;
|
2019-02-16 14:02:21 -08:00
|
|
|
use solana_sdk::native_program::ProgramError;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-12-04 20:19:48 -08:00
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
2019-02-18 19:28:10 -08:00
|
|
|
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
|
2018-09-25 15:01:51 -07:00
|
|
|
use std::thread::sleep;
|
|
|
|
|
|
|
|
#[test]
|
2018-09-26 05:52:13 -07:00
|
|
|
fn test_banking_stage_shutdown1() {
|
2019-02-12 15:49:23 -08:00
|
|
|
let (genesis_block, _mint_keypair) = GenesisBlock::new(2);
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2018-09-25 15:01:51 -07:00
|
|
|
let (verified_sender, verified_receiver) = channel();
|
2018-10-12 00:39:10 -07:00
|
|
|
let (banking_stage, _entry_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
2019-02-09 09:20:43 -08:00
|
|
|
PohServiceConfig::default(),
|
2018-10-12 00:39:10 -07:00
|
|
|
&bank.last_id(),
|
2019-02-14 08:58:55 -08:00
|
|
|
DEFAULT_TICKS_PER_SLOT,
|
2019-02-05 08:03:52 -08:00
|
|
|
genesis_block.bootstrap_leader_id,
|
2018-10-12 00:39:10 -07:00
|
|
|
);
|
2018-09-25 15:01:51 -07:00
|
|
|
drop(verified_sender);
|
2019-02-13 19:12:14 -08:00
|
|
|
banking_stage.join().unwrap();
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
|
|
|
fn test_banking_stage_tick() {
|
2019-02-12 15:49:23 -08:00
|
|
|
let (genesis_block, _mint_keypair) = GenesisBlock::new(2);
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2018-09-25 15:01:51 -07:00
|
|
|
let start_hash = bank.last_id();
|
|
|
|
let (verified_sender, verified_receiver) = channel();
|
|
|
|
let (banking_stage, entry_receiver) = BankingStage::new(
|
2018-09-27 13:49:50 -07:00
|
|
|
&bank,
|
2018-09-25 15:01:51 -07:00
|
|
|
verified_receiver,
|
2019-02-09 09:25:11 -08:00
|
|
|
PohServiceConfig::Sleep(Duration::from_millis(1)),
|
2018-10-12 00:39:10 -07:00
|
|
|
&bank.last_id(),
|
2019-02-14 08:58:55 -08:00
|
|
|
DEFAULT_TICKS_PER_SLOT,
|
2019-02-05 08:03:52 -08:00
|
|
|
genesis_block.bootstrap_leader_id,
|
2018-09-25 15:01:51 -07:00
|
|
|
);
|
2018-09-26 05:52:13 -07:00
|
|
|
sleep(Duration::from_millis(500));
|
2018-09-25 15:01:51 -07:00
|
|
|
drop(verified_sender);
|
|
|
|
|
|
|
|
let entries: Vec<_> = entry_receiver.iter().flat_map(|x| x).collect();
|
|
|
|
assert!(entries.len() != 0);
|
|
|
|
assert!(entries.verify(&start_hash));
|
2018-09-26 13:31:39 -07:00
|
|
|
assert_eq!(entries[entries.len() - 1].id, bank.last_id());
|
2019-02-13 19:12:14 -08:00
|
|
|
banking_stage.join().unwrap();
|
2018-09-25 15:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-26 13:31:39 -07:00
|
|
|
fn test_banking_stage_entries_only() {
|
2019-02-12 15:49:23 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(2);
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2018-09-25 15:01:51 -07:00
|
|
|
let start_hash = bank.last_id();
|
|
|
|
let (verified_sender, verified_receiver) = channel();
|
2018-10-12 00:39:10 -07:00
|
|
|
let (banking_stage, entry_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
2019-02-09 09:20:43 -08:00
|
|
|
PohServiceConfig::default(),
|
2018-10-12 00:39:10 -07:00
|
|
|
&bank.last_id(),
|
2019-02-14 08:58:55 -08:00
|
|
|
DEFAULT_TICKS_PER_SLOT,
|
2019-02-05 08:03:52 -08:00
|
|
|
genesis_block.bootstrap_leader_id,
|
2018-10-12 00:39:10 -07:00
|
|
|
);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// good tx
|
2019-01-24 12:04:04 -08:00
|
|
|
let keypair = mint_keypair;
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_account(&keypair, keypair.pubkey(), 1, start_hash, 0);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// good tx, but no verify
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx_no_ver =
|
|
|
|
SystemTransaction::new_account(&keypair, keypair.pubkey(), 1, start_hash, 0);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// bad tx, AccountNotFound
|
|
|
|
let keypair = Keypair::new();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx_anf = SystemTransaction::new_account(&keypair, keypair.pubkey(), 1, start_hash, 0);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// send 'em over
|
2018-09-26 09:50:12 -07:00
|
|
|
let packets = to_packets(&[tx, tx_no_ver, tx_anf]);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// glad they all fit
|
|
|
|
assert_eq!(packets.len(), 1);
|
2018-10-25 16:51:51 -07:00
|
|
|
verified_sender // tx, no_ver, anf
|
2018-09-25 15:01:51 -07:00
|
|
|
.send(vec![(packets[0].clone(), vec![1u8, 0u8, 1u8])])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
drop(verified_sender);
|
|
|
|
|
2018-09-26 05:52:13 -07:00
|
|
|
//receive entries + ticks
|
|
|
|
let entries: Vec<_> = entry_receiver.iter().map(|x| x).collect();
|
|
|
|
assert!(entries.len() >= 1);
|
|
|
|
|
|
|
|
let mut last_id = start_hash;
|
|
|
|
entries.iter().for_each(|entries| {
|
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
assert!(entries.verify(&last_id));
|
|
|
|
last_id = entries.last().unwrap().id;
|
|
|
|
});
|
|
|
|
drop(entry_receiver);
|
2019-02-13 19:12:14 -08:00
|
|
|
banking_stage.join().unwrap();
|
2018-09-26 13:31:39 -07:00
|
|
|
}
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
|
|
|
fn test_banking_stage_entryfication() {
|
|
|
|
// In this attack we'll demonstrate that a verifier can interpret the ledger
|
|
|
|
// differently if either the server doesn't signal the ledger to add an
|
|
|
|
// Entry OR if the verifier tries to parallelize across multiple Entries.
|
2019-02-12 15:49:23 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(2);
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2018-09-25 15:01:51 -07:00
|
|
|
let (verified_sender, verified_receiver) = channel();
|
2018-10-12 00:39:10 -07:00
|
|
|
let (banking_stage, entry_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
2019-02-09 09:20:43 -08:00
|
|
|
PohServiceConfig::default(),
|
2018-10-12 00:39:10 -07:00
|
|
|
&bank.last_id(),
|
2019-02-14 08:58:55 -08:00
|
|
|
DEFAULT_TICKS_PER_SLOT,
|
2019-02-05 08:03:52 -08:00
|
|
|
genesis_block.bootstrap_leader_id,
|
2018-10-12 00:39:10 -07:00
|
|
|
);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// Process a batch that includes a transaction that receives two tokens.
|
|
|
|
let alice = Keypair::new();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_account(
|
|
|
|
&mint_keypair,
|
|
|
|
alice.pubkey(),
|
|
|
|
2,
|
|
|
|
genesis_block.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
let packets = to_packets(&[tx]);
|
2018-09-25 15:01:51 -07:00
|
|
|
verified_sender
|
|
|
|
.send(vec![(packets[0].clone(), vec![1u8])])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Process a second batch that spends one of those tokens.
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_account(
|
|
|
|
&alice,
|
|
|
|
mint_keypair.pubkey(),
|
|
|
|
1,
|
|
|
|
genesis_block.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
2018-09-26 09:50:12 -07:00
|
|
|
let packets = to_packets(&[tx]);
|
2018-09-25 15:01:51 -07:00
|
|
|
verified_sender
|
|
|
|
.send(vec![(packets[0].clone(), vec![1u8])])
|
|
|
|
.unwrap();
|
|
|
|
drop(verified_sender);
|
2019-02-13 19:12:14 -08:00
|
|
|
banking_stage.join().unwrap();
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// Collect the ledger and feed it to a new bank.
|
2018-09-26 13:31:39 -07:00
|
|
|
let entries: Vec<_> = entry_receiver.iter().flat_map(|x| x).collect();
|
|
|
|
// same assertion as running through the bank, really...
|
2018-09-26 05:52:13 -07:00
|
|
|
assert!(entries.len() >= 2);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
// Assert the user holds one token, not two. If the stage only outputs one
|
|
|
|
// entry, then the second transaction will be rejected, because it drives
|
|
|
|
// the account balance below zero before the credit is added.
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2018-09-26 13:31:39 -07:00
|
|
|
for entry in entries {
|
2018-10-04 13:15:54 -07:00
|
|
|
bank.process_transactions(&entry.transactions)
|
|
|
|
.iter()
|
|
|
|
.for_each(|x| assert_eq!(*x, Ok(())));
|
2018-09-25 15:01:51 -07:00
|
|
|
}
|
|
|
|
assert_eq!(bank.get_balance(&alice.pubkey()), 1);
|
|
|
|
}
|
2018-10-18 22:57:48 -07:00
|
|
|
|
|
|
|
// Test that when the max_tick_height is reached, the banking stage exits
|
|
|
|
#[test]
|
|
|
|
fn test_max_tick_height_shutdown() {
|
2019-02-12 15:49:23 -08:00
|
|
|
let (genesis_block, _mint_keypair) = GenesisBlock::new(2);
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2019-02-13 19:12:14 -08:00
|
|
|
let (verified_sender, verified_receiver) = channel();
|
2018-10-18 22:57:48 -07:00
|
|
|
let max_tick_height = 10;
|
|
|
|
let (banking_stage, _entry_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
2019-02-09 09:20:43 -08:00
|
|
|
PohServiceConfig::default(),
|
2018-10-18 22:57:48 -07:00
|
|
|
&bank.last_id(),
|
2019-02-05 08:03:52 -08:00
|
|
|
max_tick_height,
|
|
|
|
genesis_block.bootstrap_leader_id,
|
2018-10-18 22:57:48 -07:00
|
|
|
);
|
2019-02-15 22:10:21 -08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let bank_tick_height = bank.tick_height();
|
|
|
|
if bank_tick_height >= max_tick_height {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(10));
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:12:14 -08:00
|
|
|
drop(verified_sender);
|
|
|
|
banking_stage.join().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_returns_unprocessed_packet() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(2);
|
2019-02-16 11:36:22 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
|
|
|
let ticks_per_slot = 1;
|
2019-02-13 19:12:14 -08:00
|
|
|
let (verified_sender, verified_receiver) = channel();
|
|
|
|
let (mut banking_stage, _entry_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
|
|
|
PohServiceConfig::default(),
|
|
|
|
&bank.last_id(),
|
2019-02-16 11:36:22 -08:00
|
|
|
ticks_per_slot,
|
2019-02-13 19:12:14 -08:00
|
|
|
genesis_block.bootstrap_leader_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Wait for Poh recorder to hit max height
|
2019-02-15 22:10:21 -08:00
|
|
|
loop {
|
|
|
|
let bank_tick_height = bank.tick_height();
|
2019-02-16 05:44:19 -08:00
|
|
|
if bank_tick_height >= ticks_per_slot {
|
2019-02-15 22:10:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(10));
|
|
|
|
}
|
2019-02-13 19:12:14 -08:00
|
|
|
|
|
|
|
// Now send a transaction to the banking stage
|
|
|
|
let transaction = SystemTransaction::new_account(
|
|
|
|
&mint_keypair,
|
|
|
|
Keypair::new().pubkey(),
|
|
|
|
2,
|
|
|
|
genesis_block.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let packets = to_packets(&[transaction]);
|
|
|
|
verified_sender
|
|
|
|
.send(vec![(packets[0].clone(), vec![1u8])])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Shut down the banking stage, it should give back the transaction
|
|
|
|
drop(verified_sender);
|
|
|
|
let unprocessed_packets = banking_stage.join_and_collect_unprocessed_packets();
|
|
|
|
assert_eq!(unprocessed_packets.len(), 1);
|
|
|
|
let (packets, start_index) = &unprocessed_packets[0];
|
|
|
|
assert_eq!(packets.read().unwrap().packets.len(), 1); // TODO: maybe compare actual packet contents too
|
|
|
|
assert_eq!(*start_index, 0);
|
2018-10-18 22:57:48 -07:00
|
|
|
}
|
2019-02-16 14:02:21 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bank_record_transactions() {
|
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(10_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
|
|
|
let (entry_sender, entry_receiver) = channel();
|
2019-02-19 16:17:36 -08:00
|
|
|
let working_bank = WorkingBank {
|
|
|
|
bank: bank.clone(),
|
|
|
|
sender: entry_sender,
|
|
|
|
min_tick_height: bank.tick_height(),
|
|
|
|
max_tick_height: std::u64::MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
let poh_recorder = PohRecorder::new(bank.tick_height(), bank.last_id());
|
2019-02-16 14:02:21 -08:00
|
|
|
let pubkey = Keypair::new().pubkey();
|
|
|
|
|
|
|
|
let transactions = vec![
|
|
|
|
SystemTransaction::new_move(&mint_keypair, pubkey, 1, genesis_block.last_id(), 0),
|
|
|
|
SystemTransaction::new_move(&mint_keypair, pubkey, 1, genesis_block.last_id(), 0),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut results = vec![Ok(()), Ok(())];
|
2019-02-19 16:17:36 -08:00
|
|
|
BankingStage::record_transactions(&transactions, &results, &poh_recorder, &working_bank)
|
2019-02-19 16:22:33 -08:00
|
|
|
.unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
let entries = entry_receiver.recv().unwrap();
|
|
|
|
assert_eq!(entries[0].transactions.len(), transactions.len());
|
|
|
|
|
|
|
|
// ProgramErrors should still be recorded
|
|
|
|
results[0] = Err(BankError::ProgramError(
|
|
|
|
1,
|
|
|
|
ProgramError::ResultWithNegativeTokens,
|
|
|
|
));
|
2019-02-19 16:17:36 -08:00
|
|
|
BankingStage::record_transactions(&transactions, &results, &poh_recorder, &working_bank)
|
2019-02-19 16:22:33 -08:00
|
|
|
.unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
let entries = entry_receiver.recv().unwrap();
|
|
|
|
assert_eq!(entries[0].transactions.len(), transactions.len());
|
|
|
|
|
|
|
|
// Other BankErrors should not be recorded
|
|
|
|
results[0] = Err(BankError::AccountNotFound);
|
2019-02-19 16:17:36 -08:00
|
|
|
BankingStage::record_transactions(&transactions, &results, &poh_recorder, &working_bank)
|
2019-02-19 16:22:33 -08:00
|
|
|
.unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
let entries = entry_receiver.recv().unwrap();
|
|
|
|
assert_eq!(entries[0].transactions.len(), transactions.len() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bank_process_and_record_transactions() {
|
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(10_000);
|
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
|
|
|
let pubkey = Keypair::new().pubkey();
|
|
|
|
|
|
|
|
let transactions = vec![SystemTransaction::new_move(
|
|
|
|
&mint_keypair,
|
|
|
|
pubkey,
|
|
|
|
1,
|
|
|
|
genesis_block.last_id(),
|
|
|
|
0,
|
|
|
|
)];
|
|
|
|
|
|
|
|
let (entry_sender, entry_receiver) = channel();
|
2019-02-19 16:17:36 -08:00
|
|
|
let working_bank = WorkingBank {
|
|
|
|
bank: bank.clone(),
|
|
|
|
sender: entry_sender,
|
|
|
|
min_tick_height: bank.tick_height(),
|
|
|
|
max_tick_height: bank.tick_height() + 1,
|
|
|
|
};
|
|
|
|
let poh_recorder = PohRecorder::new(bank.tick_height(), bank.last_id());
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-02-19 16:22:33 -08:00
|
|
|
BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
|
|
|
&poh_recorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
&working_bank,
|
2019-02-19 16:22:33 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-02-19 16:17:36 -08:00
|
|
|
poh_recorder.tick(&working_bank).unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
|
|
|
|
let mut need_tick = true;
|
|
|
|
// read entries until I find mine, might be ticks...
|
|
|
|
while need_tick {
|
|
|
|
let entries = entry_receiver.recv().unwrap();
|
|
|
|
for entry in entries {
|
|
|
|
if !entry.is_tick() {
|
|
|
|
assert_eq!(entry.transactions.len(), transactions.len());
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
|
|
|
} else {
|
|
|
|
need_tick = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let transactions = vec![SystemTransaction::new_move(
|
|
|
|
&mint_keypair,
|
|
|
|
pubkey,
|
|
|
|
2,
|
|
|
|
genesis_block.last_id(),
|
|
|
|
0,
|
|
|
|
)];
|
|
|
|
|
2019-02-17 14:12:06 -08:00
|
|
|
assert_matches!(
|
2019-02-19 16:22:33 -08:00
|
|
|
BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
|
|
|
&poh_recorder,
|
2019-02-19 16:17:36 -08:00
|
|
|
&working_bank
|
2019-02-19 16:22:33 -08:00
|
|
|
),
|
2019-02-17 14:12:06 -08:00
|
|
|
Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached))
|
2019-02-16 14:02:21 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
|
|
|
}
|
2018-09-25 15:01:51 -07:00
|
|
|
}
|