mirror of https://github.com/poanetwork/hbbft.git
Simplify observer/peer-specific code.
Extract some methods to make the branches more readable.
This commit is contained in:
parent
2db67df325
commit
aaec3d4074
|
@ -292,14 +292,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
|
|||
}
|
||||
|
||||
// Otherwise multicast the proof in an `Echo` message, and handle it ourselves.
|
||||
self.echo_sent = true;
|
||||
if self.netinfo.is_peer() {
|
||||
let our_uid = &self.netinfo.our_uid().clone();
|
||||
self.handle_echo(our_uid, p.clone())?;
|
||||
let echo_msg = Target::All.message(BroadcastMessage::Echo(p));
|
||||
self.messages.push_back(echo_msg);
|
||||
}
|
||||
Ok(())
|
||||
self.send_echo(p)
|
||||
}
|
||||
|
||||
/// Handles a received `Echo` message.
|
||||
|
@ -329,14 +322,7 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
|
|||
}
|
||||
|
||||
// Upon receiving `N - f` `Echo`s with this root hash, multicast `Ready`.
|
||||
self.ready_sent = true;
|
||||
if self.netinfo.is_peer() {
|
||||
let ready_msg = Target::All.message(BroadcastMessage::Ready(hash.clone()));
|
||||
self.messages.push_back(ready_msg);
|
||||
let our_uid = &self.netinfo.our_uid().clone();
|
||||
self.handle_ready(our_uid, &hash)?;
|
||||
}
|
||||
Ok(())
|
||||
self.send_ready(&hash)
|
||||
}
|
||||
|
||||
/// Handles a received `Ready` message.
|
||||
|
@ -357,13 +343,33 @@ impl<NodeUid: Debug + Clone + Ord> Broadcast<NodeUid> {
|
|||
// has not yet been sent, multicast Ready(h).
|
||||
if self.count_readys(hash) == self.netinfo.num_faulty() + 1 && !self.ready_sent {
|
||||
// Enqueue a broadcast of a Ready message.
|
||||
self.ready_sent = true;
|
||||
if self.netinfo.is_peer() {
|
||||
let ready_msg = Target::All.message(BroadcastMessage::Ready(hash.to_vec()));
|
||||
self.messages.push_back(ready_msg);
|
||||
}
|
||||
self.send_ready(hash)?;
|
||||
}
|
||||
self.compute_output(&hash)
|
||||
self.compute_output(hash)
|
||||
}
|
||||
|
||||
/// 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<()> {
|
||||
self.echo_sent = true;
|
||||
if !self.netinfo.is_peer() {
|
||||
return Ok(());
|
||||
}
|
||||
let echo_msg = Target::All.message(BroadcastMessage::Echo(p.clone()));
|
||||
self.messages.push_back(echo_msg);
|
||||
let our_uid = &self.netinfo.our_uid().clone();
|
||||
self.handle_echo(our_uid, p)
|
||||
}
|
||||
|
||||
/// Sends a `Ready` message and handles it. Does nothing if we are only an observer.
|
||||
fn send_ready(&mut self, hash: &[u8]) -> BroadcastResult<()> {
|
||||
self.ready_sent = true;
|
||||
if !self.netinfo.is_peer() {
|
||||
return Ok(());
|
||||
}
|
||||
let ready_msg = Target::All.message(BroadcastMessage::Ready(hash.to_vec()));
|
||||
self.messages.push_back(ready_msg);
|
||||
let our_uid = &self.netinfo.our_uid().clone();
|
||||
self.handle_ready(our_uid, hash)
|
||||
}
|
||||
|
||||
/// Checks whether the condition for output are met for this hash, and if so, sets the output
|
||||
|
|
|
@ -428,31 +428,10 @@ where
|
|||
self.verify_pending_decryption_shares(&proposer_id, &ciphertext);
|
||||
self.remove_incorrect_decryption_shares(&proposer_id, incorrect_senders);
|
||||
|
||||
if self.netinfo.is_peer() {
|
||||
if let Some(share) = self.netinfo.secret_key().decrypt_share(&ciphertext) {
|
||||
// Send the share to remote nodes.
|
||||
self.messages.0.push_back(
|
||||
Target::All.message(
|
||||
MessageContent::DecryptionShare {
|
||||
proposer_id: proposer_id.clone(),
|
||||
share: share.clone(),
|
||||
}.with_epoch(self.epoch),
|
||||
),
|
||||
);
|
||||
let our_id = self.netinfo.our_uid().clone();
|
||||
let epoch = self.epoch;
|
||||
// Receive the share locally.
|
||||
self.handle_decryption_share_message(
|
||||
&our_id,
|
||||
epoch,
|
||||
proposer_id.clone(),
|
||||
share,
|
||||
)?;
|
||||
} else {
|
||||
warn!("Share decryption failed for proposer {:?}", proposer_id);
|
||||
// TODO: Log the decryption failure.
|
||||
continue;
|
||||
}
|
||||
if !self.send_decryption_share(&proposer_id, &ciphertext)? {
|
||||
warn!("Share decryption failed for proposer {:?}", proposer_id);
|
||||
// TODO: Log the decryption failure.
|
||||
continue;
|
||||
}
|
||||
let ciphertexts = self
|
||||
.ciphertexts
|
||||
|
@ -463,6 +442,33 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Verifies the ciphertext and sends decryption shares. Returns whether it is valid.
|
||||
fn send_decryption_share(
|
||||
&mut self,
|
||||
proposer_id: &NodeUid,
|
||||
ciphertext: &Ciphertext,
|
||||
) -> HoneyBadgerResult<bool> {
|
||||
if !self.netinfo.is_peer() {
|
||||
return Ok(ciphertext.verify());
|
||||
}
|
||||
let share = match self.netinfo.secret_key().decrypt_share(&ciphertext) {
|
||||
None => return Ok(false),
|
||||
Some(share) => share,
|
||||
};
|
||||
// Send the share to remote nodes.
|
||||
let content = MessageContent::DecryptionShare {
|
||||
proposer_id: proposer_id.clone(),
|
||||
share: share.clone(),
|
||||
};
|
||||
let message = Target::All.message(content.with_epoch(self.epoch));
|
||||
self.messages.0.push_back(message);
|
||||
let our_id = self.netinfo.our_uid().clone();
|
||||
let epoch = self.epoch;
|
||||
// Receive the share locally.
|
||||
self.handle_decryption_share_message(&our_id, epoch, proposer_id.clone(), share)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Verifies the shares of the current epoch that are pending verification. Returned are the
|
||||
/// senders with incorrect pending shares.
|
||||
fn verify_pending_decryption_shares(
|
||||
|
|
Loading…
Reference in New Issue