From 7e9279c44a3cc1a2af8c332a8b7766c2b2cfbb63 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Thu, 11 Jun 2020 14:39:36 +0200 Subject: [PATCH] Throw an error if there are no witnesses when doing fork detection --- light-client/src/errors.rs | 3 +++ light-client/src/peer_list.rs | 4 ++-- light-client/src/supervisor.rs | 14 ++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/light-client/src/errors.rs b/light-client/src/errors.rs index 8f58cb45a..afb2de148 100644 --- a/light-client/src/errors.rs +++ b/light-client/src/errors.rs @@ -21,6 +21,9 @@ pub enum ErrorKind { #[error("store error")] Store, + #[error("no witnesses")] + NoWitnesses, + #[error("no valid peer left")] NoValidPeerLeft, diff --git a/light-client/src/peer_list.rs b/light-client/src/peer_list.rs index ce9081ec7..1c5961fdc 100644 --- a/light-client/src/peer_list.rs +++ b/light-client/src/peer_list.rs @@ -35,7 +35,7 @@ impl PeerList { self.peers.get_mut(&self.primary) } - pub fn secondaries(&self) -> Vec<&Instance> { + pub fn witnesses(&self) -> Vec<&Instance> { self.peers .keys() .filter(|peer_id| peer_id != &&self.primary) @@ -44,7 +44,7 @@ impl PeerList { } #[pre(peer_id != &self.primary)] - pub fn remove_secondary(&mut self, peer_id: &PeerId) { + pub fn remove_witness(&mut self, peer_id: &PeerId) { self.peers.remove(peer_id); } diff --git a/light-client/src/supervisor.rs b/light-client/src/supervisor.rs index dde0fffe9..6b7c197d1 100644 --- a/light-client/src/supervisor.rs +++ b/light-client/src/supervisor.rs @@ -115,7 +115,7 @@ impl Supervisor { forked.push(block.provider); } Fork::Faulty(block, _error) => { - self.peers.remove_secondary(&block.provider); + self.peers.remove_witness(&block.provider); // TODO: Log/record the error } } @@ -156,15 +156,13 @@ impl Supervisor { light_block: &LightBlock, trusted_state: &LightBlock, ) -> Result>, Error> { - if self.peers.secondaries().is_empty() { - return Ok(None); + if self.peers.witnesses().is_empty() { + bail!(ErrorKind::NoWitnesses); } - let result = self.fork_detector.detect_forks( - light_block, - &trusted_state, - self.peers.secondaries(), - )?; + let result = + self.fork_detector + .detect_forks(light_block, &trusted_state, self.peers.witnesses())?; match result { ForkDetection::Detected(forks) => Ok(Some(forks)),