Skip to content

Commit

Permalink
Clear prevote/precommit timeout when we get 2/3+ votes for a value
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Feb 23, 2024
1 parent 363b6f5 commit 70a55cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ where
self.round_state.round
}

/// Return a reference to the votekeper
pub fn votes(&self) -> &VoteKeeper<Ctx> {
&self.vote_keeper
}

/// Return the proposer for the current round.
pub fn get_proposer(
&self,
Expand Down
4 changes: 3 additions & 1 deletion code/node/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use tracing::info;
mod cli;
use cli::Cli;

const VOTING_PWERS: [u64; 3] = [5, 20, 10];

#[tokio::main(flavor = "current_thread")]
pub async fn main() {
tracing_subscriber::fmt::init();

let args = Cli::from_env();

// Validators keys are deterministic and match the ones in the config file
let vs = make_validators([2, 3, 2]);
let vs = make_validators(VOTING_PWERS);

let config = std::fs::read_to_string("node/peers.toml").expect("Error: missing peers.toml");
let config = toml::from_str::<Config>(&config).expect("Error: invalid peers.toml");
Expand Down
37 changes: 34 additions & 3 deletions code/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use tokio::sync::{mpsc, oneshot};
use tracing::{debug, info, warn, Instrument};

use malachite_common::{
Context, Height, Proposal, Round, SignedProposal, SignedVote, Timeout, TimeoutStep, Vote,
VoteType,
Context, Height, NilOrVal, Proposal, Round, SignedProposal, SignedVote, Timeout, TimeoutStep,
Vote, VoteType,
};
use malachite_driver::{Driver, Input, Output, ProposerSelector, Validity};
use malachite_proto::{self as proto, Protobuf};
use malachite_vote::ThresholdParams;
use malachite_vote::{Threshold, ThresholdParams};

use crate::network::Msg as NetworkMsg;
use crate::network::{Network, PeerId};
Expand Down Expand Up @@ -195,8 +195,39 @@ where
Input::TimeoutElapsed(_) => (),
}

let check_threshold = if let Input::Vote(vote) = &input {
let round = Vote::<Ctx>::round(vote);
let value = Vote::<Ctx>::value(vote);

Some((vote.vote_type(), round, value.clone()))
} else {
None
};

let outputs = self.driver.process(input).unwrap();

// When we receive a vote, check if we've gotten +2/3 votes for the value we just received a vote for.
if let Some((vote_type, round, value)) = check_threshold {
let threshold = match value {
NilOrVal::Nil => Threshold::Nil,
NilOrVal::Val(value) => Threshold::Value(value),
};

if self
.driver
.votes()
.is_threshold_met(&round, vote_type, threshold.clone())
{
let timeout = match vote_type {
VoteType::Prevote => Timeout::prevote(round),
VoteType::Precommit => Timeout::precommit(round),
};

info!("Threshold met for {threshold:?} at round {round}, cancelling {timeout}");
self.timers.cancel_timeout(&timeout).await;
}
}

for output in outputs {
match self.process_output(output).await {
Next::None => (),
Expand Down

0 comments on commit 70a55cb

Please sign in to comment.