Skip to content

Commit

Permalink
use rust i64 over BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Nov 22, 2023
1 parent f82a8a5 commit e179f02
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 63 deletions.
1 change: 1 addition & 0 deletions Code/itf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
22 changes: 18 additions & 4 deletions Code/itf/src/votekeeper.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
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;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Bookkeeper {
#[serde(with = "As::<Integer>")]
pub height: Height,
#[serde(with = "As::<Integer>")]
pub current_round: Round,
#[serde(with = "As::<Integer>")]
pub total_weight: Weight,
#[serde(with = "As::<HashMap<Integer, Same>>")]
pub rounds: HashMap<Round, RoundVotes>,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct Vote {
pub typ: VoteType,
#[serde(with = "As::<Integer>")]
pub height: Height,
#[serde(with = "As::<Integer>")]
pub round: Round,
pub value: Value,
pub address: Address,
Expand All @@ -31,24 +38,30 @@ pub struct Vote {
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RoundVotes {
#[serde(with = "As::<Integer>")]
pub height: Height,
#[serde(with = "As::<Integer>")]
pub round: Round,
pub prevotes: VoteCount,
pub precommits: VoteCount,
pub emitted_events: HashSet<ExecutorEvent>,
#[serde(with = "As::<HashMap<Same, Integer>>")]
pub votes_addresses_weights: HashMap<Address, Weight>,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VoteCount {
#[serde(with = "As::<Integer>")]
pub total_weight: Weight,
#[serde(with = "As::<HashMap<Same, Integer>>")]
pub values_weights: HashMap<Value, Weight>,
pub votes_addresses: HashSet<Address>,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)]
pub struct ExecutorEvent {
#[serde(with = "As::<Integer>")]
pub round: Round,
pub name: String,
pub value: Value,
Expand All @@ -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),
}
70 changes: 14 additions & 56 deletions Code/itf/tests/votekeeper/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use num_bigint::BigInt;
use rand::rngs::StdRng;
use rand::SeedableRng;

Expand Down Expand Up @@ -30,12 +29,7 @@ impl ItfRunner for VoteKeeperRunner {
fn init(&mut self, expected: &Self::ExpectedState) -> Result<Self::ActualState, Self::Error> {
// 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(),
))
}
Expand All @@ -47,20 +41,8 @@ impl ItfRunner for VoteKeeperRunner {
) -> Result<Self::Result, Self::Error> {
// 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() {
Expand All @@ -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(
Expand Down Expand Up @@ -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:?}")),
},
Expand All @@ -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();
Expand Down Expand Up @@ -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:?}"
);
}
Expand Down
5 changes: 2 additions & 3 deletions Code/itf/tests/votekeeper/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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:?}"
);
}
Expand Down

0 comments on commit e179f02

Please sign in to comment.