Skip to content

Commit

Permalink
common: Rename Consensus trait to Context (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 8, 2023
1 parent cf8ff15 commit 759438a
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

/// This trait allows to abstract over the various datatypes
/// that are used in the consensus engine.
pub trait Consensus
pub trait Context
where
Self: Sized,
{
Expand Down
12 changes: 6 additions & 6 deletions Code/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]

mod consensus;
mod context;
mod height;
mod proposal;
mod round;
Expand All @@ -26,12 +26,12 @@ mod vote;
pub use ::signature;

/// Type alias to make it easier to refer the `ValueId` type of a given `Consensus` engine.
pub type ValueId<C> = <<C as Consensus>::Value as Value>::Id;
pub type PublicKey<C> = <<C as Consensus>::SigningScheme as SigningScheme>::PublicKey;
pub type PrivateKey<C> = <<C as Consensus>::SigningScheme as SigningScheme>::PrivateKey;
pub type Signature<C> = <<C as Consensus>::SigningScheme as SigningScheme>::Signature;
pub type ValueId<Ctx> = <<Ctx as Context>::Value as Value>::Id;
pub type PublicKey<Ctx> = <<Ctx as Context>::SigningScheme as SigningScheme>::PublicKey;
pub type PrivateKey<Ctx> = <<Ctx as Context>::SigningScheme as SigningScheme>::PrivateKey;
pub type Signature<Ctx> = <<Ctx as Context>::SigningScheme as SigningScheme>::Signature;

pub use consensus::Consensus;
pub use context::Context;
pub use height::Height;
pub use proposal::Proposal;
pub use round::Round;
Expand Down
9 changes: 5 additions & 4 deletions Code/common/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use core::fmt::Debug;

use crate::{Consensus, Round};
use crate::{Context, Round};

/// Defines the requirements for a proposal type.
pub trait Proposal<C: Consensus>
pub trait Proposal<Ctx>
where
Self: Clone + Debug + PartialEq + Eq,
Ctx: Context,
{
/// The height for which the proposal is for.
fn height(&self) -> C::Height;
fn height(&self) -> Ctx::Height;

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

/// The value that is proposed.
fn value(&self) -> &C::Value;
fn value(&self) -> &Ctx::Value;

/// The POL round for which the proposal is for.
fn pol_round(&self) -> Round;
Expand Down
18 changes: 9 additions & 9 deletions Code/common/src/signed_vote.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use crate::{Consensus, Signature};
use crate::{Context, Signature};

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignedVote<C>
pub struct SignedVote<Ctx>
where
C: Consensus,
Ctx: Context,
{
pub vote: C::Vote,
pub address: C::Address,
pub signature: Signature<C>,
pub vote: Ctx::Vote,
pub address: Ctx::Address,
pub signature: Signature<Ctx>,
}

impl<C> SignedVote<C>
impl<Ctx> SignedVote<Ctx>
where
C: Consensus,
Ctx: Context,
{
pub fn new(vote: C::Vote, address: C::Address, signature: Signature<C>) -> Self {
pub fn new(vote: Ctx::Vote, address: Ctx::Address, signature: Signature<Ctx>) -> Self {
Self {
vote,
address,
Expand Down
20 changes: 10 additions & 10 deletions Code/common/src/validator_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::Debug;

use crate::{Consensus, PublicKey};
use crate::{Context, PublicKey};

/// Voting power held by a validator.
///
Expand All @@ -17,16 +17,16 @@ where
}

/// Defines the requirements for a validator.
pub trait Validator<C>
pub trait Validator<Ctx>
where
Self: Clone + Debug + PartialEq + Eq,
C: Consensus,
Ctx: Context,
{
/// The address of the validator, typically derived from its public key.
fn address(&self) -> &C::Address;
fn address(&self) -> &Ctx::Address;

/// The public key of the validator, used to verify signatures.
fn public_key(&self) -> &PublicKey<C>;
fn public_key(&self) -> &PublicKey<Ctx>;

/// The voting power held by the validaror.
fn voting_power(&self) -> VotingPower;
Expand All @@ -35,19 +35,19 @@ where
/// Defines the requirements for a validator set.
///
/// A validator set is a collection of validators.
pub trait ValidatorSet<C>
pub trait ValidatorSet<Ctx>
where
C: Consensus,
Ctx: Context,
{
/// The total voting power of the validator set.
fn total_voting_power(&self) -> VotingPower;

/// The proposer in the validator set.
fn get_proposer(&self) -> C::Validator;
fn get_proposer(&self) -> Ctx::Validator;

/// Get the validator with the given public key.
fn get_by_public_key(&self, public_key: &PublicKey<C>) -> Option<&C::Validator>;
fn get_by_public_key(&self, public_key: &PublicKey<Ctx>) -> Option<&Ctx::Validator>;

/// Get the validator with the given address.
fn get_by_address(&self, address: &C::Address) -> Option<&C::Validator>;
fn get_by_address(&self, address: &Ctx::Address) -> Option<&Ctx::Validator>;
}
9 changes: 5 additions & 4 deletions Code/common/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::Debug;

use crate::{Consensus, Round, Value};
use crate::{Context, Round, Value};

/// A type of vote.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand All @@ -16,18 +16,19 @@ pub enum VoteType {
///
/// Votes are signed messages from validators for a particular value which
/// include information about the validator signing it.
pub trait Vote<C: Consensus>
pub trait Vote<Ctx>
where
Self: Clone + Debug + PartialEq + Eq,
Ctx: Context,
{
/// The round for which the vote is for.
fn round(&self) -> Round;

/// Get a reference to the value being voted for.
fn value(&self) -> Option<&<C::Value as Value>::Id>;
fn value(&self) -> Option<&<Ctx::Value as Value>::Id>;

/// Take ownership of the value being voted for.
fn take_value(self) -> Option<<C::Value as Value>::Id>;
fn take_value(self) -> Option<<Ctx::Value as Value>::Id>;

/// The type of vote.
fn vote_type(&self) -> VoteType;
Expand Down
68 changes: 36 additions & 32 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use secrecy::{ExposeSecret, Secret};

use malachite_common::signature::Keypair;
use malachite_common::{
Consensus, PrivateKey, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator,
Context, PrivateKey, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator,
ValidatorSet, Value, Vote, VoteType,
};
use malachite_round::events::Event as RoundEvent;
Expand All @@ -16,45 +16,49 @@ use malachite_vote::keeper::VoteKeeper;

/// Messages that can be received and broadcast by the consensus executor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event<C>
pub enum Event<Ctx>
where
C: Consensus,
Ctx: Context,
{
NewRound(Round),
Proposal(C::Proposal),
Vote(SignedVote<C>),
Proposal(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Timeout(Timeout),
}

#[derive(Clone, Debug)]
pub struct Executor<C>
pub struct Executor<Ctx>
where
C: Consensus,
Ctx: Context,
{
height: C::Height,
key: Secret<PrivateKey<C>>,
validator_set: C::ValidatorSet,
height: Ctx::Height,
key: Secret<PrivateKey<Ctx>>,
validator_set: Ctx::ValidatorSet,
round: Round,
votes: VoteKeeper<C>,
round_states: BTreeMap<Round, RoundState<C>>,
votes: VoteKeeper<Ctx>,
round_states: BTreeMap<Round, RoundState<Ctx>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<C>
pub enum Message<Ctx>
where
C: Consensus,
Ctx: Context,
{
Propose(C::Proposal),
Vote(SignedVote<C>),
Decide(Round, C::Value),
Propose(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
SetTimeout(Timeout),
}

impl<C> Executor<C>
impl<Ctx> Executor<Ctx>
where
C: Consensus,
Ctx: Context,
{
pub fn new(height: C::Height, validator_set: C::ValidatorSet, key: PrivateKey<C>) -> Self {
pub fn new(
height: Ctx::Height,
validator_set: Ctx::ValidatorSet,
key: PrivateKey<Ctx>,
) -> Self {
let votes = VoteKeeper::new(
height.clone(),
Round::INITIAL,
Expand All @@ -71,12 +75,12 @@ where
}
}

pub fn get_value(&self) -> C::Value {
pub fn get_value(&self) -> Ctx::Value {
// TODO - add external interface to get the value
C::DUMMY_VALUE
Ctx::DUMMY_VALUE
}

pub fn execute(&mut self, msg: Event<C>) -> Option<Message<C>> {
pub fn execute(&mut self, msg: Event<Ctx>) -> Option<Message<Ctx>> {
let round_msg = match self.apply(msg) {
Some(msg) => msg,
None => return None,
Expand Down Expand Up @@ -104,7 +108,7 @@ where
.address()
.clone();

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

Some(Message::Vote(signed_vote))
Expand All @@ -119,7 +123,7 @@ where
}
}

fn apply(&mut self, msg: Event<C>) -> Option<RoundMessage<C>> {
fn apply(&mut self, msg: Event<Ctx>) -> Option<RoundMessage<Ctx>> {
match msg {
Event::NewRound(round) => self.apply_new_round(round),
Event::Proposal(proposal) => self.apply_proposal(proposal),
Expand All @@ -128,7 +132,7 @@ where
}
}

fn apply_new_round(&mut self, round: Round) -> Option<RoundMessage<C>> {
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() {
Expand All @@ -141,7 +145,7 @@ where
self.apply_event(round, event)
}

fn apply_proposal(&mut self, proposal: C::Proposal) -> Option<RoundMessage<C>> {
fn apply_proposal(&mut self, proposal: Ctx::Proposal) -> Option<RoundMessage<Ctx>> {
// TODO: Check for invalid proposal
let event = RoundEvent::Proposal(proposal.clone());

Expand Down Expand Up @@ -188,11 +192,11 @@ where
}
}

fn apply_vote(&mut self, signed_vote: SignedVote<C>) -> Option<RoundMessage<C>> {
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)?;

if !C::verify_signed_vote(&signed_vote, validator.public_key()) {
if !Ctx::verify_signed_vote(&signed_vote, validator.public_key()) {
// TODO: How to handle invalid votes?
return None;
}
Expand All @@ -214,7 +218,7 @@ where
self.apply_event(round, round_event)
}

fn apply_timeout(&mut self, timeout: Timeout) -> Option<RoundMessage<C>> {
fn apply_timeout(&mut self, timeout: Timeout) -> Option<RoundMessage<Ctx>> {
let event = match timeout.step {
TimeoutStep::Propose => RoundEvent::TimeoutPropose,
TimeoutStep::Prevote => RoundEvent::TimeoutPrevote,
Expand All @@ -225,7 +229,7 @@ where
}

/// Apply the event, update the state.
fn apply_event(&mut self, round: Round, event: RoundEvent<C>) -> Option<RoundMessage<C>> {
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
Expand All @@ -242,7 +246,7 @@ where
transition.message
}

pub fn round_state(&self, round: Round) -> Option<&RoundState<C>> {
pub fn round_state(&self, round: Round) -> Option<&RoundState<Ctx>> {
self.round_states.get(&round)
}
}
33 changes: 18 additions & 15 deletions Code/round/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use malachite_common::{Consensus, ValueId};
use malachite_common::{Context, ValueId};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event<C: Consensus> {
NewRound, // Start a new round, not as proposer.
NewRoundProposer(C::Value), // Start a new round and propose the Value.
Proposal(C::Proposal), // Receive a proposal with possible polka round.
ProposalInvalid, // Receive an invalid proposal.
PolkaAny, // Receive +2/3 prevotes for anything.
PolkaNil, // Receive +2/3 prevotes for nil.
PolkaValue(ValueId<C>), // Receive +2/3 prevotes for Value.
PrecommitAny, // Receive +2/3 precommits for anything.
PrecommitValue(ValueId<C>), // 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.
TimeoutPrecommit, // Timeout waiting for precommits.
pub enum Event<Ctx>
where
Ctx: Context,
{
NewRound, // Start a new round, not as proposer.
NewRoundProposer(Ctx::Value), // Start a new round and propose the Value.
Proposal(Ctx::Proposal), // Receive a proposal with possible polka round.
ProposalInvalid, // Receive an invalid proposal.
PolkaAny, // Receive +2/3 prevotes for anything.
PolkaNil, // Receive +2/3 prevotes for nil.
PolkaValue(ValueId<Ctx>), // Receive +2/3 prevotes for Value.
PrecommitAny, // Receive +2/3 precommits for anything.
PrecommitValue(ValueId<Ctx>), // 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.
TimeoutPrecommit, // Timeout waiting for precommits.
}
Loading

0 comments on commit 759438a

Please sign in to comment.