Skip to content

Commit

Permalink
Use primary error as context of NoWitnessLeft error (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang authored Jul 24, 2020
1 parent 548d76d commit cf4a28d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 12 additions & 5 deletions light-client/src/peer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ impl<T> PeerList<T> {
/// ## Errors
/// - If there are no witness left, returns `ErrorKind::NoWitnessLeft`.
#[post(ret.is_ok() ==> Self::invariant(&self))]
pub fn replace_faulty_primary(&mut self) -> Result<PeerId, Error> {
pub fn replace_faulty_primary(
&mut self,
primary_error: Option<Error>,
) -> Result<PeerId, Error> {
self.faulty_nodes.insert(self.primary);

if let Some(new_primary) = self.witnesses.iter().next().copied() {
Expand All @@ -147,7 +150,11 @@ impl<T> PeerList<T> {
return Ok(new_primary);
}

bail!(ErrorKind::NoWitnessLeft)
if let Some(err) = primary_error {
bail!(ErrorKind::NoWitnessLeft.context(err))
} else {
bail!(ErrorKind::NoWitnessLeft)
}
}
}

Expand Down Expand Up @@ -283,7 +290,7 @@ mod tests {
fn replace_faulty_primary_succeeds() {
let mut peer_list = dummy_peer_list();
assert_eq!(peer_list.primary(), &1);
let new_primary = peer_list.replace_faulty_primary();
let new_primary = peer_list.replace_faulty_primary(None);
assert_eq!(new_primary.unwrap(), b());
assert_eq!(peer_list.primary(), &2);
assert!(peer_list.witnesses_ids().is_empty());
Expand All @@ -292,8 +299,8 @@ mod tests {
#[test]
fn replace_faulty_primary_fails_if_no_more_witnesses() {
let mut peer_list = dummy_peer_list();
let _ = peer_list.replace_faulty_primary().unwrap();
let new_primary = peer_list.replace_faulty_primary();
let _ = peer_list.replace_faulty_primary(None).unwrap();
let new_primary = peer_list.replace_faulty_primary(None);
assert_eq!(
new_primary.err().map(|e| e.kind().clone()),
Some(ErrorKind::NoWitnessLeft)
Expand Down
6 changes: 2 additions & 4 deletions light-client/src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,9 @@ impl Supervisor {
}
}
// Verification failed
Err(_err) => {
// TODO: Log/record error

Err(err) => {
// Swap primary, and continue with new primary, if there is any witness left.
self.peers.replace_faulty_primary()?;
self.peers.replace_faulty_primary(Some(err))?;
self.verify(height)
}
}
Expand Down

0 comments on commit cf4a28d

Please sign in to comment.