Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Add VidDisperseShare data type #12

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use crate::utils::{View, ViewInner};
use displaydoc::Display;

use crate::{
data::{Leaf, VidDisperse},
data::{Leaf, VidDisperseShare},
error::HotShotError,
message::Proposal,
simple_certificate::{DACertificate, QuorumCertificate, UpgradeCertificate},
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Consensus<TYPES: NodeType> {
/// In the future we will need a different struct similar to VidDisperse except
/// it stores only one share.
/// TODO <https://github.com/EspressoSystems/HotShot/issues/1732>
pub vid_shares: BTreeMap<TYPES::Time, Proposal<TYPES, VidDisperse<TYPES>>>,
pub vid_shares: BTreeMap<TYPES::Time, HashMap<TYPES::SignatureKey, Proposal<TYPES, VidDisperseShare<TYPES>>>>,

/// All the DA certs we've received for current and future views.
/// view -> DA cert
Expand Down
82 changes: 82 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ use std::{
hash::Hash,
sync::Arc,
};
use std::marker::PhantomData;
use tracing::error;
use crate::message::Proposal;

/// Type-safe wrapper around `u64` so we know the thing we're talking about is a view number.
#[derive(
Expand Down Expand Up @@ -171,6 +174,79 @@ impl<TYPES: NodeType> VidDisperse<TYPES> {
}
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
pub struct VidDisperseShare<TYPES: NodeType> {
/// The view number for which this VID data is intended
pub view_number: TYPES::Time,
/// Block payload commitment
pub payload_commitment: VidCommitment,
/// A storage node's key and its corresponding VID share
pub share: VidShare,
/// VID common data sent to all storage nodes
pub common: VidCommon,
/// a public key of the share recipient
pub recipient_key: TYPES::SignatureKey,
}

impl<TYPES: NodeType> VidDisperseShare<TYPES> {
/// Create a vector of `VidDisperseShare` from `VidDisperse`
pub fn from_vid_disperse(
vid_disperse: VidDisperse<TYPES>,
) -> Vec<Self> {
vid_disperse.shares.into_iter().map(|(recipient_key, share)| {
VidDisperseShare {
share,
recipient_key,
view_number: vid_disperse.view_number,
common: vid_disperse.common.clone(),
payload_commitment: vid_disperse.payload_commitment,
}
}).collect()
}

/// Consume `self` and return a `Proposal`
pub fn to_proposal(
self,
private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
) -> Option<Proposal<TYPES, Self>> {
let Ok(signature) = TYPES::SignatureKey::sign(
private_key,
self.payload_commitment.as_ref().as_ref(),
) else {
error!("VID: failed to sign dispersal share payload");
return None;
};
Some(Proposal {
signature,
_pd: PhantomData,
data: self
})
}

/// Create `VidDisperse` out of an iterator to `VidDisperseShare`s
pub fn to_vid_disperse<'a, I>(mut it: I) -> Option<VidDisperse<TYPES>>
where
I: Iterator<Item=&'a VidDisperseShare<TYPES>>
{
let first_vid_disperse_share = it.next()?.clone();
let mut share_map = BTreeMap::new();
share_map.insert(
first_vid_disperse_share.recipient_key,
first_vid_disperse_share.share);
let mut vid_disperse = VidDisperse {
view_number: first_vid_disperse_share.view_number,
payload_commitment: first_vid_disperse_share.payload_commitment,
common: first_vid_disperse_share.common,
shares: share_map,
};
let _ = it.map(|vid_disperse_share| vid_disperse
.shares
.insert(vid_disperse_share.recipient_key.clone(), vid_disperse_share.share.clone())
);
Some(vid_disperse)
}
}

/// Proposal to append a block.
#[derive(custom_debug::Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
#[serde(bound(deserialize = ""))]
Expand Down Expand Up @@ -210,6 +286,12 @@ impl<TYPES: NodeType> HasViewNumber<TYPES> for VidDisperse<TYPES> {
}
}

impl<TYPES: NodeType> HasViewNumber<TYPES> for VidDisperseShare<TYPES> {
fn get_view_number(&self) -> TYPES::Time {
self.view_number
}
}

impl<TYPES: NodeType> HasViewNumber<TYPES> for QuorumProposal<TYPES> {
fn get_view_number(&self) -> TYPES::Time {
self.view_number
Expand Down
6 changes: 3 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! `HotShot` nodes can send among themselves.

use crate::constants::Version;
use crate::data::{QuorumProposal, UpgradeProposal};
use crate::data::{QuorumProposal, UpgradeProposal, VidDisperseShare};
use crate::simple_certificate::{
DACertificate, ViewSyncCommitCertificate2, ViewSyncFinalizeCertificate2,
ViewSyncPreCommitCertificate2,
Expand All @@ -17,7 +17,7 @@ use crate::traits::network::ResponseMessage;
use crate::traits::signature_key::SignatureKey;
use crate::vote::HasViewNumber;
use crate::{
data::{DAProposal, VidDisperse},
data::DAProposal,
simple_vote::QuorumVote,
traits::{
network::{DataRequest, NetworkMsg, ViewMessage},
Expand Down Expand Up @@ -191,7 +191,7 @@ pub enum CommitteeConsensusMessage<TYPES: NodeType> {
///
/// Like [`DAProposal`]. Use `Msg` suffix to distinguish from [`VidDisperse`].
/// TODO this variant should not be a [`CommitteeConsensusMessage`] because <https://github.com/EspressoSystems/HotShot/issues/1696>
VidDisperseMsg(Proposal<TYPES, VidDisperse<TYPES>>),
VidDisperseMsg(Proposal<TYPES, VidDisperseShare<TYPES>>),
}

/// Messages for sequencing consensus.
Expand Down
Loading