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

Integrate VID Vote and Cert #2004

Merged
merged 4 commits into from
Nov 9, 2023
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
5 changes: 1 addition & 4 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ use hotshot_task::{
};
use hotshot_task_impls::{events::HotShotEvent, network::NetworkTaskKind};
use hotshot_types::{
certificate::{TimeoutCertificate, VIDCertificate},
data::VidDisperse,
simple_certificate::QuorumCertificate2,
certificate::TimeoutCertificate, data::VidDisperse, simple_certificate::QuorumCertificate2,
traits::node_implementation::TimeoutEx,
};

Expand Down Expand Up @@ -670,7 +668,6 @@ where
TYPES,
Message<TYPES, I>,
Proposal = VidDisperse<TYPES>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
Membership = MEMBERSHIP,
> + 'static,
Expand Down
10 changes: 3 additions & 7 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use hotshot_task_impls::{
};
use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
certificate::{TimeoutCertificate, VIDCertificate, ViewSyncCertificate},
certificate::{TimeoutCertificate, ViewSyncCertificate},
data::{Leaf, ProposalType, QuorumProposal},
event::Event,
message::{Message, Messages, SequencingMessage},
Expand Down Expand Up @@ -359,12 +359,8 @@ pub async fn add_vid_task<
handle: SystemContextHandle<TYPES, I>,
) -> TaskRunner
where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
// build the vid task
let c_api: HotShotConsensusApi<TYPES, I> = HotShotConsensusApi {
Expand Down
8 changes: 4 additions & 4 deletions crates/task-impls/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use hotshot_task::{
};
use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
certificate::{DACertificate, QuorumCertificate, TimeoutCertificate, VIDCertificate},
certificate::{DACertificate, QuorumCertificate, TimeoutCertificate},
consensus::{Consensus, View},
data::{Leaf, LeafType, ProposalType, QuorumProposal},
event::{Event, EventType},
message::{GeneralConsensusMessage, Message, Proposal, SequencingMessage},
simple_certificate::{DACertificate2, QuorumCertificate2},
simple_certificate::{DACertificate2, QuorumCertificate2, VIDCertificate2},
simple_vote::{QuorumData, QuorumVote},
traits::{
block_contents::BlockHeader,
Expand Down Expand Up @@ -132,7 +132,7 @@ pub struct ConsensusTaskState<
pub da_certs: HashMap<TYPES::Time, DACertificate2<TYPES>>,

/// All the VID certs we've received for current and future views.
pub vid_certs: HashMap<TYPES::Time, VIDCertificate<TYPES>>,
pub vid_certs: HashMap<TYPES::Time, VIDCertificate2<TYPES>>,

/// The most recent proposal we have, will correspond to the current view if Some()
/// Will be none if the view advanced through timeout/view_sync
Expand Down Expand Up @@ -1212,7 +1212,7 @@ where
HotShotEvent::VidCertRecv(cert) => {
debug!("VID cert received for view ! {}", *cert.view_number);

let view = cert.view_number;
let view = cert.get_view_number();
self.vid_certs.insert(view, cert);

// RM TODO: VOTING
Expand Down
18 changes: 9 additions & 9 deletions crates/task-impls/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use crate::view_sync::ViewSyncPhase;
use commit::Commitment;
use either::Either;
use hotshot_types::{
certificate::{TimeoutCertificate, VIDCertificate},
certificate::TimeoutCertificate,
data::{DAProposal, VidDisperse},
message::Proposal,
simple_certificate::{DACertificate2, QuorumCertificate2},
simple_vote::{DAVote2, QuorumVote},
simple_certificate::{DACertificate2, QuorumCertificate2, VIDCertificate2},
simple_vote::{DAVote2, QuorumVote, VIDVote2},
traits::node_implementation::{
CommitteeMembership, NodeImplementation, NodeType, QuorumMembership, QuorumProposalType,
ViewSyncProposalType,
VIDMembership, ViewSyncProposalType,
},
vote::{TimeoutVote, VIDVote, ViewSyncVote},
vote::{TimeoutVote, ViewSyncVote},
};

/// All of the possible events that can be passed between Sequecning `HotShot` tasks
Expand Down Expand Up @@ -86,17 +86,17 @@ pub enum HotShotEvent<TYPES: NodeType, I: NodeImplementation<TYPES>> {
/// Send a VID vote to the VID leader; emitted by VID storage nodes in the DA task after seeing a valid VID dispersal
///
/// Like [`DAVoteSend`]
VidVoteSend(VIDVote<TYPES>),
VidVoteSend(VIDVote2<TYPES, VIDMembership<TYPES, I>>),
/// A VID vote has been received by the network; handled by the DA task
///
/// Like [`DAVoteRecv`]
VidVoteRecv(VIDVote<TYPES>),
VidVoteRecv(VIDVote2<TYPES, VIDMembership<TYPES, I>>),
/// The VID leader has collected enough votes to form a VID cert; emitted by the VID leader in the DA task; sent to the entire network via the networking task
///
/// Like [`DACSend`]
VidCertSend(VIDCertificate<TYPES>, TYPES::SignatureKey),
VidCertSend(VIDCertificate2<TYPES>, TYPES::SignatureKey),
/// A VID cert has been recieved by the network; handled by the consensus task
///
/// Like [`DACRecv`]
VidCertRecv(VIDCertificate<TYPES>),
VidCertRecv(VIDCertificate2<TYPES>),
}
4 changes: 2 additions & 2 deletions crates/task-impls/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ impl<
None,
),
HotShotEvent::VidVoteSend(vote) => (
vote.signature_key(),
vote.get_signing_key(),
MessageKind::<TYPES, I>::from_consensus_message(SequencingMessage(Right(
CommitteeConsensusMessage::VidVote(vote.clone()),
))),
TransmitType::Direct,
Some(membership.get_leader(vote.get_view())), // TODO who is VID leader? https://github.com/EspressoSystems/HotShot/issues/1699
Some(membership.get_leader(vote.get_view_number())),
),
HotShotEvent::DAVoteSend(vote) => (
vote.get_signing_key(),
Expand Down
110 changes: 42 additions & 68 deletions crates/task-impls/src/vid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ use hotshot_task::{
task::{FilterEvent, HandleEvent, HotShotTaskCompleted, HotShotTaskTypes, TS},
task_impls::{HSTWithEvent, TaskBuilder},
};
use hotshot_types::traits::network::CommunicationChannel;
use hotshot_types::traits::network::ConsensusIntentEvent;
use hotshot_types::{
certificate::VIDCertificate, traits::election::SignedCertificate, vote::VIDVoteAccumulator,
};
use hotshot_types::traits::{network::ConsensusIntentEvent, node_implementation::VIDMembership};
use hotshot_types::{
consensus::{Consensus, View},
data::{Leaf, ProposalType},
message::{Message, SequencingMessage},
traits::{
consensus_api::ConsensusApi,
election::{ConsensusExchange, VIDExchangeType},
election::ConsensusExchange,
node_implementation::{NodeImplementation, NodeType, VIDEx},
signature_key::SignatureKey,
state::ConsensusTime,
},
utils::ViewInner,
};
use hotshot_types::{
simple_certificate::VIDCertificate2,
simple_vote::{VIDData, VIDVote2},
traits::network::CommunicationChannel,
vote2::{HasViewNumber, VoteAccumulator2},
};

use snafu::Snafu;
use std::marker::PhantomData;
Expand All @@ -46,12 +48,8 @@ pub struct VIDTaskState<
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
> where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
/// The state's api
pub api: A,
Expand Down Expand Up @@ -82,25 +80,16 @@ pub struct VIDVoteCollectionTaskState<
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>>,
> where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
/// the vid exchange
pub vid_exchange: Arc<VIDEx<TYPES, I>>,
#[allow(clippy::type_complexity)]
/// Accumulates VID votes
pub accumulator: Either<
<VIDCertificate<TYPES> as SignedCertificate<
TYPES,
TYPES::Time,
TYPES::VoteTokenType,
Commitment<TYPES::BlockPayload>,
>>::VoteAccumulator,
VIDCertificate<TYPES>,
VoteAccumulator2<TYPES, VIDVote2<TYPES, VIDMembership<TYPES, I>>, VIDCertificate2<TYPES>>,
VIDCertificate2<TYPES>,
>,
/// the current view
pub cur_view: TYPES::Time,
Expand All @@ -113,12 +102,8 @@ pub struct VIDVoteCollectionTaskState<
impl<TYPES: NodeType, I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>>> TS
for VIDVoteCollectionTaskState<TYPES, I>
where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
}

Expand All @@ -133,16 +118,15 @@ async fn vote_handle<TYPES, I>(
where
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>>,
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
match event {
HotShotEvent::VidVoteRecv(vote) => {
debug!("VID vote recv, collection task {:?}", vote.current_view);
debug!(
"VID vote recv, collection task {:?}",
vote.get_view_number()
);
// panic!("Vote handle received VID vote for view {}", *vote.current_view);

// For the case where we receive votes after we've made a certificate
Expand All @@ -152,10 +136,7 @@ where
}

let accumulator = state.accumulator.left().unwrap();
match state
.vid_exchange
.accumulate_vote(accumulator, &vote, &vote.payload_commitment)
{
match accumulator.accumulate(&vote, state.vid_exchange.membership()) {
Left(new_accumulator) => {
state.accumulator = either::Left(new_accumulator);
}
Expand Down Expand Up @@ -201,12 +182,8 @@ impl<
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
> VIDTaskState<TYPES, I, A>
where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
/// main task event handler
#[instrument(skip_all, fields(id = self.id, view = *self.cur_view), name = "VID Main Task", level = "error")]
Expand All @@ -222,7 +199,7 @@ where
// self.committee_exchange.public_key()
// );
// Check if we are the leader and the vote is from the sender.
let view = vote.current_view;
let view = vote.get_view_number();
if !self.vid_exchange.is_leader(view) {
error!(
"We are not the VID leader for view {} are we leader for next view? {}",
Expand All @@ -247,19 +224,14 @@ where
TYPES::Time::new(0)
};

let new_accumulator = VIDVoteAccumulator {
vid_vote_outcomes: HashMap::new(),
success_threshold: self.vid_exchange.success_threshold(),
let new_accumulator = VoteAccumulator2 {
vote_outcomes: HashMap::new(),
sig_lists: Vec::new(),
signers: bitvec![0; self.vid_exchange.total_nodes()],
phantom: PhantomData,
};

let accumulator = self.vid_exchange.accumulate_vote(
new_accumulator,
&vote,
&vote.clone().payload_commitment,
);
let accumulator = new_accumulator.accumulate(&vote, self.vid_exchange.membership());

if view > collection_view {
let state = VIDVoteCollectionTaskState {
Expand Down Expand Up @@ -336,18 +308,24 @@ where
Ok(None) => {
debug!("We were not chosen for VID quorum on {:?}", view);
}
Ok(Some(vote_token)) => {
Ok(Some(_vote_token)) => {
// Generate and send vote
let vote = self.vid_exchange.create_vid_message(
payload_commitment,
let vote = VIDVote2::create_signed_vote(
VIDData {
payload_commit: payload_commitment,
},
view,
vote_token,
self.vid_exchange.public_key(),
self.vid_exchange.private_key(),
);

// ED Don't think this is necessary?
// self.cur_view = view;

debug!("Sending vote to the VID leader {:?}", vote.current_view);
debug!(
"Sending vote to the VID leader {:?}",
vote.get_view_number()
);
self.event_stream
.publish(HotShotEvent::VidVoteSend(vote))
.await;
Expand Down Expand Up @@ -447,12 +425,8 @@ impl<
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
> TS for VIDTaskState<TYPES, I, A>
where
VIDEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
VIDEx<TYPES, I>:
ConsensusExchange<TYPES, Message<TYPES, I>, Commitment = Commitment<TYPES::BlockPayload>>,
{
}

Expand Down
15 changes: 9 additions & 6 deletions crates/testing/tests/vid_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use hotshot_testing::{
node_types::{MemoryImpl, TestTypes},
task_helpers::vid_init,
};
use hotshot_types::traits::election::VIDExchangeType;
use hotshot_types::{
block_impl::VIDTransaction,
data::{DAProposal, VidDisperse, VidSchemeTrait, ViewNumber},
Expand All @@ -14,6 +13,7 @@ use hotshot_types::{
node_implementation::ExchangesType, state::ConsensusTime,
},
};
use hotshot_types::{simple_vote::VIDVote2, traits::election::VIDExchangeType};
use std::collections::HashMap;

#[cfg_attr(
Expand Down Expand Up @@ -83,11 +83,14 @@ async fn test_vid_task() {
1,
);

let vote_token = vid_exchange
.make_vote_token(ViewNumber::new(2))
.unwrap()
.unwrap();
let vid_vote = vid_exchange.create_vid_message(block.commit(), ViewNumber::new(2), vote_token);
let vid_vote = VIDVote2::create_signed_vote(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So nice that the vote token + message creation are now combined into one function. 👍

hotshot_types::simple_vote::VIDData {
payload_commit: block.commit(),
},
ViewNumber::new(2),
vid_exchange.public_key(),
vid_exchange.private_key(),
);
output.insert(HotShotEvent::VidVoteSend(vid_vote), 1);

output.insert(HotShotEvent::VidDisperseRecv(vid_proposal, pub_key), 1);
Expand Down
Loading
Loading