Files
solana/src/recorder.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2018-03-29 12:20:54 -06:00
//! The `recorder` module provides an object for generating a Proof of History.
//! It records Transaction items on behalf of its users.
2018-03-03 14:24:32 -07:00
2018-05-16 17:49:58 -06:00
use entry::Entry;
use hash::{hash, Hash};
use std::time::{Duration, Instant};
2018-05-24 00:29:01 -06:00
use transaction::Transaction;
2018-03-03 14:24:32 -07:00
pub struct Recorder {
2018-03-20 23:15:44 -06:00
last_hash: Hash,
num_hashes: u64,
2018-05-14 13:58:42 -06:00
num_ticks: u32,
2018-03-03 14:24:32 -07:00
}
impl Recorder {
pub fn new(last_hash: Hash) -> Self {
Recorder {
2018-04-02 09:30:10 -06:00
last_hash,
2018-03-03 14:24:32 -07:00
num_hashes: 0,
num_ticks: 0,
}
}
2018-03-20 23:15:44 -06:00
pub fn hash(&mut self) {
self.last_hash = hash(&self.last_hash);
self.num_hashes += 1;
}
2018-05-25 15:51:41 -06:00
pub fn record(&mut self, transactions: Vec<Transaction>) -> Entry {
Entry::new_mut(&mut self.last_hash, &mut self.num_hashes, transactions)
2018-03-03 14:24:32 -07:00
}
pub fn tick(&mut self, start_time: Instant, tick_duration: Duration) -> Option<Entry> {
if start_time.elapsed() > tick_duration * (self.num_ticks + 1) {
// TODO: don't let this overflow u32
self.num_ticks += 1;
Some(self.record(vec![]))
} else {
None
2018-03-03 14:24:32 -07:00
}
}
}