|
| 1 | +use super::{AllocationStats, DefaultFromExtra, DurationInfo, SenderFeeStats}; |
| 2 | +use alloy::primitives::Address; |
| 3 | +use std::{ |
| 4 | + collections::{HashMap, HashSet}, |
| 5 | + ops::AddAssign, |
| 6 | + time::{Duration, Instant}, |
| 7 | +}; |
| 8 | + |
| 9 | +pub trait GlobalTracker { |
| 10 | + fn should_remove(&self) -> bool; |
| 11 | +} |
| 12 | + |
| 13 | +impl GlobalTracker for u128 { |
| 14 | + fn should_remove(&self) -> bool { |
| 15 | + *self == 0 |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +type GlobalFeeTracker = u128; |
| 20 | + |
| 21 | +#[derive(Debug, Clone, Default)] |
| 22 | +pub struct GenericTracker<F, E> { |
| 23 | + pub(super) id_to_fee: HashMap<Address, F>, |
| 24 | + pub(super) total_fee: u128, |
| 25 | + pub(super) extra_data: E, |
| 26 | +} |
| 27 | + |
| 28 | +impl<F, E> GenericTracker<F, E> |
| 29 | +where |
| 30 | + F: AddAssign<GlobalFeeTracker> + AllocationStats<GlobalFeeTracker> + DefaultFromExtra<E>, |
| 31 | +{ |
| 32 | + /// Updates and overwrite the fee counter into the specific |
| 33 | + /// value provided. |
| 34 | + /// |
| 35 | + /// IMPORTANT: This function does not affect the buffer window fee |
| 36 | + pub fn update(&mut self, id: Address, value: u128) { |
| 37 | + if !value.should_remove() { |
| 38 | + // insert or update, if update remove old fee from total |
| 39 | + let fee = self |
| 40 | + .id_to_fee |
| 41 | + .entry(id) |
| 42 | + .or_insert(F::default_from_extra(&self.extra_data)); |
| 43 | + self.total_fee -= fee.get_fee(); |
| 44 | + fee.update(value); |
| 45 | + self.total_fee += value; |
| 46 | + } else if let Some(old_fee) = self.id_to_fee.remove(&id) { |
| 47 | + self.total_fee -= old_fee.get_fee(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn get_heaviest_allocation_id(&mut self) -> Option<Address> { |
| 52 | + // just loop over and get the biggest fee |
| 53 | + self.id_to_fee |
| 54 | + .iter_mut() |
| 55 | + .filter(|(_, fee)| !fee.should_filter_out()) |
| 56 | + .fold(None, |acc: Option<(&Address, u128)>, (addr, value)| { |
| 57 | + if let Some((_, max_fee)) = acc { |
| 58 | + if value.get_valid_fee() > max_fee { |
| 59 | + Some((addr, value.get_valid_fee())) |
| 60 | + } else { |
| 61 | + acc |
| 62 | + } |
| 63 | + } else { |
| 64 | + Some((addr, value.get_valid_fee())) |
| 65 | + } |
| 66 | + }) |
| 67 | + .filter(|(_, fee)| *fee > 0) |
| 68 | + .map(|(&id, _)| id) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn get_list_of_allocation_ids(&self) -> HashSet<Address> { |
| 72 | + self.id_to_fee.keys().cloned().collect() |
| 73 | + } |
| 74 | + |
| 75 | + pub fn get_total_fee(&self) -> u128 { |
| 76 | + self.total_fee |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl GenericTracker<SenderFeeStats, DurationInfo> { |
| 81 | + pub fn new(buffer_duration: Duration) -> Self { |
| 82 | + Self { |
| 83 | + extra_data: DurationInfo { buffer_duration }, |
| 84 | + total_fee: 0, |
| 85 | + id_to_fee: Default::default(), |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Adds into the total_fee entry and buffer window totals |
| 90 | + /// |
| 91 | + /// It's important to notice that `value` cannot be less than |
| 92 | + /// zero, so the only way to make this counter lower is by using |
| 93 | + /// `update` function |
| 94 | + pub fn add(&mut self, id: Address, value: u128) { |
| 95 | + let entry = self |
| 96 | + .id_to_fee |
| 97 | + .entry(id) |
| 98 | + .or_insert(SenderFeeStats::default_from_extra(&self.extra_data)); |
| 99 | + self.total_fee += value; |
| 100 | + *entry += value; |
| 101 | + if self.extra_data.buffer_duration > Duration::ZERO { |
| 102 | + let now = Instant::now(); |
| 103 | + entry.entries.push_back((now, value)); |
| 104 | + entry.fee_in_buffer += value; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + pub fn get_total_fee_outside_buffer(&mut self) -> u128 { |
| 109 | + self.total_fee - self.get_buffer_fee().min(self.total_fee) |
| 110 | + } |
| 111 | + |
| 112 | + pub fn get_buffer_fee(&mut self) -> u128 { |
| 113 | + self.id_to_fee |
| 114 | + .values_mut() |
| 115 | + .fold(0u128, |acc, expiring| acc + expiring.get_sum()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<E> GenericTracker<SenderFeeStats, E> { |
| 120 | + pub fn block_allocation_id(&mut self, address: Address) { |
| 121 | + self.id_to_fee.entry(address).and_modify(|v| { |
| 122 | + v.blocked = true; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + pub fn unblock_allocation_id(&mut self, address: Address) { |
| 127 | + self.id_to_fee.entry(address).and_modify(|v| { |
| 128 | + v.blocked = false; |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl AllocationStats<GlobalFeeTracker> for u128 { |
| 134 | + fn update(&mut self, v: u128) { |
| 135 | + *self = v; |
| 136 | + } |
| 137 | + |
| 138 | + fn should_filter_out(&self) -> bool { |
| 139 | + *self == 0 |
| 140 | + } |
| 141 | + |
| 142 | + fn get_fee(&self) -> u128 { |
| 143 | + *self |
| 144 | + } |
| 145 | + |
| 146 | + fn get_valid_fee(&mut self) -> u128 { |
| 147 | + self.get_fee() |
| 148 | + } |
| 149 | +} |
0 commit comments