Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft of Protobuf definitions for Malachite types #164

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ members = [
"common",
"driver",
"itf",
"node",
"node",
"proto",
"round",
"test",
"vote",
Expand All @@ -19,18 +20,32 @@ license = "Apache-2.0"
publish = false

[workspace.dependencies]
malachite-common = { version = "0.1.0", path = "common" }
malachite-driver = { version = "0.1.0", path = "driver" }
malachite-itf = { version = "0.1.0", path = "itf" }
malachite-node = { version = "0.1.0", path = "node" }
malachite-proto = { version = "0.1.0", path = "proto" }
malachite-round = { version = "0.1.0", path = "round" }
malachite-test = { version = "0.1.0", path = "test" }
malachite-vote = { version = "0.1.0", path = "vote" }

derive-where = "1.2.7"
ed25519-consensus = "2.1.0"
futures = "0.3"
glob = "0.3.0"
itf = "0.2.2"
num-bigint = "0.4.4"
num-traits = "0.2.17"
pretty_assertions = "1.4"
prost = "0.12.3"
prost-types = "0.12.3"
prost-build = "0.12.3"
rand = { version = "0.8.5", features = ["std_rng"] }
serde = "1.0"
serde_json = "1.0"
serde_with = "3.4"
sha2 = "0.10.8"
signature = "2.1.0"
thiserror = "1.0"
tokio = "1.35.1"
tokio-stream = "0.1"
4 changes: 3 additions & 1 deletion code/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ license.workspace = true
publish.workspace = true

[dependencies]
signature.workspace = true
malachite-proto.workspace = true
derive-where.workspace = true
signature.workspace = true
3 changes: 3 additions & 0 deletions code/common/src/height.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::Debug;

use malachite_proto::Protobuf;

/// Defines the requirements for a height type.
///
/// A height denotes the number of blocks (values) created since the chain began.
Expand All @@ -8,5 +10,6 @@ use core::fmt::Debug;
pub trait Height
where
Self: Default + Copy + Clone + Debug + PartialEq + Eq + PartialOrd + Ord,
Self: Protobuf<malachite_proto::Height>,
{
}
2 changes: 2 additions & 0 deletions code/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

extern crate alloc;

mod context;
mod height;
mod proposal;
Expand Down
3 changes: 3 additions & 0 deletions code/common/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use core::fmt::Debug;

use malachite_proto::Protobuf;

use crate::{Context, Round};

/// Defines the requirements for a proposal type.
pub trait Proposal<Ctx>
where
Self: Clone + Debug + Eq + Send + Sync + 'static,
Self: Protobuf<malachite_proto::Proposal>,
Ctx: Context,
{
/// The height for which the proposal is for.
Expand Down
17 changes: 17 additions & 0 deletions code/common/src/round.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::cmp;
use core::convert::Infallible;

/// A round number.
///
Expand Down Expand Up @@ -72,6 +73,22 @@ impl Ord for Round {
}
}

impl TryFrom<malachite_proto::Round> for Round {
type Error = Infallible;

fn try_from(proto: malachite_proto::Round) -> Result<Self, Self::Error> {
Ok(Self::new(proto.round))
}
}

impl From<Round> for malachite_proto::Round {
fn from(round: Round) -> malachite_proto::Round {
malachite_proto::Round {
round: round.as_i64(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
36 changes: 2 additions & 34 deletions code/common/src/signed_vote.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::fmt;
use derive_where::derive_where;

use crate::{Context, Signature, Vote};

/// A signed vote, ie. a vote emitted by a validator and signed by its private key.
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub struct SignedVote<Ctx>
where
Ctx: Context,
Expand All @@ -28,36 +29,3 @@ where
self.vote.validator_address()
}
}

// NOTE: We have to derive these instances manually, otherwise
// the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`,
// which may not hold for all contexts.

impl<Ctx: Context> Clone for SignedVote<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
Self {
vote: self.vote.clone(),
signature: self.signature.clone(),
}
}
}

impl<Ctx: Context> fmt::Debug for SignedVote<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SignedVote")
.field("vote", &self.vote)
.field("signature", &self.signature)
.finish()
}
}

impl<Ctx: Context> PartialEq for SignedVote<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn eq(&self, other: &Self) -> bool {
self.vote == other.vote && self.signature == other.signature
}
}

impl<Ctx: Context> Eq for SignedVote<Ctx> {}
3 changes: 3 additions & 0 deletions code/common/src/validator_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::{Debug, Display};

use malachite_proto::Protobuf;

use crate::{Context, PublicKey};

/// Voting power held by a validator.
Expand All @@ -11,6 +13,7 @@ pub type VotingPower = u64;
pub trait Address
where
Self: Clone + Debug + Display + Eq + Ord,
Self: Protobuf<malachite_proto::Address>,
{
}

Expand Down
31 changes: 31 additions & 0 deletions code/common/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::Debug;

use malachite_proto::Protobuf;

/// Represents either `Nil` or a value of type `Value`.
///
/// This type is isomorphic to `Option<Value>` but is more explicit about its intent.
Expand Down Expand Up @@ -53,10 +55,39 @@ impl<Value> NilOrVal<Value> {
}
}

// impl<Value> TryFrom<malachite_proto::Value> for NilOrVal<Value>
// where
// Value: From<u64>, // FIXME
// {
// type Error = String;
//
// fn try_from(proto: malachite_proto::Value) -> Result<Self, Self::Error> {
// match proto.value {
// Some(value) => Ok(NilOrVal::Val(Value::from(value))), // FIXME
// None => Ok(NilOrVal::Nil),
// }
// }
// }
//
// impl<Value> TryFrom<malachite_proto::ValueId> for NilOrVal<Value>
// where
// Value: TryFrom<Vec<u8>>, // FIXME
// {
// type Error = String;
//
// fn try_from(proto: malachite_proto::ValueId) -> Result<Self, Self::Error> {
// match proto.value {
// Some(value) => Ok(NilOrVal::Val(Value::from(value))), // FIXME
// None => Ok(NilOrVal::Nil),
// }
// }
// }

/// Defines the requirements for the type of value to decide on.
pub trait Value
where
Self: Clone + Debug + PartialEq + Eq + PartialOrd + Ord,
Self: Protobuf<malachite_proto::Value>,
{
/// The type of the ID of the value.
/// Typically a representation of the value with a lower memory footprint.
Expand Down
24 changes: 24 additions & 0 deletions code/common/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::convert::Infallible;
use core::fmt::Debug;

use malachite_proto::Protobuf;

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

/// A type of vote.
Expand All @@ -12,13 +15,34 @@ pub enum VoteType {
Precommit,
}

impl TryFrom<malachite_proto::VoteType> for VoteType {
type Error = Infallible;

fn try_from(vote_type: malachite_proto::VoteType) -> Result<Self, Self::Error> {
match vote_type {
malachite_proto::VoteType::Prevote => Ok(VoteType::Prevote),
malachite_proto::VoteType::Precommit => Ok(VoteType::Precommit),
}
}
}

impl From<VoteType> for malachite_proto::VoteType {
fn from(vote_type: VoteType) -> malachite_proto::VoteType {
match vote_type {
VoteType::Prevote => malachite_proto::VoteType::Prevote,
VoteType::Precommit => malachite_proto::VoteType::Precommit,
}
}
}

/// Defines the requirements for a vote.
///
/// Votes are signed messages from validators for a particular value which
/// include information about the validator signing it.
pub trait Vote<Ctx>
where
Self: Clone + Debug + Eq + Send + Sync + 'static,
Self: Protobuf<malachite_proto::Vote>,
Ctx: Context,
{
/// The height for which the vote is for.
Expand Down
2 changes: 2 additions & 0 deletions code/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ publish.workspace = true
malachite-common = { version = "0.1.0", path = "../common" }
malachite-round = { version = "0.1.0", path = "../round" }
malachite-vote = { version = "0.1.0", path = "../vote" }

derive-where.workspace = true
18 changes: 3 additions & 15 deletions code/driver/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use core::fmt;

use derive_where::derive_where;

use malachite_common::Context;

/// The type of errors that can be yielded by the `Driver`.
#[derive(Clone, Debug)]
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub enum Error<Ctx>
where
Ctx: Context,
Expand All @@ -27,17 +29,3 @@ where
}
}
}

impl<Ctx> PartialEq for Error<Ctx>
where
Ctx: Context,
{
#[cfg_attr(coverage_nightly, coverage(off))]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Error::ProposerNotFound(addr1), Error::ProposerNotFound(addr2)) => addr1 == addr2,
(Error::ValidatorNotFound(addr1), Error::ValidatorNotFound(addr2)) => addr1 == addr2,
_ => false,
}
}
}
67 changes: 2 additions & 65 deletions code/driver/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::fmt;
use derive_where::derive_where;

use malachite_common::{Context, Round, Timeout};

/// Messages emitted by the [`Driver`](crate::Driver)
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub enum Output<Ctx>
where
Ctx: Context,
Expand All @@ -26,67 +27,3 @@ where
/// The timeout tells the proposal builder how long it has to build a value.
GetValue(Ctx::Height, Round, Timeout),
}

// NOTE: We have to derive these instances manually, otherwise
// the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`,
// which may not hold for all contexts.

impl<Ctx: Context> Clone for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
match self {
Output::NewRound(height, round) => Output::NewRound(*height, *round),
Output::Propose(proposal) => Output::Propose(proposal.clone()),
Output::Vote(signed_vote) => Output::Vote(signed_vote.clone()),
Output::Decide(round, value) => Output::Decide(*round, value.clone()),
Output::ScheduleTimeout(timeout) => Output::ScheduleTimeout(*timeout),
Output::GetValue(height, round, timeout) => Output::GetValue(*height, *round, *timeout),
}
}
}

impl<Ctx: Context> fmt::Debug for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Output::NewRound(height, round) => write!(f, "NewRound({:?}, {:?})", height, round),
Output::Propose(proposal) => write!(f, "Propose({:?})", proposal),
Output::Vote(signed_vote) => write!(f, "Vote({:?})", signed_vote),
Output::Decide(round, value) => write!(f, "Decide({:?}, {:?})", round, value),
Output::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout),
Output::GetValue(height, round, timeout) => {
write!(f, "GetValue({:?}, {:?}, {:?})", height, round, timeout)
}
}
}
}

impl<Ctx: Context> PartialEq for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Output::NewRound(height, round), Output::NewRound(other_height, other_round)) => {
height == other_height && round == other_round
}
(Output::Propose(proposal), Output::Propose(other_proposal)) => {
proposal == other_proposal
}
(Output::Vote(signed_vote), Output::Vote(other_signed_vote)) => {
signed_vote == other_signed_vote
}
(Output::Decide(round, value), Output::Decide(other_round, other_value)) => {
round == other_round && value == other_value
}
(Output::ScheduleTimeout(timeout), Output::ScheduleTimeout(other_timeout)) => {
timeout == other_timeout
}
(
Output::GetValue(height, round, timeout),
Output::GetValue(other_height, other_round, other_timeout),
) => height == other_height && round == other_round && timeout == other_timeout,
_ => false,
}
}
}

impl<Ctx: Context> Eq for Output<Ctx> {}
Loading
Loading