Skip to content

Commit

Permalink
Attempt to abstract over the various data types needed by the consens…
Browse files Browse the repository at this point in the history
…us engine
  • Loading branch information
romac committed Oct 24, 2023
1 parent a80f2a4 commit 4430a23
Show file tree
Hide file tree
Showing 17 changed files with 822 additions and 399 deletions.
81 changes: 81 additions & 0 deletions Code/common/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{
Address, Height, Proposal, PublicKey, Round, Validator, ValidatorSet, Value, ValueId, Vote,
};

pub trait Consensus
where
Self: Sized,
{
type Address: Address;
type Height: Height;
type Proposal: Proposal<Self>;
type PublicKey: PublicKey;
type ValidatorSet: ValidatorSet<Self>;
type Validator: Validator<Self>;
type Value: Value;
type Vote: Vote<Self>;

// FIXME: Remove this and thread it through where necessary
const DUMMY_ADDRESS: Self::Address;

// FIXME: Remove
const DUMMY_VALUE: Self::Value;

fn new_proposal(
height: Self::Height,
round: Round,
value: Self::Value,
pol_round: Round,
) -> Self::Proposal;

fn new_prevote(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;

fn new_precommit(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;
}

pub mod test {
use crate::height::test::*;
use crate::proposal::test::*;
use crate::validator_set::test::*;
use crate::value::test::*;
use crate::vote::test::*;
use crate::Round;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TestConsensus;

impl super::Consensus for TestConsensus {
type Address = Address;
type Height = Height;
type Proposal = Proposal;
type PublicKey = PublicKey;
type ValidatorSet = ValidatorSet;
type Validator = Validator;
type Value = Value;
type Vote = Vote;

const DUMMY_ADDRESS: Address = Address::new(42);

const DUMMY_VALUE: Self::Value = Value::new(0xdeadbeef);

fn new_proposal(height: Height, round: Round, value: Value, pol_round: Round) -> Proposal {
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_precommit(round: Round, value_id: Option<ValueId>, address: Address) -> Vote {
Vote::new_precommit(round, value_id, address)
}
}
}
32 changes: 22 additions & 10 deletions Code/common/src/height.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// TODO: Abstract over Height
use core::fmt::Debug;

/// A blockchain height
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Height(u64);
// TODO: Keep the trait or just add the bounds to Consensus::Height?
pub trait Height
where
// TODO: Require Copy as well?
Self: Clone + Debug + PartialEq + Eq + PartialOrd + Ord,
{
}

impl Height {
pub fn new(height: u64) -> Self {
Self(height)
}
pub mod test {
/// A blockchain height
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Height(u64);

pub fn as_u64(&self) -> u64 {
self.0
impl Height {
pub fn new(height: u64) -> Self {
Self(height)
}

pub fn as_u64(&self) -> u64 {
self.0
}
}

impl super::Height for Height {}
}
27 changes: 20 additions & 7 deletions Code/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]

mod consensus;
mod height;
mod proposal;
mod round;
Expand All @@ -18,10 +19,22 @@ mod validator_set;
mod value;
mod vote;

pub use height::*;
pub use proposal::*;
pub use round::*;
pub use timeout::*;
pub use validator_set::*;
pub use value::*;
pub use vote::*;
pub type ValueId<C> = <<C as Consensus>::Value as Value>::Id;

pub use consensus::Consensus;
pub use height::Height;
pub use proposal::Proposal;
pub use round::Round;
pub use timeout::{Timeout, TimeoutStep};
pub use validator_set::{Address, PublicKey, Validator, ValidatorSet};
pub use value::Value;
pub use vote::{Vote, VoteType};

pub mod test {
pub use crate::consensus::test::*;
pub use crate::height::test::*;
pub use crate::proposal::test::*;
pub use crate::validator_set::test::*;
pub use crate::value::test::*;
pub use crate::vote::test::*;
}
67 changes: 51 additions & 16 deletions Code/common/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
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,
use core::fmt::Debug;

use crate::{Consensus, Round};

pub trait Proposal<C: Consensus>
where
Self: Clone + Debug + PartialEq + Eq,
{
fn height(&self) -> C::Height;
fn round(&self) -> Round;
fn value(&self) -> &C::Value;
fn pol_round(&self) -> Round;
}

impl Proposal {
pub fn new(height: Height, round: Round, value: Value, pol_round: Round) -> Self {
Self {
height,
round,
value,
pol_round,
pub mod test {
use crate::test::{Height, TestConsensus, Value};
use crate::Round;

/// 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(height: Height, round: Round, value: Value, pol_round: Round) -> Self {
Self {
height,
round,
value,
pol_round,
}
}
}

impl super::Proposal<TestConsensus> for Proposal {
fn height(&self) -> Height {
self.height
}

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

fn value(&self) -> &Value {
&self.value
}

fn pol_round(&self) -> Round {
self.pol_round
}
}
}
2 changes: 1 addition & 1 deletion Code/common/src/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum TimeoutStep {
Precommit,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Timeout {
pub round: Round,
pub step: TimeoutStep,
Expand Down
Loading

0 comments on commit 4430a23

Please sign in to comment.