Move sender queue impls out of Honey Badger algorithms (#321)

* moved SenderQueueable impls out of HB algorithm modules and into sender_queue

* added sender_queue::honey_badger
This commit is contained in:
Vladimir Komendantskiy 2018-11-06 17:22:28 +00:00 committed by GitHub
parent bb64be55af
commit 697565c97a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 116 deletions

View File

@ -72,8 +72,6 @@ mod dynamic_honey_badger;
mod error;
mod votes;
pub mod sender_queueable;
use std::cmp::Ordering;
use std::collections::BTreeMap;

View File

@ -1,84 +0,0 @@
use rand::Rand;
use serde::{de::DeserializeOwned, Serialize};
use super::{Batch, Change, ChangeState, DynamicHoneyBadger, Epoch, Message, NodeChange};
use sender_queue::{
SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
SenderQueueableOutput,
};
use {Contribution, Epoched, NodeIdT};
impl<C, N> SenderQueueableOutput<N, Message<N>> for Batch<C, N>
where
C: Contribution,
N: NodeIdT + Rand,
{
fn added_node(&self) -> Option<N> {
if let ChangeState::InProgress(Change::NodeChange(NodeChange::Add(ref id, _))) = self.change
{
// Register the new node to send broadcast messages to it from now on.
Some(id.clone())
} else {
None
}
}
fn next_epoch(&self) -> (u64, u64) {
let epoch = self.epoch;
let era = self.era;
if self.change == ChangeState::None {
(era, epoch - era + 1)
} else {
(epoch + 1, 0)
}
}
}
impl<N> SenderQueueableMessage for Message<N>
where
N: Rand,
{
fn is_accepted(&self, (them_era, them): (u64, u64), max_future_epochs: u64) -> bool {
let Epoch(era, us) = self.epoch();
if era != them_era {
return false;
}
if let Some(us) = us {
them <= us && us <= them + max_future_epochs
} else {
true
}
}
fn is_obsolete(&self, (them_era, them): (u64, u64)) -> bool {
let Epoch(era, us) = self.epoch();
if era < them_era {
return true;
}
if let Some(us) = us {
era == them_era && us < them
} else {
false
}
}
}
impl SenderQueueableEpoch for Epoch {
fn spanning_epochs(&self) -> Vec<Self> {
if let Epoch(era, Some(_)) = *self {
vec![Epoch(era, None)]
} else {
vec![]
}
}
}
impl<C, N> SenderQueueableDistAlgorithm for DynamicHoneyBadger<C, N>
where
C: Contribution + Serialize + DeserializeOwned,
N: NodeIdT + Serialize + DeserializeOwned + Rand,
{
fn max_future_epochs(&self) -> u64 {
self.max_future_epochs()
}
}

View File

@ -29,8 +29,6 @@ mod error;
mod honey_badger;
mod message;
pub mod sender_queueable;
pub use self::batch::Batch;
pub use self::builder::HoneyBadgerBuilder;
pub use self::error::{Error, ErrorKind, Result};

View File

@ -22,8 +22,6 @@
//! entries, any two nodes will likely make almost disjoint contributions instead of proposing
//! the same transaction multiple times.
pub mod sender_queueable;
use std::fmt::{self, Display};
use std::marker::PhantomData;
use std::{cmp, iter};

View File

@ -1,18 +0,0 @@
use rand::Rand;
use serde::{de::DeserializeOwned, Serialize};
use super::QueueingHoneyBadger;
use sender_queue::SenderQueueableDistAlgorithm;
use transaction_queue::TransactionQueue;
use {Contribution, NodeIdT};
impl<T, N, Q> SenderQueueableDistAlgorithm for QueueingHoneyBadger<T, N, Q>
where
T: Contribution + Serialize + DeserializeOwned + Clone,
N: NodeIdT + Serialize + DeserializeOwned + Rand,
Q: TransactionQueue<T>,
{
fn max_future_epochs(&self) -> u64 {
self.dyn_hb.max_future_epochs()
}
}

View File

@ -4,9 +4,91 @@ use crypto::PublicKey;
use rand::Rand;
use serde::{de::DeserializeOwned, Serialize};
use super::{SenderQueue, Step};
use dynamic_honey_badger::{Change, DynamicHoneyBadger};
use {Contribution, NodeIdT};
use super::{
SenderQueue, SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
SenderQueueableOutput, Step,
};
use {Contribution, Epoched, NodeIdT};
use dynamic_honey_badger::{
Batch, Change, ChangeState, DynamicHoneyBadger, Epoch, Message, NodeChange,
};
impl<C, N> SenderQueueableOutput<N, Message<N>> for Batch<C, N>
where
C: Contribution,
N: NodeIdT + Rand,
{
fn added_node(&self) -> Option<N> {
if let ChangeState::InProgress(Change::NodeChange(NodeChange::Add(ref id, _))) =
self.change()
{
// Register the new node to send broadcast messages to it from now on.
Some(id.clone())
} else {
None
}
}
fn next_epoch(&self) -> (u64, u64) {
let epoch = self.epoch();
let era = self.era();
if *self.change() == ChangeState::None {
(era, epoch - era + 1)
} else {
(epoch + 1, 0)
}
}
}
impl<N> SenderQueueableMessage for Message<N>
where
N: Rand,
{
fn is_accepted(&self, (them_era, them): (u64, u64), max_future_epochs: u64) -> bool {
let Epoch(era, us) = self.epoch();
if era != them_era {
return false;
}
if let Some(us) = us {
them <= us && us <= them + max_future_epochs
} else {
true
}
}
fn is_obsolete(&self, (them_era, them): (u64, u64)) -> bool {
let Epoch(era, us) = self.epoch();
if era < them_era {
return true;
}
if let Some(us) = us {
era == them_era && us < them
} else {
false
}
}
}
impl SenderQueueableEpoch for Epoch {
fn spanning_epochs(&self) -> Vec<Self> {
if let Epoch(era, Some(_)) = *self {
vec![Epoch(era, None)]
} else {
vec![]
}
}
}
impl<C, N> SenderQueueableDistAlgorithm for DynamicHoneyBadger<C, N>
where
C: Contribution + Serialize + DeserializeOwned,
N: NodeIdT + Serialize + DeserializeOwned + Rand,
{
fn max_future_epochs(&self) -> u64 {
self.max_future_epochs()
}
}
type Result<C, N> = super::Result<Step<DynamicHoneyBadger<C, N>>, DynamicHoneyBadger<C, N>>;

View File

@ -1,11 +1,11 @@
use rand::Rand;
use serde::{de::DeserializeOwned, Serialize};
use super::{Batch, HoneyBadger, Message};
use sender_queue::{
use super::{
SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
SenderQueueableOutput,
};
use honey_badger::{Batch, HoneyBadger, Message};
use {Contribution, Epoched, NodeIdT};
impl<C, N> SenderQueueableOutput<N, Message<N>> for Batch<C, N>

View File

@ -5,9 +5,11 @@
//! epoch matches the epoch of the message. Thus no queueing is required for incoming messages since
//! any incoming messages with non-matching epochs can be safely discarded.
mod dynamic_honey_badger;
mod message;
mod queueing_honey_badger;
pub mod dynamic_honey_badger;
pub mod honey_badger;
pub mod queueing_honey_badger;
use std::collections::BTreeMap;
use std::fmt::Debug;

View File

@ -4,11 +4,22 @@ use crypto::PublicKey;
use rand::Rand;
use serde::{de::DeserializeOwned, Serialize};
use super::{SenderQueue, Step};
use super::{SenderQueue, SenderQueueableDistAlgorithm, Step};
use queueing_honey_badger::{Change, QueueingHoneyBadger};
use transaction_queue::TransactionQueue;
use {Contribution, NodeIdT};
impl<T, N, Q> SenderQueueableDistAlgorithm for QueueingHoneyBadger<T, N, Q>
where
T: Contribution + Serialize + DeserializeOwned + Clone,
N: NodeIdT + Serialize + DeserializeOwned + Rand,
Q: TransactionQueue<T>,
{
fn max_future_epochs(&self) -> u64 {
self.dyn_hb().max_future_epochs()
}
}
type Result<T, N, Q> =
super::Result<Step<QueueingHoneyBadger<T, N, Q>>, QueueingHoneyBadger<T, N, Q>>;