2018-12-07 14:09:29 -08:00
|
|
|
//! The `replay_stage` replays transactions broadcast by the leader.
|
2018-05-22 14:26:28 -07:00
|
|
|
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use crate::cluster_info::ClusterInfo;
|
|
|
|
use crate::counter::Counter;
|
2019-02-04 15:33:43 -08:00
|
|
|
use crate::db_ledger::DbLedger;
|
|
|
|
use crate::entry::{Entry, EntryReceiver, EntrySender, EntrySlice};
|
2019-01-29 00:21:27 -08:00
|
|
|
#[cfg(not(test))]
|
|
|
|
use crate::entry_stream::EntryStream;
|
|
|
|
use crate::entry_stream::EntryStreamHandler;
|
|
|
|
#[cfg(test)]
|
|
|
|
use crate::entry_stream::MockEntryStream as EntryStream;
|
2019-01-26 00:28:08 -08:00
|
|
|
use crate::fullnode::TvuRotationSender;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet::BlobError;
|
|
|
|
use crate::result::{Error, Result};
|
|
|
|
use crate::service::Service;
|
2019-01-26 00:28:08 -08:00
|
|
|
use crate::tvu::TvuReturnType;
|
2019-01-31 20:12:51 -08:00
|
|
|
use crate::voting_keypair::VotingKeypair;
|
2018-08-06 11:35:45 -07:00
|
|
|
use log::Level;
|
2018-11-16 08:45:59 -08:00
|
|
|
use solana_metrics::{influxdb, submit};
|
2019-01-26 00:28:08 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2019-01-30 19:28:48 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-11-16 08:45:59 -08:00
|
|
|
use solana_sdk::timing::duration_as_ms;
|
2019-01-31 19:53:48 -08:00
|
|
|
use solana_sdk::vote_transaction::VoteTransaction;
|
2018-09-25 15:41:29 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
2019-02-04 15:33:43 -08:00
|
|
|
use std::sync::mpsc::{channel, Receiver, SyncSender};
|
2018-07-05 12:01:40 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, Builder, JoinHandle};
|
2018-09-26 20:58:06 -07:00
|
|
|
use std::time::Instant;
|
2018-05-22 14:26:28 -07:00
|
|
|
|
2018-12-13 18:43:10 -08:00
|
|
|
pub const MAX_ENTRY_RECV_PER_ITER: usize = 512;
|
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
// Implement a destructor for the ReplayStage thread to signal it exited
|
2018-09-25 15:41:29 -07:00
|
|
|
// even on panics
|
|
|
|
struct Finalizer {
|
|
|
|
exit_sender: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Finalizer {
|
|
|
|
fn new(exit_sender: Arc<AtomicBool>) -> Self {
|
|
|
|
Finalizer { exit_sender }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Implement a destructor for Finalizer.
|
|
|
|
impl Drop for Finalizer {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.exit_sender.clone().store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
pub struct ReplayStage {
|
2019-01-26 00:28:08 -08:00
|
|
|
t_replay: JoinHandle<()>,
|
2019-02-04 15:33:43 -08:00
|
|
|
exit: Arc<AtomicBool>,
|
|
|
|
ledger_signal_sender: SyncSender<bool>,
|
2018-05-22 14:26:28 -07:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
impl ReplayStage {
|
2018-06-15 14:27:06 -07:00
|
|
|
/// Process entry blobs, already in order
|
2019-01-05 12:57:52 -08:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2018-12-07 14:09:29 -08:00
|
|
|
fn process_entries(
|
2019-02-04 15:33:43 -08:00
|
|
|
mut entries: Vec<Entry>,
|
2018-07-05 12:01:40 -07:00
|
|
|
bank: &Arc<Bank>,
|
2018-10-08 19:55:54 -07:00
|
|
|
cluster_info: &Arc<RwLock<ClusterInfo>>,
|
2019-01-31 20:12:51 -08:00
|
|
|
voting_keypair: Option<&Arc<VotingKeypair>>,
|
2018-10-23 14:42:48 -07:00
|
|
|
ledger_entry_sender: &EntrySender,
|
2019-01-26 00:28:08 -08:00
|
|
|
entry_height: &Arc<RwLock<u64>>,
|
|
|
|
last_entry_id: &Arc<RwLock<Hash>>,
|
2019-01-29 00:21:27 -08:00
|
|
|
entry_stream: Option<&mut EntryStream>,
|
2018-11-12 11:40:32 -08:00
|
|
|
) -> Result<()> {
|
2019-01-29 00:21:27 -08:00
|
|
|
if let Some(stream) = entry_stream {
|
|
|
|
stream.stream_entries(&entries).unwrap_or_else(|e| {
|
|
|
|
error!("Entry Stream error: {:?}, {:?}", e, stream.socket);
|
|
|
|
});
|
|
|
|
}
|
2019-02-05 08:03:52 -08:00
|
|
|
// Coalesce all the available entries into a single vote
|
2018-11-16 08:45:59 -08:00
|
|
|
submit(
|
2018-12-07 15:18:09 -08:00
|
|
|
influxdb::Point::new("replicate-stage")
|
2018-10-20 06:38:20 -07:00
|
|
|
.add_field("count", influxdb::Value::Integer(entries.len() as i64))
|
2018-10-16 12:54:23 -07:00
|
|
|
.to_owned(),
|
|
|
|
);
|
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
let mut res = Ok(());
|
2018-11-12 11:40:32 -08:00
|
|
|
let mut num_entries_to_write = entries.len();
|
2018-11-12 12:41:19 -08:00
|
|
|
let now = Instant::now();
|
2019-01-26 00:28:08 -08:00
|
|
|
if !entries.as_slice().verify(&last_entry_id.read().unwrap()) {
|
2018-12-07 15:18:09 -08:00
|
|
|
inc_new_counter_info!("replicate_stage-verify-fail", entries.len());
|
2018-11-12 12:41:19 -08:00
|
|
|
return Err(Error::BlobError(BlobError::VerificationFailed));
|
|
|
|
}
|
|
|
|
inc_new_counter_info!(
|
2018-12-07 15:18:09 -08:00
|
|
|
"replicate_stage-verify-duration",
|
2018-11-12 12:41:19 -08:00
|
|
|
duration_as_ms(&now.elapsed()) as usize
|
|
|
|
);
|
2018-12-12 15:58:29 -08:00
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
let mut num_ticks_to_next_vote = bank
|
|
|
|
.leader_scheduler
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.num_ticks_left_in_slot(bank.tick_height());
|
2018-12-18 16:06:05 -08:00
|
|
|
|
2018-11-12 11:40:32 -08:00
|
|
|
for (i, entry) in entries.iter().enumerate() {
|
2018-12-18 16:06:05 -08:00
|
|
|
inc_new_counter_info!("replicate-stage_bank-tick", bank.tick_height() as usize);
|
|
|
|
if entry.is_tick() {
|
2019-02-05 08:03:52 -08:00
|
|
|
if num_ticks_to_next_vote == 0 {
|
|
|
|
num_ticks_to_next_vote = bank
|
|
|
|
.leader_scheduler
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.leader_rotation_interval;
|
|
|
|
}
|
2018-12-18 16:06:05 -08:00
|
|
|
num_ticks_to_next_vote -= 1;
|
2018-12-13 18:43:10 -08:00
|
|
|
}
|
2018-12-18 16:06:05 -08:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"replicate-stage_tick-to-vote",
|
|
|
|
num_ticks_to_next_vote as usize
|
|
|
|
);
|
|
|
|
// If it's the last entry in the vector, i will be vec len - 1.
|
|
|
|
// If we don't process the entry now, the for loop will exit and the entry
|
|
|
|
// will be dropped.
|
|
|
|
if 0 == num_ticks_to_next_vote || (i + 1) == entries.len() {
|
2019-02-05 08:03:52 -08:00
|
|
|
res = bank.process_entries(&entries[0..=i]);
|
2018-12-18 16:06:05 -08:00
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
// TODO: This will return early from the first entry that has an erroneous
|
|
|
|
// transaction, instead of processing the rest of the entries in the vector
|
|
|
|
// of received entries. This is in line with previous behavior when
|
|
|
|
// bank.process_entries() was used to process the entries, but doesn't solve the
|
|
|
|
// issue that the bank state was still changed, leading to inconsistencies with the
|
|
|
|
// leader as the leader currently should not be publishing erroneous transactions
|
2019-02-05 08:03:52 -08:00
|
|
|
inc_new_counter_info!("replicate-stage_failed_process_entries", i);
|
2018-12-18 16:06:05 -08:00
|
|
|
break;
|
2018-12-13 18:43:10 -08:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:06:05 -08:00
|
|
|
if 0 == num_ticks_to_next_vote {
|
2019-01-31 20:12:51 -08:00
|
|
|
if let Some(voting_keypair) = voting_keypair {
|
|
|
|
let keypair = voting_keypair.as_ref();
|
2019-02-01 07:36:35 -08:00
|
|
|
let vote = VoteTransaction::new_vote(
|
|
|
|
keypair,
|
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
2019-01-31 15:51:29 -08:00
|
|
|
cluster_info.write().unwrap().push_vote(vote);
|
2018-12-18 16:06:05 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 08:03:52 -08:00
|
|
|
num_entries_to_write = i + 1;
|
|
|
|
break;
|
2018-10-10 16:49:41 -07:00
|
|
|
}
|
2018-11-12 11:40:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If leader rotation happened, only write the entries up to leader rotation.
|
|
|
|
entries.truncate(num_entries_to_write);
|
2019-01-26 00:28:08 -08:00
|
|
|
*last_entry_id.write().unwrap() = entries
|
2018-11-12 12:41:19 -08:00
|
|
|
.last()
|
|
|
|
.expect("Entries cannot be empty at this point")
|
|
|
|
.id;
|
2018-09-10 08:32:52 -07:00
|
|
|
|
2018-08-06 11:35:45 -07:00
|
|
|
inc_new_counter_info!(
|
2018-12-07 15:18:09 -08:00
|
|
|
"replicate-transactions",
|
2018-07-16 18:33:50 -07:00
|
|
|
entries.iter().map(|x| x.transactions.len()).sum()
|
|
|
|
);
|
2018-08-06 00:59:42 -07:00
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
let entries_len = entries.len() as u64;
|
|
|
|
// TODO: In line with previous behavior, this will write all the entries even if
|
|
|
|
// an error occurred processing one of the entries (causing the rest of the entries to
|
|
|
|
// not be processed).
|
2018-10-23 14:42:48 -07:00
|
|
|
if entries_len != 0 {
|
|
|
|
ledger_entry_sender.send(entries)?;
|
2018-08-05 22:04:27 -07:00
|
|
|
}
|
2018-08-03 11:06:06 -07:00
|
|
|
|
2019-01-26 00:28:08 -08:00
|
|
|
*entry_height.write().unwrap() += entries_len;
|
2019-02-04 15:33:43 -08:00
|
|
|
|
2018-09-03 02:48:11 -07:00
|
|
|
res?;
|
2018-12-13 18:43:10 -08:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"replicate_stage-duration",
|
|
|
|
duration_as_ms(&now.elapsed()) as usize
|
|
|
|
);
|
2018-11-12 11:40:32 -08:00
|
|
|
Ok(())
|
2018-05-22 15:30:46 -07:00
|
|
|
}
|
2018-09-25 15:41:29 -07:00
|
|
|
|
2019-01-29 00:21:27 -08:00
|
|
|
#[allow(clippy::new_ret_no_self, clippy::too_many_arguments)]
|
2018-07-05 12:01:40 -07:00
|
|
|
pub fn new(
|
2019-01-30 19:28:48 -08:00
|
|
|
my_id: Pubkey,
|
2019-01-31 20:12:51 -08:00
|
|
|
voting_keypair: Option<Arc<VotingKeypair>>,
|
2019-02-04 15:33:43 -08:00
|
|
|
db_ledger: Arc<DbLedger>,
|
2018-07-05 12:01:40 -07:00
|
|
|
bank: Arc<Bank>,
|
2018-10-08 19:55:54 -07:00
|
|
|
cluster_info: Arc<RwLock<ClusterInfo>>,
|
2018-09-25 15:41:29 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2019-01-26 00:28:08 -08:00
|
|
|
entry_height: Arc<RwLock<u64>>,
|
|
|
|
last_entry_id: Arc<RwLock<Hash>>,
|
|
|
|
to_leader_sender: TvuRotationSender,
|
2019-01-31 20:41:03 -08:00
|
|
|
entry_stream: Option<&String>,
|
2019-02-04 15:33:43 -08:00
|
|
|
ledger_signal_sender: SyncSender<bool>,
|
|
|
|
ledger_signal_receiver: Receiver<bool>,
|
2018-10-23 14:42:48 -07:00
|
|
|
) -> (Self, EntryReceiver) {
|
|
|
|
let (ledger_entry_sender, ledger_entry_receiver) = channel();
|
2019-01-31 20:41:03 -08:00
|
|
|
let mut entry_stream = entry_stream.cloned().map(EntryStream::new);
|
2018-07-19 21:27:35 -07:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
let exit_ = exit.clone();
|
2018-12-07 14:09:29 -08:00
|
|
|
let t_replay = Builder::new()
|
|
|
|
.name("solana-replay-stage".to_string())
|
2018-09-25 15:41:29 -07:00
|
|
|
.spawn(move || {
|
2019-02-04 15:33:43 -08:00
|
|
|
let _exit = Finalizer::new(exit_.clone());
|
2019-02-05 08:03:52 -08:00
|
|
|
let mut last_leader_id = Self::get_leader_for_next_tick(&bank);
|
|
|
|
|
|
|
|
let (mut current_slot, mut max_tick_height_for_slot) = {
|
|
|
|
let tick_height = bank.tick_height();
|
|
|
|
let leader_scheduler = bank.leader_scheduler.read().unwrap();
|
|
|
|
let current_slot = leader_scheduler.tick_height_to_slot(tick_height + 1);
|
|
|
|
let first_tick_in_current_slot =
|
|
|
|
current_slot * leader_scheduler.leader_rotation_interval;
|
|
|
|
(
|
|
|
|
current_slot,
|
|
|
|
first_tick_in_current_slot
|
|
|
|
+ leader_scheduler.num_ticks_left_in_slot(first_tick_in_current_slot),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
// Loop through db_ledger MAX_ENTRY_RECV_PER_ITER entries at a time for each
|
|
|
|
// relevant slot to see if there are any available updates
|
2018-09-25 15:41:29 -07:00
|
|
|
loop {
|
2019-02-04 15:33:43 -08:00
|
|
|
// Stop getting entries if we get exit signal
|
|
|
|
if exit_.load(Ordering::Relaxed) {
|
|
|
|
break;
|
2018-10-10 16:49:41 -07:00
|
|
|
}
|
2019-02-04 15:33:43 -08:00
|
|
|
|
|
|
|
let current_entry_height = *entry_height.read().unwrap();
|
|
|
|
|
|
|
|
let entries = {
|
|
|
|
if let Ok(entries) = db_ledger.get_slot_entries(
|
|
|
|
current_slot,
|
|
|
|
current_entry_height,
|
|
|
|
Some(MAX_ENTRY_RECV_PER_ITER as u64),
|
|
|
|
) {
|
|
|
|
entries
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let entry_len = entries.len();
|
|
|
|
// Fetch the next entries from the database
|
|
|
|
if !entries.is_empty() {
|
|
|
|
if let Err(e) = Self::process_entries(
|
|
|
|
entries,
|
|
|
|
&bank,
|
|
|
|
&cluster_info,
|
|
|
|
voting_keypair.as_ref(),
|
|
|
|
&ledger_entry_sender,
|
|
|
|
&entry_height,
|
|
|
|
&last_entry_id,
|
|
|
|
entry_stream.as_mut(),
|
|
|
|
) {
|
2019-02-05 08:03:52 -08:00
|
|
|
error!("process_entries failed: {:?}", e);
|
2019-02-04 15:33:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let current_tick_height = bank.tick_height();
|
|
|
|
|
|
|
|
// We've reached the end of a slot, reset our state and check
|
|
|
|
// for leader rotation
|
|
|
|
if max_tick_height_for_slot == current_tick_height {
|
|
|
|
// Check for leader rotation
|
2019-02-05 08:03:52 -08:00
|
|
|
let leader_id = Self::get_leader_for_next_tick(&bank);
|
|
|
|
|
|
|
|
// TODO: Remove this soon once we boot the leader from ClusterInfo
|
|
|
|
cluster_info.write().unwrap().set_leader(leader_id);
|
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
if leader_id != last_leader_id && my_id == leader_id {
|
|
|
|
to_leader_sender
|
|
|
|
.send(TvuReturnType::LeaderRotation(
|
2019-02-05 08:03:52 -08:00
|
|
|
current_tick_height,
|
2019-02-04 15:33:43 -08:00
|
|
|
*entry_height.read().unwrap(),
|
|
|
|
*last_entry_id.read().unwrap(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
current_slot += 1;
|
2019-02-05 08:03:52 -08:00
|
|
|
max_tick_height_for_slot += bank
|
2019-02-04 15:33:43 -08:00
|
|
|
.leader_scheduler
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-02-05 08:03:52 -08:00
|
|
|
.leader_rotation_interval;
|
2019-02-04 15:33:43 -08:00
|
|
|
last_leader_id = leader_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block until there are updates again
|
|
|
|
if entry_len < MAX_ENTRY_RECV_PER_ITER && ledger_signal_receiver.recv().is_err()
|
|
|
|
{
|
|
|
|
// Update disconnected, exit
|
|
|
|
break;
|
2018-07-05 15:29:49 -07:00
|
|
|
}
|
2018-05-30 13:38:15 -07:00
|
|
|
}
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-07-19 21:27:35 -07:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
(
|
|
|
|
Self {
|
|
|
|
t_replay,
|
|
|
|
exit,
|
|
|
|
ledger_signal_sender,
|
|
|
|
},
|
|
|
|
ledger_entry_receiver,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self) -> thread::Result<()> {
|
|
|
|
self.exit();
|
|
|
|
self.join()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit(&self) {
|
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
|
|
|
let _ = self.ledger_signal_sender.send(true);
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
fn get_leader_for_next_tick(bank: &Bank) -> Pubkey {
|
|
|
|
let tick_height = bank.tick_height();
|
|
|
|
let leader_scheduler = bank.leader_scheduler.read().unwrap();
|
|
|
|
let slot = leader_scheduler.tick_height_to_slot(tick_height + 1);
|
|
|
|
leader_scheduler
|
|
|
|
.get_leader_for_slot(slot)
|
|
|
|
.expect("Scheduled leader should be calculated by this point")
|
2018-05-22 14:26:28 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 21:14:08 -07:00
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
impl Service for ReplayStage {
|
2019-01-26 00:28:08 -08:00
|
|
|
type JoinReturnType = ();
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-01-26 00:28:08 -08:00
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-12-07 14:09:29 -08:00
|
|
|
self.t_replay.join()
|
2018-10-10 16:49:41 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 14:00:17 -07:00
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2019-01-29 00:21:27 -08:00
|
|
|
use super::*;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use crate::cluster_info::{ClusterInfo, Node};
|
2019-01-09 14:33:44 -08:00
|
|
|
use crate::db_ledger::create_tmp_sample_ledger;
|
2019-01-03 21:29:21 -08:00
|
|
|
use crate::db_ledger::{DbLedger, DEFAULT_SLOT_HEIGHT};
|
2019-01-09 14:33:44 -08:00
|
|
|
use crate::entry::create_ticks;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::entry::Entry;
|
|
|
|
use crate::fullnode::Fullnode;
|
2019-02-04 15:33:43 -08:00
|
|
|
use crate::genesis_block::GenesisBlock;
|
2019-02-05 08:03:52 -08:00
|
|
|
use crate::leader_scheduler::{make_active_set_entries, LeaderSchedulerConfig};
|
2019-01-26 00:28:08 -08:00
|
|
|
use crate::replay_stage::ReplayStage;
|
|
|
|
use crate::tvu::TvuReturnType;
|
2019-01-31 20:12:51 -08:00
|
|
|
use crate::voting_keypair::VotingKeypair;
|
2019-01-30 10:12:31 -08:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
|
|
|
use serde_json::Value;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-10-17 13:42:54 -07:00
|
|
|
use std::fs::remove_dir_all;
|
2018-10-10 16:49:41 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
#[test]
|
2019-02-05 08:03:52 -08:00
|
|
|
#[ignore] // TODO: Fix this test to not send all entries in slot 0
|
2018-12-07 14:09:29 -08:00
|
|
|
pub fn test_replay_stage_leader_rotation_exit() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
// Set up dummy node to host a ReplayStage
|
2018-10-10 16:49:41 -07:00
|
|
|
let my_keypair = Keypair::new();
|
|
|
|
let my_id = my_keypair.pubkey();
|
|
|
|
let my_node = Node::new_localhost_with_pubkey(my_id);
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info_me = ClusterInfo::new(my_node.info.clone());
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2018-11-02 14:32:05 -07:00
|
|
|
// Create keypair for the old leader
|
|
|
|
let old_leader_id = Keypair::new().pubkey();
|
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
// Create a ledger
|
2019-01-30 10:26:16 -08:00
|
|
|
let (mint_keypair, my_ledger_path, genesis_entry_height, mut last_id) =
|
2019-01-29 20:57:38 -08:00
|
|
|
create_tmp_sample_ledger(
|
|
|
|
"test_replay_stage_leader_rotation_exit",
|
|
|
|
10_000,
|
2019-02-05 08:03:52 -08:00
|
|
|
0,
|
2019-01-29 20:57:38 -08:00
|
|
|
old_leader_id,
|
|
|
|
500,
|
|
|
|
);
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
info!("my_id: {:?}", my_id);
|
|
|
|
info!("old_leader_id: {:?}", old_leader_id);
|
|
|
|
|
|
|
|
// Set up the LeaderScheduler so that my_id becomes the leader for epoch 1
|
|
|
|
let leader_rotation_interval = 16;
|
|
|
|
let leader_scheduler_config = LeaderSchedulerConfig::new(
|
|
|
|
leader_rotation_interval,
|
|
|
|
leader_rotation_interval,
|
|
|
|
leader_rotation_interval,
|
|
|
|
);
|
|
|
|
|
2019-01-11 12:58:31 -08:00
|
|
|
let my_keypair = Arc::new(my_keypair);
|
2019-02-05 08:03:52 -08:00
|
|
|
let (active_set_entries, voting_keypair) = make_active_set_entries(
|
|
|
|
&my_keypair,
|
|
|
|
&mint_keypair,
|
|
|
|
100,
|
|
|
|
leader_rotation_interval, // add a vote for tick_height = leader_rotation_interval
|
|
|
|
&last_id,
|
|
|
|
&last_id,
|
|
|
|
0,
|
|
|
|
);
|
2018-10-18 22:57:48 -07:00
|
|
|
last_id = active_set_entries.last().unwrap().id;
|
2019-01-03 21:29:21 -08:00
|
|
|
{
|
2019-02-04 15:33:43 -08:00
|
|
|
let (db_ledger, l_sender, l_receiver) =
|
|
|
|
DbLedger::open_with_signal(&my_ledger_path).unwrap();
|
|
|
|
let db_ledger = Arc::new(db_ledger);
|
2019-01-03 21:29:21 -08:00
|
|
|
db_ledger
|
|
|
|
.write_entries(
|
|
|
|
DEFAULT_SLOT_HEIGHT,
|
2019-01-29 20:57:38 -08:00
|
|
|
genesis_entry_height,
|
2019-01-03 21:29:21 -08:00
|
|
|
&active_set_entries,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
let genesis_block = GenesisBlock::load(&my_ledger_path)
|
|
|
|
.expect("Expected to successfully open genesis block");
|
|
|
|
|
|
|
|
// Set up the bank
|
2019-02-05 08:03:52 -08:00
|
|
|
let (bank, _, last_entry_id) = Fullnode::new_bank_from_db_ledger(
|
|
|
|
&genesis_block,
|
|
|
|
&db_ledger,
|
|
|
|
Some(&leader_scheduler_config),
|
|
|
|
);
|
2019-02-04 15:33:43 -08:00
|
|
|
|
|
|
|
// Set up the replay stage
|
|
|
|
let (rotation_sender, rotation_receiver) = channel();
|
|
|
|
let meta = db_ledger.meta().unwrap().unwrap();
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-02-05 08:03:52 -08:00
|
|
|
let bank = Arc::new(bank);
|
2019-02-04 15:33:43 -08:00
|
|
|
let (replay_stage, ledger_writer_recv) = ReplayStage::new(
|
|
|
|
my_id,
|
|
|
|
Some(Arc::new(voting_keypair)),
|
|
|
|
db_ledger.clone(),
|
2019-02-05 08:03:52 -08:00
|
|
|
bank.clone(),
|
2019-02-04 15:33:43 -08:00
|
|
|
Arc::new(RwLock::new(cluster_info_me)),
|
|
|
|
exit.clone(),
|
|
|
|
Arc::new(RwLock::new(meta.consumed)),
|
|
|
|
Arc::new(RwLock::new(last_entry_id)),
|
|
|
|
rotation_sender,
|
|
|
|
None,
|
|
|
|
l_sender,
|
|
|
|
l_receiver,
|
|
|
|
);
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
let total_entries_to_send = 2 * leader_rotation_interval as usize - 2;
|
2019-02-04 15:33:43 -08:00
|
|
|
let mut entries_to_send = vec![];
|
|
|
|
while entries_to_send.len() < total_entries_to_send {
|
2019-02-05 08:03:52 -08:00
|
|
|
let entry = Entry::new(&mut last_id, 0, 1, vec![]);
|
2019-02-04 15:33:43 -08:00
|
|
|
last_id = entry.id;
|
|
|
|
entries_to_send.push(entry);
|
|
|
|
}
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
let expected_entry_height = (active_set_entries.len() + total_entries_to_send) as u64;
|
|
|
|
let expected_last_id = entries_to_send.last().unwrap().id;
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
// Write the entries to the ledger, replay_stage should get notified of changes
|
|
|
|
db_ledger
|
|
|
|
.write_entries(DEFAULT_SLOT_HEIGHT, meta.consumed, &entries_to_send)
|
|
|
|
.unwrap();
|
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
info!("Wait for replay_stage to exit and check return value is correct");
|
2019-02-04 15:33:43 -08:00
|
|
|
assert_eq!(
|
|
|
|
Some(TvuReturnType::LeaderRotation(
|
2019-02-05 08:03:52 -08:00
|
|
|
2 * leader_rotation_interval - 1,
|
2019-02-04 15:33:43 -08:00
|
|
|
expected_entry_height,
|
|
|
|
expected_last_id,
|
|
|
|
)),
|
|
|
|
{
|
|
|
|
Some(
|
|
|
|
rotation_receiver
|
|
|
|
.recv()
|
|
|
|
.expect("should have signaled leader rotation"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
info!("Check that the entries on the ledger writer channel are correct");
|
2019-02-04 15:33:43 -08:00
|
|
|
let mut received_ticks = ledger_writer_recv
|
|
|
|
.recv()
|
2019-02-05 08:03:52 -08:00
|
|
|
.expect("Expected to receive an entry on the ledger writer receiver");
|
2019-02-04 15:33:43 -08:00
|
|
|
|
|
|
|
while let Ok(entries) = ledger_writer_recv.try_recv() {
|
|
|
|
received_ticks.extend(entries);
|
2019-01-26 00:28:08 -08:00
|
|
|
}
|
2019-02-05 08:03:52 -08:00
|
|
|
assert_eq!(&received_ticks[..], &entries_to_send[..]);
|
2018-10-10 16:49:41 -07:00
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
// Replay stage should continue running even after rotation has happened (tvu never goes down)
|
2019-02-04 15:33:43 -08:00
|
|
|
assert_eq!(exit.load(Ordering::Relaxed), false);
|
2019-02-05 08:03:52 -08:00
|
|
|
|
|
|
|
info!("Close replay_stage");
|
2019-02-04 15:33:43 -08:00
|
|
|
replay_stage
|
|
|
|
.close()
|
|
|
|
.expect("Expect successful ReplayStage exit");
|
|
|
|
}
|
2018-10-17 13:42:54 -07:00
|
|
|
let _ignored = remove_dir_all(&my_ledger_path);
|
2018-07-03 21:14:08 -07:00
|
|
|
}
|
2018-11-12 11:40:32 -08:00
|
|
|
|
|
|
|
#[test]
|
2018-12-07 14:09:29 -08:00
|
|
|
fn test_vote_error_replay_stage_correctness() {
|
|
|
|
// Set up dummy node to host a ReplayStage
|
2018-11-12 11:40:32 -08:00
|
|
|
let my_keypair = Keypair::new();
|
|
|
|
let my_id = my_keypair.pubkey();
|
|
|
|
let my_node = Node::new_localhost_with_pubkey(my_id);
|
|
|
|
|
|
|
|
// Create keypair for the leader
|
|
|
|
let leader_id = Keypair::new().pubkey();
|
|
|
|
|
2019-01-30 10:26:16 -08:00
|
|
|
let (_, my_ledger_path, _, _) = create_tmp_sample_ledger(
|
2019-01-29 20:57:38 -08:00
|
|
|
"test_vote_error_replay_stage_correctness",
|
|
|
|
10_000,
|
2019-02-05 08:03:52 -08:00
|
|
|
1,
|
2019-01-29 20:57:38 -08:00
|
|
|
leader_id,
|
|
|
|
500,
|
|
|
|
);
|
2018-11-12 11:40:32 -08:00
|
|
|
|
|
|
|
// Set up the cluster info
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
2018-11-12 11:40:32 -08:00
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
// Set up the replay stage
|
2018-11-12 11:40:32 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-01-05 12:57:52 -08:00
|
|
|
let my_keypair = Arc::new(my_keypair);
|
2019-01-31 20:12:51 -08:00
|
|
|
let voting_keypair = Arc::new(VotingKeypair::new_local(&my_keypair));
|
2019-01-26 00:28:08 -08:00
|
|
|
let (to_leader_sender, _) = channel();
|
2019-02-04 15:33:43 -08:00
|
|
|
{
|
|
|
|
let (db_ledger, l_sender, l_receiver) =
|
|
|
|
DbLedger::open_with_signal(&my_ledger_path).unwrap();
|
|
|
|
let db_ledger = Arc::new(db_ledger);
|
|
|
|
// Set up the bank
|
|
|
|
let genesis_block = GenesisBlock::load(&my_ledger_path)
|
|
|
|
.expect("Expected to successfully open genesis block");
|
|
|
|
let (bank, entry_height, last_entry_id) =
|
2019-02-05 08:03:52 -08:00
|
|
|
Fullnode::new_bank_from_db_ledger(&genesis_block, &db_ledger, None);
|
2019-02-04 15:33:43 -08:00
|
|
|
let bank = Arc::new(bank);
|
|
|
|
let (replay_stage, ledger_writer_recv) = ReplayStage::new(
|
|
|
|
my_keypair.pubkey(),
|
|
|
|
Some(voting_keypair.clone()),
|
|
|
|
db_ledger.clone(),
|
|
|
|
bank.clone(),
|
|
|
|
cluster_info_me.clone(),
|
|
|
|
exit.clone(),
|
|
|
|
Arc::new(RwLock::new(entry_height)),
|
|
|
|
Arc::new(RwLock::new(last_entry_id)),
|
|
|
|
to_leader_sender,
|
|
|
|
None,
|
|
|
|
l_sender,
|
|
|
|
l_receiver,
|
|
|
|
);
|
|
|
|
|
|
|
|
let keypair = voting_keypair.as_ref();
|
|
|
|
let vote = VoteTransaction::new_vote(keypair, bank.tick_height(), bank.last_id(), 0);
|
|
|
|
cluster_info_me.write().unwrap().push_vote(vote);
|
|
|
|
|
|
|
|
// Send ReplayStage an entry, should see it on the ledger writer receiver
|
|
|
|
let next_tick = create_ticks(1, last_entry_id);
|
2018-11-12 11:40:32 -08:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
db_ledger
|
|
|
|
.write_entries(DEFAULT_SLOT_HEIGHT, entry_height, next_tick.clone())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let received_tick = ledger_writer_recv
|
|
|
|
.recv()
|
|
|
|
.expect("Expected to recieve an entry on the ledger writer receiver");
|
|
|
|
|
|
|
|
assert_eq!(next_tick, received_tick);
|
|
|
|
|
|
|
|
replay_stage
|
|
|
|
.close()
|
|
|
|
.expect("Expect successful ReplayStage exit");
|
|
|
|
}
|
2018-11-12 11:40:32 -08:00
|
|
|
let _ignored = remove_dir_all(&my_ledger_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-01-31 15:51:29 -08:00
|
|
|
#[ignore]
|
2018-12-07 14:09:29 -08:00
|
|
|
fn test_vote_error_replay_stage_leader_rotation() {
|
2019-01-29 15:49:29 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
// Set up dummy node to host a ReplayStage
|
2018-11-12 11:40:32 -08:00
|
|
|
let my_keypair = Keypair::new();
|
|
|
|
let my_id = my_keypair.pubkey();
|
|
|
|
let my_node = Node::new_localhost_with_pubkey(my_id);
|
|
|
|
|
|
|
|
// Create keypair for the leader
|
|
|
|
let leader_id = Keypair::new().pubkey();
|
|
|
|
|
|
|
|
// Create the ledger
|
2019-01-30 10:26:16 -08:00
|
|
|
let (mint_keypair, my_ledger_path, genesis_entry_height, last_id) =
|
2019-01-24 12:04:04 -08:00
|
|
|
create_tmp_sample_ledger(
|
|
|
|
"test_vote_error_replay_stage_leader_rotation",
|
|
|
|
10_000,
|
|
|
|
1,
|
|
|
|
leader_id,
|
|
|
|
500,
|
|
|
|
);
|
2018-11-12 11:40:32 -08:00
|
|
|
|
2019-01-11 12:58:31 -08:00
|
|
|
let my_keypair = Arc::new(my_keypair);
|
2018-11-12 11:40:32 -08:00
|
|
|
// Write two entries to the ledger so that the validator is in the active set:
|
|
|
|
// 1) Give the validator a nonzero number of tokens 2) A vote from the validator.
|
|
|
|
// This will cause leader rotation after the bootstrap height
|
2019-01-31 20:12:51 -08:00
|
|
|
let (active_set_entries, voting_keypair) =
|
2019-02-05 08:03:52 -08:00
|
|
|
make_active_set_entries(&my_keypair, &mint_keypair, 100, 1, &last_id, &last_id, 0);
|
2019-01-29 20:57:38 -08:00
|
|
|
let mut last_id = active_set_entries.last().unwrap().id;
|
|
|
|
let initial_tick_height = genesis_entry_height;
|
2018-11-12 11:40:32 -08:00
|
|
|
let active_set_entries_len = active_set_entries.len() as u64;
|
2019-01-29 20:57:38 -08:00
|
|
|
let initial_non_tick_height = genesis_entry_height - initial_tick_height;
|
2019-01-03 21:29:21 -08:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
// Set up the LeaderScheduler so that this this node becomes the leader at
|
|
|
|
// bootstrap_height = num_bootstrap_slots * leader_rotation_interval
|
2018-11-12 11:40:32 -08:00
|
|
|
// Set up the LeaderScheduler so that this this node becomes the leader at
|
|
|
|
// bootstrap_height = num_bootstrap_slots * leader_rotation_interval
|
|
|
|
let leader_rotation_interval = 10;
|
|
|
|
let num_bootstrap_slots = 2;
|
|
|
|
let bootstrap_height = num_bootstrap_slots * leader_rotation_interval;
|
|
|
|
let leader_scheduler_config = LeaderSchedulerConfig::new(
|
2019-01-28 13:29:13 -08:00
|
|
|
leader_rotation_interval,
|
|
|
|
leader_rotation_interval * 2,
|
|
|
|
bootstrap_height,
|
2018-11-12 11:40:32 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Set up the cluster info
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
2018-11-12 11:40:32 -08:00
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
// Set up the replay stage
|
2019-01-26 00:28:08 -08:00
|
|
|
let (rotation_tx, rotation_rx) = channel();
|
2018-11-12 11:40:32 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-02-04 15:33:43 -08:00
|
|
|
{
|
|
|
|
let (db_ledger, l_sender, l_receiver) =
|
|
|
|
DbLedger::open_with_signal(&my_ledger_path).unwrap();
|
|
|
|
let db_ledger = Arc::new(db_ledger);
|
|
|
|
db_ledger
|
|
|
|
.write_entries(
|
|
|
|
DEFAULT_SLOT_HEIGHT,
|
|
|
|
genesis_entry_height,
|
|
|
|
&active_set_entries,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let meta = db_ledger
|
|
|
|
.meta()
|
|
|
|
.unwrap()
|
|
|
|
.expect("First slot metadata must exist");
|
|
|
|
|
|
|
|
// Set up the bank
|
|
|
|
let genesis_block = GenesisBlock::load(&my_ledger_path)
|
|
|
|
.expect("Expected to successfully open genesis block");
|
2019-02-05 08:03:52 -08:00
|
|
|
let (bank, _, last_entry_id) = Fullnode::new_bank_from_db_ledger(
|
|
|
|
&genesis_block,
|
|
|
|
&db_ledger,
|
|
|
|
Some(&leader_scheduler_config),
|
|
|
|
);
|
2019-02-04 15:33:43 -08:00
|
|
|
|
|
|
|
let voting_keypair = Arc::new(voting_keypair);
|
|
|
|
let bank = Arc::new(bank);
|
|
|
|
let (replay_stage, ledger_writer_recv) = ReplayStage::new(
|
|
|
|
my_keypair.pubkey(),
|
|
|
|
Some(voting_keypair.clone()),
|
|
|
|
db_ledger.clone(),
|
|
|
|
bank.clone(),
|
|
|
|
cluster_info_me.clone(),
|
|
|
|
exit.clone(),
|
|
|
|
Arc::new(RwLock::new(meta.consumed)),
|
|
|
|
Arc::new(RwLock::new(last_entry_id)),
|
|
|
|
rotation_tx,
|
|
|
|
None,
|
|
|
|
l_sender,
|
|
|
|
l_receiver,
|
2019-01-24 12:04:04 -08:00
|
|
|
);
|
2018-11-12 11:40:32 -08:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
let keypair = voting_keypair.as_ref();
|
|
|
|
let vote = VoteTransaction::new_vote(keypair, bank.tick_height(), bank.last_id(), 0);
|
|
|
|
cluster_info_me.write().unwrap().push_vote(vote);
|
|
|
|
|
|
|
|
// Send enough ticks to trigger leader rotation
|
|
|
|
let total_entries_to_send = (bootstrap_height - initial_tick_height) as usize;
|
|
|
|
let num_hashes = 1;
|
|
|
|
|
|
|
|
// Add on the only entries that weren't ticks to the bootstrap height to get the
|
|
|
|
// total expected entry length
|
|
|
|
let expected_entry_height =
|
|
|
|
bootstrap_height + initial_non_tick_height + active_set_entries_len;
|
|
|
|
let leader_rotation_index = (bootstrap_height - initial_tick_height - 1) as usize;
|
|
|
|
let mut expected_last_id = Hash::default();
|
|
|
|
for i in 0..total_entries_to_send {
|
|
|
|
let entry = Entry::new(&mut last_id, 0, num_hashes, vec![]);
|
|
|
|
last_id = entry.id;
|
|
|
|
db_ledger
|
|
|
|
.write_entries(
|
|
|
|
DEFAULT_SLOT_HEIGHT,
|
|
|
|
meta.consumed + i as u64,
|
|
|
|
vec![entry.clone()],
|
|
|
|
)
|
|
|
|
.expect("Expected successful database write");
|
|
|
|
// Check that the entries on the ledger writer channel are correct
|
|
|
|
let received_entry = ledger_writer_recv
|
|
|
|
.recv()
|
|
|
|
.expect("Expected to recieve an entry on the ledger writer receiver");
|
|
|
|
assert_eq!(received_entry[0], entry);
|
|
|
|
|
|
|
|
if i == leader_rotation_index {
|
|
|
|
expected_last_id = entry.id;
|
|
|
|
}
|
2019-01-26 00:28:08 -08:00
|
|
|
}
|
2019-01-29 15:49:29 -08:00
|
|
|
|
2019-02-04 15:33:43 -08:00
|
|
|
// Wait for replay_stage to exit and check return value is correct
|
|
|
|
assert_eq!(
|
|
|
|
Some(TvuReturnType::LeaderRotation(
|
|
|
|
bootstrap_height,
|
|
|
|
expected_entry_height,
|
|
|
|
expected_last_id,
|
|
|
|
)),
|
|
|
|
{
|
|
|
|
Some(
|
|
|
|
rotation_rx
|
|
|
|
.recv()
|
|
|
|
.expect("should have signaled leader rotation"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_ne!(expected_last_id, Hash::default());
|
|
|
|
//replay stage should continue running even after rotation has happened (tvu never goes down)
|
|
|
|
replay_stage
|
|
|
|
.close()
|
|
|
|
.expect("Expect successful ReplayStage exit");
|
|
|
|
}
|
2018-11-12 11:40:32 -08:00
|
|
|
let _ignored = remove_dir_all(&my_ledger_path);
|
|
|
|
}
|
2018-11-16 15:48:10 -08:00
|
|
|
|
|
|
|
#[test]
|
2018-12-07 14:09:29 -08:00
|
|
|
fn test_replay_stage_poh_error_entry_receiver() {
|
|
|
|
// Set up dummy node to host a ReplayStage
|
2018-11-16 15:48:10 -08:00
|
|
|
let my_keypair = Keypair::new();
|
|
|
|
let my_id = my_keypair.pubkey();
|
|
|
|
let my_node = Node::new_localhost_with_pubkey(my_id);
|
|
|
|
// Set up the cluster info
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
2018-11-16 15:48:10 -08:00
|
|
|
let (ledger_entry_sender, _ledger_entry_receiver) = channel();
|
2019-01-26 00:28:08 -08:00
|
|
|
let last_entry_id = Hash::default();
|
2018-11-16 15:48:10 -08:00
|
|
|
|
2019-01-26 00:28:08 -08:00
|
|
|
let entry_height = 0;
|
2018-11-16 15:48:10 -08:00
|
|
|
let mut last_id = Hash::default();
|
|
|
|
let mut entries = Vec::new();
|
|
|
|
for _ in 0..5 {
|
2018-12-10 20:03:04 -08:00
|
|
|
let entry = Entry::new(&mut last_id, 0, 1, vec![]); //just ticks
|
2018-11-16 15:48:10 -08:00
|
|
|
last_id = entry.id;
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
2019-01-05 12:57:52 -08:00
|
|
|
let my_keypair = Arc::new(my_keypair);
|
2019-01-31 20:12:51 -08:00
|
|
|
let voting_keypair = Arc::new(VotingKeypair::new_local(&my_keypair));
|
2018-12-07 14:09:29 -08:00
|
|
|
let res = ReplayStage::process_entries(
|
2019-02-04 15:33:43 -08:00
|
|
|
entries.clone(),
|
2019-02-05 08:03:52 -08:00
|
|
|
&Arc::new(Bank::new(&GenesisBlock::new(10_000).0)),
|
2018-11-16 15:48:10 -08:00
|
|
|
&cluster_info_me,
|
2019-01-31 20:12:51 -08:00
|
|
|
Some(&voting_keypair),
|
2018-11-16 15:48:10 -08:00
|
|
|
&ledger_entry_sender,
|
2019-01-26 00:28:08 -08:00
|
|
|
&Arc::new(RwLock::new(entry_height)),
|
|
|
|
&Arc::new(RwLock::new(last_entry_id)),
|
2019-01-29 00:21:27 -08:00
|
|
|
None,
|
2018-11-16 15:48:10 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => assert!(false, "Entries were not sent correctly {:?}", e),
|
|
|
|
}
|
|
|
|
|
|
|
|
entries.clear();
|
|
|
|
for _ in 0..5 {
|
2019-01-26 00:28:08 -08:00
|
|
|
let entry = Entry::new(&mut Hash::default(), 0, 1, vec![]); //just broken entries
|
2018-11-16 15:48:10 -08:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:09:29 -08:00
|
|
|
let res = ReplayStage::process_entries(
|
2019-02-04 15:33:43 -08:00
|
|
|
entries.clone(),
|
2018-11-16 15:48:10 -08:00
|
|
|
&Arc::new(Bank::default()),
|
|
|
|
&cluster_info_me,
|
2019-01-31 20:12:51 -08:00
|
|
|
Some(&voting_keypair),
|
2018-11-16 15:48:10 -08:00
|
|
|
&ledger_entry_sender,
|
2019-01-26 00:28:08 -08:00
|
|
|
&Arc::new(RwLock::new(entry_height)),
|
|
|
|
&Arc::new(RwLock::new(last_entry_id)),
|
2019-01-29 00:21:27 -08:00
|
|
|
None,
|
2018-11-16 15:48:10 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(_) => assert!(false, "Should have failed because entries are broken"),
|
|
|
|
Err(Error::BlobError(BlobError::VerificationFailed)) => (),
|
|
|
|
Err(e) => assert!(
|
|
|
|
false,
|
|
|
|
"Should have failed because with blob error, instead, got {:?}",
|
|
|
|
e
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 00:21:27 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_replay_stage_stream_entries() {
|
|
|
|
// Set up entry stream
|
|
|
|
let mut entry_stream = EntryStream::new("test_stream".to_string());
|
|
|
|
|
|
|
|
// Set up dummy node to host a ReplayStage
|
|
|
|
let my_keypair = Keypair::new();
|
|
|
|
let my_id = my_keypair.pubkey();
|
|
|
|
let my_node = Node::new_localhost_with_pubkey(my_id);
|
|
|
|
// Set up the cluster info
|
|
|
|
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
|
|
|
let (ledger_entry_sender, _ledger_entry_receiver) = channel();
|
|
|
|
let last_entry_id = Hash::default();
|
|
|
|
|
|
|
|
let entry_height = 0;
|
|
|
|
let mut last_id = Hash::default();
|
|
|
|
let mut entries = Vec::new();
|
|
|
|
let mut expected_entries = Vec::new();
|
|
|
|
for _ in 0..5 {
|
|
|
|
let entry = Entry::new(&mut last_id, 0, 1, vec![]); //just ticks
|
|
|
|
last_id = entry.id;
|
2019-01-30 10:12:31 -08:00
|
|
|
expected_entries.push(entry.clone());
|
2019-01-29 00:21:27 -08:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
let my_keypair = Arc::new(my_keypair);
|
2019-01-31 20:12:51 -08:00
|
|
|
let voting_keypair = Arc::new(VotingKeypair::new_local(&my_keypair));
|
2019-02-05 08:03:52 -08:00
|
|
|
let bank = Bank::new(&GenesisBlock::new(123).0);
|
2019-01-29 00:21:27 -08:00
|
|
|
ReplayStage::process_entries(
|
2019-02-04 15:33:43 -08:00
|
|
|
entries.clone(),
|
2019-02-05 08:03:52 -08:00
|
|
|
&Arc::new(bank),
|
2019-01-29 00:21:27 -08:00
|
|
|
&cluster_info_me,
|
2019-01-31 20:12:51 -08:00
|
|
|
Some(&voting_keypair),
|
2019-01-29 00:21:27 -08:00
|
|
|
&ledger_entry_sender,
|
|
|
|
&Arc::new(RwLock::new(entry_height)),
|
|
|
|
&Arc::new(RwLock::new(last_entry_id)),
|
|
|
|
Some(&mut entry_stream),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(entry_stream.socket.len(), 5);
|
2019-01-30 10:12:31 -08:00
|
|
|
|
|
|
|
for (i, item) in entry_stream.socket.iter().enumerate() {
|
|
|
|
let json: Value = serde_json::from_str(&item).unwrap();
|
|
|
|
let dt_str = json["dt"].as_str().unwrap();
|
|
|
|
|
|
|
|
// Ensure `ts` field parses as valid DateTime
|
|
|
|
let _dt: DateTime<FixedOffset> = DateTime::parse_from_rfc3339(dt_str).unwrap();
|
|
|
|
|
|
|
|
let entry_obj = json["entry"].clone();
|
|
|
|
let entry: Entry = serde_json::from_value(entry_obj).unwrap();
|
|
|
|
assert_eq!(entry, expected_entries[i]);
|
|
|
|
}
|
2019-01-29 00:21:27 -08:00
|
|
|
}
|
2018-07-03 21:14:08 -07:00
|
|
|
}
|