Skip to content

Commit

Permalink
make scp::Msg derive(Digestible) (#127)
Browse files Browse the repository at this point in the history
* initial take on making scp::Topic and friends digestible

* scp::Msg (and friends) is Digestible

* testcase for enum serialization

* lock file and downgrades

* clippppppy

* cr fixes

* fix and relocate tests

* include specific type
  • Loading branch information
eranrund authored May 1, 2020
1 parent 308e5de commit 17c6328
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 154 deletions.
131 changes: 66 additions & 65 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion common/src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::{
};
use failure::Fail;
use hex_fmt::HexFmt;
use mc_crypto_digestible::Digestible;
use mc_crypto_keys::{Ed25519Public, KeyError};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -45,7 +46,7 @@ impl From<KeyError> for NodeIDError {
}

/// Node unique identifier containing a responder_id as well as a unique public key
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize, Digestible)]
pub struct NodeID {
pub responder_id: ResponderId,
pub public_key: Ed25519Public,
Expand Down
5 changes: 4 additions & 1 deletion common/src/responder_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::{
str::FromStr,
};
use failure::Fail;
use mc_crypto_digestible::Digestible;
use serde::{Deserialize, Serialize};

/// Potential parse errors
Expand All @@ -23,7 +24,9 @@ pub enum ResponderIdParseError {
}

/// Node unique identifier.
#[derive(Clone, Default, Debug, Eq, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Hash)]
#[derive(
Clone, Default, Debug, Eq, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Hash, Digestible,
)]
pub struct ResponderId(pub String);

impl Display for ResponderId {
Expand Down
41 changes: 3 additions & 38 deletions consensus/enclave/trusted/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions consensus/scp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test_utils = []
[dependencies]
mc-common = { path = "../../common", features = ["log"] }
mc-crypto-keys = { path = "../../crypto/keys" }
mc-crypto-digestible = { path = "../../crypto/digestible" }
mc-util-from-random = { path = "../../util/from-random" }
mc-util-metrics = { path = "../../util/metrics" }
mc-util-serial = { path = "../../util/serial", features = ["std"] }
Expand Down
19 changes: 10 additions & 9 deletions consensus/scp/src/core_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2018-2020 MobileCoin Inc.

//! Core types for MobileCoin's implementation of SCP.
use mc_crypto_digestible::Digestible;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
clone::Clone,
Expand All @@ -13,9 +14,9 @@ use std::{
};

/// A generic node identifier.
pub trait GenericNodeId: Clone + Debug + Display + Eq + PartialEq + Hash {}
pub trait GenericNodeId: Clone + Debug + Display + Eq + PartialEq + Hash + Digestible {}
impl<T> GenericNodeId for T where
T: Clone + Debug + Display + Serialize + DeserializeOwned + Eq + PartialEq + Hash
T: Clone + Debug + Display + Serialize + DeserializeOwned + Eq + PartialEq + Hash + Digestible
{
}

Expand All @@ -40,12 +41,12 @@ pub type SlotIndex = u64;

/// The value on which to consense.
pub trait Value:
Hash + Eq + PartialEq + Debug + Clone + PartialOrd + Ord + Send + Serialize
Hash + Eq + PartialEq + Debug + Clone + PartialOrd + Ord + Send + Serialize + Digestible
{
}

impl<T> Value for T where
T: Hash + Eq + PartialEq + Debug + Clone + PartialOrd + Ord + Send + Serialize
T: Hash + Eq + PartialEq + Debug + Clone + PartialOrd + Ord + Send + Serialize + Digestible
{
}

Expand All @@ -55,7 +56,7 @@ impl<T> Value for T where
/// which are moving through the phases of the federated voting.
///
/// Ballots are totally ordered, with "counter" more significant than "value."
#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, Digestible)]
pub struct Ballot<V: Value> {
/// Counter.
pub N: u32,
Expand Down Expand Up @@ -114,8 +115,8 @@ mod core_types_tests {
fn total_ordering() {
// Ballots are ordered first by counter `N`.
{
let high_ballot: Ballot<u8> = Ballot { N: 13, X: vec![] };
let low_ballot = Ballot {
let high_ballot: Ballot<u32> = Ballot { N: 13, X: vec![] };
let low_ballot: Ballot<u32> = Ballot {
N: 4,
X: vec![100, 200, 88],
};
Expand All @@ -124,11 +125,11 @@ mod core_types_tests {

// Ballots are then ordered lexicographically by `X`.
{
let high_ballot = Ballot {
let high_ballot: Ballot<u32> = Ballot {
N: 13,
X: vec![2000, 1000],
};
let low_ballot = Ballot {
let low_ballot: Ballot<u32> = Ballot {
N: 13,
X: vec![1000, 2001],
};
Expand Down
13 changes: 7 additions & 6 deletions consensus/scp/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
quorum_set::QuorumSet,
};
use mc_common::{HashSet, NodeID};
use mc_crypto_digestible::Digestible;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
cmp,
Expand All @@ -20,7 +21,7 @@ use std::{
pub const INFINITY: u32 = <u32>::max_value();

/// The contents of a Nominate Message.
#[derive(Clone, Debug, Eq, Hash, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Serialize, Deserialize, PartialEq, Digestible)]
pub struct NominatePayload<V: Value> {
/// Voted values.
pub X: BTreeSet<V>,
Expand Down Expand Up @@ -57,7 +58,7 @@ impl<V: Value> PartialOrd for NominatePayload<V> {
/// The contents of a Prepare Message.
///
/// See [IETF Draft 0](https://tools.ietf.org/html/draft-mazieres-dinrg-scp-00#page-7)
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash, Digestible)]
pub struct PreparePayload<V: Value> {
/// The ballot, containing the current and highest prepare vote.
pub B: Ballot<V>,
Expand Down Expand Up @@ -106,7 +107,7 @@ impl<V: Value> PartialOrd for PreparePayload<V> {
/// The contents of a Commit Message.
///
/// See Commit Message in [IETF Draft 05](https://tools.ietf.org/pdf/draft-mazieres-dinrg-scp-05.pdf)
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash, Digestible)]
pub struct CommitPayload<V: Value> {
/// The ballot, containing the current and highest commit vote.
///
Expand Down Expand Up @@ -147,7 +148,7 @@ impl<V: Value> PartialOrd for CommitPayload<V> {
}

/// The contents of an Externalize Message.
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, Serialize, Deserialize, PartialEq, Hash, Digestible)]
pub struct ExternalizePayload<V: Value> {
/// The lowest confirmed committed ballot.
pub C: Ballot<V>,
Expand All @@ -174,7 +175,7 @@ impl<V: Value> PartialOrd for ExternalizePayload<V> {
}

/// Encapsulates phase of SCP, and contains the appropriate payload.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Digestible)]
pub enum Topic<V: Value> {
/// Nominate Messages.
Nominate(NominatePayload<V>),
Expand Down Expand Up @@ -253,7 +254,7 @@ impl<V: Value> PartialOrd for Topic<V> {
}

/// The Messsage type for Consensus.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Digestible)]
pub struct Msg<V: Value, ID: GenericNodeId = NodeID> {
/// ID of the node sending this message.
pub sender_id: ID,
Expand Down
5 changes: 3 additions & 2 deletions consensus/scp/src/quorum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!
//! A quorum set includes the members of the network, which a given node trusts and depends on.
use mc_common::{HashMap, HashSet, NodeID, ResponderId};
use mc_crypto_digestible::Digestible;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
Expand All @@ -18,7 +19,7 @@ use crate::{
};

/// The quorum set defining the trusted set of peers.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Digestible)]
pub struct QuorumSet<ID: GenericNodeId = NodeID> {
/// Threshold (how many members do we need to reach quorum).
pub threshold: u32,
Expand All @@ -28,7 +29,7 @@ pub struct QuorumSet<ID: GenericNodeId = NodeID> {
}

/// A member in a QuorumSet. Can be either a Node or another QuorumSet.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Digestible)]
#[serde(tag = "type", content = "args")]
pub enum QuorumSetMember<ID: GenericNodeId> {
/// A single trusted entity with an identity.
Expand Down
6 changes: 3 additions & 3 deletions crypto/digestible/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ edition = "2018"
proc_macro = true

[dependencies]
proc-macro2 = "0.4.4"
quote = "0.6.3"
syn = { version = "0.15", features = [ "extra-traits" ] }
proc-macro2 = "1.0.8"
quote = "1.0.2"
syn = { version = "1.0.14", features = [ "extra-traits" ] }

[dev-dependencies]
mc-crypto-digestible = { path = "../../digestible" }
Expand Down
Loading

0 comments on commit 17c6328

Please sign in to comment.