Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into ag/debloat
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinI committed Oct 23, 2024
2 parents ebca3ef + e438036 commit ba06013
Show file tree
Hide file tree
Showing 44 changed files with 1,642 additions and 702 deletions.
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" ]
1 change: 1 addition & 0 deletions crates/hotshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ blake3.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
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::{
};
use primitive_types::U256;
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 primitive_types::U256;
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 primitive_types::U256;
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
1 change: 1 addition & 0 deletions crates/task-impls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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
10 changes: 5 additions & 5 deletions crates/task-impls/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::sync::Arc;

use anyhow::Result;
use async_broadcast::{Receiver, Sender};
use async_lock::RwLock;
#[cfg(async_executor_impl = "async-std")]
Expand All @@ -27,6 +26,7 @@ use hotshot_types::{
#[cfg(async_executor_impl = "tokio")]
use tokio::task::JoinHandle;
use tracing::instrument;
use utils::anytrace::Result;

use self::handlers::{
handle_quorum_vote_recv, handle_timeout, handle_timeout_vote_recv, handle_view_change,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> ConsensusTaskSt
&mut self,
event: Arc<HotShotEvent<TYPES>>,
sender: Sender<Arc<HotShotEvent<TYPES>>>,
) {
) -> Result<()> {
match event.as_ref() {
HotShotEvent::QuorumVoteRecv(ref vote) => {
if let Err(e) =
Expand Down Expand Up @@ -149,6 +149,8 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> ConsensusTaskSt
}
_ => {}
}

Ok(())
}
}

Expand All @@ -164,9 +166,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> TaskState
sender: &Sender<Arc<Self::Event>>,
_receiver: &Receiver<Arc<Self::Event>>,
) -> Result<()> {
self.handle(event, sender.clone()).await;

Ok(())
self.handle(event, sender.clone()).await
}

/// Joins all subtasks.
Expand Down
Loading

0 comments on commit ba06013

Please sign in to comment.