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

src/consensus: misc vote validations

aggstam 4 лет назад
Родитель
Сommit
d7bceafba5
2 измененных файлов с 25 добавлено и 2 удалено
  1. 1 1
      src/consensus/metadata.rs
  2. 24 1
      src/consensus/state.rs

+ 1 - 1
src/consensus/metadata.rs

@@ -80,7 +80,7 @@ impl MetadataStore {
     }
 
     /// Insert metadata into the metadatastore.
-    /// The block hash for the madatad is used as the key, where value is the serialized metadata.
+    /// The block hash for the metadata is used as the key, where value is the serialized metadata.
     pub fn insert(&self, metadata: &Metadata, block: blake3::Hash) -> Result<()> {
         self.0.insert(block.as_bytes(), serialize(metadata))?;
         Ok(())

+ 24 - 1
src/consensus/state.rs

@@ -356,6 +356,20 @@ impl ValidatorState {
         let nodes_count = self.consensus.participants.len();
         self.zero_participants_check();
 
+        // Checking that the voter can actually vote.
+        match self.consensus.participants.get(&vote.id) {
+            Some(participant) => {
+                if self.current_epoch() <= participant.joined {
+                    debug!("Voter joined after current epoch. Voter: {:?}", vote.id);
+                    return Ok(false)
+                }
+            }
+            None => {
+                debug!("Voter is not a participant. Voter: {:?}", vote.id);
+                return Ok(false)
+            }
+        }
+
         let proposal = self.find_proposal(&vote.proposal).unwrap();
         if proposal == None {
             debug!("Received vote for unknown proposal.");
@@ -382,7 +396,16 @@ impl ValidatorState {
                 Some(p) => p.clone(),
                 None => Participant::new(vote.id, vote.sl),
             };
-            participant.voted = Some(vote.sl);
+
+            match participant.voted {
+                Some(voted) => {
+                    if vote.sl > voted {
+                        participant.voted = Some(vote.sl);
+                    }
+                }
+                None => participant.voted = Some(vote.sl),
+            }
+
             self.consensus.participants.insert(participant.id, participant);
 
             return Ok(true)