Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit afb830c

Browse files
authored
Merge pull request #18 from garious/add-historian
self-ticking logger
2 parents d7dfa8c + c1326ac commit afb830c

File tree

5 files changed

+80
-38
lines changed

5 files changed

+80
-38
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "silk"
33
description = "A silky smooth implementation of the Loom architecture"
4-
version = "0.2.1"
4+
version = "0.2.2"
55
documentation = "https://docs.rs/silk"
66
homepage = "http://loomprotocol.com/"
77
repository = "https://github.com/loomprotocol/silk"

README.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,27 @@ Create a *Historian* and send it *events* to generate an *event log*, where each
2424
is tagged with the historian's latest *hash*. Then ensure the order of events was not tampered
2525
with by verifying each entry's hash can be generated from the hash in the previous entry:
2626

27-
![historian](https://user-images.githubusercontent.com/55449/36492930-97a572be-16eb-11e8-8289-358e9507189e.png)
27+
![historian](https://user-images.githubusercontent.com/55449/36499105-7c8db6a0-16fd-11e8-8b88-c6e0f52d7a50.png)
2828

2929
```rust
3030
extern crate silk;
3131

3232
use silk::historian::Historian;
3333
use silk::log::{verify_slice, Entry, Event, Sha256Hash};
34-
use std::{thread, time};
34+
use std::thread::sleep;
35+
use std::time::Duration;
3536
use std::sync::mpsc::SendError;
3637

3738
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
38-
hist.sender.send(Event::Tick)?;
39-
thread::sleep(time::Duration::new(0, 100_000));
39+
sleep(Duration::from_millis(15));
4040
hist.sender.send(Event::UserDataKey(Sha256Hash::default()))?;
41-
thread::sleep(time::Duration::new(0, 100_000));
42-
hist.sender.send(Event::Tick)?;
41+
sleep(Duration::from_millis(10));
4342
Ok(())
4443
}
4544

4645
fn main() {
4746
let seed = Sha256Hash::default();
48-
let hist = Historian::new(&seed);
47+
let hist = Historian::new(&seed, Some(10));
4948
create_log(&hist).expect("send error");
5049
drop(hist.sender);
5150
let entries: Vec<Entry> = hist.receiver.iter().collect();

diagrams/historian.msc

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
msc {
22
client,historian,logger;
33

4-
client=>historian [ label = "Tick" ] ;
5-
historian=>logger [ label = "Tick" ] ;
64
logger=>historian [ label = "e0 = Entry{hash: h0, n: 0, event: Tick}" ] ;
75
logger=>logger [ label = "h1 = hash(h0)" ] ;
86
logger=>logger [ label = "h2 = hash(h1)" ] ;
@@ -13,8 +11,6 @@ msc {
1311
logger=>logger [ label = "h4 = hash(h3)" ] ;
1412
logger=>logger [ label = "h5 = hash(h4)" ] ;
1513
logger=>logger [ label = "h6 = hash(h5)" ] ;
16-
client=>historian [ label = "Tick" ] ;
17-
historian=>logger [ label = "Tick" ] ;
1814
logger=>historian [ label = "e2 = Entry{hash: h6, n: 3, event: Tick}" ] ;
1915
client=>historian [ label = "collect()" ] ;
2016
historian=>client [ label = "entries = [e0, e1, e2]" ] ;

src/bin/demo.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ extern crate silk;
22

33
use silk::historian::Historian;
44
use silk::log::{verify_slice, Entry, Event, Sha256Hash};
5-
use std::{thread, time};
5+
use std::thread::sleep;
6+
use std::time::Duration;
67
use std::sync::mpsc::SendError;
78

89
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
9-
hist.sender.send(Event::Tick)?;
10-
thread::sleep(time::Duration::new(0, 100_000));
10+
sleep(Duration::from_millis(15));
1111
hist.sender.send(Event::UserDataKey(Sha256Hash::default()))?;
12-
thread::sleep(time::Duration::new(0, 100_000));
13-
hist.sender.send(Event::Tick)?;
12+
sleep(Duration::from_millis(10));
1413
Ok(())
1514
}
1615

1716
fn main() {
1817
let seed = Sha256Hash::default();
19-
let hist = Historian::new(&seed);
18+
let hist = Historian::new(&seed, Some(10));
2019
create_log(&hist).expect("send error");
2120
drop(hist.sender);
2221
let entries: Vec<Entry> = hist.receiver.iter().collect();

src/historian.rs

+68-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use std::thread::JoinHandle;
99
use std::sync::mpsc::{Receiver, Sender};
10+
use std::time::{Duration, SystemTime};
1011
use log::{extend_and_hash, hash, Entry, Event, Sha256Hash};
1112

1213
pub struct Historian {
@@ -20,29 +21,48 @@ pub enum ExitReason {
2021
RecvDisconnected,
2122
SendDisconnected,
2223
}
24+
fn log_event(
25+
sender: &Sender<Entry>,
26+
num_hashes: &mut u64,
27+
end_hash: &mut Sha256Hash,
28+
event: Event,
29+
) -> Result<(), (Entry, ExitReason)> {
30+
if let Event::UserDataKey(key) = event {
31+
*end_hash = extend_and_hash(end_hash, &key);
32+
}
33+
let entry = Entry {
34+
end_hash: *end_hash,
35+
num_hashes: *num_hashes,
36+
event,
37+
};
38+
if let Err(_) = sender.send(entry.clone()) {
39+
return Err((entry, ExitReason::SendDisconnected));
40+
}
41+
*num_hashes = 0;
42+
Ok(())
43+
}
2344

2445
fn log_events(
2546
receiver: &Receiver<Event>,
2647
sender: &Sender<Entry>,
2748
num_hashes: &mut u64,
2849
end_hash: &mut Sha256Hash,
50+
epoch: SystemTime,
51+
num_ticks: &mut u64,
52+
ms_per_tick: Option<u64>,
2953
) -> Result<(), (Entry, ExitReason)> {
3054
use std::sync::mpsc::TryRecvError;
3155
loop {
56+
if let Some(ms) = ms_per_tick {
57+
let now = SystemTime::now();
58+
if now > epoch + Duration::from_millis((*num_ticks + 1) * ms) {
59+
log_event(sender, num_hashes, end_hash, Event::Tick)?;
60+
*num_ticks += 1;
61+
}
62+
}
3263
match receiver.try_recv() {
3364
Ok(event) => {
34-
if let Event::UserDataKey(key) = event {
35-
*end_hash = extend_and_hash(end_hash, &key);
36-
}
37-
let entry = Entry {
38-
end_hash: *end_hash,
39-
num_hashes: *num_hashes,
40-
event,
41-
};
42-
if let Err(_) = sender.send(entry.clone()) {
43-
return Err((entry, ExitReason::SendDisconnected));
44-
}
45-
*num_hashes = 0;
65+
log_event(sender, num_hashes, end_hash, event)?;
4666
}
4767
Err(TryRecvError::Empty) => {
4868
return Ok(());
@@ -63,15 +83,26 @@ fn log_events(
6383
/// sending back Entry messages until either the receiver or sender channel is closed.
6484
pub fn create_logger(
6585
start_hash: Sha256Hash,
86+
ms_per_tick: Option<u64>,
6687
receiver: Receiver<Event>,
6788
sender: Sender<Entry>,
6889
) -> JoinHandle<(Entry, ExitReason)> {
6990
use std::thread;
7091
thread::spawn(move || {
7192
let mut end_hash = start_hash;
7293
let mut num_hashes = 0;
94+
let mut num_ticks = 0;
95+
let epoch = SystemTime::now();
7396
loop {
74-
if let Err(err) = log_events(&receiver, &sender, &mut num_hashes, &mut end_hash) {
97+
if let Err(err) = log_events(
98+
&receiver,
99+
&sender,
100+
&mut num_hashes,
101+
&mut end_hash,
102+
epoch,
103+
&mut num_ticks,
104+
ms_per_tick,
105+
) {
75106
return err;
76107
}
77108
end_hash = hash(&end_hash);
@@ -81,11 +112,11 @@ pub fn create_logger(
81112
}
82113

83114
impl Historian {
84-
pub fn new(start_hash: &Sha256Hash) -> Self {
115+
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
85116
use std::sync::mpsc::channel;
86117
let (sender, event_receiver) = channel();
87118
let (entry_sender, receiver) = channel();
88-
let thread_hdl = create_logger(*start_hash, event_receiver, entry_sender);
119+
let thread_hdl = create_logger(*start_hash, ms_per_tick, event_receiver, entry_sender);
89120
Historian {
90121
sender,
91122
receiver,
@@ -98,14 +129,13 @@ impl Historian {
98129
mod tests {
99130
use super::*;
100131
use log::*;
132+
use std::thread::sleep;
133+
use std::time::Duration;
101134

102135
#[test]
103136
fn test_historian() {
104-
use std::thread::sleep;
105-
use std::time::Duration;
106-
107137
let zero = Sha256Hash::default();
108-
let hist = Historian::new(&zero);
138+
let hist = Historian::new(&zero, None);
109139

110140
hist.sender.send(Event::Tick).unwrap();
111141
sleep(Duration::new(0, 1_000_000));
@@ -129,12 +159,30 @@ mod tests {
129159
#[test]
130160
fn test_historian_closed_sender() {
131161
let zero = Sha256Hash::default();
132-
let hist = Historian::new(&zero);
162+
let hist = Historian::new(&zero, None);
133163
drop(hist.receiver);
134164
hist.sender.send(Event::Tick).unwrap();
135165
assert_eq!(
136166
hist.thread_hdl.join().unwrap().1,
137167
ExitReason::SendDisconnected
138168
);
139169
}
170+
171+
#[test]
172+
fn test_ticking_historian() {
173+
let zero = Sha256Hash::default();
174+
let hist = Historian::new(&zero, Some(20));
175+
sleep(Duration::from_millis(30));
176+
hist.sender.send(Event::UserDataKey(zero)).unwrap();
177+
sleep(Duration::from_millis(15));
178+
drop(hist.sender);
179+
assert_eq!(
180+
hist.thread_hdl.join().unwrap().1,
181+
ExitReason::RecvDisconnected
182+
);
183+
184+
let entries: Vec<Entry> = hist.receiver.iter().collect();
185+
assert!(entries.len() > 1);
186+
assert!(verify_slice(&entries, &zero));
187+
}
140188
}

0 commit comments

Comments
 (0)