-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract over the various data types needed by the consensus engine (#8)
Closes: #11 This gist of this PR is that it adds a `Consensus` trait which defines the various datatypes that the consensus engine operates over, as well as a trait per such datatype that defines the requirement for said datatype. ```rust pub trait Consensus where Self: Sized, { type Address: Address; type Height: Height; type Proposal: Proposal<Self>; type PublicKey: PublicKey; type Validator: Validator<Self>; type ValidatorSet: ValidatorSet<Self>; type Value: Value; type Vote: Vote<Self>; ``` ```rust pub trait Height where Self: Clone + Debug + PartialEq + Eq + PartialOrd + Ord, { } ``` etc. In test code, we define our own simple versions of these datatypes as well as a `TestConsensus` struct, for which we implement the `Consensus` trait. ```rust #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Address(u64); impl Address { pub const fn new(value: u64) -> Self { Self(value) } } impl malachite_common::Address for Address {} ``` ```rust pub struct TestConsensus; impl 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; } ``` --- * Attempt to abstract over the various data types needed by the consensus engine * Fix tests * Fix lints names * Move tests and associated data types into their own crate (#10) * Move tests and associated data types into their own crate * Add small threshold to codecov * Small cleanup * Move tests into `tests` folder * Adjust base when computing coverage in the presence of removed code See: https://docs.codecov.com/docs/commit-status#removed_code_behavior * Document the `Round` type and rename `Round::None` to `Round::Nil` * More doc comments * Remove `PublicKey::hash` requirement * Few more comments * Add propose+precommit timeout test
- Loading branch information
Showing
34 changed files
with
1,823 additions
and
884 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ members = [ | |
"consensus", | ||
"round", | ||
"vote", | ||
"test", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::{ | ||
Address, Height, Proposal, PublicKey, Round, Validator, ValidatorSet, Value, ValueId, Vote, | ||
}; | ||
|
||
/// This trait allows to abstract over the various datatypes | ||
/// that are used in the consensus engine. | ||
pub trait Consensus | ||
where | ||
Self: Sized, | ||
{ | ||
type Address: Address; | ||
type Height: Height; | ||
type Proposal: Proposal<Self>; | ||
type PublicKey: PublicKey; | ||
type Validator: Validator<Self>; | ||
type ValidatorSet: ValidatorSet<Self>; | ||
type Value: Value; | ||
type Vote: Vote<Self>; | ||
|
||
// FIXME: Remove this and thread it through where necessary | ||
const DUMMY_ADDRESS: Self::Address; | ||
|
||
// FIXME: Remove altogether | ||
const DUMMY_VALUE: Self::Value; | ||
|
||
/// Build a new proposal for the given value at the given height, round and POL round. | ||
fn new_proposal( | ||
height: Self::Height, | ||
round: Round, | ||
value: Self::Value, | ||
pol_round: Round, | ||
) -> Self::Proposal; | ||
|
||
/// 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>>, | ||
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>>, | ||
address: Self::Address, | ||
) -> Self::Vote; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
// TODO: Abstract over Height | ||
use core::fmt::Debug; | ||
|
||
/// A blockchain height | ||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Height(u64); | ||
|
||
impl Height { | ||
pub fn new(height: u64) -> Self { | ||
Self(height) | ||
} | ||
|
||
pub fn as_u64(&self) -> u64 { | ||
self.0 | ||
} | ||
// TODO: Keep the trait or just add the bounds to Consensus::Height? | ||
/// Defines the requirements for a height type. | ||
/// | ||
/// A height denotes the number of blocks (values) created since the chain began. | ||
/// | ||
/// A height of 0 represents a chain which has not yet produced a block. | ||
pub trait Height | ||
where | ||
// TODO: Require Copy as well? | ||
Self: Clone + Debug + PartialEq + Eq + PartialOrd + Ord, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
use crate::{Height, Round, Value}; | ||
use core::fmt::Debug; | ||
|
||
/// 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 crate::{Consensus, Round}; | ||
|
||
/// Defines the requirements for a proposal type. | ||
pub trait Proposal<C: Consensus> | ||
where | ||
Self: Clone + Debug + PartialEq + Eq, | ||
{ | ||
/// The height for which the proposal is for. | ||
fn height(&self) -> C::Height; | ||
|
||
/// The round for which the proposal is for. | ||
fn round(&self) -> Round; | ||
|
||
/// The value that is proposed. | ||
fn value(&self) -> &C::Value; | ||
|
||
impl Proposal { | ||
pub fn new(height: Height, round: Round, value: Value, pol_round: Round) -> Self { | ||
Self { | ||
height, | ||
round, | ||
value, | ||
pol_round, | ||
} | ||
} | ||
/// The POL round for which the proposal is for. | ||
fn pol_round(&self) -> Round; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
use crate::Round; | ||
|
||
/// The round step for which the timeout is for. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub enum TimeoutStep { | ||
Propose, | ||
Prevote, | ||
Precommit, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
/// A timeout for a round step. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub struct Timeout { | ||
pub round: Round, | ||
pub step: TimeoutStep, | ||
} | ||
|
||
impl Timeout { | ||
pub fn new(round: Round, step: TimeoutStep) -> Self { | ||
Self { round, step } | ||
} | ||
|
||
pub fn propose(round: Round) -> Self { | ||
Self::new(round, TimeoutStep::Propose) | ||
} | ||
|
||
pub fn prevote(round: Round) -> Self { | ||
Self::new(round, TimeoutStep::Prevote) | ||
} | ||
|
||
pub fn precommit(round: Round) -> Self { | ||
Self::new(round, TimeoutStep::Precommit) | ||
} | ||
} |
Oops, something went wrong.