Просмотр исходного кода

consensus: removed public key from Vote, validation through Participant.public_key

aggstam 4 лет назад
Родитель
Сommit
54ede683a2
2 измененных файлов с 17 добавлено и 31 удалено
  1. 14 20
      src/consensus/state.rs
  2. 3 11
      src/consensus/vote.rs

+ 14 - 20
src/consensus/state.rs

@@ -437,13 +437,7 @@ impl ValidatorState {
         }
 
         let signed_hash = self.secret.sign(&serialize(&proposal_hash));
-        Ok(Some(Vote::new(
-            self.public,
-            signed_hash,
-            proposal_hash,
-            proposal.block.header.slot,
-            self.address,
-        )))
+        Ok(Some(Vote::new(signed_hash, proposal_hash, proposal.block.header.slot, self.address)))
     }
 
     /// Verify if the provided chain is notarized excluding the last block.
@@ -527,19 +521,6 @@ impl ValidatorState {
             None => return Ok((false, None)),
         }
 
-        let mut encoded_proposal = vec![];
-
-        if let Err(e) = vote.proposal.encode(&mut encoded_proposal) {
-            error!("consensus: Proposal encoding failed: {:?}", e);
-            return Ok((false, None))
-        };
-
-        let va = vote.address.to_string();
-        if !vote.public_key.verify(&encoded_proposal, &vote.vote) {
-            warn!("consensus: Voter ({}), signature couldn't be verified", va);
-            return Ok((false, None))
-        }
-
         // Node refreshes participants records
         self.refresh_participants()?;
         let node_count = self.consensus.participants.len();
@@ -548,11 +529,24 @@ impl ValidatorState {
         match self.consensus.participants.get(&vote.address) {
             Some(participant) => {
                 let mut participant = participant.clone();
+                let va = vote.address.to_string();
                 if current_slot <= participant.joined {
                     warn!("consensus: Voter ({}) joined after current slot.", va);
                     return Ok((false, None))
                 }
 
+                let mut encoded_proposal = vec![];
+
+                if let Err(e) = vote.proposal.encode(&mut encoded_proposal) {
+                    error!("consensus: Proposal encoding failed: {:?}", e);
+                    return Ok((false, None))
+                };
+
+                if !participant.public_key.verify(&encoded_proposal, &vote.vote) {
+                    warn!("consensus: Voter ({}), signature couldn't be verified", va);
+                    return Ok((false, None))
+                }
+
                 // Updating participant vote
                 match participant.voted {
                     Some(voted) => {

+ 3 - 11
src/consensus/vote.rs

@@ -1,7 +1,7 @@
 use std::io;
 
 use crate::{
-    crypto::{address::Address, keypair::PublicKey, schnorr::Signature},
+    crypto::{address::Address, schnorr::Signature},
     impl_vec, net,
     util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Result,
@@ -10,8 +10,6 @@ use crate::{
 /// This struct represents a `Vote` used by the Streamlet consensus
 #[derive(Debug, Clone, PartialEq, Eq, SerialDecodable, SerialEncodable)]
 pub struct Vote {
-    /// Node public key
-    pub public_key: PublicKey,
     /// Block signature
     pub vote: Signature,
     /// Block proposal hash to vote on
@@ -23,14 +21,8 @@ pub struct Vote {
 }
 
 impl Vote {
-    pub fn new(
-        public_key: PublicKey,
-        vote: Signature,
-        proposal: blake3::Hash,
-        slot: u64,
-        address: Address,
-    ) -> Self {
-        Self { public_key, vote, proposal, slot, address }
+    pub fn new(vote: Signature, proposal: blake3::Hash, slot: u64, address: Address) -> Self {
+        Self { vote, proposal, slot, address }
     }
 }