From e179f02352548af4c3b55ba4391eb01eb92a5742 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Wed, 22 Nov 2023 12:30:49 +0100 Subject: [PATCH] use rust i64 over BigInt --- Code/itf/Cargo.toml | 1 + Code/itf/src/votekeeper.rs | 22 +++++++-- Code/itf/tests/votekeeper/runner.rs | 70 ++++++----------------------- Code/itf/tests/votekeeper/utils.rs | 5 +-- 4 files changed, 35 insertions(+), 63 deletions(-) diff --git a/Code/itf/Cargo.toml b/Code/itf/Cargo.toml index 246054fee..d19698371 100644 --- a/Code/itf/Cargo.toml +++ b/Code/itf/Cargo.toml @@ -17,3 +17,4 @@ malachite-test = { version = "0.1.0", path = "../test" } num-bigint = { version = "0.4", features = ["serde"] } rstest = { version = "0.18.2", default-features = false } serde = { workspace = true, features = ["derive"] } +serde_with = { version = "3.4.0" } diff --git a/Code/itf/src/votekeeper.rs b/Code/itf/src/votekeeper.rs index a690f9c6f..ed6cf509e 100644 --- a/Code/itf/src/votekeeper.rs +++ b/Code/itf/src/votekeeper.rs @@ -1,11 +1,12 @@ -use num_bigint::BigInt; +use itf::de::Integer; +use serde_with::{As, Same}; use std::collections::{HashMap, HashSet}; use serde::Deserialize; -pub type Height = BigInt; -pub type Weight = BigInt; -pub type Round = BigInt; +pub type Height = i64; +pub type Weight = i64; +pub type Round = i64; pub type Address = String; pub type Value = String; pub type VoteType = String; @@ -13,16 +14,22 @@ pub type VoteType = String; #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Bookkeeper { + #[serde(with = "As::")] pub height: Height, + #[serde(with = "As::")] pub current_round: Round, + #[serde(with = "As::")] pub total_weight: Weight, + #[serde(with = "As::>")] pub rounds: HashMap, } #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] pub struct Vote { pub typ: VoteType, + #[serde(with = "As::")] pub height: Height, + #[serde(with = "As::")] pub round: Round, pub value: Value, pub address: Address, @@ -31,24 +38,30 @@ pub struct Vote { #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "camelCase")] pub struct RoundVotes { + #[serde(with = "As::")] pub height: Height, + #[serde(with = "As::")] pub round: Round, pub prevotes: VoteCount, pub precommits: VoteCount, pub emitted_events: HashSet, + #[serde(with = "As::>")] pub votes_addresses_weights: HashMap, } #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "camelCase")] pub struct VoteCount { + #[serde(with = "As::")] pub total_weight: Weight, + #[serde(with = "As::>")] pub values_weights: HashMap, pub votes_addresses: HashSet
, } #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)] pub struct ExecutorEvent { + #[serde(with = "As::")] pub round: Round, pub name: String, pub value: Value, @@ -61,5 +74,6 @@ pub struct State { #[serde(rename = "voteBookkeeperTest::voteBookkeeperSM::lastEmitted")] pub last_emitted: ExecutorEvent, #[serde(rename = "voteBookkeeperTest::voteBookkeeperSM::weightedVote")] + #[serde(with = "As::<(Same, Integer)>")] pub weighted_vote: (Vote, Weight), } diff --git a/Code/itf/tests/votekeeper/runner.rs b/Code/itf/tests/votekeeper/runner.rs index 442d58162..7588365ff 100644 --- a/Code/itf/tests/votekeeper/runner.rs +++ b/Code/itf/tests/votekeeper/runner.rs @@ -1,6 +1,5 @@ use std::collections::HashMap; -use num_bigint::BigInt; use rand::rngs::StdRng; use rand::SeedableRng; @@ -30,12 +29,7 @@ impl ItfRunner for VoteKeeperRunner { fn init(&mut self, expected: &Self::ExpectedState) -> Result { // Initialize VoteKeeper from the initial total_weight from the first state in the model. Ok(VoteKeeper::new( - expected - .bookkeeper - .total_weight - .clone() - .try_into() - .expect("integer overflow"), + expected.bookkeeper.total_weight as u64, ThresholdParams::default(), )) } @@ -47,20 +41,8 @@ impl ItfRunner for VoteKeeperRunner { ) -> Result { // Build step to execute. let (input_vote, weight) = &expected.weighted_vote; - let round = Round::new( - input_vote - .round - .clone() - .try_into() - .expect("integer overflow"), - ); - let height = Height::new( - input_vote - .height - .clone() - .try_into() - .expect("integer overflow"), - ); + let round = Round::new(input_vote.round); + let height = Height::new(input_vote.height as u64); let value = value_from_model(&input_vote.value); let address = self.address_map.get(input_vote.address.as_str()).unwrap(); let vote = match input_vote.typ.as_str() { @@ -73,21 +55,10 @@ impl ItfRunner for VoteKeeperRunner { input_vote.typ, round, value, input_vote.address, weight ); - let current_round = Round::new( - expected - .bookkeeper - .current_round - .clone() - .try_into() - .expect("integer overflow"), - ); + let current_round = Round::new(expected.bookkeeper.current_round); // Execute step. - Ok(actual.apply_vote( - vote, - weight.try_into().expect("integer overflow"), - current_round, - )) + Ok(actual.apply_vote(vote, *weight as u64, current_round)) } fn result_invariant( @@ -120,16 +91,7 @@ impl ItfRunner for VoteKeeperRunner { } Message::SkipRound(round) => { assert_eq!(expected_result.name, "Skip"); - assert_eq!( - &Round::new( - expected_result - .round - .clone() - .try_into() - .expect("integer overflow") - ), - round - ); + assert_eq!(&Round::new(expected_result.round), round); } msg => assert_eq!(expected_result.name, format!("{msg:?}")), }, @@ -149,20 +111,17 @@ impl ItfRunner for VoteKeeperRunner { let expected_state = &expected.bookkeeper; assert_eq!( - BigInt::from(*actual_state.total_weight()), - expected_state.total_weight, + actual_state.total_weight(), + &(expected_state.total_weight as u64), "total_weight for the current height" ); assert_eq!(actual_state.per_round().len(), expected_state.rounds.len()); - for (round, expected_round) in &expected_state.rounds { + for (&round, expected_round) in &expected_state.rounds { // doesn't check for current Height and Round - let actual_round = actual_state - .per_round() - .get(&Round::new(round.try_into().expect("integer overflow"))) - .unwrap(); + let actual_round = actual_state.per_round().get(&Round::new(round)).unwrap(); let expected_events = &expected_round.emitted_events; let actual_events = actual_round.emitted_msgs(); @@ -199,12 +158,11 @@ impl ItfRunner for VoteKeeperRunner { let actual_addresses_weights = &actual_round.addresses_weights().get_inner(); for address in expected_addresses_weights.keys() { assert_eq!( - actual_addresses_weights - .get(self.address_map.get(address).unwrap()) - .cloned() - .map(BigInt::from) + actual_addresses_weights.get(self.address_map.get(address).unwrap()), + expected_addresses_weights + .get(address) + .map(|&w| w as u64) .as_ref(), - expected_addresses_weights.get(address), "weight for address {address:?}" ); } diff --git a/Code/itf/tests/votekeeper/utils.rs b/Code/itf/tests/votekeeper/utils.rs index ce0cf0c3a..91504697d 100644 --- a/Code/itf/tests/votekeeper/utils.rs +++ b/Code/itf/tests/votekeeper/utils.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use malachite_itf::votekeeper::Value; use malachite_test::{Address, ValueId}; -use num_bigint::BigInt; pub const ADDRESSES: [&str; 3] = ["alice", "bob", "john"]; pub const NIL_VALUE: &str = "nil"; @@ -32,8 +31,8 @@ pub fn check_votes( for value in expected_values_weights.keys() { assert_eq!( - &BigInt::from(actual_values_weights.get(&value_from_model(value))), - expected_values_weights.get(value).unwrap(), + actual_values_weights.get(&value_from_model(value)), + *expected_values_weights.get(value).unwrap() as u64, "weight for value {value:?}" ); }