Do not remove entry cache when the threshold is reached (#266)
Co-authored-by: Gusto <bacvinka@gmail.com>
This commit is contained in:
parent
9f71dbb24c
commit
adf935830e
@ -1,18 +1,18 @@
|
|||||||
use consensus_engine::View;
|
use consensus_engine::View;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
pub(crate) struct Tally<T: core::hash::Hash + Eq> {
|
pub(crate) struct Tally<T: core::hash::Hash + Eq + Clone> {
|
||||||
cache: HashMap<View, HashSet<T>>,
|
cache: HashMap<View, HashSet<T>>,
|
||||||
threshold: usize,
|
threshold: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: core::hash::Hash + Eq> Default for Tally<T> {
|
impl<T: core::hash::Hash + Eq + Clone> Default for Tally<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(2)
|
Self::new(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: core::hash::Hash + Eq> Tally<T> {
|
impl<T: core::hash::Hash + Eq + Clone> Tally<T> {
|
||||||
pub fn new(threshold: usize) -> Self {
|
pub fn new(threshold: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
cache: Default::default(),
|
cache: Default::default(),
|
||||||
@ -27,9 +27,9 @@ impl<T: core::hash::Hash + Eq> Tally<T> {
|
|||||||
pub fn tally_by(&mut self, view: View, message: T, threshold: usize) -> Option<HashSet<T>> {
|
pub fn tally_by(&mut self, view: View, message: T, threshold: usize) -> Option<HashSet<T>> {
|
||||||
let entries = self.cache.entry(view).or_default();
|
let entries = self.cache.entry(view).or_default();
|
||||||
entries.insert(message);
|
entries.insert(message);
|
||||||
let entries = entries.len();
|
let entries_len = entries.len();
|
||||||
if entries == threshold {
|
if entries_len == threshold {
|
||||||
Some(self.cache.remove(&view).unwrap())
|
Some(entries.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user