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

Make Membership::leader() return a Result<_> #3738

Merged
merged 23 commits into from
Oct 23, 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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"crates/types",
"crates/builder-api",
"crates/fakeapi",
"crates/utils",
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed-wildcard-imports = [ "utils", "hotshot_task_impls", "hotshot_types" ]
16 changes: 8 additions & 8 deletions crates/example-types/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

use crate::{
auction_results_provider_types::{TestAuctionResult, TestAuctionResultsProvider},
block_types::{TestBlockHeader, TestBlockPayload, TestTransaction},
state_types::{TestInstanceState, TestValidatedState},
storage_types::TestStorage,
};
use hotshot::traits::{
election::{
randomized_committee::RandomizedCommittee, static_committee::StaticCommittee,
Expand All @@ -18,15 +12,21 @@ use hotshot::traits::{
implementations::{CombinedNetworks, Libp2pNetwork, MemoryNetwork, PushCdnNetwork},
NodeImplementation,
};
use hotshot_types::data::EpochNumber;
use hotshot_types::{
data::ViewNumber,
data::{EpochNumber, ViewNumber},
signature_key::{BLSPubKey, BuilderKey},
traits::node_implementation::{NodeType, Versions},
};
use serde::{Deserialize, Serialize};
use vbs::version::StaticVersion;

use crate::{
auction_results_provider_types::{TestAuctionResult, TestAuctionResultsProvider},
block_types::{TestBlockHeader, TestBlockPayload, TestTransaction},
state_types::{TestInstanceState, TestValidatedState},
storage_types::TestStorage,
};

#[derive(
Copy,
Clone,
Expand Down
1 change: 1 addition & 0 deletions crates/hotshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sha2 = { workspace = true }
url = { workspace = true }
num_enum = "0.7"
parking_lot = "0.12"
utils = { path = "../utils" }

[target.'cfg(all(async_executor_impl = "tokio"))'.dependencies]
tokio = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ pub mod task_state;
use std::{fmt::Debug, sync::Arc, time::Duration};

use async_broadcast::{broadcast, RecvError};
use async_compatibility_layer::art::async_sleep;
use async_compatibility_layer::art::async_spawn;
use async_compatibility_layer::art::{async_sleep, async_spawn};
use async_lock::RwLock;
use async_trait::async_trait;
use futures::{
Expand Down
5 changes: 3 additions & 2 deletions crates/hotshot/src/traits/election/randomized_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use hotshot_types::{
PeerConfig,
};
use rand::{rngs::StdRng, Rng};
use utils::anytrace::Result;

#[derive(Clone, Debug, Eq, PartialEq, Hash)]

Expand Down Expand Up @@ -142,7 +143,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
) -> TYPES::SignatureKey {
) -> Result<TYPES::SignatureKey> {
let mut rng: StdRng = rand::SeedableRng::seed_from_u64(*view_number);

let randomized_view_number: u64 = rng.gen_range(0..=u64::MAX);
Expand All @@ -151,7 +152,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {

let res = self.eligible_leaders[index].clone();

TYPES::SignatureKey::public_key(&res)
Ok(TYPES::SignatureKey::public_key(&res))
}

/// Get the total number of nodes in the committee
Expand Down
5 changes: 3 additions & 2 deletions crates/hotshot/src/traits/election/static_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hotshot_types::{
},
PeerConfig,
};
use utils::anytrace::Result;

#[derive(Clone, Debug, Eq, PartialEq, Hash)]

Expand Down Expand Up @@ -140,11 +141,11 @@ impl<TYPES: NodeType> Membership<TYPES> for StaticCommittee<TYPES> {
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
) -> TYPES::SignatureKey {
) -> Result<TYPES::SignatureKey> {
#[allow(clippy::cast_possible_truncation)]
let index = *view_number as usize % self.eligible_leaders.len();
let res = self.eligible_leaders[index].clone();
TYPES::SignatureKey::public_key(&res)
Ok(TYPES::SignatureKey::public_key(&res))
}

/// Get the total number of nodes in the committee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hotshot_types::{
},
PeerConfig,
};
use utils::anytrace::Result;

#[derive(Clone, Debug, Eq, PartialEq, Hash)]

Expand Down Expand Up @@ -140,11 +141,12 @@ impl<TYPES: NodeType> Membership<TYPES> for StaticCommitteeLeaderForTwoViews<TYP
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
) -> TYPES::SignatureKey {
) -> Result<TYPES::SignatureKey> {
let index =
usize::try_from((*view_number / 2) % self.eligible_leaders.len() as u64).unwrap();
let res = self.eligible_leaders[index].clone();
TYPES::SignatureKey::public_key(&res)

Ok(TYPES::SignatureKey::public_key(&res))
}

/// Get the total number of nodes in the committee
Expand Down
10 changes: 9 additions & 1 deletion crates/hotshot/src/traits/networking/libp2p_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,15 @@ impl<K: SignatureKey + 'static> ConnectedNetwork<K> for Libp2pNetwork<K> {
{
let future_view = <TYPES as NodeType>::View::new(view) + LOOK_AHEAD;
let epoch = <TYPES as NodeType>::Epoch::new(epoch);
let future_leader = membership.leader(future_view, epoch);
let future_leader = match membership.leader(future_view, epoch) {
Ok(l) => l,
Err(e) => {
return tracing::info!(
"Failed to calculate leader for view {:?}: {e}",
future_view
);
}
};

let _ = self
.queue_node_lookup(ViewNumber::new(*future_view), future_leader)
Expand Down
8 changes: 6 additions & 2 deletions crates/hotshot/src/types/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::sync::Arc;

use anyhow::{anyhow, Ok, Result};
use anyhow::{anyhow, Context, Ok, Result};
use async_broadcast::{InactiveReceiver, Receiver, Sender};
use async_lock::RwLock;
use committable::{Commitment, Committable};
Expand Down Expand Up @@ -315,16 +315,20 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES> + 'static, V: Versions>
}

/// Wrapper for `HotShotConsensusApi`'s `leader` function
///
/// # Errors
/// Returns an error if the leader cannot be calculated
#[allow(clippy::unused_async)] // async for API compatibility reasons
pub async fn leader(
&self,
view_number: TYPES::View,
epoch_number: TYPES::Epoch,
) -> TYPES::SignatureKey {
) -> Result<TYPES::SignatureKey> {
self.hotshot
.memberships
.quorum_membership
.leader(view_number, epoch_number)
.context("Failed to lookup leader")
}

// Below is for testing only:
Expand Down
12 changes: 10 additions & 2 deletions crates/libp2p-networking/src/network/behaviours/dht/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ impl<R: RecordStore, K: SignatureKey> RecordStore for ValidatedStore<R, K>
where
K: 'static,
{
type ProvidedIter<'a> = R::ProvidedIter<'a> where R: 'a, K: 'a;
type RecordsIter<'a> = R::RecordsIter<'a> where R: 'a, K: 'a;
type ProvidedIter<'a>
= R::ProvidedIter<'a>
where
R: 'a,
K: 'a;
type RecordsIter<'a>
= R::RecordsIter<'a>
where
R: 'a,
K: 'a;

// Delegate all `RecordStore` methods except `put` to the inner store
delegate! {
Expand Down
1 change: 1 addition & 0 deletions crates/task-impls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tagged-base64 = { workspace = true }
time = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }
utils = { path = "../utils" }
vbs = { workspace = true }
vec1 = { workspace = true }

Expand Down
31 changes: 16 additions & 15 deletions crates/task-impls/src/consensus/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::{sync::Arc, time::Duration};

use anyhow::{ensure, Context, Result};
use async_broadcast::Sender;
use async_compatibility_layer::art::{async_sleep, async_spawn};
use chrono::Utc;
Expand All @@ -19,7 +18,8 @@ use hotshot_types::{
},
vote::HasViewNumber,
};
use tracing::{debug, error, instrument};
use tracing::instrument;
use utils::anytrace::*;

use super::ConsensusTaskState;
use crate::{
Expand All @@ -44,9 +44,9 @@ pub(crate) async fn handle_quorum_vote_recv<
ensure!(
task_state
.quorum_membership
.leader(vote.view_number() + 1, task_state.cur_epoch)
.leader(vote.view_number() + 1, task_state.cur_epoch)?
== task_state.public_key,
format!(
info!(
"We are not the leader for view {:?}",
vote.view_number() + 1
)
Expand All @@ -63,7 +63,7 @@ pub(crate) async fn handle_quorum_vote_recv<
sender,
&task_state.upgrade_lock,
)
.await;
.await?;

Ok(())
}
Expand All @@ -83,9 +83,9 @@ pub(crate) async fn handle_timeout_vote_recv<
ensure!(
task_state
.timeout_membership
.leader(vote.view_number() + 1, task_state.cur_epoch)
.leader(vote.view_number() + 1, task_state.cur_epoch)?
== task_state.public_key,
format!(
info!(
"We are not the leader for view {:?}",
vote.view_number() + 1
)
Expand All @@ -102,7 +102,7 @@ pub(crate) async fn handle_timeout_vote_recv<
sender,
&task_state.upgrade_lock,
)
.await;
.await?;

Ok(())
}
Expand All @@ -124,7 +124,7 @@ pub(crate) async fn handle_view_change<
);

let old_view_number = task_state.cur_view;
debug!("Updating view from {old_view_number:?} to {new_view_number:?}");
tracing::debug!("Updating view from {old_view_number:?} to {new_view_number:?}");

// Move this node to the next view
task_state.cur_view = new_view_number;
Expand All @@ -138,7 +138,7 @@ pub(crate) async fn handle_view_change<
.clone();
if let Some(cert) = decided_upgrade_certificate_read {
if new_view_number == cert.data.new_version_first_view {
error!(
tracing::error!(
"Version upgraded based on a decided upgrade cert: {:?}",
cert
);
Expand Down Expand Up @@ -177,7 +177,7 @@ pub(crate) async fn handle_view_change<
let cur_view_time = Utc::now().timestamp();
if task_state
.quorum_membership
.leader(old_view_number, task_state.cur_epoch)
.leader(old_view_number, task_state.cur_epoch)?
== task_state.public_key
{
#[allow(clippy::cast_precision_loss)]
Expand Down Expand Up @@ -228,7 +228,7 @@ pub(crate) async fn handle_timeout<TYPES: NodeType, I: NodeImplementation<TYPES>
task_state
.timeout_membership
.has_stake(&task_state.public_key, task_state.cur_epoch),
format!("We were not chosen for the consensus committee for view {view_number:?}")
debug!("We were not chosen for the consensus committee for view {view_number:?}")
);

let vote = TimeoutVote::create_signed_vote(
Expand All @@ -239,7 +239,8 @@ pub(crate) async fn handle_timeout<TYPES: NodeType, I: NodeImplementation<TYPES>
&task_state.upgrade_lock,
)
.await
.context("Failed to sign TimeoutData")?;
.wrap()
.context(error!("Failed to sign TimeoutData"))?;

broadcast_event(Arc::new(HotShotEvent::TimeoutVoteSend(vote)), sender).await;
broadcast_event(
Expand All @@ -251,7 +252,7 @@ pub(crate) async fn handle_timeout<TYPES: NodeType, I: NodeImplementation<TYPES>
)
.await;

debug!(
tracing::debug!(
"We did not receive evidence for view {} in time, sending timeout vote for that view!",
*view_number
);
Expand All @@ -274,7 +275,7 @@ pub(crate) async fn handle_timeout<TYPES: NodeType, I: NodeImplementation<TYPES>
.add(1);
if task_state
.quorum_membership
.leader(view_number, task_state.cur_epoch)
.leader(view_number, task_state.cur_epoch)?
== task_state.public_key
{
task_state
Expand Down
Loading