diff --git a/light-client/src/peer_list.rs b/light-client/src/peer_list.rs index 8454efef5..0f8776836 100644 --- a/light-client/src/peer_list.rs +++ b/light-client/src/peer_list.rs @@ -138,7 +138,10 @@ impl PeerList { /// ## 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 { + pub fn replace_faulty_primary( + &mut self, + primary_error: Option, + ) -> Result { self.faulty_nodes.insert(self.primary); if let Some(new_primary) = self.witnesses.iter().next().copied() { @@ -147,7 +150,11 @@ impl PeerList { return Ok(new_primary); } - bail!(ErrorKind::NoWitnessLeft) + if let Some(err) = primary_error { + bail!(ErrorKind::NoWitnessLeft.context(err)) + } else { + bail!(ErrorKind::NoWitnessLeft) + } } } @@ -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()); @@ -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) diff --git a/light-client/src/supervisor.rs b/light-client/src/supervisor.rs index 827417a01..30994f5c1 100644 --- a/light-client/src/supervisor.rs +++ b/light-client/src/supervisor.rs @@ -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) } }