Skip to content

Commit

Permalink
Optimize calculation of prioritization fees stats (#301)
Browse files Browse the repository at this point in the history
* Optimize calculation of prioritization fees stats

* comment on arrays in response

* Fix the calculation of supp

* Fix fmt

---------

Co-authored-by: GroovieGermanikus <groovie@mango.markets>
  • Loading branch information
godmodegalactus and grooviegermanikus committed Jan 31, 2024
1 parent 5034b57 commit 4479c43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
5 changes: 5 additions & 0 deletions block_priofees/src/rpc_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ pub struct TxAggregateStats {

#[derive(Clone, Serialize, Debug)]
pub struct PrioFeesStats {
// the arrays are same size and ordered monotonically
pub by_tx: Vec<u64>,
pub by_tx_percentiles: Vec<f32>,

// the arrays are same size and ordered monotonically
pub by_cu: Vec<u64>,
pub by_cu_percentiles: Vec<f32>,

// per block stats
pub tx_count: TxAggregateStats,
pub cu_consumed: TxAggregateStats,
}
Expand Down
34 changes: 15 additions & 19 deletions block_priofees/src/stats_calculation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::rpc_data::FeePoint;
use itertools::Itertools;
use std::collections::HashMap;
use std::iter::zip;

/// `quantile` function is the same as the median if q=50, the same as the minimum if q=0 and the same as the maximum if q=100.
Expand Down Expand Up @@ -39,25 +38,22 @@ pub fn calculate_supp_percentiles(

// get stats by CU
let cu_sum: u64 = prio_fees_in_block.iter().map(|x| x.1).sum();
let mut dist_fee_by_cu: HashMap<i32, u64> = HashMap::new();
let mut agg: u64 = 0;
let mut p = 0;
let mut agg: u64 = prio_fees_in_block[0].1;
let mut index = 0;
let p_step = 5;
for (prio, cu) in &prio_fees_in_block {
agg += cu;
// write p's as long as agg beats the aggregated cu
while agg >= (cu_sum as f64 * p as f64 / 100.0) as u64 && p <= 100 {
dist_fee_by_cu.insert(p, *prio);
assert_ne!(p_step, 0, "zero steps might cause infinite loop");
p += p_step;
}
}
let dist_fee_by_cu: Vec<FeePoint> = dist_fee_by_cu
.into_iter()
.sorted_by_key(|(p, _)| *p)
.map(|(p, fees)| FeePoint {
percentile: p as u32,
fees,

let dist_fee_by_cu = (0..=100)
.step_by(p_step)
.map(|p| {
if agg < (cu_sum * p) / 100 {
index += 1;
agg += prio_fees_in_block[index].1;
}
let (prio, _) = prio_fees_in_block[index];
FeePoint {
percentile: p as u32,
fees: prio,
}
})
.collect_vec();

Expand Down

0 comments on commit 4479c43

Please sign in to comment.