made Step depend on DistAlgorithm instead of base types

This commit is contained in:
Vladimir Komendantskiy 2018-07-19 12:56:30 +01:00
parent 17339c322c
commit 099fbc999e
9 changed files with 105 additions and 151 deletions

View File

@ -137,11 +137,7 @@ pub struct TestNode<D: DistAlgorithm> {
hw_quality: HwQuality,
}
type TestNodeStep<D> = Step<
<D as DistAlgorithm>::NodeUid,
<D as DistAlgorithm>::Output,
<D as DistAlgorithm>::Message,
>;
type TestNodeStepResult<D> = Step<D>;
impl<D: DistAlgorithm> TestNode<D>
where
@ -182,7 +178,7 @@ where
}
/// Handles the algorithm's output and messages.
fn send_output_and_msgs(&mut self, step: TestNodeStep<D>) {
fn send_output_and_msgs(&mut self, step: TestNodeStepResult<D>) {
let start = Instant::now();
let out_msgs: Vec<_> = step
.messages

View File

@ -75,7 +75,7 @@ use itertools::Itertools;
use agreement::bin_values::BinValues;
use common_coin;
use common_coin::{CommonCoin, CommonCoinMessage, CommonCoinStep};
use common_coin::{CommonCoin, CommonCoinMessage};
use fault_log::FaultLog;
use messaging::{DistAlgorithm, NetworkInfo, Step, Target, TargetedMessage};
@ -84,10 +84,6 @@ error_chain!{
CommonCoin(common_coin::Error, common_coin::ErrorKind);
}
types {
Error, ErrorKind, ResultExt, AgreementResult;
}
errors {
UnknownProposer {
description("unknown proposer")
@ -208,7 +204,7 @@ pub struct Agreement<NodeUid> {
coin_schedule: CoinSchedule,
}
pub type AgreementStep<NodeUid> = Step<NodeUid, bool, AgreementMessage>;
type AgreementStepResult<NodeUid> = Result<Step<Agreement<NodeUid>>>;
impl<NodeUid: Clone + Debug + Ord> DistAlgorithm for Agreement<NodeUid> {
type NodeUid = NodeUid;
@ -217,7 +213,7 @@ impl<NodeUid: Clone + Debug + Ord> DistAlgorithm for Agreement<NodeUid> {
type Message = AgreementMessage;
type Error = Error;
fn input(&mut self, input: Self::Input) -> AgreementResult<AgreementStep<NodeUid>> {
fn input(&mut self, input: Self::Input) -> AgreementStepResult<NodeUid> {
let fault_log = self.set_input(input)?;
self.step(fault_log)
}
@ -227,7 +223,7 @@ impl<NodeUid: Clone + Debug + Ord> DistAlgorithm for Agreement<NodeUid> {
&mut self,
sender_id: &Self::NodeUid,
message: Self::Message,
) -> AgreementResult<AgreementStep<NodeUid>> {
) -> AgreementStepResult<NodeUid> {
let fault_log = if self.terminated || message.epoch < self.epoch {
// Message is obsolete: We are already in a later epoch or terminated.
FaultLog::new()
@ -262,7 +258,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
netinfo: Arc<NetworkInfo<NodeUid>>,
session_id: u64,
proposer_id: NodeUid,
) -> AgreementResult<Self> {
) -> Result<Self> {
let invocation_id = netinfo.invocation_id();
if let Some(proposer_i) = netinfo.node_index(&proposer_id) {
Ok(Agreement {
@ -294,7 +290,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
}
}
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> AgreementResult<AgreementStep<NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> AgreementStepResult<NodeUid> {
Ok(Step::new(
self.output.take().into_iter().collect(),
fault_log,
@ -306,7 +302,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
}
/// Sets the input value for agreement.
fn set_input(&mut self, input: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn set_input(&mut self, input: bool) -> Result<FaultLog<NodeUid>> {
if self.epoch != 0 || self.estimated.is_some() {
return Err(ErrorKind::InputNotAccepted.into());
}
@ -328,7 +324,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
self.epoch == 0 && self.estimated.is_none()
}
fn handle_bval(&mut self, sender_id: &NodeUid, b: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn handle_bval(&mut self, sender_id: &NodeUid, b: bool) -> Result<FaultLog<NodeUid>> {
self.received_bval
.entry(sender_id.clone())
.or_insert_with(BTreeSet::new)
@ -366,7 +362,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
/// Called when `bin_values` changes as a result of receiving a `BVal` message. Tries to update
/// the epoch.
fn on_bin_values_changed(&mut self) -> AgreementResult<FaultLog<NodeUid>> {
fn on_bin_values_changed(&mut self) -> Result<FaultLog<NodeUid>> {
match self.coin_schedule {
CoinSchedule::False => {
let (aux_count, aux_vals) = self.count_aux();
@ -392,7 +388,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
}
}
fn send_bval(&mut self, b: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn send_bval(&mut self, b: bool) -> Result<FaultLog<NodeUid>> {
if !self.netinfo.is_validator() {
return Ok(FaultLog::new());
}
@ -406,7 +402,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
self.handle_bval(our_uid, b)
}
fn send_conf(&mut self) -> AgreementResult<FaultLog<NodeUid>> {
fn send_conf(&mut self) -> Result<FaultLog<NodeUid>> {
if self.conf_round {
// Only one `Conf` message is allowed in an epoch.
return Ok(FaultLog::new());
@ -433,7 +429,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
/// bin_values (note that bin_values_r may continue to change as `BVal`
/// messages are received, thus this condition may be triggered upon arrival
/// of either an `Aux` or a `BVal` message).
fn handle_aux(&mut self, sender_id: &NodeUid, b: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn handle_aux(&mut self, sender_id: &NodeUid, b: bool) -> Result<FaultLog<NodeUid>> {
// Perform the `Aux` message round only if a `Conf` round hasn't started yet.
if self.conf_round {
return Ok(FaultLog::new());
@ -459,11 +455,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
}
}
fn handle_conf(
&mut self,
sender_id: &NodeUid,
v: BinValues,
) -> AgreementResult<FaultLog<NodeUid>> {
fn handle_conf(&mut self, sender_id: &NodeUid, v: BinValues) -> Result<FaultLog<NodeUid>> {
self.received_conf.insert(sender_id.clone(), v);
self.try_finish_conf_round()
}
@ -471,7 +463,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
/// Receives a `Term(v)` message. If we haven't yet decided on a value and there are more than
/// `num_faulty` such messages with the same value from different nodes, performs expedite
/// termination: decides on `v`, broadcasts `Term(v)` and terminates the instance.
fn handle_term(&mut self, sender_id: &NodeUid, b: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn handle_term(&mut self, sender_id: &NodeUid, b: bool) -> Result<FaultLog<NodeUid>> {
self.received_term.insert(sender_id.clone(), b);
// Check for the expedite termination condition.
if self.decision.is_none()
@ -489,15 +481,15 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
&mut self,
sender_id: &NodeUid,
msg: CommonCoinMessage,
) -> AgreementResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
let coin_step = self.common_coin.handle_message(sender_id, msg)?;
self.on_coin_step(coin_step)
}
fn on_coin_step(
&mut self,
coin_step: CommonCoinStep<NodeUid>,
) -> AgreementResult<FaultLog<NodeUid>> {
coin_step: Step<CommonCoin<NodeUid, Nonce>>,
) -> Result<FaultLog<NodeUid>> {
let Step {
output,
mut fault_log,
@ -518,11 +510,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
/// When the common coin has been computed, tries to decide on an output value, updates the
/// `Agreement` epoch and handles queued messages for the new epoch.
fn on_coin(
&mut self,
coin: bool,
def_bin_value: Option<bool>,
) -> AgreementResult<FaultLog<NodeUid>> {
fn on_coin(&mut self, coin: bool, def_bin_value: Option<bool>) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
if self.terminated {
// Avoid an infinite regression without making an Agreement step.
@ -589,7 +577,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
self.terminated = true;
}
fn try_finish_conf_round(&mut self) -> AgreementResult<FaultLog<NodeUid>> {
fn try_finish_conf_round(&mut self) -> Result<FaultLog<NodeUid>> {
if self.conf_round
&& self.count_conf().0 >= self.netinfo.num_nodes() - self.netinfo.num_faulty()
{
@ -602,7 +590,7 @@ impl<NodeUid: Clone + Debug + Ord> Agreement<NodeUid> {
}
}
fn send_aux(&mut self, b: bool) -> AgreementResult<FaultLog<NodeUid>> {
fn send_aux(&mut self, b: bool) -> Result<FaultLog<NodeUid>> {
if !self.netinfo.is_validator() {
return Ok(FaultLog::new());
}

View File

@ -144,10 +144,6 @@ use fmt::{HexBytes, HexList, HexProof};
use messaging::{DistAlgorithm, NetworkInfo, Step, Target, TargetedMessage};
error_chain!{
types {
Error, ErrorKind, ResultExt, BroadcastResult;
}
foreign_links {
ReedSolomon(rse::Error);
}
@ -228,7 +224,7 @@ pub struct Broadcast<NodeUid> {
output: Option<Vec<u8>>,
}
pub type BroadcastStep<NodeUid> = Step<NodeUid, Vec<u8>, BroadcastMessage>;
type BroadcastStepResult<NodeUid> = Result<Step<Broadcast<NodeUid>>>;
impl<NodeUid: Debug + Clone + Ord> DistAlgorithm for Broadcast<NodeUid> {
type NodeUid = NodeUid;
@ -239,7 +235,7 @@ impl<NodeUid: Debug + Clone + Ord> DistAlgorithm for Broadcast<NodeUid> {
type Message = BroadcastMessage;
type Error = Error;
fn input(&mut self, input: Self::Input) -> BroadcastResult<BroadcastStep<NodeUid>> {
fn input(&mut self, input: Self::Input) -> BroadcastStepResult<NodeUid> {
if *self.netinfo.our_uid() != self.proposer_id {
return Err(ErrorKind::InstanceCannotPropose.into());
}
@ -256,7 +252,7 @@ impl<NodeUid: Debug + Clone + Ord> DistAlgorithm for Broadcast<NodeUid> {
&mut self,
sender_id: &NodeUid,
message: Self::Message,
) -> BroadcastResult<BroadcastStep<NodeUid>> {
) -> BroadcastStepResult<NodeUid> {
if !self.netinfo.is_node_validator(sender_id) {
return Err(ErrorKind::UnknownSender.into());
}
@ -282,7 +278,7 @@ impl<NodeUid: Debug + Clone + Ord> DistAlgorithm for Broadcast<NodeUid> {
impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
/// Creates a new broadcast instance to be used by node `our_id` which expects a value proposal
/// from node `proposer_id`.
pub fn new(netinfo: Arc<NetworkInfo<NodeUid>>, proposer_id: NodeUid) -> BroadcastResult<Self> {
pub fn new(netinfo: Arc<NetworkInfo<NodeUid>>, proposer_id: NodeUid) -> Result<Self> {
let parity_shard_num = 2 * netinfo.num_faulty();
let data_shard_num = netinfo.num_nodes() - parity_shard_num;
let coding = Coding::new(data_shard_num, parity_shard_num)?;
@ -302,7 +298,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
})
}
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> BroadcastResult<BroadcastStep<NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> BroadcastStepResult<NodeUid> {
Ok(Step::new(
self.output.take().into_iter().collect(),
fault_log,
@ -315,7 +311,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
/// scheme. The returned value contains the shard assigned to this
/// node. That shard doesn't need to be sent anywhere. It gets recorded in
/// the broadcast instance.
fn send_shards(&mut self, mut value: Vec<u8>) -> BroadcastResult<Proof<Vec<u8>>> {
fn send_shards(&mut self, mut value: Vec<u8>) -> Result<Proof<Vec<u8>>> {
let data_shard_num = self.coding.data_shard_count();
let parity_shard_num = self.coding.parity_shard_count();
@ -393,7 +389,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
&mut self,
sender_id: &NodeUid,
p: Proof<Vec<u8>>,
) -> BroadcastResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
// If the sender is not the proposer or if this is not the first `Value`, ignore.
if *sender_id != self.proposer_id {
info!(
@ -425,11 +421,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
}
/// Handles a received `Echo` message.
fn handle_echo(
&mut self,
sender_id: &NodeUid,
p: Proof<Vec<u8>>,
) -> BroadcastResult<FaultLog<NodeUid>> {
fn handle_echo(&mut self, sender_id: &NodeUid, p: Proof<Vec<u8>>) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
// If the sender has already sent `Echo`, ignore.
if self.echos.contains_key(sender_id) {
@ -465,7 +457,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
}
/// Handles a received `Ready` message.
fn handle_ready(&mut self, sender_id: &NodeUid, hash: &[u8]) -> BroadcastResult<()> {
fn handle_ready(&mut self, sender_id: &NodeUid, hash: &[u8]) -> Result<()> {
// If the sender has already sent a `Ready` before, ignore.
if self.readys.contains_key(sender_id) {
info!(
@ -488,7 +480,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
}
/// Sends an `Echo` message and handles it. Does nothing if we are only an observer.
fn send_echo(&mut self, p: Proof<Vec<u8>>) -> BroadcastResult<FaultLog<NodeUid>> {
fn send_echo(&mut self, p: Proof<Vec<u8>>) -> Result<FaultLog<NodeUid>> {
self.echo_sent = true;
if !self.netinfo.is_validator() {
return Ok(FaultLog::new());
@ -500,7 +492,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
}
/// Sends a `Ready` message and handles it. Does nothing if we are only an observer.
fn send_ready(&mut self, hash: &[u8]) -> BroadcastResult<()> {
fn send_ready(&mut self, hash: &[u8]) -> Result<()> {
self.ready_sent = true;
if !self.netinfo.is_validator() {
return Ok(());
@ -513,7 +505,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
/// Checks whether the condition for output are met for this hash, and if so, sets the output
/// value.
fn compute_output(&mut self, hash: &[u8]) -> BroadcastResult<()> {
fn compute_output(&mut self, hash: &[u8]) -> Result<()> {
if self.decided
|| self.count_readys(hash) <= 2 * self.netinfo.num_faulty()
|| self.count_echos(hash) <= self.netinfo.num_faulty()
@ -592,7 +584,7 @@ enum Coding {
impl Coding {
/// Creates a new `Coding` instance with the given number of shards.
fn new(data_shard_num: usize, parity_shard_num: usize) -> BroadcastResult<Self> {
fn new(data_shard_num: usize, parity_shard_num: usize) -> Result<Self> {
Ok(if parity_shard_num > 0 {
let rs = ReedSolomon::new(data_shard_num, parity_shard_num)?;
Coding::ReedSolomon(Box::new(rs))
@ -618,7 +610,7 @@ impl Coding {
}
/// Constructs (and overwrites) the parity shards.
fn encode(&self, slices: &mut [&mut [u8]]) -> BroadcastResult<()> {
fn encode(&self, slices: &mut [&mut [u8]]) -> Result<()> {
match *self {
Coding::ReedSolomon(ref rs) => rs.encode(slices)?,
Coding::Trivial(_) => (),
@ -627,7 +619,7 @@ impl Coding {
}
/// If enough shards are present, reconstructs the missing ones.
fn reconstruct_shards(&self, shards: &mut [Option<Box<[u8]>>]) -> BroadcastResult<()> {
fn reconstruct_shards(&self, shards: &mut [Option<Box<[u8]>>]) -> Result<()> {
match *self {
Coding::ReedSolomon(ref rs) => rs.reconstruct_shards(shards)?,
Coding::Trivial(_) => {

View File

@ -78,7 +78,7 @@ pub struct CommonCoin<NodeUid, T> {
terminated: bool,
}
pub type CommonCoinStep<NodeUid> = Step<NodeUid, bool, CommonCoinMessage>;
type CommonCoinStepResult<NodeUid, T> = Result<Step<CommonCoin<NodeUid, T>>>;
impl<NodeUid, T> DistAlgorithm for CommonCoin<NodeUid, T>
where
@ -92,7 +92,7 @@ where
type Error = Error;
/// Sends our threshold signature share if not yet sent.
fn input(&mut self, _input: Self::Input) -> Result<CommonCoinStep<NodeUid>> {
fn input(&mut self, _input: Self::Input) -> CommonCoinStepResult<NodeUid, T> {
let fault_log = if !self.had_input {
self.had_input = true;
self.get_coin()?
@ -107,7 +107,7 @@ where
&mut self,
sender_id: &Self::NodeUid,
message: Self::Message,
) -> Result<CommonCoinStep<NodeUid>> {
) -> CommonCoinStepResult<NodeUid, T> {
let fault_log = if !self.terminated {
let CommonCoinMessage(share) = message;
self.handle_share(sender_id, share)?
@ -144,7 +144,7 @@ where
}
}
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> Result<CommonCoinStep<NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> CommonCoinStepResult<NodeUid, T> {
Ok(Step::new(
self.output.take().into_iter().collect(),
fault_log,

View File

@ -25,20 +25,17 @@
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::fmt::Debug;
use std::result;
use std::sync::Arc;
use agreement::{self, Agreement, AgreementMessage, AgreementStep};
use broadcast::{self, Broadcast, BroadcastMessage, BroadcastStep};
use agreement::{self, Agreement, AgreementMessage};
use broadcast::{self, Broadcast, BroadcastMessage};
use fault_log::FaultLog;
use fmt::HexBytes;
use messaging::{DistAlgorithm, NetworkInfo, Step, TargetedMessage};
use rand::Rand;
error_chain!{
types {
Error, ErrorKind, ResultExt, CommonSubsetResult;
}
links {
Agreement(agreement::Error, agreement::ErrorKind);
Broadcast(broadcast::Error, broadcast::ErrorKind);
@ -110,8 +107,7 @@ pub struct CommonSubset<NodeUid: Rand> {
decided: bool,
}
pub type CommonSubsetStep<NodeUid> =
Step<NodeUid, BTreeMap<NodeUid, ProposedValue>, Message<NodeUid>>;
type CommonSubsetStepResult<NodeUid> = Result<Step<CommonSubset<NodeUid>>>;
impl<NodeUid: Clone + Debug + Ord + Rand> DistAlgorithm for CommonSubset<NodeUid> {
type NodeUid = NodeUid;
@ -120,7 +116,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> DistAlgorithm for CommonSubset<NodeUid
type Message = Message<NodeUid>;
type Error = Error;
fn input(&mut self, input: Self::Input) -> CommonSubsetResult<CommonSubsetStep<NodeUid>> {
fn input(&mut self, input: Self::Input) -> CommonSubsetStepResult<NodeUid> {
debug!(
"{:?} Proposing {:?}",
self.netinfo.our_uid(),
@ -134,7 +130,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> DistAlgorithm for CommonSubset<NodeUid
&mut self,
sender_id: &Self::NodeUid,
message: Self::Message,
) -> CommonSubsetResult<CommonSubsetStep<NodeUid>> {
) -> CommonSubsetStepResult<NodeUid> {
let fault_log = match message {
Message::Broadcast(p_id, b_msg) => self.handle_broadcast(sender_id, &p_id, b_msg)?,
Message::Agreement(p_id, a_msg) => self.handle_agreement(sender_id, &p_id, a_msg)?,
@ -152,7 +148,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> DistAlgorithm for CommonSubset<NodeUid
}
impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
pub fn new(netinfo: Arc<NetworkInfo<NodeUid>>, session_id: u64) -> CommonSubsetResult<Self> {
pub fn new(netinfo: Arc<NetworkInfo<NodeUid>>, session_id: u64) -> Result<Self> {
// Create all broadcast instances.
let mut broadcast_instances: BTreeMap<NodeUid, Broadcast<NodeUid>> = BTreeMap::new();
for proposer_id in netinfo.all_uids() {
@ -183,10 +179,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
})
}
fn step(
&mut self,
fault_log: FaultLog<NodeUid>,
) -> CommonSubsetResult<CommonSubsetStep<NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> CommonSubsetStepResult<NodeUid> {
Ok(Step::new(
self.output.take().into_iter().collect(),
fault_log,
@ -196,10 +189,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
/// Common Subset input message handler. It receives a value for broadcast
/// and redirects it to the corresponding broadcast instance.
pub fn send_proposed_value(
&mut self,
value: ProposedValue,
) -> CommonSubsetResult<FaultLog<NodeUid>> {
pub fn send_proposed_value(&mut self, value: ProposedValue) -> Result<FaultLog<NodeUid>> {
if !self.netinfo.is_validator() {
return Ok(FaultLog::new());
}
@ -215,7 +205,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
sender_id: &NodeUid,
proposer_id: &NodeUid,
bmessage: BroadcastMessage,
) -> CommonSubsetResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
self.process_broadcast(proposer_id, |bc| bc.handle_message(sender_id, bmessage))
}
@ -226,7 +216,7 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
sender_id: &NodeUid,
proposer_id: &NodeUid,
amessage: AgreementMessage,
) -> CommonSubsetResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
// Send the message to the local instance of Agreement
self.process_agreement(proposer_id, |agreement| {
agreement.handle_message(sender_id, amessage)
@ -235,13 +225,10 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
/// Upon delivery of v_j from RBC_j, if input has not yet been provided to
/// BA_j, then provide input 1 to BA_j. See Figure 11.
fn process_broadcast<F>(
&mut self,
proposer_id: &NodeUid,
f: F,
) -> CommonSubsetResult<FaultLog<NodeUid>>
fn process_broadcast<F>(&mut self, proposer_id: &NodeUid, f: F) -> Result<FaultLog<NodeUid>>
where
F: FnOnce(&mut Broadcast<NodeUid>) -> Result<BroadcastStep<NodeUid>, broadcast::Error>,
F: FnOnce(&mut Broadcast<NodeUid>)
-> result::Result<Step<Broadcast<NodeUid>>, broadcast::Error>,
{
let mut fault_log = FaultLog::new();
let value = {
@ -274,13 +261,10 @@ impl<NodeUid: Clone + Debug + Ord + Rand> CommonSubset<NodeUid> {
/// Callback to be invoked on receipt of the decision value of the Agreement
/// instance `uid`.
fn process_agreement<F>(
&mut self,
proposer_id: &NodeUid,
f: F,
) -> CommonSubsetResult<FaultLog<NodeUid>>
fn process_agreement<F>(&mut self, proposer_id: &NodeUid, f: F) -> Result<FaultLog<NodeUid>>
where
F: FnOnce(&mut Agreement<NodeUid>) -> Result<AgreementStep<NodeUid>, agreement::Error>,
F: FnOnce(&mut Agreement<NodeUid>)
-> result::Result<Step<Agreement<NodeUid>>, agreement::Error>,
{
let mut fault_log = FaultLog::new();
let value = {

View File

@ -68,7 +68,7 @@ use serde::{Deserialize, Serialize};
use self::votes::{SignedVote, VoteCounter};
use crypto::{PublicKey, PublicKeySet, SecretKey, Signature};
use fault_log::{FaultKind, FaultLog};
use honey_badger::{HoneyBadger, HoneyBadgerStep, Message as HbMessage};
use honey_badger::{HoneyBadger, Message as HbMessage};
use messaging::{DistAlgorithm, NetworkInfo, Step, Target, TargetedMessage};
use sync_key_gen::{Ack, Part, PartOutcome, SyncKeyGen};
@ -116,7 +116,7 @@ pub struct DynamicHoneyBadger<C, NodeUid: Rand> {
output: VecDeque<Batch<C, NodeUid>>,
}
pub type DynamicHoneyBadgerStep<C, NodeUid> = Step<NodeUid, Batch<C, NodeUid>, Message<NodeUid>>;
type DhbStepResult<C, NodeUid> = Result<Step<DynamicHoneyBadger<C, NodeUid>>>;
impl<C, NodeUid> DistAlgorithm for DynamicHoneyBadger<C, NodeUid>
where
@ -129,7 +129,7 @@ where
type Message = Message<NodeUid>;
type Error = Error;
fn input(&mut self, input: Self::Input) -> Result<DynamicHoneyBadgerStep<C, NodeUid>> {
fn input(&mut self, input: Self::Input) -> DhbStepResult<C, NodeUid> {
// User contributions are forwarded to `HoneyBadger` right away. Votes are signed and
// broadcast.
let fault_log = match input {
@ -143,7 +143,7 @@ where
&mut self,
sender_id: &NodeUid,
message: Self::Message,
) -> Result<DynamicHoneyBadgerStep<C, NodeUid>> {
) -> DhbStepResult<C, NodeUid> {
let epoch = message.start_epoch();
let fault_log = if epoch < self.start_epoch {
// Obsolete message.
@ -183,7 +183,7 @@ where
C: Eq + Serialize + for<'r> Deserialize<'r> + Debug + Hash,
NodeUid: Eq + Ord + Clone + Debug + Serialize + for<'r> Deserialize<'r> + Hash + Rand,
{
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> Result<DynamicHoneyBadgerStep<C, NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> DhbStepResult<C, NodeUid> {
Ok(Step::new(
self.output.drain(..).collect(),
fault_log,
@ -276,7 +276,7 @@ where
/// Processes all pending batches output by Honey Badger.
fn process_output(
&mut self,
step: HoneyBadgerStep<InternalContrib<C, NodeUid>, NodeUid>,
step: Step<HoneyBadger<InternalContrib<C, NodeUid>, NodeUid>>,
) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
fault_log.extend(step.fault_log);

View File

@ -34,16 +34,12 @@ use bincode;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use common_subset::{self, CommonSubset, CommonSubsetStep};
use common_subset::{self, CommonSubset};
use crypto::{Ciphertext, DecryptionShare};
use fault_log::{FaultKind, FaultLog};
use messaging::{DistAlgorithm, NetworkInfo, Step, Target, TargetedMessage};
error_chain!{
types {
Error, ErrorKind, ResultExt, HoneyBadgerResult;
}
links {
CommonSubset(common_subset::Error, common_subset::ErrorKind);
}
@ -134,7 +130,7 @@ pub struct HoneyBadger<C, NodeUid: Rand> {
ciphertexts: BTreeMap<u64, BTreeMap<NodeUid, Ciphertext>>,
}
pub type HoneyBadgerStep<C, NodeUid> = Step<NodeUid, Batch<C, NodeUid>, Message<NodeUid>>;
type HoneyBadgerStepResult<C, NodeUid> = Result<Step<HoneyBadger<C, NodeUid>>>;
impl<C, NodeUid> DistAlgorithm for HoneyBadger<C, NodeUid>
where
@ -147,7 +143,7 @@ where
type Message = Message<NodeUid>;
type Error = Error;
fn input(&mut self, input: Self::Input) -> HoneyBadgerResult<HoneyBadgerStep<C, NodeUid>> {
fn input(&mut self, input: Self::Input) -> HoneyBadgerStepResult<C, NodeUid> {
let fault_log = self.propose(&input)?;
self.step(fault_log)
}
@ -156,7 +152,7 @@ where
&mut self,
sender_id: &NodeUid,
message: Self::Message,
) -> HoneyBadgerResult<HoneyBadgerStep<C, NodeUid>> {
) -> HoneyBadgerStepResult<C, NodeUid> {
if !self.netinfo.is_node_validator(sender_id) {
return Err(ErrorKind::UnknownSender.into());
}
@ -194,10 +190,7 @@ where
HoneyBadgerBuilder::new(netinfo)
}
fn step(
&mut self,
fault_log: FaultLog<NodeUid>,
) -> HoneyBadgerResult<HoneyBadgerStep<C, NodeUid>> {
fn step(&mut self, fault_log: FaultLog<NodeUid>) -> HoneyBadgerStepResult<C, NodeUid> {
Ok(Step::new(
self.output.drain(..).collect(),
fault_log,
@ -206,7 +199,7 @@ where
}
/// Proposes a new item in the current epoch.
pub fn propose(&mut self, proposal: &C) -> HoneyBadgerResult<FaultLog<NodeUid>> {
pub fn propose(&mut self, proposal: &C) -> Result<FaultLog<NodeUid>> {
if !self.netinfo.is_validator() {
return Ok(FaultLog::new());
}
@ -236,7 +229,7 @@ where
sender_id: &NodeUid,
epoch: u64,
content: MessageContent<NodeUid>,
) -> HoneyBadgerResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
match content {
MessageContent::CommonSubset(cs_msg) => {
self.handle_common_subset_message(sender_id, epoch, cs_msg)
@ -253,7 +246,7 @@ where
sender_id: &NodeUid,
epoch: u64,
message: common_subset::Message<NodeUid>,
) -> HoneyBadgerResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
let step = {
// Borrow the instance for `epoch`, or create it.
@ -282,7 +275,7 @@ where
epoch: u64,
proposer_id: NodeUid,
share: DecryptionShare,
) -> HoneyBadgerResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
if let Some(ciphertext) = self
@ -331,7 +324,7 @@ where
/// When contributions of transactions have been decrypted for all valid proposers in this
/// epoch, moves those contributions into a batch, outputs the batch and updates the epoch.
fn try_output_batch(&mut self) -> HoneyBadgerResult<FaultLog<NodeUid>> {
fn try_output_batch(&mut self) -> Result<FaultLog<NodeUid>> {
// Wait until contributions have been successfully decoded for all proposer nodes with correct
// ciphertext outputs.
if !self.all_contributions_decrypted() {
@ -371,7 +364,7 @@ where
}
/// Increments the epoch number and clears any state that is local to the finished epoch.
fn update_epoch(&mut self) -> HoneyBadgerResult<FaultLog<NodeUid>> {
fn update_epoch(&mut self) -> Result<FaultLog<NodeUid>> {
// Clear the state of the old epoch.
self.ciphertexts.remove(&self.epoch);
self.decrypted_contributions.clear();
@ -394,7 +387,7 @@ where
}
/// Tries to decrypt contributions from all proposers and output those in a batch.
fn try_decrypt_and_output_batch(&mut self) -> HoneyBadgerResult<FaultLog<NodeUid>> {
fn try_decrypt_and_output_batch(&mut self) -> Result<FaultLog<NodeUid>> {
// Return if we don't have ciphertexts yet.
let proposer_ids: Vec<_> = match self.ciphertexts.get(&self.epoch) {
Some(cts) => cts.keys().cloned().collect(),
@ -466,7 +459,7 @@ where
fn send_decryption_shares(
&mut self,
cs_output: BTreeMap<NodeUid, Vec<u8>>,
) -> HoneyBadgerResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
let mut fault_log = FaultLog::new();
let mut ciphertexts = BTreeMap::new();
for (proposer_id, v) in cs_output {
@ -504,7 +497,7 @@ where
&mut self,
proposer_id: &NodeUid,
ciphertext: &Ciphertext,
) -> HoneyBadgerResult<(bool, FaultLog<NodeUid>)> {
) -> Result<(bool, FaultLog<NodeUid>)> {
if !self.netinfo.is_validator() {
return Ok((ciphertext.verify(), FaultLog::new()));
}
@ -574,9 +567,9 @@ where
/// `epoch == Some(given_epoch)`.
fn process_output(
&mut self,
step: CommonSubsetStep<NodeUid>,
step: Step<CommonSubset<NodeUid>>,
epoch: Option<u64>,
) -> HoneyBadgerResult<FaultLog<NodeUid>> {
) -> Result<FaultLog<NodeUid>> {
let Step {
output,
mut fault_log,

View File

@ -52,20 +52,22 @@ impl<M, N> TargetedMessage<M, N> {
/// Result of one step of the local state machine of a distributed algorithm. Such a result should
/// be used and never discarded by the client of the algorithm.
#[must_use = "The algorithm step result must be used."]
pub struct Step<N, O, M>
pub struct Step<D>
where
N: Clone,
D: DistAlgorithm,
<D as DistAlgorithm>::NodeUid: Clone,
{
pub output: VecDeque<O>,
pub fault_log: FaultLog<N>,
pub messages: VecDeque<TargetedMessage<M, N>>,
pub output: VecDeque<D::Output>,
pub fault_log: FaultLog<D::NodeUid>,
pub messages: VecDeque<TargetedMessage<D::Message, D::NodeUid>>,
}
impl<N, O, M> Default for Step<N, O, M>
impl<D> Default for Step<D>
where
N: Clone,
D: DistAlgorithm,
<D as DistAlgorithm>::NodeUid: Clone,
{
fn default() -> Step<N, O, M> {
fn default() -> Step<D> {
Step {
output: VecDeque::default(),
fault_log: FaultLog::default(),
@ -74,14 +76,14 @@ where
}
}
impl<N, O, M> Step<N, O, M>
impl<D: DistAlgorithm> Step<D>
where
N: Clone,
<D as DistAlgorithm>::NodeUid: Clone,
{
pub fn new(
output: VecDeque<O>,
fault_log: FaultLog<N>,
messages: VecDeque<TargetedMessage<M, N>>,
output: VecDeque<D::Output>,
fault_log: FaultLog<D::NodeUid>,
messages: VecDeque<TargetedMessage<D::Message, D::NodeUid>>,
) -> Self {
Step {
output,
@ -91,8 +93,6 @@ where
}
}
type StepResult<N, O, M, E> = Result<Step<N, O, M>, E>;
/// A distributed algorithm that defines a message flow.
pub trait DistAlgorithm {
/// Unique node identifier.
@ -108,17 +108,18 @@ pub trait DistAlgorithm {
type Error: Debug;
/// Handles an input provided by the user, and returns
fn input(
&mut self,
input: Self::Input,
) -> StepResult<Self::NodeUid, Self::Output, Self::Message, Self::Error>;
fn input(&mut self, input: Self::Input) -> Result<Step<Self>, Self::Error>
where
Self: Sized;
/// Handles a message received from node `sender_id`.
fn handle_message(
&mut self,
sender_id: &Self::NodeUid,
message: Self::Message,
) -> StepResult<Self::NodeUid, Self::Output, Self::Message, Self::Error>;
) -> Result<Step<Self>, Self::Error>
where
Self: Sized;
/// Returns `true` if execution has completed and this instance can be dropped.
fn terminated(&self) -> bool;

View File

@ -119,7 +119,7 @@ where
output: VecDeque<Batch<Tx, NodeUid>>,
}
pub type QueueingHoneyBadgerStep<Tx, NodeUid> = Step<NodeUid, Batch<Tx, NodeUid>, Message<NodeUid>>;
type QhbStepResult<Tx, NodeUid> = Result<Step<QueueingHoneyBadger<Tx, NodeUid>>>;
impl<Tx, NodeUid> DistAlgorithm for QueueingHoneyBadger<Tx, NodeUid>
where
@ -132,7 +132,7 @@ where
type Message = Message<NodeUid>;
type Error = Error;
fn input(&mut self, input: Self::Input) -> Result<QueueingHoneyBadgerStep<Tx, NodeUid>> {
fn input(&mut self, input: Self::Input) -> QhbStepResult<Tx, NodeUid> {
// User transactions are forwarded to `HoneyBadger` right away. Internal messages are
// in addition signed and broadcast.
let (fault_log, messages) = match input {
@ -153,7 +153,7 @@ where
&mut self,
sender_id: &NodeUid,
message: Self::Message,
) -> Result<QueueingHoneyBadgerStep<Tx, NodeUid>> {
) -> QhbStepResult<Tx, NodeUid> {
let Step {
output,
mut fault_log,
@ -200,7 +200,7 @@ where
&mut self,
fault_log: FaultLog<NodeUid>,
messages: VecDeque<TargetedMessage<Message<NodeUid>, NodeUid>>,
) -> Result<QueueingHoneyBadgerStep<Tx, NodeUid>> {
) -> QhbStepResult<Tx, NodeUid> {
Ok(Step::new(
self.output.drain(..).collect(),
fault_log,