Skip to content

Commit

Permalink
Split cert impls by marker
Browse files Browse the repository at this point in the history
  • Loading branch information
bfish713 authored and tbro committed Nov 15, 2024
1 parent 15763be commit 3dd314f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
71 changes: 67 additions & 4 deletions crates/types/src/simple_certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use crate::{
data::serialize_signature2,
message::UpgradeLock,
simple_vote::{
DaData, QuorumData, TimeoutData, UpgradeProposalData, VersionedVoteData,
ViewSyncCommitData, ViewSyncFinalizeData, ViewSyncPreCommitData, Voteable,
DaData, QuorumData, QuorumMaker, TimeoutData, UpgradeProposalData,
VersionedVoteData, ViewSyncCommitData, ViewSyncFinalizeData, ViewSyncPreCommitData,
Voteable,
},
traits::{
election::Membership,
Expand Down Expand Up @@ -124,8 +125,70 @@ impl<TYPES: NodeType, VOTEABLE: Voteable + Committable, THRESHOLD: Threshold<TYP
}
}

impl<TYPES: NodeType, VOTEABLE: Voteable + 'static, THRESHOLD: Threshold<TYPES>> Certificate<TYPES>
for SimpleCertificate<TYPES, VOTEABLE, THRESHOLD>
impl<TYPES: NodeType, THRESHOLD: Threshold<TYPES>> Certificate<TYPES, DaData>
for SimpleCertificate<TYPES, DaData, THRESHOLD>
{
type Voteable = DaData;
type Threshold = THRESHOLD;

fn create_signed_certificate<V: Versions>(
vote_commitment: Commitment<VersionedVoteData<TYPES, DaData, V>>,
data: Self::Voteable,
sig: <TYPES::SignatureKey as SignatureKey>::QcType,
view: TYPES::View,
) -> Self {
let vote_commitment_bytes: [u8; 32] = vote_commitment.into();

SimpleCertificate {
data,
vote_commitment: Commitment::from_raw(vote_commitment_bytes),
view_number: view,
signatures: Some(sig),
_pd: PhantomData,
}
}
async fn is_valid_cert<MEMBERSHIP: Membership<TYPES>, V: Versions>(
&self,
membership: &MEMBERSHIP,
epoch: TYPES::Epoch,
upgrade_lock: &UpgradeLock<TYPES, V>,
) -> bool {
if self.view_number == TYPES::View::genesis() {
return true;
}
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::public_parameter(
membership.stake_table(epoch),
U256::from(Self::threshold(membership)),
);
let Ok(commit) = self.data_commitment(upgrade_lock).await else {
return false;
};
<TYPES::SignatureKey as SignatureKey>::check(
&real_qc_pp,
commit.as_ref(),
self.signatures.as_ref().unwrap(),
)
}
fn threshold<MEMBERSHIP: Membership<TYPES>>(membership: &MEMBERSHIP) -> u64 {
THRESHOLD::threshold(membership)
}
fn data(&self) -> &Self::Voteable {
&self.data
}
async fn data_commitment<V: Versions>(
&self,
upgrade_lock: &UpgradeLock<TYPES, V>,
) -> Result<Commitment<VersionedVoteData<TYPES, DaData, V>>> {
Ok(
VersionedVoteData::new(self.data.clone(), self.view_number, upgrade_lock)
.await?
.commit(),
)
}
}

impl<TYPES: NodeType, VOTEABLE: Voteable + 'static + QuorumMaker, THRESHOLD: Threshold<TYPES>>
Certificate<TYPES, VOTEABLE> for SimpleCertificate<TYPES, VOTEABLE, THRESHOLD>
{
type Voteable = VOTEABLE;
type Threshold = THRESHOLD;
Expand Down
26 changes: 12 additions & 14 deletions crates/types/src/simple_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use crate::{
vote::{HasViewNumber, Vote},
};

/// Marker that data should use the quorum cert type
pub(crate) trait QuorumMaker {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash, Eq)]
/// Data used for a yes vote.
#[serde(bound(deserialize = ""))]
Expand All @@ -43,12 +46,7 @@ pub struct TimeoutData<TYPES: NodeType> {
/// View the timeout is for
pub view: TYPES::View,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash, Eq)]
/// Data used for a VID vote.
pub struct VidData {
/// Commitment to the block payload the VID vote is on.
pub payload_commit: VidCommitment,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash, Eq)]
/// Data used for a Pre Commit vote.
pub struct ViewSyncPreCommitData<TYPES: NodeType> {
Expand Down Expand Up @@ -112,6 +110,14 @@ mod sealed {
impl<C: Committable> Sealed for C {}
}


impl<T: NodeType> QuorumMaker for QuorumData<T> {}
impl<T: NodeType> QuorumMaker for TimeoutData<T> {}
impl<T: NodeType> QuorumMaker for ViewSyncPreCommitData<T> {}
impl<T: NodeType> QuorumMaker for ViewSyncCommitData<T> {}
impl<T: NodeType> QuorumMaker for ViewSyncFinalizeData<T> {}
impl<T: NodeType + DeserializeOwned> QuorumMaker for UpgradeProposalData<T> {}

/// A simple yes vote over some votable type.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash, Eq)]
pub struct SimpleVote<TYPES: NodeType, DATA: Voteable> {
Expand Down Expand Up @@ -273,14 +279,6 @@ impl Committable for DaData {
}
}

impl Committable for VidData {
fn commit(&self) -> Commitment<Self> {
committable::RawCommitmentBuilder::new("VID data")
.var_size_bytes(self.payload_commit.as_ref())
.finalize()
}
}

impl<TYPES: NodeType> Committable for UpgradeProposalData<TYPES> {
fn commit(&self) -> Commitment<Self> {
let builder = committable::RawCommitmentBuilder::new("Upgrade data");
Expand Down
6 changes: 3 additions & 3 deletions crates/types/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The certificate formed from the collection of signatures a committee.
The committee is defined by the `Membership` associated type.
The votes all must be over the `Commitment` associated type.
*/
pub trait Certificate<TYPES: NodeType>: HasViewNumber<TYPES> {
pub trait Certificate<TYPES: NodeType, T>: HasViewNumber<TYPES> {
/// The data commitment this certificate certifies.
type Voteable: Voteable;

Expand Down Expand Up @@ -117,7 +117,7 @@ type SignersMap<COMMITMENT, KEY> = HashMap<
pub struct VoteAccumulator<
TYPES: NodeType,
VOTE: Vote<TYPES>,
CERT: Certificate<TYPES, Voteable = VOTE::Commitment>,
CERT: Certificate<TYPES, VOTE::Commitment, Voteable = VOTE::Commitment>,
V: Versions,
> {
/// Map of all signatures accumulated so far
Expand All @@ -141,7 +141,7 @@ pub struct VoteAccumulator<
impl<
TYPES: NodeType,
VOTE: Vote<TYPES>,
CERT: Certificate<TYPES, Voteable = VOTE::Commitment>,
CERT: Certificate<TYPES, VOTE::Commitment, Voteable = VOTE::Commitment>,
V: Versions,
> VoteAccumulator<TYPES, VOTE, CERT, V>
{
Expand Down

0 comments on commit 3dd314f

Please sign in to comment.