use std::collections::BTreeMap;
use std::collections::BTreeSet;
use rings_core::dht::Did;
use super::ADMISSION_WINDOW_QUANTA;
use super::ADMISSION_WINDOW_QUANTA_WIDE;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct OnionSlotId(usize);
impl OnionSlotId {
pub(crate) const fn new(index: usize) -> Self {
Self(index)
}
pub(crate) const fn index(self) -> usize {
self.0
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct QuantumCharge {
quantum: u128,
units: u32,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(super) struct QuantumLedger {
charges: [QuantumCharge; ADMISSION_WINDOW_QUANTA],
}
impl QuantumLedger {
pub(super) fn load(&self, quantum: u128) -> u32 {
self.charges
.iter()
.filter(|charge| Self::in_window(charge.quantum, quantum))
.fold(0_u32, |load, charge| load.saturating_add(charge.units))
}
pub(super) fn charged(self, quantum: u128, units: u32, cap: u32) -> Option<Self> {
self.load(quantum)
.checked_add(units)
.filter(|load| *load <= cap)?;
let mut next = self;
let [head, tail @ ..] = &mut next.charges;
let cell = tail.iter_mut().fold(head, |best, charge| {
if (charge.quantum != quantum, charge.quantum, charge.units)
< (best.quantum != quantum, best.quantum, best.units)
{
charge
} else {
best
}
});
let charged = if cell.quantum == quantum {
cell.units.saturating_add(units)
} else {
units
};
*cell = QuantumCharge {
quantum,
units: charged,
};
Some(next)
}
pub(super) fn merge_in_window(mut self, quantum: u128, other: &Self, cap: u32) -> Self {
for charge in &other.charges {
if charge.units == 0 || !Self::in_window(charge.quantum, quantum) {
continue;
}
let headroom = cap.saturating_sub(self.load(quantum));
let added = charge.units.min(headroom);
if added == 0 {
continue;
}
let [head, tail @ ..] = &mut self.charges;
let cell = tail.iter_mut().fold(head, |best, current| {
if (
current.quantum != charge.quantum,
current.quantum,
current.units,
) < (best.quantum != charge.quantum, best.quantum, best.units)
{
current
} else {
best
}
});
let units = if cell.quantum == charge.quantum {
cell.units.saturating_add(added)
} else {
added
};
*cell = QuantumCharge {
quantum: charge.quantum,
units,
};
}
self
}
pub(super) fn in_window(charged: u128, quantum: u128) -> bool {
charged.saturating_add(ADMISSION_WINDOW_QUANTA_WIDE) > quantum
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(super) struct SlotLedger {
pub(super) ledger: QuantumLedger,
pub(super) live: BTreeMap<Did, BTreeSet<u64>>,
pub(super) retained: BTreeMap<Did, u128>,
}
impl SlotLedger {
pub(super) fn is_free(&self) -> bool {
self.live.is_empty()
}
pub(super) fn is_releasable_at(&self, quantum: u128) -> bool {
self.live.is_empty() && self.ledger.load(quantum) == 0
}
pub(super) fn contains_live(&self, did: Did, generation: u64) -> bool {
self.live
.get(&did)
.is_some_and(|generations| generations.contains(&generation))
}
pub(super) fn contains_live_peer(&self, did: Did) -> bool {
self.live.contains_key(&did)
}
pub(super) fn has_unexpired_charge(&self, did: Did, quantum: u128) -> bool {
self.ledger.load(quantum) > 0
&& self
.retained
.get(&did)
.is_some_and(|&charged_quantum| QuantumLedger::in_window(charged_quantum, quantum))
}
pub(super) fn live_link_count(&self) -> usize {
self.live.values().map(BTreeSet::len).sum()
}
pub(super) fn remove_live_generation(&mut self, did: Did, generation: u64) -> Option<bool> {
let generations = self.live.get_mut(&did)?;
if !generations.remove(&generation) {
return None;
}
let empty = generations.is_empty();
if empty {
self.live.remove(&did);
}
Some(empty)
}
pub(super) fn take_drained_retained(&mut self, quantum: u128) -> Vec<Did> {
let retained = std::mem::take(&mut self.retained);
let (kept, drained) = match self.ledger.load(quantum) == 0 {
true => (BTreeMap::new(), retained),
false => retained.into_iter().partition(|&(_, retired_quantum)| {
QuantumLedger::in_window(retired_quantum, quantum)
}),
};
self.retained = kept;
drained
.into_keys()
.filter(|did| !self.live.contains_key(did))
.collect()
}
}