2018-10-25 14:56:21 -07:00
|
|
|
//! The `poh_service` module implements a service that records the passing of
|
|
|
|
//! "ticks", a measure of time in the PoH stream
|
|
|
|
|
2019-02-24 08:59:49 -08:00
|
|
|
use crate::poh_recorder::PohRecorder;
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::service::Service;
|
2019-02-18 20:28:10 -07:00
|
|
|
use solana_sdk::timing::NUM_TICKS_PER_SECOND;
|
2018-10-25 14:56:21 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2019-02-26 10:48:18 -08:00
|
|
|
use std::sync::mpsc::SyncSender;
|
2019-02-24 08:59:49 -08:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-02-18 20:28:10 -07:00
|
|
|
use std::thread::{self, sleep, Builder, JoinHandle};
|
2018-10-25 14:56:21 -07:00
|
|
|
use std::time::Duration;
|
2019-02-06 21:09:46 -08:00
|
|
|
|
2019-02-26 10:48:18 -08:00
|
|
|
#[derive(Clone)]
|
2019-02-09 09:25:11 -08:00
|
|
|
pub enum PohServiceConfig {
|
2019-02-06 21:09:46 -08:00
|
|
|
/// * `Tick` - Run full PoH thread. Tick is a rough estimate of how many hashes to roll before
|
|
|
|
/// transmitting a new entry.
|
2018-10-25 14:56:21 -07:00
|
|
|
Tick(usize),
|
2019-02-06 21:09:46 -08:00
|
|
|
/// * `Sleep`- Low power mode. Sleep is a rough estimate of how long to sleep before rolling 1
|
|
|
|
/// PoH once and producing 1 tick.
|
2018-10-25 14:56:21 -07:00
|
|
|
Sleep(Duration),
|
2019-02-26 10:48:18 -08:00
|
|
|
/// each node in simulation will be blocked until the receiver reads their step
|
|
|
|
Step(SyncSender<()>),
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
|
2019-02-09 09:25:11 -08:00
|
|
|
impl Default for PohServiceConfig {
|
|
|
|
fn default() -> PohServiceConfig {
|
2018-10-25 14:56:21 -07:00
|
|
|
// TODO: Change this to Tick to enable PoH
|
2019-03-01 13:10:17 -08:00
|
|
|
PohServiceConfig::Sleep(Duration::from_millis(1000 / NUM_TICKS_PER_SECOND))
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PohService {
|
2019-02-24 08:59:49 -08:00
|
|
|
tick_producer: JoinHandle<()>,
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PohService {
|
2019-02-17 20:15:34 -07:00
|
|
|
pub fn new(
|
2019-02-24 08:59:49 -08:00
|
|
|
poh_recorder: Arc<Mutex<PohRecorder>>,
|
2019-02-26 10:48:18 -08:00
|
|
|
config: &PohServiceConfig,
|
2019-03-04 16:33:14 -08:00
|
|
|
poh_exit: &Arc<AtomicBool>,
|
2019-02-24 08:59:49 -08:00
|
|
|
) -> Self {
|
2018-10-25 14:56:21 -07:00
|
|
|
// PohService is a headless producer, so when it exits it should notify the banking stage.
|
|
|
|
// Since channel are not used to talk between these threads an AtomicBool is used as a
|
|
|
|
// signal.
|
|
|
|
let poh_exit_ = poh_exit.clone();
|
|
|
|
// Single thread to generate ticks
|
2019-02-26 10:48:18 -08:00
|
|
|
let config = config.clone();
|
2018-10-25 14:56:21 -07:00
|
|
|
let tick_producer = Builder::new()
|
|
|
|
.name("solana-poh-service-tick_producer".to_string())
|
|
|
|
.spawn(move || {
|
2019-02-24 08:59:49 -08:00
|
|
|
let poh_recorder = poh_recorder;
|
2019-02-26 10:48:18 -08:00
|
|
|
Self::tick_producer(&poh_recorder, &config, &poh_exit_);
|
2018-10-25 14:56:21 -07:00
|
|
|
poh_exit_.store(true, Ordering::Relaxed);
|
2018-12-07 20:01:28 -07:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-10-25 14:56:21 -07:00
|
|
|
|
2019-03-04 20:50:02 -08:00
|
|
|
Self { tick_producer }
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
|
2019-01-26 13:58:08 +05:30
|
|
|
fn tick_producer(
|
2019-02-24 08:59:49 -08:00
|
|
|
poh: &Arc<Mutex<PohRecorder>>,
|
2019-02-26 10:48:18 -08:00
|
|
|
config: &PohServiceConfig,
|
2019-01-26 13:58:08 +05:30
|
|
|
poh_exit: &AtomicBool,
|
2019-02-24 08:59:49 -08:00
|
|
|
) {
|
2018-10-25 14:56:21 -07:00
|
|
|
loop {
|
|
|
|
match config {
|
2019-02-09 09:25:11 -08:00
|
|
|
PohServiceConfig::Tick(num) => {
|
2019-02-26 10:48:18 -08:00
|
|
|
for _ in 1..*num {
|
2019-02-24 08:59:49 -08:00
|
|
|
poh.lock().unwrap().hash();
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-09 09:25:11 -08:00
|
|
|
PohServiceConfig::Sleep(duration) => {
|
2019-02-26 10:48:18 -08:00
|
|
|
sleep(*duration);
|
|
|
|
}
|
|
|
|
PohServiceConfig::Step(sender) => {
|
|
|
|
let r = sender.send(());
|
|
|
|
if r.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 08:59:49 -08:00
|
|
|
poh.lock().unwrap().tick();
|
2018-10-25 14:56:21 -07:00
|
|
|
if poh_exit.load(Ordering::Relaxed) {
|
2019-02-24 08:59:49 -08:00
|
|
|
return;
|
2018-10-25 14:56:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service for PohService {
|
2019-02-24 08:59:49 -08:00
|
|
|
type JoinReturnType = ();
|
2018-10-25 14:56:21 -07:00
|
|
|
|
2019-02-24 08:59:49 -08:00
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-10-25 14:56:21 -07:00
|
|
|
self.tick_producer.join()
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 12:49:41 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-02-09 09:25:11 -08:00
|
|
|
use super::*;
|
2019-02-24 08:59:49 -08:00
|
|
|
use crate::poh_recorder::WorkingBank;
|
|
|
|
use crate::result::Result;
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::test_tx::test_tx;
|
2019-02-19 16:17:36 -08:00
|
|
|
use solana_runtime::bank::Bank;
|
2019-02-18 23:26:22 -07:00
|
|
|
use solana_sdk::genesis_block::GenesisBlock;
|
2018-12-05 12:49:41 -08:00
|
|
|
use solana_sdk::hash::hash;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poh_service() {
|
2019-02-05 08:03:52 -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-03-02 10:25:16 -08:00
|
|
|
let prev_hash = bank.last_blockhash();
|
2019-03-15 13:22:16 -07:00
|
|
|
let (poh_recorder, entry_receiver) = PohRecorder::new(
|
|
|
|
bank.tick_height(),
|
|
|
|
prev_hash,
|
|
|
|
bank.slot(),
|
|
|
|
Some(4),
|
|
|
|
bank.ticks_per_slot(),
|
|
|
|
);
|
2019-03-03 16:44:06 -08:00
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
2018-12-05 12:49:41 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-02-19 16:17:36 -08:00
|
|
|
let working_bank = WorkingBank {
|
|
|
|
bank: bank.clone(),
|
|
|
|
min_tick_height: bank.tick_height(),
|
|
|
|
max_tick_height: std::u64::MAX,
|
|
|
|
};
|
2018-12-05 12:49:41 -08:00
|
|
|
|
|
|
|
let entry_producer: JoinHandle<Result<()>> = {
|
|
|
|
let poh_recorder = poh_recorder.clone();
|
|
|
|
let exit = exit.clone();
|
|
|
|
|
|
|
|
Builder::new()
|
|
|
|
.name("solana-poh-service-entry_producer".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
loop {
|
|
|
|
// send some data
|
|
|
|
let h1 = hash(b"hello world!");
|
|
|
|
let tx = test_tx();
|
2019-02-24 08:59:49 -08:00
|
|
|
poh_recorder.lock().unwrap().record(h1, vec![tx]).unwrap();
|
2018-12-05 12:49:41 -08:00
|
|
|
|
|
|
|
if exit.load(Ordering::Relaxed) {
|
|
|
|
break Ok(());
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 20:01:28 -07:00
|
|
|
})
|
|
|
|
.unwrap()
|
2018-12-05 12:49:41 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const HASHES_PER_TICK: u64 = 2;
|
2019-02-24 08:59:49 -08:00
|
|
|
let poh_service = PohService::new(
|
2019-02-19 16:17:36 -08:00
|
|
|
poh_recorder.clone(),
|
2019-02-26 10:48:18 -08:00
|
|
|
&PohServiceConfig::Tick(HASHES_PER_TICK as usize),
|
2019-03-04 20:50:02 -08:00
|
|
|
&exit,
|
2019-02-09 09:25:11 -08:00
|
|
|
);
|
2019-02-24 08:59:49 -08:00
|
|
|
poh_recorder.lock().unwrap().set_working_bank(working_bank);
|
2019-02-19 16:17:36 -08:00
|
|
|
|
2018-12-05 12:49:41 -08:00
|
|
|
// get some events
|
|
|
|
let mut hashes = 0;
|
|
|
|
let mut need_tick = true;
|
|
|
|
let mut need_entry = true;
|
|
|
|
let mut need_partial = true;
|
|
|
|
|
|
|
|
while need_tick || need_entry || need_partial {
|
2019-03-03 16:44:06 -08:00
|
|
|
for entry in entry_receiver.recv().unwrap().1 {
|
2019-02-25 13:50:31 -08:00
|
|
|
let entry = &entry.0;
|
2018-12-05 12:49:41 -08:00
|
|
|
if entry.is_tick() {
|
|
|
|
assert!(entry.num_hashes <= HASHES_PER_TICK);
|
|
|
|
|
|
|
|
if entry.num_hashes == HASHES_PER_TICK {
|
|
|
|
need_tick = false;
|
|
|
|
} else {
|
|
|
|
need_partial = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes += entry.num_hashes;
|
|
|
|
|
|
|
|
assert_eq!(hashes, HASHES_PER_TICK);
|
|
|
|
|
|
|
|
hashes = 0;
|
|
|
|
} else {
|
|
|
|
assert!(entry.num_hashes >= 1);
|
|
|
|
need_entry = false;
|
|
|
|
hashes += entry.num_hashes - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
2019-01-28 14:52:35 -08:00
|
|
|
let _ = poh_service.join().unwrap();
|
|
|
|
let _ = entry_producer.join().unwrap();
|
2018-12-05 12:49:41 -08:00
|
|
|
}
|
|
|
|
}
|