-
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.
Attempt to abstract over the various data types needed by the consens…
…us engine
- Loading branch information
Showing
17 changed files
with
822 additions
and
399 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
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) | ||
} | ||
} | ||
} |
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,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 {} | ||
} |
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,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 | ||
} | ||
} | ||
} |
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
Oops, something went wrong.