Skip to content

Commit

Permalink
Fix make_fee_histogram()
Browse files Browse the repository at this point in the history
Based on romanz/electrs@c88a0dc

Plus some minor refactoring
  • Loading branch information
shesek committed Nov 7, 2020
1 parent 8fffe57 commit 5af7bfc
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,41 @@ lazy_static! {

const VSIZE_BIN_WIDTH: u32 = 50_000; // vbytes

// Make the fee histogram our of a list of `getrawmempool true` entries
// Make the fee histogram out of a list of `getrawmempool true` entries
pub fn make_fee_histogram(mempool_entries: HashMap<Txid, Value>) -> Vec<(f32, u32)> {
let mut entries: Vec<(u32, f32)> = mempool_entries
.values()
.map(|entry| {
let size = entry["vsize"]
let mut entries: Vec<_> = mempool_entries
.into_iter()
.map(|(_, entry)| {
let vsize = entry["vsize"]
.as_u64()
.or_else(|| entry["size"].as_u64())
.unwrap(); // bitcoind is borked if this fails
let fee = entry["fee"].as_f64().unwrap();
let feerate = fee as f32 / size as f32 * 100_000_000f32;
(size as u32, feerate)
let feerate = fee as f32 / vsize as f32 * 100_000_000f32;
(vsize as u32, feerate)
})
.collect();

// XXX we should take unconfirmed parents feerates into account
// XXX should take unconfirmed parents feerates into account

entries.sort_unstable_by(|a, b| a.1.partial_cmp(&b.1).unwrap());

let mut histogram = vec![];
let mut bin_size = 0;
let mut last_feerate = None;
let mut last_feerate = 0.0;

for (size, feerate) in entries.into_iter().rev() {
bin_size += size;
if bin_size > VSIZE_BIN_WIDTH && last_feerate.map_or(true, |last| feerate > last) {
// vsize of transactions paying >= e.fee_per_vbyte()
histogram.push((feerate, bin_size));
for (vsize, feerate) in entries.into_iter().rev() {
if bin_size > VSIZE_BIN_WIDTH && last_feerate != feerate {
// vsize of transactions paying >= last_feerate
histogram.push((last_feerate, bin_size));
bin_size = 0;
}
last_feerate = Some(feerate);
bin_size += vsize;
last_feerate = feerate;
}

if let Some(feerate) = last_feerate {
histogram.push((feerate, bin_size));
if bin_size > 0 {
histogram.push((last_feerate, bin_size));
}

histogram
Expand Down

0 comments on commit 5af7bfc

Please sign in to comment.