2018-07-31 13:27:22 -07:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2018-12-11 05:44:36 -08:00
|
|
|
use crate::NodeIdT;
|
2018-08-02 14:27:55 -07:00
|
|
|
|
2018-07-31 13:27:22 -07:00
|
|
|
/// A batch of contributions the algorithm has output.
|
|
|
|
#[derive(Clone, Debug)]
|
2018-08-02 14:27:55 -07:00
|
|
|
pub struct Batch<C, N> {
|
2018-11-12 03:24:29 -08:00
|
|
|
/// This batch's epoch number. Each epoch produces exactly one batch.
|
2018-07-31 13:27:22 -07:00
|
|
|
pub epoch: u64,
|
2018-11-12 03:24:29 -08:00
|
|
|
/// The set of agreed contributions, by the contributor's node ID.
|
2018-08-02 14:27:55 -07:00
|
|
|
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).
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn len<T>(&self) -> usize
|
2018-07-31 13:27:22 -07:00
|
|
|
where
|
2018-08-02 14:27:55 -07:00
|
|
|
C: AsRef<[T]>,
|
2018-07-31 13:27:22 -07:00
|
|
|
{
|
|
|
|
self.contributions
|
|
|
|
.values()
|
|
|
|
.map(C::as_ref)
|
2018-08-02 14:27:55 -07:00
|
|
|
.map(<[T]>::len)
|
2018-07-31 13:27:22 -07:00
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the batch contains no transactions.
|
2018-08-02 14:27:55 -07:00
|
|
|
pub fn is_empty<T>(&self) -> bool
|
2018-07-31 13:27:22 -07:00
|
|
|
where
|
2018-08-02 14:27:55 -07:00
|
|
|
C: AsRef<[T]>,
|
2018-07-31 13:27:22 -07:00
|
|
|
{
|
|
|
|
self.contributions
|
|
|
|
.values()
|
|
|
|
.map(C::as_ref)
|
2018-08-02 14:27:55 -07:00
|
|
|
.all(<[T]>::is_empty)
|
2018-07-31 13:27:22 -07:00
|
|
|
}
|
|
|
|
}
|