src/consensus: misc vote validations

This commit is contained in:
aggstam
2022-04-14 16:40:09 +03:00
parent 7a34ec3d45
commit d7bceafba5
2 changed files with 25 additions and 2 deletions

View File

@@ -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(())

View File

@@ -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)