Lines
90.7 %
Functions
76 %
Branches
100 %
//! General functions for use in consensus calculations
//!
//! Most functions here have signatures like [`Aggregate::aggregate`], for use with `calc!`,
//! for the precise types found in netstatus field(s).
use super::*;
/// Return the low median of the inputs
pub(super) fn low_median<'i, T: Ord + Clone + 'i, AC>(
_context: ConsensusContextRefs<AC>,
inputs: impl ComponentInVotes<&'i T>,
) -> Result<T, ConsensusError> {
low_median_raw(inputs.votes().cloned()).ok_or(ConsensusError::NoVotes)
}
/// Return the low median of the inputs, without cloning, and without context
pub(super) fn low_median_raw<'i, T: Ord + 'i>(inputs: impl Iterator<Item = T>) -> Option<T> {
let mut all = inputs.collect_vec();
all.sort();
let i = all.len().checked_sub(1)? / 2;
all.into_iter().nth(i)
/// Return the value which is most popular, without cloning, and without context
///
/// Returns `None` iff the input iterator is empty.
/// See [`choose_most_popular_amongst_votes`].
pub(super) fn choose_most_popular_amongst_votes_raw<T, K>(
inputs: impl ComponentInVotes<T>,
ord_key: impl for<'i> Fn(&'i T) -> K,
) -> Option<T>
where
T: Eq + Hash,
K: Ord,
{
let counts = inputs.votes().counts();
let best = counts.values().copied().max()?;
counts
.into_iter()
.filter(|(_v, count)| count == &best)
.map(|(v, _count)| v)
.at_most_one()
.unwrap_or_else(|several| several.min_by_key(ord_key))
/// Return the value which is most popular
/// If there are several values which are equally most popular,
/// ties are broken in favour of the earliest according to `ord`.
/// (We avoid calling `ord` unless it is necessary.)
/// This is used for `v` and `pr` items in routerstatuses:
/// * <https://spec.torproject.org/dir-spec/computing-consensus.html>
/// * <https://spec.torproject.org/dir-spec/consensus-formats.html#item:pr>
pub(super) fn choose_most_popular_amongst_votes<'i, T, K, AC>(
ord_key: impl Fn(&'i T) -> K,
) -> Result<Option<T>, ConsensusError>
T: Eq + Hash + Clone + 'i,
Ok(choose_most_popular_amongst_votes_raw(inputs, |v| ord_key(v)).cloned())
/// Helper for choosing the lexically last by string representation
/// Pass as `ord_key` to eg [`choose_most_popular_amongst_votes`].
pub(super) fn ord_string_reverse_choose_lexically_latest<T: ToString>(t: &T) -> impl Ord {
std::cmp::Reverse(t.to_string())
/// Extension trait for `ComponentInVotes<Option<_>>`
pub(super) trait ComponentInVotesOptionExt<'i, T: 'i> {
/// Filter out the votes with opinions
/// Convenience method equivalent to `.iter().map(|(_vnum, v)| v)`.
fn with_opinions(self) -> impl Iterator<Item = (VoterNum, &'i T)> + Clone;
impl<'i, I, T> ComponentInVotesOptionExt<'i, T> for I
I: Iterator<Item = (VoterNum, &'i Option<T>)> + Clone,
T: 'i,
/// Yield the votes with opinions, ie, that voted `Some`
fn with_opinions(self) -> impl Iterator<Item = (VoterNum, &'i T)> + Clone {
self.clone()
.filter_map(|(vnum, rs_v)| Some((vnum, rs_v.as_ref()?)))
#[cfg(test)]
mod test {
// @@ begin test lint list maintained by maint/add_warning @@
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::string_slice)] // See arti#2571
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
#[test]
fn low_medians() {
let check = |exp: i32, inp: &[i32]| {
assert_eq!(low_median_raw(inp.iter().cloned()), Some(exp), "{inp:?}");
};
assert_eq!(low_median_raw::<i32>([].into_iter()), None);
check(1, &[1]);
check(1, &[1, 2]);
check(2, &[1, 2, 3]);
check(2, &[1, 2, 3, 4]);
check(3, &[1, 2, 3, 4, 5]);