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).
  • Loading branch information
tbro committed Nov 11, 2024
1 parent b10ea3f commit 859aa47
Show file tree
Hide file tree
Showing 30 changed files with 485 additions and 340 deletions.
17 changes: 2 additions & 15 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 Down Expand Up @@ -381,23 +381,11 @@ 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 =
let memberships =
<TYPES as NodeType>::Membership::new(all_nodes.clone(), da_nodes, Topic::Da);

let memberships = Memberships {
quorum_membership: quorum_membership.clone(),
da_membership,
};

let marketplace_config = MarketplaceConfig {
auction_results_provider: TestAuctionResultsProvider::<TYPES>::default().into(),
// TODO: we need to pass a valid fallback builder url here somehow
Expand Down Expand Up @@ -544,7 +532,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
22 changes: 6 additions & 16 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,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 @@ -114,7 +105,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 @@ -197,7 +188,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 @@ -237,7 +228,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 @@ -482,7 +473,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 @@ -494,7 +484,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 @@ -579,7 +569,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 @@ -742,7 +732,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
35 changes: 11 additions & 24 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use hotshot_task_impls::{
vid::VidTaskState,
view_sync::ViewSyncTaskState,
};
use hotshot_types::traits::election::Membership;
use hotshot_types::{
consensus::Consensus,
constants::EVENT_CHANNEL_SIZE,
Expand All @@ -46,7 +47,7 @@ use vbs::version::StaticVersionType;
use crate::{
tasks::task_state::CreateTaskState, types::SystemContextHandle, ConsensusApi,
ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer,
MarketplaceConfig, Memberships, NetworkTaskRegistry, SignatureKey, SystemContext, Versions,
MarketplaceConfig, NetworkTaskRegistry, SignatureKey, SystemContext, Versions,
};

/// event for global event stream
Expand Down Expand Up @@ -82,7 +83,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.eject().into(),
handle.public_key().clone(),
handle.private_key().clone(),
handle.hotshot.id,
Expand Down Expand Up @@ -189,15 +190,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: Arc::clone(&handle.consensus()),
upgrade_lock: handle.hotshot.upgrade_lock.clone(),
Expand Down Expand Up @@ -322,7 +321,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 @@ -517,26 +516,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 @@ -569,13 +560,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(),
);
}
29 changes: 13 additions & 16 deletions crates/hotshot/src/tasks/task_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use hotshot_types::{
consensus::OuterConsensus,
traits::{
consensus_api::ConsensusApi,
election::Membership,
node_implementation::{ConsensusTime, NodeImplementation, NodeType},
},
};
Expand Down Expand Up @@ -51,7 +52,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 +72,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 +94,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 +124,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 +140,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 +166,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.eject().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
num_timeouts_tracked: 0,
Expand Down Expand Up @@ -194,7 +194,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.eject().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 +235,8 @@ 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(),
quorum_membership: handle.hotshot.memberships.eject().into(),
da_membership: handle.hotshot.memberships.eject().into(),
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
id: handle.hotshot.id,
storage: Arc::clone(&handle.storage),
Expand All @@ -259,8 +259,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.eject().into(),
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
storage: Arc::clone(&handle.storage),
Expand All @@ -286,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.eject().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 @@ -309,9 +308,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 859aa47

Please sign in to comment.