mirror of https://github.com/poanetwork/hbbft.git
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:
parent
bb64be55af
commit
697565c97a
|
@ -72,8 +72,6 @@ mod dynamic_honey_badger;
|
||||||
mod error;
|
mod error;
|
||||||
mod votes;
|
mod votes;
|
||||||
|
|
||||||
pub mod sender_queueable;
|
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -29,8 +29,6 @@ mod error;
|
||||||
mod honey_badger;
|
mod honey_badger;
|
||||||
mod message;
|
mod message;
|
||||||
|
|
||||||
pub mod sender_queueable;
|
|
||||||
|
|
||||||
pub use self::batch::Batch;
|
pub use self::batch::Batch;
|
||||||
pub use self::builder::HoneyBadgerBuilder;
|
pub use self::builder::HoneyBadgerBuilder;
|
||||||
pub use self::error::{Error, ErrorKind, Result};
|
pub use self::error::{Error, ErrorKind, Result};
|
||||||
|
|
|
@ -22,8 +22,6 @@
|
||||||
//! entries, any two nodes will likely make almost disjoint contributions instead of proposing
|
//! entries, any two nodes will likely make almost disjoint contributions instead of proposing
|
||||||
//! the same transaction multiple times.
|
//! the same transaction multiple times.
|
||||||
|
|
||||||
pub mod sender_queueable;
|
|
||||||
|
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::{cmp, iter};
|
use std::{cmp, iter};
|
||||||
|
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,9 +4,91 @@ use crypto::PublicKey;
|
||||||
use rand::Rand;
|
use rand::Rand;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use super::{SenderQueue, Step};
|
use super::{
|
||||||
use dynamic_honey_badger::{Change, DynamicHoneyBadger};
|
SenderQueue, SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
|
||||||
use {Contribution, NodeIdT};
|
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>>;
|
type Result<C, N> = super::Result<Step<DynamicHoneyBadger<C, N>>, DynamicHoneyBadger<C, N>>;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use rand::Rand;
|
use rand::Rand;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use super::{Batch, HoneyBadger, Message};
|
use super::{
|
||||||
use sender_queue::{
|
|
||||||
SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
|
SenderQueueableDistAlgorithm, SenderQueueableEpoch, SenderQueueableMessage,
|
||||||
SenderQueueableOutput,
|
SenderQueueableOutput,
|
||||||
};
|
};
|
||||||
|
use honey_badger::{Batch, HoneyBadger, Message};
|
||||||
use {Contribution, Epoched, NodeIdT};
|
use {Contribution, Epoched, NodeIdT};
|
||||||
|
|
||||||
impl<C, N> SenderQueueableOutput<N, Message<N>> for Batch<C, N>
|
impl<C, N> SenderQueueableOutput<N, Message<N>> for Batch<C, N>
|
|
@ -5,9 +5,11 @@
|
||||||
//! epoch matches the epoch of the message. Thus no queueing is required for incoming messages since
|
//! 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.
|
//! any incoming messages with non-matching epochs can be safely discarded.
|
||||||
|
|
||||||
mod dynamic_honey_badger;
|
|
||||||
mod message;
|
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::collections::BTreeMap;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
|
@ -4,11 +4,22 @@ use crypto::PublicKey;
|
||||||
use rand::Rand;
|
use rand::Rand;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use super::{SenderQueue, Step};
|
use super::{SenderQueue, SenderQueueableDistAlgorithm, Step};
|
||||||
use queueing_honey_badger::{Change, QueueingHoneyBadger};
|
use queueing_honey_badger::{Change, QueueingHoneyBadger};
|
||||||
use transaction_queue::TransactionQueue;
|
use transaction_queue::TransactionQueue;
|
||||||
use {Contribution, NodeIdT};
|
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> =
|
type Result<T, N, Q> =
|
||||||
super::Result<Step<QueueingHoneyBadger<T, N, Q>>, QueueingHoneyBadger<T, N, Q>>;
|
super::Result<Step<QueueingHoneyBadger<T, N, Q>>, QueueingHoneyBadger<T, N, Q>>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue