Skip to content

Commit

Permalink
replace calls to Memberships
Browse files Browse the repository at this point in the history
Delete Memberships and replace functionality. Add some methods to
`Membership` trait to deal w/ collapsing into one type both kinds of
memberships (stake and DA).

  * avoid passing membership into `is_valid_cert
  * for DA, avoid proxying threshold through `Threshold` trait
  * remove `Topic` param from `Membership::new
  * Split cert impls by marker (#3891)
  * add membership methods to Cert trait
  * remove non-existent tests from justfile
  • Loading branch information
tbro committed Nov 19, 2024
1 parent 7abc4ba commit 572e6a2
Show file tree
Hide file tree
Showing 42 changed files with 951 additions and 584 deletions.
22 changes: 4 additions & 18 deletions crates/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use hotshot::{
BlockPayload, NodeImplementation,
},
types::SystemContextHandle,
MarketplaceConfig, Memberships, SystemContext,
MarketplaceConfig, SystemContext,
};
use hotshot_example_types::{
auction_results_provider_types::TestAuctionResultsProvider,
Expand All @@ -55,7 +55,7 @@ use hotshot_types::{
traits::{
block_contents::{BlockHeader, TestableBlock},
election::Membership,
network::{ConnectedNetwork, Topic},
network::ConnectedNetwork,
node_implementation::{ConsensusTime, NodeType, Versions},
states::TestableState,
},
Expand Down Expand Up @@ -381,22 +381,9 @@ pub trait RunDa<

let da_nodes = config.config.known_da_nodes.clone();

// Create the quorum membership from all nodes
let quorum_membership = <TYPES as NodeType>::Membership::new(
all_nodes.clone(),
all_nodes.clone(),
Topic::Global,
);

// Create the quorum membership from all nodes, specifying the committee
// as the known da nodes
let da_membership =
<TYPES as NodeType>::Membership::new(all_nodes.clone(), da_nodes, Topic::Da);

let memberships = Memberships {
quorum_membership: quorum_membership.clone(),
da_membership,
};
let memberships = <TYPES as NodeType>::Membership::new(all_nodes.clone(), da_nodes);

let marketplace_config = MarketplaceConfig {
auction_results_provider: TestAuctionResultsProvider::<TYPES>::default().into(),
Expand Down Expand Up @@ -544,7 +531,6 @@ pub trait RunDa<
let num_eligible_leaders = context
.hotshot
.memberships
.quorum_membership
.committee_leaders(TYPES::View::genesis(), TYPES::Epoch::genesis())
.len();
let total_num_views = usize::try_from(consensus.locked_view().u64()).unwrap();
Expand Down Expand Up @@ -752,7 +738,7 @@ where

// Create the qurorum membership from the list of known nodes
let all_nodes = config.config.known_nodes_with_stake.clone();
let quorum_membership = TYPES::Membership::new(all_nodes.clone(), all_nodes, Topic::Global);
let quorum_membership = TYPES::Membership::new(all_nodes.clone(), all_nodes);

// Derive the bind address
let bind_address =
Expand Down
22 changes: 6 additions & 16 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ pub struct MarketplaceConfig<TYPES: NodeType, I: NodeImplementation<TYPES>> {
pub fallback_builder_url: Url,
}

/// Bundle of all the memberships a consensus instance uses
#[derive(Clone)]
pub struct Memberships<TYPES: NodeType> {
/// The entire quorum
pub quorum_membership: TYPES::Membership,
/// The DA nodes
pub da_membership: TYPES::Membership,
}

/// Holds the state needed to participate in `HotShot` consensus
pub struct SystemContext<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> {
/// The public key of this node
Expand All @@ -116,7 +107,7 @@ pub struct SystemContext<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versi
pub network: Arc<I::Network>,

/// Memberships used by consensus
pub memberships: Arc<Memberships<TYPES>>,
pub memberships: Arc<TYPES::Membership>,

/// the metrics that the implementor is using.
metrics: Arc<ConsensusMetricsValue>,
Expand Down Expand Up @@ -207,7 +198,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
nonce: u64,
config: HotShotConfig<TYPES::SignatureKey>,
memberships: Memberships<TYPES>,
memberships: TYPES::Membership,
network: Arc<I::Network>,
initializer: HotShotInitializer<TYPES>,
metrics: ConsensusMetricsValue,
Expand Down Expand Up @@ -260,7 +251,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
nonce: u64,
config: HotShotConfig<TYPES::SignatureKey>,
memberships: Memberships<TYPES>,
memberships: TYPES::Membership,
network: Arc<I::Network>,
initializer: HotShotInitializer<TYPES>,
metrics: ConsensusMetricsValue,
Expand Down Expand Up @@ -507,7 +498,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
})?;

spawn(async move {
let da_membership = &api.memberships.da_membership.clone();
join! {
// TODO We should have a function that can return a network error if there is one
// but first we'd need to ensure our network implementations can support that
Expand All @@ -519,7 +509,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
api
.network.da_broadcast_message(
serialized_message,
da_membership.committee_members(view_number, TYPES::Epoch::new(1)).iter().cloned().collect(),
api.memberships.da_committee_members(view_number, TYPES::Epoch::new(1)).iter().cloned().collect(),
BroadcastDelay::None,
),
api
Expand Down Expand Up @@ -604,7 +594,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
node_id: u64,
config: HotShotConfig<TYPES::SignatureKey>,
memberships: Memberships<TYPES>,
memberships: TYPES::Membership,
network: Arc<I::Network>,
initializer: HotShotInitializer<TYPES>,
metrics: ConsensusMetricsValue,
Expand Down Expand Up @@ -767,7 +757,7 @@ where
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
nonce: u64,
config: HotShotConfig<TYPES::SignatureKey>,
memberships: Memberships<TYPES>,
memberships: TYPES::Membership,
network: Arc<I::Network>,
initializer: HotShotInitializer<TYPES>,
metrics: ConsensusMetricsValue,
Expand Down
37 changes: 14 additions & 23 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ use crate::{
ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer,
MarketplaceConfig, Memberships, NetworkTaskRegistry, SignatureKey, SystemContext, Versions,
};
use crate::{
tasks::task_state::CreateTaskState, types::SystemContextHandle, ConsensusApi,
ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer,
MarketplaceConfig, NetworkTaskRegistry, SignatureKey, SystemContext, Versions,
};

/// event for global event stream
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -82,7 +87,7 @@ pub fn add_response_task<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versi
) {
let state = NetworkResponseState::<TYPES>::new(
handle.hotshot.consensus(),
handle.hotshot.memberships.quorum_membership.clone().into(),
(*handle.hotshot.memberships).clone().into(),
handle.public_key().clone(),
handle.private_key().clone(),
handle.hotshot.id,
Expand Down Expand Up @@ -190,15 +195,13 @@ pub fn add_network_event_task<
>(
handle: &mut SystemContextHandle<TYPES, I, V>,
network: Arc<NET>,
quorum_membership: TYPES::Membership,
da_membership: TYPES::Membership,
membership: TYPES::Membership,
) {
let network_state: NetworkEventTaskState<_, V, _, _> = NetworkEventTaskState {
network,
view: TYPES::View::genesis(),
epoch: TYPES::Epoch::genesis(),
quorum_membership,
da_membership,
membership,
storage: Arc::clone(&handle.storage()),
consensus: OuterConsensus::new(handle.consensus()),
upgrade_lock: handle.hotshot.upgrade_lock.clone(),
Expand Down Expand Up @@ -323,7 +326,7 @@ where
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
nonce: u64,
config: HotShotConfig<TYPES::SignatureKey>,
memberships: Memberships<TYPES>,
memberships: TYPES::Membership,
network: Arc<I::Network>,
initializer: HotShotInitializer<TYPES>,
metrics: ConsensusMetricsValue,
Expand Down Expand Up @@ -518,26 +521,18 @@ where
/// Adds the `NetworkEventTaskState` tasks possibly modifying them as well.
fn add_network_event_tasks(&self, handle: &mut SystemContextHandle<TYPES, I, V>) {
let network = Arc::clone(&handle.network);
let quorum_membership = handle.memberships.quorum_membership.clone();
let da_membership = handle.memberships.da_membership.clone();

self.add_network_event_task(
handle,
Arc::clone(&network),
quorum_membership.clone(),
da_membership,
);

self.add_network_event_task(handle, Arc::clone(&network), (*handle.memberships).clone());
}

/// Adds a `NetworkEventTaskState` task. Can be reimplemented to modify its behaviour.
fn add_network_event_task(
&self,
handle: &mut SystemContextHandle<TYPES, I, V>,
channel: Arc<<I as NodeImplementation<TYPES>>::Network>,
quorum_membership: TYPES::Membership,
da_membership: TYPES::Membership,
membership: TYPES::Membership,
) {
add_network_event_task(handle, channel, quorum_membership, da_membership);
add_network_event_task(handle, channel, membership);
}
}

Expand Down Expand Up @@ -570,13 +565,9 @@ pub async fn add_network_message_and_request_receiver_tasks<
pub fn add_network_event_tasks<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions>(
handle: &mut SystemContextHandle<TYPES, I, V>,
) {
let quorum_membership = handle.memberships.quorum_membership.clone();
let da_membership = handle.memberships.da_membership.clone();

add_network_event_task(
handle,
Arc::clone(&handle.network),
quorum_membership,
da_membership,
(*handle.memberships).clone(),
);
}
27 changes: 11 additions & 16 deletions crates/hotshot/src/tasks/task_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
consensus: OuterConsensus::new(handle.hotshot.consensus()),
view: handle.cur_view().await,
delay: handle.hotshot.config.data_request_delay,
da_membership: handle.hotshot.memberships.da_membership.clone(),
da_membership: (*handle.hotshot.memberships).clone(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
id: handle.hotshot.id,
Expand All @@ -71,7 +71,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
cur_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
network: Arc::clone(&handle.hotshot.network),
vote_collectors: BTreeMap::default(),
public_key: handle.public_key().clone(),
Expand All @@ -93,7 +93,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
cur_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
network: Arc::clone(&handle.hotshot.network),
vote_collector: None.into(),
public_key: handle.public_key().clone(),
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
cur_epoch: handle.cur_epoch().await,
vote_collector: None,
network: Arc::clone(&handle.hotshot.network),
membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
id: handle.hotshot.id,
Expand All @@ -139,9 +139,8 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
Self {
consensus: OuterConsensus::new(handle.hotshot.consensus()),
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
da_membership: handle.hotshot.memberships.da_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
network: Arc::clone(&handle.hotshot.network),
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
cur_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
vote_collectors: BTreeMap::default(),
Expand All @@ -166,7 +165,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
next_view: cur_view,
cur_epoch: handle.cur_epoch().await,
network: Arc::clone(&handle.hotshot.network),
membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
num_timeouts_tracked: 0,
Expand Down Expand Up @@ -194,7 +193,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
cur_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
network: Arc::clone(&handle.hotshot.network),
membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
instance_state: handle.hotshot.instance_state(),
Expand Down Expand Up @@ -235,8 +234,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
latest_voted_view: handle.cur_view().await,
vote_dependencies: BTreeMap::new(),
network: Arc::clone(&handle.hotshot.network),
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
da_membership: handle.hotshot.memberships.da_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
id: handle.hotshot.id,
storage: Arc::clone(&handle.storage),
Expand All @@ -260,8 +258,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
consensus: OuterConsensus::new(consensus),
instance_state: handle.hotshot.instance_state(),
timeout_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
storage: Arc::clone(&handle.storage),
Expand All @@ -288,7 +285,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
consensus: OuterConsensus::new(consensus),
cur_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
quorum_membership: (*handle.hotshot.memberships).clone().into(),
timeout: handle.hotshot.config.next_view_timeout,
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
storage: Arc::clone(&handle.storage),
Expand All @@ -312,9 +309,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
private_key: handle.private_key().clone(),
instance_state: handle.hotshot.instance_state(),
network: Arc::clone(&handle.hotshot.network),
timeout_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
quorum_membership: handle.hotshot.memberships.quorum_membership.clone().into(),
committee_membership: handle.hotshot.memberships.da_membership.clone().into(),
membership: (*handle.hotshot.memberships).clone().into(),
vote_collectors: BTreeMap::default(),
timeout_vote_collectors: BTreeMap::default(),
storage: Arc::clone(&handle.storage),
Expand Down
Loading

0 comments on commit 572e6a2

Please sign in to comment.