Skip to content

Commit 9be7a70

Browse files
authored
Merge pull request #60 from rustaceanrob/12-17-hintsfile-analysis
Add hintfile analyzer
2 parents 6775a1b + 64177fa commit 9be7a70

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["accumulator", "hintfile", "node"]
2+
members = ["accumulator", "analysis", "hintfile", "node"]
33
default-members = ["accumulator"]
44
resolver = "2"
55

analysis/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "analysis"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
csv = "1.4.0"
8+
hintfile = { path = "../hintfile/" }

analysis/src/main.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::fs::File;
2+
3+
use hintfile::Hints;
4+
5+
fn differentials(indexes: &[u64]) -> Vec<u64> {
6+
indexes
7+
.iter()
8+
.zip(indexes.iter().skip(1))
9+
.map(|(a, b)| b - a)
10+
.collect()
11+
}
12+
13+
fn average(diffs: &[u64]) -> f64 {
14+
let sum = diffs.iter().sum::<u64>() as f64;
15+
if !diffs.is_empty() {
16+
return sum / diffs.len() as f64;
17+
}
18+
0.
19+
}
20+
21+
fn main() {
22+
let path = std::env::args()
23+
.nth(1)
24+
.expect("Usage: </path/to/hintsfile>");
25+
let file = File::open(path).unwrap();
26+
let mut hints = Hints::from_file(file);
27+
let csv = File::create("./results.csv").unwrap();
28+
let mut wtr = csv::Writer::from_writer(csv);
29+
for height in 1..=hints.stop_height() {
30+
let unspents = hints.get_indexes(height);
31+
let max_unspent = unspents.iter().max().unwrap_or(&0);
32+
let diffs = differentials(&unspents);
33+
let average_diff = average(&diffs);
34+
println!("Block height: {height}; average diff: {average_diff}; max index: {max_unspent}; diffs: {diffs:?}");
35+
wtr.write_record(vec![
36+
height.to_string(),
37+
average_diff.to_string(),
38+
max_unspent.to_string(),
39+
])
40+
.unwrap();
41+
// std::thread::sleep(Duration::from_millis(500));
42+
}
43+
}

0 commit comments

Comments
 (0)