Skip to content

Commit

Permalink
Merge branch 'main' into hvanz/mbt-votekeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy authored Nov 17, 2023
2 parents fe2a629 + 8654e59 commit 3e7e0ef
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 64 deletions.
2 changes: 2 additions & 0 deletions Code/common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ where
/// Build a new prevote vote by the validator with the given address,
/// for the value identified by the given value id, at the given round.
fn new_prevote(
height: Self::Height,
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
Expand All @@ -48,6 +49,7 @@ where
/// Build a new precommit vote by the validator with the given address,
/// for the value identified by the given value id, at the given round.
fn new_precommit(
height: Self::Height,
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
Expand Down
2 changes: 1 addition & 1 deletion Code/common/src/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ use core::fmt::Debug;
pub trait Height
where
// TODO: Require Copy as well?
Self: Clone + Debug + PartialEq + Eq + PartialOrd + Ord,
Self: Default + Clone + Debug + PartialEq + Eq + PartialOrd + Ord,
{
}
3 changes: 3 additions & 0 deletions Code/common/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ where
Self: Clone + Debug + Eq,
Ctx: Context,
{
/// The height for which the vote is for.
fn height(&self) -> Ctx::Height;

/// The round for which the vote is for.
fn round(&self) -> Round;

Expand Down
6 changes: 4 additions & 2 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ where
};

assert!(self.round < round);
self.round_states
.insert(round, RoundState::default().new_round(round));
self.round_states.insert(
round,
RoundState::default().new_round(self.height.clone(), round),
);
self.round = round;

Ok(self.apply_event(round, event))
Expand Down
18 changes: 14 additions & 4 deletions Code/round/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ impl<Ctx: Context> Message<Ctx> {
Message::Proposal(Ctx::new_proposal(height, round, value, pol_round))
}

pub fn prevote(round: Round, value_id: Option<ValueId<Ctx>>, address: Ctx::Address) -> Self {
Message::Vote(Ctx::new_prevote(round, value_id, address))
pub fn prevote(
height: Ctx::Height,
round: Round,
value_id: Option<ValueId<Ctx>>,
address: Ctx::Address,
) -> Self {
Message::Vote(Ctx::new_prevote(height, round, value_id, address))
}

pub fn precommit(round: Round, value_id: Option<ValueId<Ctx>>, address: Ctx::Address) -> Self {
Message::Vote(Ctx::new_precommit(round, value_id, address))
pub fn precommit(
height: Ctx::Height,
round: Round,
value_id: Option<ValueId<Ctx>>,
address: Ctx::Address,
) -> Self {
Message::Vote(Ctx::new_precommit(height, round, value_id, address))
}

pub fn schedule_timeout(round: Round, step: TimeoutStep) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion Code/round/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct State<Ctx>
where
Ctx: Context,
{
pub height: Ctx::Height,
pub round: Round,
pub step: Step,
pub proposal: Option<Ctx::Proposal>,
Expand All @@ -47,6 +48,7 @@ where
{
pub fn new() -> Self {
Self {
height: Ctx::Height::default(),
round: Round::INITIAL,
step: Step::NewRound,
proposal: None,
Expand All @@ -55,8 +57,9 @@ where
}
}

pub fn new_round(self, round: Round) -> Self {
pub fn new_round(self, height: Ctx::Height, round: Round) -> Self {
Self {
height,
round,
step: Step::NewRound,
..self
Expand Down Expand Up @@ -105,6 +108,7 @@ where
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
Self {
height: self.height.clone(),
round: self.round,
step: self.step,
proposal: self.proposal.clone(),
Expand Down
16 changes: 11 additions & 5 deletions Code/round/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
None => Some(proposed), // not locked, prevote the value
};

let message = Message::prevote(state.round, value, address.clone());
let message = Message::prevote(state.height.clone(), state.round, value, address.clone());
Transition::to(state.with_step(Step::Prevote)).with_message(message)
}

Expand All @@ -184,7 +184,7 @@ pub fn prevote_nil<Ctx>(state: State<Ctx>, address: &Ctx::Address) -> Transition
where
Ctx: Context,
{
let message = Message::prevote(state.round, None, address.clone());
let message = Message::prevote(state.height.clone(), state.round, None, address.clone());
Transition::to(state.with_step(Step::Prevote)).with_message(message)
}

Expand All @@ -211,7 +211,12 @@ where
}

let value = proposal.value();
let message = Message::precommit(state.round, Some(value.id()), address.clone());
let message = Message::precommit(
state.height.clone(),
state.round,
Some(value.id()),
address.clone(),
);

let current_value = match state.proposal {
Some(ref proposal) => proposal.value().clone(),
Expand All @@ -238,7 +243,7 @@ pub fn precommit_nil<Ctx>(state: State<Ctx>, address: &Ctx::Address) -> Transiti
where
Ctx: Context,
{
let message = Message::precommit(state.round, None, address.clone());
let message = Message::precommit(state.height.clone(), state.round, None, address.clone());
Transition::to(state.with_step(Step::Precommit)).with_message(message)
}

Expand Down Expand Up @@ -326,7 +331,8 @@ pub fn round_skip<Ctx>(state: State<Ctx>, round: Round) -> Transition<Ctx>
where
Ctx: Context,
{
Transition::to(state.new_round(round)).with_message(Message::NewRound(round))
Transition::to(state.clone().new_round(state.height.clone(), round))
.with_message(Message::NewRound(round))
}

/// We received +2/3 precommits for a value - commit and decide that value!
Expand Down
18 changes: 14 additions & 4 deletions Code/test/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ impl Context for TestContext {
Proposal::new(height, round, value, pol_round)
}

fn new_prevote(round: Round, value_id: Option<ValueId>, address: Address) -> Vote {
Vote::new_prevote(round, value_id, address)
fn new_prevote(
height: Height,
round: Round,
value_id: Option<ValueId>,
address: Address,
) -> Vote {
Vote::new_prevote(height, round, value_id, address)
}

fn new_precommit(round: Round, value_id: Option<ValueId>, address: Address) -> Vote {
Vote::new_precommit(round, value_id, address)
fn new_precommit(
height: Height,
round: Round,
value_id: Option<ValueId>,
address: Address,
) -> Vote {
Vote::new_precommit(height, round, value_id, address)
}
}
23 changes: 20 additions & 3 deletions Code/test/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,43 @@ use signature::Signer;

use malachite_common::{Round, SignedVote, VoteType};

use crate::{Address, PrivateKey, TestContext, ValueId};
use crate::{Address, Height, PrivateKey, TestContext, ValueId};

/// A vote for a value in a round
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Vote {
pub typ: VoteType,
pub height: Height,
pub round: Round,
pub value: Option<ValueId>,
pub validator_address: Address,
}

impl Vote {
pub fn new_prevote(round: Round, value: Option<ValueId>, validator_address: Address) -> Self {
pub fn new_prevote(
height: Height,
round: Round,
value: Option<ValueId>,
validator_address: Address,
) -> Self {
Self {
typ: VoteType::Prevote,
height,
round,
value,
validator_address,
}
}

pub fn new_precommit(round: Round, value: Option<ValueId>, address: Address) -> Self {
pub fn new_precommit(
height: Height,
round: Round,
value: Option<ValueId>,
address: Address,
) -> Self {
Self {
typ: VoteType::Precommit,
height,
round,
value,
validator_address: address,
Expand Down Expand Up @@ -61,6 +74,10 @@ impl Vote {
}

impl malachite_common::Vote<TestContext> for Vote {
fn height(&self) -> Height {
self.height
}

fn round(&self) -> Round {
self.round
}
Expand Down
Loading

0 comments on commit 3e7e0ef

Please sign in to comment.