Skip to content

Commit

Permalink
Merge pull request #1993 from EspressoSystems/bf/data-vote-cert
Browse files Browse the repository at this point in the history
Integrate new DAVote and DACertificate types
  • Loading branch information
bfish713 authored Nov 7, 2023
2 parents 029ad7f + 3a687dc commit b80c877
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 127 deletions.
11 changes: 5 additions & 6 deletions crates/task-impls/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hotshot_types::{
data::{Leaf, LeafType, ProposalType, QuorumProposal},
event::{Event, EventType},
message::{GeneralConsensusMessage, Message, Proposal, SequencingMessage},
simple_certificate::QuorumCertificate2,
simple_certificate::{DACertificate2, QuorumCertificate2},
simple_vote::{QuorumData, QuorumVote},
traits::{
block_contents::BlockHeader,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub struct ConsensusTaskState<
pub output_event_stream: ChannelStream<Event<TYPES, I::Leaf>>,

/// All the DA certs we've received for current and future views.
pub da_certs: HashMap<TYPES::Time, DACertificate<TYPES>>,
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>>,
Expand Down Expand Up @@ -529,10 +529,10 @@ where
};

// Validate the DAC.
let message = if self.committee_exchange.is_valid_cert(cert) {
let message = if cert.is_valid_cert(self.committee_exchange.membership()) {
// Validate the block payload commitment for non-genesis DAC.
if !cert.is_genesis()
&& cert.leaf_commitment()
if !cert.is_genesis
&& cert.get_data().payload_commit
!= proposal.block_header.payload_commitment()
{
error!("Block payload commitment does not equal parent commitment");
Expand Down Expand Up @@ -1372,7 +1372,6 @@ where
justify_qc: consensus.high_qc.clone(),
timeout_certificate: timeout_certificate.or_else(|| None),
proposer_id: leaf.proposer_id,
dac: None,
};

let message = Proposal {
Expand Down
59 changes: 29 additions & 30 deletions crates/task-impls/src/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ use hotshot_task::{
task::{FilterEvent, HandleEvent, HotShotTaskCompleted, HotShotTaskTypes, TS},
task_impls::{HSTWithEvent, TaskBuilder},
};
use hotshot_types::vote::DAVoteAccumulator;
use hotshot_types::{
certificate::DACertificate,
consensus::{Consensus, View},
data::{DAProposal, Leaf, ProposalType},
message::{Message, Proposal, SequencingMessage},
simple_vote::{DAData, DAVote2},
traits::{
consensus_api::ConsensusApi,
election::{CommitteeExchangeType, ConsensusExchange, Membership, SignedCertificate},
election::{CommitteeExchangeType, ConsensusExchange, Membership},
network::{CommunicationChannel, ConsensusIntentEvent},
node_implementation::{CommitteeEx, NodeImplementation, NodeType},
signature_key::SignatureKey,
state::ConsensusTime,
BlockPayload,
},
utils::ViewInner,
vote2::HasViewNumber,
vote2::VoteAccumulator2,
};
use hotshot_types::{
simple_certificate::DACertificate2, traits::node_implementation::CommitteeMembership,
};

use snafu::Snafu;
Expand Down Expand Up @@ -92,13 +97,12 @@ pub struct DAVoteCollectionTaskState<
#[allow(clippy::type_complexity)]
/// Accumulates DA votes
pub accumulator: Either<
<DACertificate<TYPES> as SignedCertificate<
VoteAccumulator2<
TYPES,
TYPES::Time,
TYPES::VoteTokenType,
Commitment<TYPES::BlockPayload>,
>>::VoteAccumulator,
DACertificate<TYPES>,
DAVote2<TYPES, CommitteeMembership<TYPES, I>>,
DACertificate2<TYPES>,
>,
DACertificate2<TYPES>,
>,
/// the current view
pub cur_view: TYPES::Time,
Expand Down Expand Up @@ -138,8 +142,8 @@ where
{
match event {
HotShotEvent::DAVoteRecv(vote) => {
debug!("DA vote recv, collection task {:?}", vote.current_view);
// panic!("Vote handle received DA vote for view {}", *vote.current_view);
debug!("DA vote recv, collection task {:?}", vote.get_view_number());
// panic!("Vote handle received DA vote for view {}", *vote.get_view_number());

// For the case where we receive votes after we've made a certificate
if state.accumulator.is_right() {
Expand All @@ -149,11 +153,7 @@ where

let accumulator = state.accumulator.left().unwrap();

match state.committee_exchange.accumulate_vote(
accumulator,
&vote,
&vote.payload_commitment,
) {
match accumulator.accumulate(&vote, state.committee_exchange.membership()) {
Left(new_accumulator) => {
state.accumulator = either::Left(new_accumulator);
}
Expand Down Expand Up @@ -260,18 +260,21 @@ where
Ok(None) => {
debug!("We were not chosen for DA committee on {:?}", view);
}
Ok(Some(vote_token)) => {
Ok(Some(_vote_token)) => {
// Generate and send vote
let vote = self.committee_exchange.create_da_message(
payload_commitment,
let vote = DAVote2::create_signed_vote(
DAData {
payload_commit: payload_commitment,
},
view,
vote_token,
self.committee_exchange.public_key(),
self.committee_exchange.private_key(),
);

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

debug!("Sending vote to the DA leader {:?}", vote.current_view);
debug!("Sending vote to the DA leader {:?}", vote.get_view_number());
self.event_stream
.publish(HotShotEvent::DAVoteSend(vote))
.await;
Expand All @@ -294,9 +297,9 @@ where
}
}
HotShotEvent::DAVoteRecv(vote) => {
debug!("DA vote recv, Main Task {:?}", vote.current_view,);
debug!("DA vote recv, Main Task {:?}", vote.get_view_number(),);
// 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.committee_exchange.is_leader(view) {
error!("We are not the committee leader for view {} are we leader for next view? {}", *view, self.committee_exchange.is_leader(view + 1));
return None;
Expand All @@ -317,19 +320,15 @@ where
TYPES::Time::new(0)
};

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

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

if view > collection_view {
let state = DAVoteCollectionTaskState {
Expand Down
19 changes: 10 additions & 9 deletions crates/task-impls/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use crate::view_sync::ViewSyncPhase;
use commit::Commitment;
use either::Either;
use hotshot_types::{
certificate::{DACertificate, TimeoutCertificate, VIDCertificate},
certificate::{TimeoutCertificate, VIDCertificate},
data::{DAProposal, VidDisperse},
message::Proposal,
simple_certificate::QuorumCertificate2,
simple_vote::QuorumVote,
simple_certificate::{DACertificate2, QuorumCertificate2},
simple_vote::{DAVote2, QuorumVote},
traits::node_implementation::{
NodeImplementation, NodeType, QuorumMembership, QuorumProposalType, ViewSyncProposalType,
CommitteeMembership, NodeImplementation, NodeType, QuorumMembership, QuorumProposalType,
ViewSyncProposalType,
},
vote::{DAVote, TimeoutVote, VIDVote, ViewSyncVote},
vote::{TimeoutVote, VIDVote, ViewSyncVote},
};

/// All of the possible events that can be passed between Sequecning `HotShot` tasks
Expand All @@ -30,21 +31,21 @@ pub enum HotShotEvent<TYPES: NodeType, I: NodeImplementation<TYPES>> {
/// A DA proposal has been received from the network; handled by the DA task
DAProposalRecv(Proposal<DAProposal<TYPES>>, TYPES::SignatureKey),
/// A DA vote has been received by the network; handled by the DA task
DAVoteRecv(DAVote<TYPES>),
DAVoteRecv(DAVote2<TYPES, CommitteeMembership<TYPES, I>>),
/// A Data Availability Certificate (DAC) has been recieved by the network; handled by the consensus task
DACRecv(DACertificate<TYPES>),
DACRecv(DACertificate2<TYPES>),
/// Send a quorum proposal to the network; emitted by the leader in the consensus task
QuorumProposalSend(Proposal<QuorumProposalType<TYPES, I>>, TYPES::SignatureKey),
/// Send a quorum vote to the next leader; emitted by a replica in the consensus task after seeing a valid quorum proposal
QuorumVoteSend(QuorumVote<TYPES, I::Leaf, QuorumMembership<TYPES, I>>),
/// Send a DA proposal to the DA committee; emitted by the DA leader (which is the same node as the leader of view v + 1) in the DA task
DAProposalSend(Proposal<DAProposal<TYPES>>, TYPES::SignatureKey),
/// Send a DA vote to the DA leader; emitted by DA committee members in the DA task after seeing a valid DA proposal
DAVoteSend(DAVote<TYPES>),
DAVoteSend(DAVote2<TYPES, CommitteeMembership<TYPES, I>>),
/// The next leader has collected enough votes to form a QC; emitted by the next leader in the consensus task; an internal event only
QCFormed(Either<QuorumCertificate2<TYPES, I::Leaf>, TimeoutCertificate<TYPES>>),
/// The DA leader has collected enough votes to form a DAC; emitted by the DA leader in the DA task; sent to the entire network via the networking task
DACSend(DACertificate<TYPES>, TYPES::SignatureKey),
DACSend(DACertificate2<TYPES>, TYPES::SignatureKey),
/// The current view has changed; emitted by the replica in the consensus task or replica in the view sync task; received by almost all other tasks
ViewChange(TYPES::Time),
/// Timeout for the view sync protocol; emitted by a replica in the view sync task
Expand Down
4 changes: 2 additions & 2 deletions crates/task-impls/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ impl<
Some(membership.get_leader(vote.get_view())), // TODO who is VID leader? https://github.com/EspressoSystems/HotShot/issues/1699
),
HotShotEvent::DAVoteSend(vote) => (
vote.signature_key(),
vote.get_signing_key(),
MessageKind::<TYPES, I>::from_consensus_message(SequencingMessage(Right(
CommitteeConsensusMessage::DAVote(vote.clone()),
))),
TransmitType::Direct,
Some(membership.get_leader(vote.get_view())),
Some(membership.get_leader(vote.get_view_number())),
),
HotShotEvent::VidCertSend(certificate, sender) => (
sender,
Expand Down
1 change: 0 additions & 1 deletion crates/testing/src/task_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ async fn build_quorum_proposal_and_signature(
justify_qc: QuorumCertificate2::genesis(),
timeout_certificate: None,
proposer_id: leaf.proposer_id,
dac: None,
};

(proposal, signature)
Expand Down
15 changes: 9 additions & 6 deletions crates/testing/tests/da_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use hotshot_testing::{
use hotshot_types::{
block_impl::VIDTransaction,
data::{DAProposal, VidSchemeTrait, ViewNumber},
simple_vote::{DAData, DAVote2},
traits::{
consensus_api::ConsensusSharedApi, election::ConsensusExchange,
node_implementation::ExchangesType, state::ConsensusTime,
Expand Down Expand Up @@ -79,12 +80,14 @@ async fn test_da_task() {
);
output.insert(HotShotEvent::SendPayloadCommitment(block.commit()), 1);
output.insert(HotShotEvent::DAProposalSend(message.clone(), pub_key), 1);
let vote_token = committee_exchange
.make_vote_token(ViewNumber::new(2))
.unwrap()
.unwrap();
let da_vote =
committee_exchange.create_da_message(block.commit(), ViewNumber::new(2), vote_token);
let da_vote = DAVote2::create_signed_vote(
DAData {
payload_commit: block.commit(),
},
ViewNumber::new(2),
committee_exchange.public_key(),
committee_exchange.private_key(),
);
output.insert(HotShotEvent::DAVoteSend(da_vote), 1);

output.insert(HotShotEvent::DAProposalRecv(message, pub_key), 1);
Expand Down
6 changes: 1 addition & 5 deletions crates/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! `HotShot`'s version of a block, and proposals, messages upon which to reach the consensus.
use crate::{
certificate::{AssembledSignature, DACertificate, TimeoutCertificate, ViewSyncCertificate},
certificate::{AssembledSignature, TimeoutCertificate, ViewSyncCertificate},
simple_certificate::QuorumCertificate2,
traits::{
block_contents::BlockHeader,
Expand Down Expand Up @@ -180,10 +180,6 @@ pub struct QuorumProposal<TYPES: NodeType, LEAF: LeafType<NodeType = TYPES>> {

/// the propser id
pub proposer_id: EncodedPublicKey,

/// Data availibity certificate
// TODO We should be able to remove this
pub dac: Option<DACertificate<TYPES>>,
}

impl<TYPES: NodeType> ProposalType for DAProposal<TYPES> {
Expand Down
Loading

0 comments on commit b80c877

Please sign in to comment.