Skip to content

Commit

Permalink
update trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-es committed Oct 24, 2024
1 parent 05348f0 commit c5aa0cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 3 additions & 1 deletion crates/hotshot/src/traits/election/randomized_committee.rs
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

0 comments on commit c5aa0cd

Please sign in to comment.