From 155fc0ccdea51a475800645dd0518f0f2824c73f Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Fri, 27 Oct 2023 17:24:44 +0200 Subject: [PATCH] wip: Move address from SignedVote into Vote --- Code/common/src/context.rs | 12 ++++++++++-- Code/common/src/signed_vote.rs | 15 +++++++-------- Code/common/src/vote.rs | 5 ++++- Code/consensus/src/executor.rs | 12 ++++-------- Code/round/src/message.rs | 8 ++++---- Code/test/src/consensus.rs | 8 ++++---- Code/test/src/vote.rs | 18 ++++++++++++------ 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/Code/common/src/context.rs b/Code/common/src/context.rs index a02625313..8469c6365 100644 --- a/Code/common/src/context.rs +++ b/Code/common/src/context.rs @@ -39,9 +39,17 @@ where /// Build a new prevote vote by the validator with the given address, /// for the value identified by the given value id, at the given round. - fn new_prevote(round: Round, value_id: Option>) -> Self::Vote; + fn new_prevote( + round: Round, + value_id: Option>, + address: Self::Address, + ) -> Self::Vote; /// Build a new precommit vote by the validator with the given address, /// for the value identified by the given value id, at the given round. - fn new_precommit(round: Round, value_id: Option>) -> Self::Vote; + fn new_precommit( + round: Round, + value_id: Option>, + address: Self::Address, + ) -> Self::Vote; } diff --git a/Code/common/src/signed_vote.rs b/Code/common/src/signed_vote.rs index 48bd6f0d4..29777599b 100644 --- a/Code/common/src/signed_vote.rs +++ b/Code/common/src/signed_vote.rs @@ -1,4 +1,4 @@ -use crate::{Context, Signature}; +use crate::{Context, Signature, Vote}; // TODO: Do we need to abstract over `SignedVote` as well? @@ -8,7 +8,6 @@ where Ctx: Context, { pub vote: Ctx::Vote, - pub address: Ctx::Address, pub signature: Signature, } @@ -16,11 +15,11 @@ impl SignedVote where Ctx: Context, { - pub fn new(vote: Ctx::Vote, address: Ctx::Address, signature: Signature) -> Self { - Self { - vote, - address, - signature, - } + pub fn new(vote: Ctx::Vote, signature: Signature) -> Self { + Self { vote, signature } + } + + pub fn validator_address(&self) -> &Ctx::Address { + self.vote.validator_address() } } diff --git a/Code/common/src/vote.rs b/Code/common/src/vote.rs index 069d04a18..a67190a26 100644 --- a/Code/common/src/vote.rs +++ b/Code/common/src/vote.rs @@ -18,7 +18,7 @@ pub enum VoteType { /// include information about the validator signing it. pub trait Vote where - Self: Clone + Debug + PartialEq + Eq, + Self: Clone + Debug + Eq, Ctx: Context, { /// The round for which the vote is for. @@ -32,4 +32,7 @@ where /// The type of vote. fn vote_type(&self) -> VoteType; + + /// Address of the validator who issued this vote + fn validator_address(&self) -> &Ctx::Address; } diff --git a/Code/consensus/src/executor.rs b/Code/consensus/src/executor.rs index e31b12a8b..5f263af02 100644 --- a/Code/consensus/src/executor.rs +++ b/Code/consensus/src/executor.rs @@ -102,14 +102,8 @@ where } RoundMessage::Vote(vote) => { - let address = self - .validator_set - .get_by_public_key(&self.key.expose_secret().verifying_key())? - .address() - .clone(); - let signature = Ctx::sign_vote(&vote, self.key.expose_secret()); - let signed_vote = SignedVote::new(vote, address, signature); + let signed_vote = SignedVote::new(vote, signature); Some(Message::Vote(signed_vote)) } @@ -194,7 +188,9 @@ where fn apply_vote(&mut self, signed_vote: SignedVote) -> Option> { // TODO: How to handle missing validator? - let validator = self.validator_set.get_by_address(&signed_vote.address)?; + let validator = self + .validator_set + .get_by_address(signed_vote.validator_address())?; if !Ctx::verify_signed_vote(&signed_vote, validator.public_key()) { // TODO: How to handle invalid votes? diff --git a/Code/round/src/message.rs b/Code/round/src/message.rs index 832fd05fe..dd94fe83b 100644 --- a/Code/round/src/message.rs +++ b/Code/round/src/message.rs @@ -42,12 +42,12 @@ where Message::Proposal(Ctx::new_proposal(height, round, value, pol_round)) } - pub fn prevote(round: Round, value_id: Option>) -> Self { - Message::Vote(Ctx::new_prevote(round, value_id)) + pub fn prevote(round: Round, value_id: Option>, address: Ctx::Address) -> Self { + Message::Vote(Ctx::new_prevote(round, value_id, address)) } - pub fn precommit(round: Round, value_id: Option>) -> Self { - Message::Vote(Ctx::new_precommit(round, value_id)) + pub fn precommit(round: Round, value_id: Option>, address: Ctx::Address) -> Self { + Message::Vote(Ctx::new_precommit(round, value_id, address)) } pub fn timeout(round: Round, step: TimeoutStep) -> Self { diff --git a/Code/test/src/consensus.rs b/Code/test/src/consensus.rs index aac73c2e2..3c4ef0537 100644 --- a/Code/test/src/consensus.rs +++ b/Code/test/src/consensus.rs @@ -40,11 +40,11 @@ impl Context for TestConsensus { Proposal::new(height, round, value, pol_round) } - fn new_prevote(round: Round, value_id: Option) -> Vote { - Vote::new_prevote(round, value_id) + fn new_prevote(round: Round, value_id: Option, address: Address) -> Vote { + Vote::new_prevote(round, value_id, address) } - fn new_precommit(round: Round, value_id: Option) -> Vote { - Vote::new_precommit(round, value_id) + fn new_precommit(round: Round, value_id: Option, address: Address) -> Vote { + Vote::new_precommit(round, value_id, address) } } diff --git a/Code/test/src/vote.rs b/Code/test/src/vote.rs index 5601b8a7e..98550b321 100644 --- a/Code/test/src/vote.rs +++ b/Code/test/src/vote.rs @@ -1,6 +1,7 @@ -use malachite_common::{Round, SignedVote, VoteType}; use signature::Signer; +use malachite_common::{Round, SignedVote, VoteType}; + use crate::{Address, PrivateKey, TestConsensus, ValueId}; /// A vote for a value in a round @@ -9,26 +10,29 @@ pub struct Vote { pub typ: VoteType, pub round: Round, pub value: Option, + pub validator_address: Address, } impl Vote { - pub fn new_prevote(round: Round, value: Option) -> Self { + pub fn new_prevote(round: Round, value: Option, validator_address: Address) -> Self { Self { typ: VoteType::Prevote, round, value, + validator_address, } } - pub fn new_precommit(round: Round, value: Option) -> Self { + pub fn new_precommit(round: Round, value: Option, address: Address) -> Self { Self { typ: VoteType::Precommit, round, value, + validator_address: address, } } - // TODO: Use the canonical vote + // TODO: Use a canonical vote pub fn to_bytes(&self) -> Vec { let vtpe = match self.typ { VoteType::Prevote => 0, @@ -47,12 +51,10 @@ impl Vote { } pub fn signed(self, private_key: &PrivateKey) -> SignedVote { - let address = Address::from_public_key(&private_key.public_key()); let signature = private_key.sign(&self.to_bytes()); SignedVote { vote: self, - address, signature, } } @@ -74,4 +76,8 @@ impl malachite_common::Vote for Vote { fn vote_type(&self) -> VoteType { self.typ } + + fn validator_address(&self) -> &Address { + &self.validator_address + } }