hbbft/src/messaging.rs

71 lines
2.1 KiB
Rust
Raw Normal View History

Optimized broadcast #309 (#405) * Added extra message types * Add send functions for new message types * Store original value message received from proposer * Modify handle_value for optimized broadcast * Modify handle_echo for optimized broadcast * Add handle_echo_hash function for optimized broadcast * Add handle_can_decode function for optimized broadcast * Fixes handle_ready and send_echo functions: 1) Modify handle_ready function for optimized broadcast 2) Modify send_echo function to send `Echo` messages to different subset of nodes from handle_value and handle_ready functions * Remove value_message and fix typos * Add functions for filtering all_ids * Separate send_echo to send_echo_left and send_echo_remaining * Rename pessimism_factor to fault_estimate * Remove redundant bools from Broadcast struct * Fix multiple counting of nodes who sent both `Echo` and `EchoHash` by changing `echos` map structure * Allow conflicting `CanDecode`s from same node * Fix left and right iterators for `Echo` and `EchoHash` messages * Fixes bugs in left and right iterators and adds additional checks in handle functions * Change can_decodes to BTreeMap<Digest, BTreeSet<N>> and fix send_can_decode * Minor fixes * Modify send_echo_remaining to take a hash parameter * Fix bug in left and right iterators. * Excluding proposer in iterator led to infinite loop when our_id == proposer_id * Fix bug in handle_echo and compute_output * send_can_decode call in handle_echo returned early * compute_output needed `N - f` full `Echo`s to decode * Refactor `echos` map to take an EchoContent Enum for `Echo` and `EchoHash` messages * Run rustfmt * Refactor to avoid index access and multiple map lookups * Fix comments and minor refactorings. * Add an`AllExcept(BTreeSet<N>)` type to `Target` enum to enable sending messages to non-validators from Broadcast. * Use `Target::AllExcept` in Broadcast to send `Echo` messages to all non-validator nodes. * Add `AllExcept(_)` match arms for `Target` match expressions. * Rename `AllExcept` parameter from `known` to `exclude`. * Modify send_can_decode to send to all nodes who haven't sent an `Echo`. * Update docs for broadcast * Improve formatting and add comments for broadcast docs. * Fix formatting. * Allow for sending multiple `CanDecode` messages with different hashes. * Fix comments. * Fix bug in sending `Echo`s when node has not received `CanDecode`.
2019-06-12 08:02:39 -07:00
use std::collections::BTreeSet;
use std::iter;
Optimized broadcast #309 (#405) * Added extra message types * Add send functions for new message types * Store original value message received from proposer * Modify handle_value for optimized broadcast * Modify handle_echo for optimized broadcast * Add handle_echo_hash function for optimized broadcast * Add handle_can_decode function for optimized broadcast * Fixes handle_ready and send_echo functions: 1) Modify handle_ready function for optimized broadcast 2) Modify send_echo function to send `Echo` messages to different subset of nodes from handle_value and handle_ready functions * Remove value_message and fix typos * Add functions for filtering all_ids * Separate send_echo to send_echo_left and send_echo_remaining * Rename pessimism_factor to fault_estimate * Remove redundant bools from Broadcast struct * Fix multiple counting of nodes who sent both `Echo` and `EchoHash` by changing `echos` map structure * Allow conflicting `CanDecode`s from same node * Fix left and right iterators for `Echo` and `EchoHash` messages * Fixes bugs in left and right iterators and adds additional checks in handle functions * Change can_decodes to BTreeMap<Digest, BTreeSet<N>> and fix send_can_decode * Minor fixes * Modify send_echo_remaining to take a hash parameter * Fix bug in left and right iterators. * Excluding proposer in iterator led to infinite loop when our_id == proposer_id * Fix bug in handle_echo and compute_output * send_can_decode call in handle_echo returned early * compute_output needed `N - f` full `Echo`s to decode * Refactor `echos` map to take an EchoContent Enum for `Echo` and `EchoHash` messages * Run rustfmt * Refactor to avoid index access and multiple map lookups * Fix comments and minor refactorings. * Add an`AllExcept(BTreeSet<N>)` type to `Target` enum to enable sending messages to non-validators from Broadcast. * Use `Target::AllExcept` in Broadcast to send `Echo` messages to all non-validator nodes. * Add `AllExcept(_)` match arms for `Target` match expressions. * Rename `AllExcept` parameter from `known` to `exclude`. * Modify send_can_decode to send to all nodes who haven't sent an `Echo`. * Update docs for broadcast * Improve formatting and add comments for broadcast docs. * Fix formatting. * Allow for sending multiple `CanDecode` messages with different hashes. * Fix comments. * Fix bug in sending `Echo`s when node has not received `CanDecode`.
2019-06-12 08:02:39 -07:00
2018-05-10 08:50:07 -07:00
/// Message sent by a given source.
#[derive(Clone, Debug)]
2018-05-10 08:50:07 -07:00
pub struct SourcedMessage<M, N> {
/// The ID of the sender.
2018-05-10 08:50:07 -07:00
pub source: N,
2018-11-27 03:13:42 -08:00
/// The content of a message.
2018-05-10 08:50:07 -07:00
pub message: M,
}
2018-11-27 03:13:42 -08:00
/// The intended recipient(s) of a message.
#[derive(Clone, Debug, PartialEq, Eq)]
2018-05-10 08:50:07 -07:00
pub enum Target<N> {
/// The message must be sent to the nodes with the given IDs.
/// It is _not_ automatically sent to observers.
Nodes(BTreeSet<N>),
Optimized broadcast #309 (#405) * Added extra message types * Add send functions for new message types * Store original value message received from proposer * Modify handle_value for optimized broadcast * Modify handle_echo for optimized broadcast * Add handle_echo_hash function for optimized broadcast * Add handle_can_decode function for optimized broadcast * Fixes handle_ready and send_echo functions: 1) Modify handle_ready function for optimized broadcast 2) Modify send_echo function to send `Echo` messages to different subset of nodes from handle_value and handle_ready functions * Remove value_message and fix typos * Add functions for filtering all_ids * Separate send_echo to send_echo_left and send_echo_remaining * Rename pessimism_factor to fault_estimate * Remove redundant bools from Broadcast struct * Fix multiple counting of nodes who sent both `Echo` and `EchoHash` by changing `echos` map structure * Allow conflicting `CanDecode`s from same node * Fix left and right iterators for `Echo` and `EchoHash` messages * Fixes bugs in left and right iterators and adds additional checks in handle functions * Change can_decodes to BTreeMap<Digest, BTreeSet<N>> and fix send_can_decode * Minor fixes * Modify send_echo_remaining to take a hash parameter * Fix bug in left and right iterators. * Excluding proposer in iterator led to infinite loop when our_id == proposer_id * Fix bug in handle_echo and compute_output * send_can_decode call in handle_echo returned early * compute_output needed `N - f` full `Echo`s to decode * Refactor `echos` map to take an EchoContent Enum for `Echo` and `EchoHash` messages * Run rustfmt * Refactor to avoid index access and multiple map lookups * Fix comments and minor refactorings. * Add an`AllExcept(BTreeSet<N>)` type to `Target` enum to enable sending messages to non-validators from Broadcast. * Use `Target::AllExcept` in Broadcast to send `Echo` messages to all non-validator nodes. * Add `AllExcept(_)` match arms for `Target` match expressions. * Rename `AllExcept` parameter from `known` to `exclude`. * Modify send_can_decode to send to all nodes who haven't sent an `Echo`. * Update docs for broadcast * Improve formatting and add comments for broadcast docs. * Fix formatting. * Allow for sending multiple `CanDecode` messages with different hashes. * Fix comments. * Fix bug in sending `Echo`s when node has not received `CanDecode`.
2019-06-12 08:02:39 -07:00
/// The message must be sent to all remote nodes except the passed nodes.
/// Useful for sending messages to observer nodes that aren't
/// present in a node's `all_ids()` list.
AllExcept(BTreeSet<N>),
}
impl<N: Ord> Target<N> {
/// Creates a new `Target` addressing all peers, including observers.
pub fn all() -> Self {
Target::AllExcept(BTreeSet::new())
}
/// Creates a new `Target` addressing a single peer.
pub fn node(node_id: N) -> Self {
Target::Nodes(iter::once(node_id).collect())
}
/// Returns a `TargetedMessage` with this target, and the given message.
pub fn message<M>(self, message: M) -> TargetedMessage<M, N> {
TargetedMessage {
target: self,
message,
}
}
/// Returns whether `node_id` is included in this target.
pub fn contains(&self, node_id: &N) -> bool {
match self {
Target::Nodes(ids) => ids.contains(node_id),
Target::AllExcept(ids) => !ids.contains(node_id),
}
}
}
/// Message with a designated target.
#[derive(Clone, Debug, PartialEq)]
2018-05-10 08:50:07 -07:00
pub struct TargetedMessage<M, N> {
/// The node or nodes that this message must be delivered to.
2018-05-10 08:50:07 -07:00
pub target: Target<N>,
/// The content of the message that must be serialized and sent to the target.
2018-05-10 08:50:07 -07:00
pub message: M,
}
impl<M, N> TargetedMessage<M, N> {
/// Applies the given transformation of messages, preserving the target.
2019-09-17 08:51:41 -07:00
pub fn map<T, F: FnOnce(M) -> T>(self, f: F) -> TargetedMessage<T, N> {
TargetedMessage {
target: self.target,
message: f(self.message),
}
}
}