Skip to content

Commit

Permalink
chore: Remove Committable from VoteData (#1777)
Browse files Browse the repository at this point in the history
* add get_commit method to VoteData

* remove Committable from VoteData, replace commit() -> get_commit()

* amalgamate impls of VoteData

* lint
  • Loading branch information
ggutoski authored Sep 19, 2023
1 parent fe199a2 commit 3646927
Showing 1 changed file with 29 additions and 56 deletions.
85 changes: 29 additions & 56 deletions crates/types/src/traits/election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,50 +91,21 @@ pub enum VoteData<COMMITTABLE: Committable + Serialize + Clone> {
ViewSyncFinalize(Commitment<COMMITTABLE>),
}

/// Make different types of `VoteData` committable
impl<COMMITTABLE: Committable + Serialize + Clone> Committable for VoteData<COMMITTABLE> {
fn commit(&self) -> Commitment<Self> {
impl<COMMITTABLE> VoteData<COMMITTABLE>
where
COMMITTABLE: Committable + Serialize + Clone,
{
/// Return the underlying commitment.
#[must_use]
pub fn get_commit(&self) -> Commitment<COMMITTABLE> {
#[allow(clippy::enum_glob_use)]
use VoteData::*;
match self {
VoteData::DA(block_commitment) => {
commit::RawCommitmentBuilder::new("DA BlockPayload Commit")
.field("block_commitment", *block_commitment)
.finalize()
}
VoteData::Yes(leaf_commitment) => commit::RawCommitmentBuilder::new("Yes Vote Commit")
.field("leaf_commitment", *leaf_commitment)
.finalize(),
VoteData::No(leaf_commitment) => commit::RawCommitmentBuilder::new("No Vote Commit")
.field("leaf_commitment", *leaf_commitment)
.finalize(),
VoteData::Timeout(view_number_commitment) => {
commit::RawCommitmentBuilder::new("Timeout View Number Commit")
.field("view_number_commitment", *view_number_commitment)
.finalize()
}
VoteData::ViewSyncPreCommit(commitment) => {
commit::RawCommitmentBuilder::new("ViewSyncPreCommit")
.field("commitment", *commitment)
.finalize()
}
VoteData::ViewSyncCommit(commitment) => {
commit::RawCommitmentBuilder::new("ViewSyncCommit")
.field("commitment", *commitment)
.finalize()
}
VoteData::ViewSyncFinalize(commitment) => {
commit::RawCommitmentBuilder::new("ViewSyncFinalize")
.field("commitment", *commitment)
.finalize()
}
DA(c) | Yes(c) | No(c) | Timeout(c) | ViewSyncPreCommit(c) | ViewSyncCommit(c)
| ViewSyncFinalize(c) => *c,
}
}

fn tag() -> String {
("VOTE_DATA_COMMIT").to_string()
}
}

impl<COMMITTABLE: Committable + Serialize + Clone> VoteData<COMMITTABLE> {
#[must_use]
/// Convert vote data into bytes.
///
Expand Down Expand Up @@ -367,23 +338,23 @@ pub trait ConsensusExchange<TYPES: NodeType, M: NetworkMsg>: Send + Sync {

match qc.signatures() {
AssembledSignature::DA(qc) => {
let real_commit = VoteData::DA(leaf_commitment).commit();
let real_commit = VoteData::DA(leaf_commitment).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().success_threshold().get()),
);
<TYPES::SignatureKey as SignatureKey>::check(&real_qc_pp, real_commit.as_ref(), &qc)
}
AssembledSignature::Yes(qc) => {
let real_commit = VoteData::Yes(leaf_commitment).commit();
let real_commit = VoteData::Yes(leaf_commitment).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().success_threshold().get()),
);
<TYPES::SignatureKey as SignatureKey>::check(&real_qc_pp, real_commit.as_ref(), &qc)
}
AssembledSignature::No(qc) => {
let real_commit = VoteData::No(leaf_commitment).commit();
let real_commit = VoteData::No(leaf_commitment).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().success_threshold().get()),
Expand Down Expand Up @@ -411,7 +382,7 @@ pub trait ConsensusExchange<TYPES: NodeType, M: NetworkMsg>: Send + Sync {
let mut is_valid_vote_token = false;
let mut is_valid_signature = false;
if let Some(key) = <TYPES::SignatureKey as SignatureKey>::from_bytes(encoded_key) {
is_valid_signature = key.validate(encoded_signature, data.commit().as_ref());
is_valid_signature = key.validate(encoded_signature, data.get_commit().as_ref());
let valid_vote_token = self.membership().validate_vote_token(key, vote_token);
is_valid_vote_token = match valid_vote_token {
Err(_) => {
Expand All @@ -433,7 +404,7 @@ pub trait ConsensusExchange<TYPES: NodeType, M: NetworkMsg>: Send + Sync {
data: &VoteData<Self::Commitment>,
vote_token: &Checked<TYPES::VoteTokenType>,
) -> bool {
let is_valid_signature = key.validate(encoded_signature, data.commit().as_ref());
let is_valid_signature = key.validate(encoded_signature, data.get_commit().as_ref());
let valid_vote_token = self
.membership()
.validate_vote_token(key.clone(), vote_token.clone());
Expand Down Expand Up @@ -643,7 +614,7 @@ impl<
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::<TYPES::BlockType>::DA(block_commitment)
.commit()
.get_commit()
.as_ref(),
);
(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -688,7 +659,7 @@ impl<
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::<TYPES::BlockType>::DA(block_commitment)
.commit()
.get_commit()
.as_ref(),
);
(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -931,7 +902,7 @@ impl<
) -> (EncodedPublicKey, EncodedSignature) {
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::<LEAF>::Yes(leaf_commitment).commit().as_ref(),
VoteData::<LEAF>::Yes(leaf_commitment).get_commit().as_ref(),
);
(self.public_key.to_bytes(), signature)
}
Expand All @@ -947,7 +918,7 @@ impl<
) -> (EncodedPublicKey, EncodedSignature) {
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::<LEAF>::No(leaf_commitment).commit().as_ref(),
VoteData::<LEAF>::No(leaf_commitment).get_commit().as_ref(),
);
(self.public_key.to_bytes(), signature)
}
Expand All @@ -963,7 +934,7 @@ impl<
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::<TYPES::Time>::Timeout(view_number.commit())
.commit()
.get_commit()
.as_ref(),
);
(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -1212,7 +1183,9 @@ impl<
) -> (EncodedPublicKey, EncodedSignature) {
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::ViewSyncPreCommit(commitment).commit().as_ref(),
VoteData::ViewSyncPreCommit(commitment)
.get_commit()
.as_ref(),
);

(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -1253,7 +1226,7 @@ impl<
) -> (EncodedPublicKey, EncodedSignature) {
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::ViewSyncCommit(commitment).commit().as_ref(),
VoteData::ViewSyncCommit(commitment).get_commit().as_ref(),
);

(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -1294,7 +1267,7 @@ impl<
) -> (EncodedPublicKey, EncodedSignature) {
let signature = TYPES::SignatureKey::sign(
&self.private_key,
VoteData::ViewSyncFinalize(commitment).commit().as_ref(),
VoteData::ViewSyncFinalize(commitment).get_commit().as_ref(),
);

(self.public_key.to_bytes(), signature)
Expand Down Expand Up @@ -1325,7 +1298,7 @@ impl<
};
match certificate_internal.signatures {
AssembledSignature::ViewSyncPreCommit(raw_signatures) => {
let real_commit = VoteData::ViewSyncPreCommit(vote_data.commit()).commit();
let real_commit = VoteData::ViewSyncPreCommit(vote_data.commit()).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().failure_threshold().get()),
Expand All @@ -1337,7 +1310,7 @@ impl<
)
}
AssembledSignature::ViewSyncCommit(raw_signatures) => {
let real_commit = VoteData::ViewSyncCommit(vote_data.commit()).commit();
let real_commit = VoteData::ViewSyncCommit(vote_data.commit()).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().success_threshold().get()),
Expand All @@ -1349,7 +1322,7 @@ impl<
)
}
AssembledSignature::ViewSyncFinalize(raw_signatures) => {
let real_commit = VoteData::ViewSyncFinalize(vote_data.commit()).commit();
let real_commit = VoteData::ViewSyncFinalize(vote_data.commit()).get_commit();
let real_qc_pp = <TYPES::SignatureKey as SignatureKey>::get_public_parameter(
self.membership().get_committee_qc_stake_table(),
U256::from(self.membership().success_threshold().get()),
Expand Down

0 comments on commit 3646927

Please sign in to comment.