use std::mem;
use crate::dkg::Signed;
use sn_messaging::{node::SectionSigned, SectionAuthorityProvider};
use xor_name::Prefix;
type Entry = (SectionSigned<SectionAuthorityProvider>, Signed);
pub(crate) struct SplitBarrier(Vec<Entry>);
impl SplitBarrier {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn process(
&mut self,
our_prefix: &Prefix,
section_auth: SectionSigned<SectionAuthorityProvider>,
key_signed: Signed,
) -> Vec<Entry> {
if !section_auth.value.prefix.is_extension_of(our_prefix) {
return vec![(section_auth, key_signed)];
}
let (mut give, keep) =
mem::take(&mut self.0)
.into_iter()
.partition(|(cached_section_auth, _)| {
cached_section_auth.value.prefix == section_auth.value.prefix.sibling()
});
self.0 = keep;
if give.is_empty() {
self.0.push((section_auth, key_signed));
vec![]
} else {
give.push((section_auth, key_signed));
give
}
}
}