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

vote: Complete vote keeper #39

Merged
merged 18 commits into from
Nov 7, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move dealing with skip threshold into VoteKeeper
romac committed Nov 2, 2023
commit 61954091e0c44e52cb92011404aef23f6c629809
2 changes: 1 addition & 1 deletion Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ where
private_key: PrivateKey<Ctx>,
address: Ctx::Address,
) -> Self {
let votes = VoteKeeper::new(height.clone(), validator_set.total_voting_power());
let votes = VoteKeeper::new(validator_set.total_voting_power());

Self {
height,
10 changes: 5 additions & 5 deletions Code/test/tests/vote_keeper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use malachite_common::Round;
use malachite_vote::keeper::{Message, VoteKeeper};

use malachite_test::{Address, Height, TestContext, ValueId, Vote};
use malachite_test::{Address, TestContext, ValueId, Vote};

const ADDRESS1: Address = Address::new([41; 20]);
const ADDRESS2: Address = Address::new([42; 20]);
@@ -10,7 +10,7 @@ const ADDRESS4: Address = Address::new([44; 20]);

#[test]
fn prevote_apply_nil() {
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(Height::new(1), 3);
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(3);

let vote = Vote::new_prevote(Round::new(0), None, ADDRESS1);
let msg = keeper.apply_vote(vote.clone(), 1);
@@ -27,7 +27,7 @@ fn prevote_apply_nil() {

#[test]
fn precommit_apply_nil() {
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(Height::new(1), 3);
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(3);

let vote = Vote::new_precommit(Round::new(0), None, ADDRESS1);
let msg = keeper.apply_vote(vote.clone(), 1);
@@ -44,7 +44,7 @@ fn precommit_apply_nil() {

#[test]
fn prevote_apply_single_value() {
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(Height::new(1), 4);
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(4);

let v = ValueId::new(1);
let val = Some(v);
@@ -68,7 +68,7 @@ fn prevote_apply_single_value() {

#[test]
fn precommit_apply_single_value() {
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(Height::new(1), 4);
let mut keeper: VoteKeeper<TestContext> = VoteKeeper::new(4);

let v = ValueId::new(1);
let val = Some(v);
72 changes: 46 additions & 26 deletions Code/vote/src/keeper.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use crate::round_weights::RoundWeights;
use crate::{Threshold, Weight};

/// Messages emitted by the vote keeper
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Message<Value> {
PolkaAny,
PolkaNil,
@@ -46,7 +46,7 @@ pub struct VoteKeeper<Ctx>
where
Ctx: Context,
{
height: Ctx::Height,
// height: Ctx::Height,
total_weight: Weight,
per_round: BTreeMap<Round, PerRound<Ctx>>,
}
@@ -55,9 +55,9 @@ impl<Ctx> VoteKeeper<Ctx>
where
Ctx: Context,
{
pub fn new(height: Ctx::Height, total_weight: Weight) -> Self {
pub fn new(/* height: Ctx::Height, */ total_weight: Weight) -> Self {
VoteKeeper {
height,
// height,
total_weight,
per_round: BTreeMap::new(),
}
@@ -70,16 +70,32 @@ where
.entry(vote.round())
.or_insert_with(|| PerRound::new(self.total_weight));

let vote_type = vote.vote_type();

let threshold = round.votes.add_vote(
vote_type,
vote.vote_type(),
vote.validator_address().clone(),
vote.take_value(),
vote.value().cloned(),
weight,
);

Self::to_message(vote_type, threshold)
round
.addresses_weights
.set_once(vote.validator_address().clone(), weight);

let msg = threshold_to_message(vote.vote_type(), threshold)?;

let final_msg = if !round.emitted_msgs.contains(&msg) {
Some(msg)
} else if Self::skip_round(round, self.total_weight) {
Some(Message::SkipRound)
} else {
None
};

if let Some(final_msg) = &final_msg {
round.emitted_msgs.insert(final_msg.clone());
}

final_msg
}

/// Check if a threshold is met, ie. if we have a quorum for that threshold.
@@ -94,23 +110,27 @@ where
})
}

/// Map a vote type and a threshold to a state machine event.
fn to_message(
typ: VoteType,
threshold: Threshold<ValueId<Ctx>>,
) -> Option<Message<ValueId<Ctx>>> {
match (typ, threshold) {
(_, Threshold::Unreached) => None,
(_, Threshold::Skip) => Some(Message::SkipRound),

(VoteType::Prevote, Threshold::Any) => Some(Message::PolkaAny),
(VoteType::Prevote, Threshold::Nil) => Some(Message::PolkaNil),
(VoteType::Prevote, Threshold::Value(v)) => Some(Message::PolkaValue(v)),

(VoteType::Precommit, Threshold::Any) => Some(Message::PrecommitAny),
(VoteType::Precommit, Threshold::Nil) => Some(Message::PrecommitAny),
(VoteType::Precommit, Threshold::Value(v)) => Some(Message::PrecommitValue(v)),
}
fn skip_round(round: &mut PerRound<Ctx>, total_weight: Weight) -> bool {
round.emitted_msgs.is_empty() && is_skip(round.addresses_weights.total(), total_weight)
}
}

/// Map a vote type and a threshold to a state machine event.
fn threshold_to_message<Value>(
typ: VoteType,
threshold: Threshold<Value>,
) -> Option<Message<Value>> {
match (typ, threshold) {
(_, Threshold::Unreached) => None,
(_, Threshold::Skip) => Some(Message::SkipRound),

(VoteType::Prevote, Threshold::Any) => Some(Message::PolkaAny),
(VoteType::Prevote, Threshold::Nil) => Some(Message::PolkaNil),
(VoteType::Prevote, Threshold::Value(v)) => Some(Message::PolkaValue(v)),

(VoteType::Precommit, Threshold::Any) => Some(Message::PrecommitAny),
(VoteType::Precommit, Threshold::Nil) => Some(Message::PrecommitAny),
(VoteType::Precommit, Threshold::Value(v)) => Some(Message::PrecommitValue(v)),
}
}

2 changes: 1 addition & 1 deletion Code/vote/src/round_weights.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ impl<Address> RoundWeights<Address> {
}
}

pub fn add(&mut self, address: Address, weight: Weight)
pub fn set_once(&mut self, address: Address, weight: Weight)
where
Address: Ord,
{