Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error type to Membership trait #3801

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct RandomizedCommittee<T: NodeType> {
}

impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
type Error = utils::anytrace::Error;

/// Create a new election
fn new(
eligible_leaders: Vec<PeerConfig<<TYPES as NodeType>::SignatureKey>>,
Expand Down Expand Up @@ -139,7 +141,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
}

/// Index the vector of public keys with the current view number
fn leader(
fn lookup_leader(
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
Expand Down
4 changes: 3 additions & 1 deletion crates/hotshot/src/traits/election/static_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct StaticCommittee<T: NodeType> {
}

impl<TYPES: NodeType> Membership<TYPES> for StaticCommittee<TYPES> {
type Error = utils::anytrace::Error;

/// Create a new election
fn new(
eligible_leaders: Vec<PeerConfig<<TYPES as NodeType>::SignatureKey>>,
Expand Down Expand Up @@ -137,7 +139,7 @@ impl<TYPES: NodeType> Membership<TYPES> for StaticCommittee<TYPES> {
}

/// Index the vector of public keys with the current view number
fn leader(
fn lookup_leader(
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct StaticCommitteeLeaderForTwoViews<T: NodeType> {
}

impl<TYPES: NodeType> Membership<TYPES> for StaticCommitteeLeaderForTwoViews<TYPES> {
type Error = utils::anytrace::Error;

/// Create a new election
fn new(
eligible_leaders: Vec<PeerConfig<<TYPES as NodeType>::SignatureKey>>,
Expand Down Expand Up @@ -137,7 +139,7 @@ impl<TYPES: NodeType> Membership<TYPES> for StaticCommitteeLeaderForTwoViews<TYP
}

/// Index the vector of public keys with the current view number
fn leader(
fn lookup_leader(
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
Expand Down
24 changes: 23 additions & 1 deletion crates/types/src/traits/election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use crate::{traits::signature_key::SignatureKey, PeerConfig};
pub trait Membership<TYPES: NodeType>:
Clone + Debug + Eq + PartialEq + Send + Sync + Hash + 'static
{
/// The error type returned by methods like `lookup_leader`.
type Error: std::fmt::Display;

/// Create a committee
fn new(
// Note: eligible_leaders is currently a hack because the DA leader == the quorum leader
Expand Down Expand Up @@ -56,11 +59,30 @@ pub trait Membership<TYPES: NodeType>:
/// See if a node has stake in the committee in a specific epoch
fn has_stake(&self, pub_key: &TYPES::SignatureKey, epoch: TYPES::Epoch) -> bool;

/// The leader of the committee for view `view_number` in `epoch`.
///
/// Note: this function uses a HotShot-internal error type.
/// You should implement `lookup_leader`, rather than implementing this function directly.
///
/// # Errors
/// Returns an error if the leader cannot be calculated.
fn leader(&self, view: TYPES::View, epoch: TYPES::Epoch) -> Result<TYPES::SignatureKey> {
use utils::anytrace::*;

self.lookup_leader(view, epoch).wrap().context(info!(
"Failed to get leader for view {view} in epoch {epoch}"
))
}

/// The leader of the committee for view `view_number` in `epoch`.
///
/// # Errors
/// Returns an error if the leader cannot be calculated
fn leader(&self, view: TYPES::View, epoch: TYPES::Epoch) -> Result<TYPES::SignatureKey>;
fn lookup_leader(
&self,
view: TYPES::View,
epoch: TYPES::Epoch,
) -> std::result::Result<TYPES::SignatureKey, Self::Error>;

/// Get the network topic for the committee
fn committee_topic(&self) -> Topic;
Expand Down