Skip to content

Commit

Permalink
common: Move address from SignedVote to Vote (#36)
Browse files Browse the repository at this point in the history
* Move address from SignedVote into Vote

* Rename a few fields for better clarity

* Supply validator address to the state machine

* Move `Transition` struct into its own module

* Extract immutable data required to run the round state machine into a `RoundData` struct (#37)
  • Loading branch information
romac authored Oct 31, 2023
1 parent 98d2b63 commit e46a95b
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 182 deletions.
12 changes: 10 additions & 2 deletions Code/common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ 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(round: Round, value_id: Option<ValueId<Self>>) -> Self::Vote;
fn new_prevote(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;

/// 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(round: Round, value_id: Option<ValueId<Self>>) -> Self::Vote;
fn new_precommit(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;
}
15 changes: 7 additions & 8 deletions Code/common/src/signed_vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Context, Signature};
use crate::{Context, Signature, Vote};

// TODO: Do we need to abstract over `SignedVote` as well?

Expand All @@ -8,19 +8,18 @@ where
Ctx: Context,
{
pub vote: Ctx::Vote,
pub address: Ctx::Address,
pub signature: Signature<Ctx>,
}

impl<Ctx> SignedVote<Ctx>
where
Ctx: Context,
{
pub fn new(vote: Ctx::Vote, address: Ctx::Address, signature: Signature<Ctx>) -> Self {
Self {
vote,
address,
signature,
}
pub fn new(vote: Ctx::Vote, signature: Signature<Ctx>) -> Self {
Self { vote, signature }
}

pub fn validator_address(&self) -> &Ctx::Address {
self.vote.validator_address()
}
}
5 changes: 4 additions & 1 deletion Code/common/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum VoteType {
/// include information about the validator signing it.
pub trait Vote<Ctx>
where
Self: Clone + Debug + PartialEq + Eq,
Self: Clone + Debug + Eq,
Ctx: Context,
{
/// The round for which the vote is for.
Expand All @@ -32,4 +32,7 @@ where

/// The type of vote.
fn vote_type(&self) -> VoteType;

/// Address of the validator who issued this vote
fn validator_address(&self) -> &Ctx::Address;
}
42 changes: 21 additions & 21 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;

use malachite_round::state_machine::RoundData;
use secrecy::{ExposeSecret, Secret};

use malachite_common::signature::Keypair;
Expand Down Expand Up @@ -32,7 +33,8 @@ where
Ctx: Context,
{
height: Ctx::Height,
key: Secret<PrivateKey<Ctx>>,
private_key: Secret<PrivateKey<Ctx>>,
address: Ctx::Address,
validator_set: Ctx::ValidatorSet,
round: Round,
votes: VoteKeeper<Ctx>,
Expand All @@ -57,7 +59,8 @@ where
pub fn new(
height: Ctx::Height,
validator_set: Ctx::ValidatorSet,
key: PrivateKey<Ctx>,
private_key: PrivateKey<Ctx>,
address: Ctx::Address,
) -> Self {
let votes = VoteKeeper::new(
height.clone(),
Expand All @@ -67,7 +70,8 @@ where

Self {
height,
key: Secret::new(key),
private_key: Secret::new(private_key),
address,
validator_set,
round: Round::INITIAL,
votes,
Expand All @@ -90,8 +94,9 @@ where
RoundMessage::NewRound(round) => {
// TODO: check if we are the proposer

// XXX: Check if there is an existing state?
self.round_states
.insert(round, RoundState::new(self.height.clone()).new_round(round));
.insert(round, RoundState::default().new_round(round));

None
}
Expand All @@ -102,14 +107,8 @@ where
}

RoundMessage::Vote(vote) => {
let address = self
.validator_set
.get_by_public_key(&self.key.expose_secret().verifying_key())?
.address()
.clone();

let signature = Ctx::sign_vote(&vote, self.key.expose_secret());
let signed_vote = SignedVote::new(vote, address, signature);
let signature = Ctx::sign_vote(&vote, self.private_key.expose_secret());
let signed_vote = SignedVote::new(vote, signature);

Some(Message::Vote(signed_vote))
}
Expand All @@ -135,7 +134,7 @@ where
fn apply_new_round(&mut self, round: Round) -> Option<RoundMessage<Ctx>> {
let proposer = self.validator_set.get_proposer();

let event = if proposer.public_key() == &self.key.expose_secret().verifying_key() {
let event = if proposer.public_key() == &self.private_key.expose_secret().verifying_key() {
let value = self.get_value();
RoundEvent::NewRoundProposer(value)
} else {
Expand All @@ -161,7 +160,7 @@ where
}

// Check that the proposal is for the current height and round
if round_state.height != proposal.height() || proposal.round() != self.round {
if self.height != proposal.height() || self.round != proposal.round() {
return None;
}

Expand Down Expand Up @@ -194,7 +193,9 @@ where

fn apply_vote(&mut self, signed_vote: SignedVote<Ctx>) -> Option<RoundMessage<Ctx>> {
// TODO: How to handle missing validator?
let validator = self.validator_set.get_by_address(&signed_vote.address)?;
let validator = self
.validator_set
.get_by_address(signed_vote.validator_address())?;

if !Ctx::verify_signed_vote(&signed_vote, validator.public_key()) {
// TODO: How to handle invalid votes?
Expand Down Expand Up @@ -231,16 +232,15 @@ where
/// Apply the event, update the state.
fn apply_event(&mut self, round: Round, event: RoundEvent<Ctx>) -> Option<RoundMessage<Ctx>> {
// Get the round state, or create a new one
let round_state = self
.round_states
.remove(&round)
.unwrap_or_else(|| RoundState::new(self.height.clone()));
let round_state = self.round_states.remove(&round).unwrap_or_default();

let data = RoundData::new(round, &self.height, &self.address);

// Apply the event to the round state machine
let transition = round_state.apply_event(round, event);
let transition = round_state.apply_event(&data, event);

// Update state
self.round_states.insert(round, transition.state);
self.round_states.insert(round, transition.next_state);

// Return message, if any
transition.message
Expand Down
1 change: 1 addition & 0 deletions Code/round/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod events;
pub mod message;
pub mod state;
pub mod state_machine;
pub mod transition;
8 changes: 4 additions & 4 deletions Code/round/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ where
Message::Proposal(Ctx::new_proposal(height, round, value, pol_round))
}

pub fn prevote(round: Round, value_id: Option<ValueId<Ctx>>) -> Self {
Message::Vote(Ctx::new_prevote(round, value_id))
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 precommit(round: Round, value_id: Option<ValueId<Ctx>>) -> Self {
Message::Vote(Ctx::new_precommit(round, value_id))
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 timeout(round: Round, step: TimeoutStep) -> Self {
Expand Down
21 changes: 14 additions & 7 deletions Code/round/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::events::Event;
use crate::state_machine::Transition;
use crate::state_machine::RoundData;
use crate::transition::Transition;

use malachite_common::{Context, Round};

Expand Down Expand Up @@ -32,7 +33,6 @@ pub struct State<Ctx>
where
Ctx: Context,
{
pub height: Ctx::Height,
pub round: Round,
pub step: Step,
pub proposal: Option<Ctx::Proposal>,
Expand All @@ -46,7 +46,6 @@ where
{
fn clone(&self) -> Self {
Self {
height: self.height.clone(),
round: self.round,
step: self.step,
proposal: self.proposal.clone(),
Expand All @@ -60,9 +59,8 @@ impl<Ctx> State<Ctx>
where
Ctx: Context,
{
pub fn new(height: Ctx::Height) -> Self {
pub fn new() -> Self {
Self {
height,
round: Round::INITIAL,
step: Step::NewRound,
proposal: None,
Expand Down Expand Up @@ -111,7 +109,16 @@ where
}
}

pub fn apply_event(self, round: Round, event: Event<Ctx>) -> Transition<Ctx> {
crate::state_machine::apply_event(self, round, event)
pub fn apply_event(self, data: &RoundData<Ctx>, event: Event<Ctx>) -> Transition<Ctx> {
crate::state_machine::apply_event(self, data, event)
}
}

impl<Ctx> Default for State<Ctx>
where
Ctx: Context,
{
fn default() -> Self {
Self::new()
}
}
Loading

0 comments on commit e46a95b

Please sign in to comment.