Skip to content

Commit

Permalink
Propose checks, id in prevote and precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
ancazamfir committed Oct 21, 2023
1 parent 7e260c6 commit f50a820
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 54 deletions.
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 @@ impl Round {
}

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#L37-L40

Added lines #L37 - L40 were not covered by tests
}
}

pub fn increment(&self) -> Round {
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)]
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
46 changes: 42 additions & 4 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use malachite_common::{Height, Proposal, Round, Timeout, TimeoutStep, ValidatorSet, Vote};
use malachite_common::{
Height, Proposal, Round, Timeout, TimeoutStep, ValidatorSet, Vote, VoteType,
};
use malachite_round::events::Event as RoundEvent;
use malachite_round::message::Message as RoundMessage;
use malachite_round::state::State as RoundState;
use malachite_vote::count::Threshold;
use malachite_vote::keeper::VoteKeeper;

#[derive(Clone, Debug)]
pub struct Executor {
height: Height,
validator_set: ValidatorSet,

round: Round,
votes: VoteKeeper,
round_states: BTreeMap<Round, RoundState>,
}
Expand All @@ -29,6 +33,7 @@ impl Executor {
Self {
height,
validator_set,
round: Round::INITIAL,

Check warning on line 36 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L36

Added line #L36 was not covered by tests
votes,
round_states: BTreeMap::new(),
}
Expand Down Expand Up @@ -73,9 +78,42 @@ impl Executor {
fn apply_proposal(&mut self, proposal: Proposal) -> Option<RoundMessage> {
// TODO: Check for invalid proposal
let round = proposal.round;
let event = RoundEvent::Proposal(proposal);
let event = RoundEvent::Proposal(proposal.clone());
let round_state = self.round_states.get(&self.round).unwrap();

Check warning on line 82 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L81-L82

Added lines #L81 - L82 were not covered by tests

self.apply_event(round, event)
if round_state.proposal.is_some() {
return None;
}

if round_state.height != proposal.height || proposal.round != self.round {
return None;
}

if !proposal.pol_round.is_valid()
|| proposal.pol_round.is_defined() && proposal.pol_round >= round_state.round

Check warning on line 93 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L84-L93

Added lines #L84 - L93 were not covered by tests
{
return None;
}

Check warning on line 96 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L95-L96

Added lines #L95 - L96 were not covered by tests

// TODO verify proposal signature (make some of these checks part of message validation)

match proposal.pol_round {

Check warning on line 100 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L100

Added line #L100 was not covered by tests
Round::None => {
// Is it possible to get +2/3 prevotes before the proposal?
// Do we wait for our own prevote to check the threshold?
self.apply_event(round, event)

Check warning on line 104 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L104

Added line #L104 was not covered by tests
}
Round::Some(_)
if self.votes.check_threshold(
&proposal.pol_round,
VoteType::Prevote,
Threshold::Value(Arc::from(proposal.value.id())),
) =>
{
self.apply_event(round, event)

Check warning on line 113 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L107-L113

Added lines #L107 - L113 were not covered by tests
}
_ => None,

Check warning on line 115 in Code/consensus/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

Code/consensus/src/executor.rs#L115

Added line #L115 was not covered by tests
}
}

fn apply_vote(&mut self, vote: Vote) -> Option<RoundMessage> {
Expand Down
6 changes: 3 additions & 3 deletions Code/round/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use malachite_common::Proposal;

use crate::Value;
use crate::{Value, ValueId};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
Expand All @@ -10,9 +10,9 @@ pub enum Event {
ProposalInvalid, // Receive an invalid proposal.
PolkaAny, // Receive +2/3 prevotes for anything.
PolkaNil, // Receive +2/3 prevotes for nil.
PolkaValue(Value), // Receive +2/3 prevotes for Value.
PolkaValue(ValueId), // Receive +2/3 prevotes for Value.
PrecommitAny, // Receive +2/3 precommits for anything.
PrecommitValue(Value), // Receive +2/3 precommits for Value.
PrecommitValue(ValueId), // Receive +2/3 precommits for Value.
RoundSkip, // Receive +1/3 votes from a higher round.
TimeoutPropose, // Timeout waiting for proposal.
TimeoutPrevote, // Timeout waiting for prevotes.
Expand Down
15 changes: 8 additions & 7 deletions Code/round/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use malachite_common::Address;
use malachite_common::{Address, Height};

use crate::{state::RoundValue, Proposal, Round, Timeout, TimeoutStep, Value, Vote};
use crate::{state::RoundValue, Proposal, Round, Timeout, TimeoutStep, Value, ValueId, Vote};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message {
Expand All @@ -12,20 +12,21 @@ pub enum Message {
}

impl Message {
pub fn proposal(round: Round, value: Value, pol_round: Round) -> Message {
pub fn proposal(height: Height, round: Round, value: Value, pol_round: Round) -> Message {
Message::Proposal(Proposal {
height,
round,
value,
pol_round,
})
}

pub fn prevote(round: Round, value: Option<Value>, address: Address) -> Message {
Message::Vote(Vote::new_prevote(round, value, address))
pub fn prevote(round: Round, value_id: Option<ValueId>, address: Address) -> Message {
Message::Vote(Vote::new_prevote(round, value_id, address))
}

pub fn precommit(round: Round, value: Option<Value>, address: Address) -> Message {
Message::Vote(Vote::new_precommit(round, value, address))
pub fn precommit(round: Round, value_id: Option<ValueId>, address: Address) -> Message {
Message::Vote(Vote::new_precommit(round, value_id, address))

Check warning on line 29 in Code/round/src/message.rs

View check run for this annotation

Codecov / codecov/patch

Code/round/src/message.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
}

pub fn timeout(round: Round, step: TimeoutStep) -> Message {
Expand Down
3 changes: 3 additions & 0 deletions Code/round/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::events::Event;
use crate::state_machine::Transition;
use crate::{Height, Round, Value};
use malachite_common::Proposal;

/// A value and its associated round
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -31,6 +32,7 @@ pub struct State {
pub height: Height,
pub round: Round,
pub step: Step,
pub proposal: Option<Proposal>,
pub locked: Option<RoundValue>,
pub valid: Option<RoundValue>,
}
Expand All @@ -41,6 +43,7 @@ impl State {
height,
round: Round::INITIAL,
step: Step::NewRound,
proposal: None,
locked: None,
valid: None,
}
Expand Down
Loading

0 comments on commit f50a820

Please sign in to comment.