hbbft/src/honey_badger/batch.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2018-07-31 13:27:22 -07:00
use std::collections::BTreeMap;
use crate::NodeIdT;
2018-07-31 13:27:22 -07:00
/// A batch of contributions the algorithm has output.
#[derive(Clone, Debug)]
pub struct Batch<C, N> {
/// This batch's epoch number. Each epoch produces exactly one batch.
2018-07-31 13:27:22 -07:00
pub epoch: u64,
/// The set of agreed contributions, by the contributor's node ID.
pub contributions: BTreeMap<N, C>,
2018-07-31 13:27:22 -07:00
}
2018-08-29 09:08:35 -07:00
impl<C, N: NodeIdT> Batch<C, N> {
2018-07-31 13:27:22 -07:00
/// Returns an iterator over references to all transactions included in the batch.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = <&'a C as IntoIterator>::Item>
where
&'a C: IntoIterator,
{
2019-11-06 18:06:44 -08:00
self.contributions.values().flatten()
2018-07-31 13:27:22 -07:00
}
/// Returns an iterator over all transactions included in the batch. Consumes the batch.
pub fn into_tx_iter(self) -> impl Iterator<Item = <C as IntoIterator>::Item>
where
C: IntoIterator,
{
self.contributions.into_iter().flat_map(|(_, vec)| vec)
}
/// Returns the number of transactions in the batch (without detecting duplicates).
pub fn len<T>(&self) -> usize
2018-07-31 13:27:22 -07:00
where
C: AsRef<[T]>,
2018-07-31 13:27:22 -07:00
{
self.contributions
.values()
.map(C::as_ref)
.map(<[T]>::len)
2018-07-31 13:27:22 -07:00
.sum()
}
/// Returns `true` if the batch contains no transactions.
pub fn is_empty<T>(&self) -> bool
2018-07-31 13:27:22 -07:00
where
C: AsRef<[T]>,
2018-07-31 13:27:22 -07:00
{
self.contributions
.values()
.map(C::as_ref)
.all(<[T]>::is_empty)
2018-07-31 13:27:22 -07:00
}
}