Skip to content

Commit

Permalink
Throw an error if there are no witnesses when doing fork detection
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Jun 11, 2020
1 parent e044ab5 commit 7e9279c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
3 changes: 3 additions & 0 deletions light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub enum ErrorKind {
#[error("store error")]
Store,

#[error("no witnesses")]
NoWitnesses,

#[error("no valid peer left")]
NoValidPeerLeft,

Expand Down
4 changes: 2 additions & 2 deletions light-client/src/peer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
14 changes: 6 additions & 8 deletions light-client/src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -156,15 +156,13 @@ impl Supervisor {
light_block: &LightBlock,
trusted_state: &LightBlock,
) -> Result<Option<Vec<Fork>>, 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)),
Expand Down

0 comments on commit 7e9279c

Please sign in to comment.