Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose checks, id in prevote and precommit #7

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Code/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//! Common data types and abstractions

#![forbid(unsafe_code)]
#![deny(unused_crate_dependencies, trivial_casts, trivial_numeric_casts)]
#![warn(
// missing_docs,
broken_intra_doc_links,
private_intra_doc_links,
variant_size_differences
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]

mod height;
mod proposal;
mod round;
Expand Down
6 changes: 4 additions & 2 deletions Code/common/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::{Round, Value};
use crate::{Height, Round, Value};

/// A proposal for a value in a round
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Proposal {
pub height: Height,
pub round: Round,
pub value: Value,
pub pol_round: Round,
}

impl Proposal {
pub fn new(round: Round, value: Value, pol_round: Round) -> Self {
pub fn new(height: Height, round: Round, value: Value, pol_round: Round) -> Self {
Self {
height,
round,
value,
pol_round,
Expand Down
13 changes: 12 additions & 1 deletion Code/common/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@
}

pub fn is_defined(&self) -> bool {
matches!(self, Round::Some(_))
matches!(self, Round::Some(r) if *r >= 0)
}

pub fn is_nil(&self) -> bool {
matches!(self, Round::None)
}

pub fn is_valid(&self) -> bool {
match self {
Round::None => true,
Round::Some(r) => *r >= 0,

Check warning on line 40 in Code/common/src/round.rs

View check run for this annotation

Codecov / codecov/patch

Code/common/src/round.rs#L40

Added line #L40 was not covered by tests
}
}

pub fn increment(&self) -> Round {
Expand Down
16 changes: 16 additions & 0 deletions Code/common/src/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
pub struct PublicKey(Vec<u8>);

impl PublicKey {
pub const fn new(value: Vec<u8>) -> Self {
Self(value)
}

pub fn hash(&self) -> u64 {
// TODO
self.0.iter().fold(0, |acc, x| acc ^ *x as u64)
Expand Down Expand Up @@ -99,6 +103,10 @@ impl ValidatorSet {
self.validators.iter().find(|v| &v.address() == address)
}

pub fn get_by_public_key(&self, public_key: &PublicKey) -> Option<&Validator> {
self.validators.iter().find(|v| &v.public_key == public_key)
}

/// In place sort and deduplication of a list of validators
fn sort_validators(vals: &mut Vec<Validator>) {
use core::cmp::Reverse;
Expand All @@ -109,6 +117,14 @@ impl ValidatorSet {

vals.dedup();
}

pub fn get_proposer(&mut self) -> Validator {
// TODO: Proper implementation
assert!(!self.validators.is_empty());
let proposer = self.validators[0].clone();
self.validators.rotate_left(1);
proposer
}
}

#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions Code/common/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ impl Value {
pub fn as_u64(&self) -> u64 {
self.0
}

pub fn valid(&self) -> bool {
self.0 > 0
}

pub fn id(&self) -> ValueId {
ValueId(self.0)
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Copy)]
pub struct ValueId(u64);

impl ValueId {
pub fn new(id: u64) -> Self {
Self(id)
}
}
8 changes: 4 additions & 4 deletions Code/common/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Address, Round, Value};
use crate::{Address, Round, ValueId};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum VoteType {
Expand All @@ -11,12 +11,12 @@ pub enum VoteType {
pub struct Vote {
pub typ: VoteType,
pub round: Round,
pub value: Option<Value>,
pub value: Option<ValueId>,
pub address: Address,
}

impl Vote {
pub fn new_prevote(round: Round, value: Option<Value>, address: Address) -> Self {
pub fn new_prevote(round: Round, value: Option<ValueId>, address: Address) -> Self {
Self {
typ: VoteType::Prevote,
round,
Expand All @@ -25,7 +25,7 @@ impl Vote {
}
}

pub fn new_precommit(round: Round, value: Option<Value>, address: Address) -> Self {
pub fn new_precommit(round: Round, value: Option<ValueId>, address: Address) -> Self {
Self {
typ: VoteType::Precommit,
round,
Expand Down
Loading
Loading