Skip to content

Commit

Permalink
Don't panic in ValidatorNetwork::send_to if not an elected validator
Browse files Browse the repository at this point in the history
  • Loading branch information
hrxi committed Nov 18, 2023
1 parent a15bbaa commit 7401866
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 3 additions & 0 deletions validator-network/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ where
#[error("Unreachable")]
Unreachable,

#[error("We are not an elected validator")]
NotElected,

/// If no specific set of peers was given but no connection could be established indicating that self is unreachable
#[error("Network is offline")]
Offline,
Expand Down
15 changes: 7 additions & 8 deletions validator-network/src/network_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, sync::Arc};
use std::{collections::BTreeMap, error::Error, sync::Arc};

use async_trait::async_trait;
use futures::{stream::BoxStream, StreamExt, TryFutureExt};
Expand Down Expand Up @@ -61,14 +61,13 @@ where
}
}

/// Returns the local validator ID.
fn local_validator_id(&self) -> u16 {
/// Returns the local validator ID, if elected, `Err(NotElected)` otherwise.
fn local_validator_id<T: Error + 'static>(&self) -> Result<u16, NetworkError<T>> {
self.state
.read()
.own_validator_id
.expect("active validator")
.try_into()
.unwrap()
.map(|id| id.try_into().unwrap())
.ok_or(NetworkError::NotElected)
}

/// Looks up the peer ID for a validator public key in the DHT.
Expand Down Expand Up @@ -193,7 +192,7 @@ where

async fn send_to<M: Message>(&self, validator_id: usize, msg: M) -> Result<(), Self::Error> {
let msg = ValidatorMessage {
validator_id: self.local_validator_id(),
validator_id: self.local_validator_id()?,
inner: msg,
};
if let Ok(peer_id) = self.get_validator_peer_id(validator_id).await {
Expand All @@ -215,7 +214,7 @@ where
NetworkError<<Self::NetworkType as Network>::Error>,
> {
let request = ValidatorMessage {
validator_id: self.local_validator_id(),
validator_id: self.local_validator_id()?,
inner: request,
};
if let Ok(peer_id) = self.get_validator_peer_id(validator_id).await {
Expand Down

0 comments on commit 7401866

Please sign in to comment.