Optimize calculation of prioritization fees stats (#301)
* 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>
This commit is contained in:
parent
7171b524f0
commit
7821d29975
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
@ -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 {
|
||||
p: p as u32,
|
||||
v: 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 {
|
||||
p: p as u32,
|
||||
v: prio,
|
||||
}
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
|
|
Loading…
Reference in New Issue