From 0a16726cdb88a984f575cc635947dbfc9cbc4e80 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi <106849+romac@users.noreply.github.com> Date: Thu, 19 Oct 2023 11:20:19 +0200 Subject: [PATCH] Move common code to common crate --- Code/.gitkeep | 0 Code/Cargo.toml | 5 +- Code/common/Cargo.toml | 8 +++ Code/common/src/lib.rs | 143 +++++++++++++++++++++++++++++++++++++++ Code/round-sm/Cargo.toml | 1 + Code/round-sm/src/lib.rs | 117 +------------------------------- 6 files changed, 158 insertions(+), 116 deletions(-) delete mode 100644 Code/.gitkeep create mode 100644 Code/common/Cargo.toml create mode 100644 Code/common/src/lib.rs diff --git a/Code/.gitkeep b/Code/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/Code/Cargo.toml b/Code/Cargo.toml index e444397d9..f59016dfe 100644 --- a/Code/Cargo.toml +++ b/Code/Cargo.toml @@ -1,4 +1,7 @@ [workspace] resolver = "2" -members = [] +members = [ + "common", + "round-sm" +] diff --git a/Code/common/Cargo.toml b/Code/common/Cargo.toml new file mode 100644 index 000000000..b7723d9c7 --- /dev/null +++ b/Code/common/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "common" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/Code/common/src/lib.rs b/Code/common/src/lib.rs new file mode 100644 index 000000000..04e29556a --- /dev/null +++ b/Code/common/src/lib.rs @@ -0,0 +1,143 @@ +/// A blockchain height +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] +pub struct Height(u64); + +impl Height { + pub fn as_u64(&self) -> u64 { + self.0 + } +} + +/// A round number, ie. a natural number +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum Round { + /// No round + None, + + /// Some round + Some(i64), +} + +impl Round { + pub fn new(round: i64) -> Self { + if round < 0 { + Self::None + } else { + Self::Some(round) + } + } + + pub fn as_i64(&self) -> i64 { + match self { + Round::None => -1, + Round::Some(r) => *r, + } + } + + pub fn is_defined(&self) -> bool { + matches!(self, Round::Some(_)) + } + + pub fn increment(&self) -> Round { + match self { + Round::None => Round::new(0), + Round::Some(r) => Round::new(r + 1), + } + } +} + +impl PartialOrd for Round { + fn partial_cmp(&self, other: &Self) -> Option { + self.as_i64().partial_cmp(&other.as_i64()) + } +} + +/// The value to decide on +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Value(u64); + +impl Value { + pub fn as_u64(&self) -> u64 { + self.0 + } +} + +/// A proposal for a value in a round +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Proposal { + pub round: Round, + pub value: Value, + pub polka_round: Round, +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum VoteType { + Prevote, + Precommit, +} + +/// A vote for a value in a round +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Vote { + pub typ: VoteType, + pub round: Round, + pub value: Option, +} + +impl Vote { + pub fn new_prevote(round: Round, value: Option) -> Self { + Self { + typ: VoteType::Prevote, + round, + value, + } + } + + pub fn new_precommit(round: Round, value: Option) -> Self { + Self { + typ: VoteType::Precommit, + round, + value, + } + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum TimeoutStep { + Propose, + Prevote, + Precommit, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Timeout { + pub round: Round, + pub step: TimeoutStep, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_round() { + // Test Round::new() + assert_eq!(Round::new(-42), Round::None); + assert_eq!(Round::new(-1), Round::None); + assert_eq!(Round::new(0), Round::Some(0)); + assert_eq!(Round::new(1), Round::Some(1)); + assert_eq!(Round::new(2), Round::Some(2)); + + // Test Round::as_i64() + assert_eq!(Round::None.as_i64(), -1); + assert_eq!(Round::Some(0).as_i64(), 0); + assert_eq!(Round::Some(1).as_i64(), 1); + assert_eq!(Round::Some(2).as_i64(), 2); + + // Test Round::is_defined() + assert!(!Round::None.is_defined()); + assert!(Round::Some(0).is_defined()); + assert!(Round::Some(1).is_defined()); + assert!(Round::Some(2).is_defined()); + } +} diff --git a/Code/round-sm/Cargo.toml b/Code/round-sm/Cargo.toml index c62f72d26..f65dc4f6e 100644 --- a/Code/round-sm/Cargo.toml +++ b/Code/round-sm/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +common = { version = "0.1.0", path = "../common" } diff --git a/Code/round-sm/src/lib.rs b/Code/round-sm/src/lib.rs index 00ca0ce78..40ed5a8b0 100644 --- a/Code/round-sm/src/lib.rs +++ b/Code/round-sm/src/lib.rs @@ -1,119 +1,6 @@ +pub use common::*; + pub mod events; pub mod message; pub mod state; pub mod state_machine; - -/// A blockchain height -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] -pub struct Height(u64); - -impl Height { - pub fn as_u64(&self) -> u64 { - self.0 - } -} - -/// A round number, ie. a natural number -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum Round { - /// No round - None, - - /// Some round - Some(i64), -} - -impl Round { - pub fn new(round: i64) -> Self { - assert!(round >= 0); - - Round::Some(round) - } - - pub fn as_i64(&self) -> i64 { - match self { - Round::None => -1, - Round::Some(r) => *r, - } - } - - pub fn is_defined(&self) -> bool { - matches!(self, Round::Some(_)) - } - - pub fn increment(&self) -> Round { - match self { - Round::None => Round::new(0), - Round::Some(r) => Round::new(r + 1), - } - } -} - -impl PartialOrd for Round { - fn partial_cmp(&self, other: &Self) -> Option { - self.as_i64().partial_cmp(&other.as_i64()) - } -} - -/// The value to decide on -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Value(u64); - -impl Value { - pub fn as_u64(&self) -> u64 { - self.0 - } -} - -/// A proposal for a value in a round -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Proposal { - pub round: Round, - pub value: Value, - pub polka_round: Round, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum VoteType { - Prevote, - Precommit, -} - -/// A vote for a value in a round -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Vote { - pub typ: VoteType, - pub round: Round, - pub value: Option, -} - -impl Vote { - pub fn new_prevote(round: Round, value: Option) -> Self { - Self { - typ: VoteType::Prevote, - round, - value, - } - } - - pub fn new_precommit(round: Round, value: Option) -> Self { - Self { - typ: VoteType::Precommit, - round, - value, - } - } -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum TimeoutStep { - Propose, - Prevote, - Precommit, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Timeout { - pub round: Round, - pub step: TimeoutStep, -}