diff --git a/src/bin/tracker.rs b/src/bin/tracker.rs index 85e41e8..ad352ee 100644 --- a/src/bin/tracker.rs +++ b/src/bin/tracker.rs @@ -25,6 +25,7 @@ fn main() { "update took {:.3} ms", (dt.as_secs() as f64 + 1e-9f64 * dt.subsec_nanos() as f64) * 1e3 ); + info!("histogram: {:?}", tracker.fee_histogram()); thread::sleep(Duration::from_secs(1)); } } diff --git a/src/daemon.rs b/src/daemon.rs index 6ad43c5..7143afa 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -44,6 +44,14 @@ impl MempoolEntry { pub fn fee_per_vbyte(&self) -> f32 { self.fee as f32 / self.vsize as f32 } + + pub fn fee(&self) -> u64 { + self.fee + } + + pub fn vsize(&self) -> u32 { + self.vsize + } } impl Daemon { diff --git a/src/mempool.rs b/src/mempool.rs index 1f59264..45d25a3 100644 --- a/src/mempool.rs +++ b/src/mempool.rs @@ -5,11 +5,10 @@ use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::iter::FromIterator; -use query::{FundingOutput, SpendingInput}; -use types::FullHash; - error_chain!{} +const VSIZE_BIN_WIDTH: u32 = 100_000; // in vbytes + pub struct Stats { tx: Transaction, entry: MempoolEntry, @@ -28,6 +27,26 @@ impl<'a> Tracker<'a> { } } + /// Returns vector of (fee_rate, vsize) pairs, where fee_{n-1} > fee_n and vsize_n is the + /// cumulative virtual size of mempool transaction with fee in the interval [fee_{n-1}, fee_n]. + /// Note: fee_0 is implied to be infinity. + pub fn fee_histogram(&self) -> Vec<(f32, u32)> { + let mut entries: Vec<&MempoolEntry> = self.txids.values().map(|stat| &stat.entry).collect(); + entries.sort_unstable_by(|e1, e2| { + e2.fee_per_vbyte().partial_cmp(&e1.fee_per_vbyte()).unwrap() + }); + let mut histogram = Vec::new(); + let mut cumulative_vsize = 0; + for e in entries { + cumulative_vsize += e.vsize(); + if cumulative_vsize > VSIZE_BIN_WIDTH { + histogram.push((e.fee_per_vbyte(), cumulative_vsize)); + cumulative_vsize = 0; + } + } + histogram + } + pub fn update_from_daemon(&mut self) -> Result<()> { let new_txids = HashSet::::from_iter(self.daemon .getmempooltxids()