Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-544: Alert backup preparation #320

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions consensus/src/alerts/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
alerts::{
handler::Handler, Alert, AlertMessage, AlerterResponse, ForkingNotification, NetworkMessage,
},
Data, Hasher, MultiKeychain, Multisigned, NodeCount, Receiver, Recipient, Sender, Terminator,
runway::BackupItem,
Data, Hasher, MultiKeychain, NodeCount, Receiver, Recipient, Sender, Terminator,
};
use aleph_bft_rmc::{DoublingDelayScheduler, Message as RmcMessage, ReliableMulticast};
use futures::{channel::mpsc, FutureExt, StreamExt};
Expand All @@ -20,6 +21,8 @@ pub struct Service<H: Hasher, D: Data, MK: MultiKeychain> {
messages_for_rmc: Sender<RmcMessage<H::Hash, MK::Signature, MK::PartialMultisignature>>,
messages_from_rmc: Receiver<RmcMessage<H::Hash, MK::Signature, MK::PartialMultisignature>>,
keychain: MK,
items_for_backup: Sender<BackupItem<H, D, MK>>,
responses_from_backup: Receiver<BackupItem<H, D, MK>>,
exiting: bool,
}

Expand All @@ -31,6 +34,8 @@ impl<H: Hasher, D: Data, MK: MultiKeychain> Service<H, D, MK> {
notifications_for_units: Sender<ForkingNotification<H, D, MK::Signature>>,
alerts_from_units: Receiver<Alert<H, D, MK::Signature>>,
n_members: NodeCount,
items_for_backup: Sender<BackupItem<H, D, MK>>,
responses_from_backup: Receiver<BackupItem<H, D, MK>>,
) -> Service<H, D, MK> {
let (messages_for_rmc, messages_from_us) = mpsc::unbounded();
let (messages_for_us, messages_from_rmc) = mpsc::unbounded();
Expand All @@ -52,6 +57,8 @@ impl<H: Hasher, D: Data, MK: MultiKeychain> Service<H, D, MK> {
messages_for_rmc,
messages_from_rmc,
keychain,
items_for_backup,
responses_from_backup,
exiting: false,
}
}
Expand Down Expand Up @@ -110,7 +117,7 @@ impl<H: Hasher, D: Data, MK: MultiKeychain> Service<H, D, MK> {
) {
match handler.on_message(message) {
Ok(Some(AlerterResponse::ForkAlert(alert, recipient))) => {
self.send_message_for_network(AlertMessage::ForkAlert(alert), recipient);
self.send_item_to_backup(BackupItem::NetworkAlert(alert, recipient));
}
Ok(Some(AlerterResponse::AlertRequest(hash, peer))) => {
let message = AlertMessage::AlertRequest(self.keychain.index(), hash);
Expand All @@ -137,31 +144,36 @@ impl<H: Hasher, D: Data, MK: MultiKeychain> Service<H, D, MK> {
}
}

fn handle_alert_from_runway(
fn handle_response_from_backup(
&mut self,
handler: &mut Handler<H, D, MK>,
alert: Alert<H, D, MK::Signature>,
) {
let (message, recipient, hash) = handler.on_own_alert(alert);
self.send_message_for_network(message, recipient);
self.rmc.start_rmc(hash);
}

fn handle_message_from_rmc(
&mut self,
message: RmcMessage<H::Hash, MK::Signature, MK::PartialMultisignature>,
response: BackupItem<H, D, MK>,
) {
self.rmc_message_to_network(message)
match response {
BackupItem::OwnAlert(alert) => {
let (message, recipient, hash) = handler.on_own_alert(alert);
self.send_message_for_network(message, recipient);
self.rmc.start_rmc(hash);
}
BackupItem::NetworkAlert(alert, recipient) => {
self.send_message_for_network(AlertMessage::ForkAlert(alert), recipient);
}
BackupItem::MultiSignature(multisigned) => match handler.alert_confirmed(multisigned) {
Ok(notification) => self.send_notification_for_units(notification),
Err(error) => warn!(target: LOG_TARGET, "{}", error),
},
_ => {}
}
}

fn handle_multisigned(
&mut self,
handler: &mut Handler<H, D, MK>,
multisigned: Multisigned<H::Hash, MK>,
) {
match handler.alert_confirmed(multisigned) {
Ok(notification) => self.send_notification_for_units(notification),
Err(error) => warn!(target: LOG_TARGET, "{}", error),
fn send_item_to_backup(&mut self, item: BackupItem<H, D, MK>) {
if self.items_for_backup.unbounded_send(item).is_err() {
warn!(
target: LOG_TARGET,
"{:?} Channel for passing items to backup was closed",
self.keychain.index()
);
self.exiting = true;
}
}

Expand All @@ -176,20 +188,27 @@ impl<H: Hasher, D: Data, MK: MultiKeychain> Service<H, D, MK> {
}
},
alert = self.alerts_from_units.next() => match alert {
Some(alert) => self.handle_alert_from_runway(&mut handler, alert),
Some(alert) => self.send_item_to_backup(BackupItem::OwnAlert(alert)),
None => {
error!(target: LOG_TARGET, "{:?} Alert stream closed.", self.keychain.index());
break;
}
},
message = self.messages_from_rmc.next() => match message {
Some(message) => self.handle_message_from_rmc(message),
Some(message) => self.rmc_message_to_network(message),
None => {
error!(target: LOG_TARGET, "{:?} RMC message stream closed.", self.keychain.index());
break;
}
},
multisigned = self.rmc.next_multisigned_hash().fuse() => self.handle_multisigned(&mut handler, multisigned),
multisigned = self.rmc.next_multisigned_hash().fuse() => self.send_item_to_backup(BackupItem::MultiSignature(multisigned)),
response = self.responses_from_backup.next() => match response {
Some(item) => self.handle_response_from_backup(&mut handler, item),
None => {
error!(target: LOG_TARGET, "Receiver of responses from backup closed");
break;
}
},
_ = terminator.get_exit().fuse() => {
debug!(target: LOG_TARGET, "{:?} received exit signal", self.keychain.index());
self.exiting = true;
Expand Down
4 changes: 2 additions & 2 deletions consensus/src/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ where

/// Most tasks use `requests_interval` (see [crate::DelayConfig]) as their delay.
///
/// The first exception is [Task::UnitBroadcast] - this one picks a random delay between
/// The first exception is [UnitBroadcast] - this one picks a random delay between
/// `unit_rebroadcast_interval_min` and `unit_rebroadcast_interval_max`.
///
/// The other exception is [Task::CoordRequest] - this one uses the configurable
/// The other exception is [CoordRequest] - this one uses the configurable
/// `coord_request_delay` schedule.
fn delay(&self, task: &Task<H, D, S>, counter: usize) -> Duration {
match task {
Expand Down
Loading
Loading