Skip to content

Commit

Permalink
consensus: Refactor/Rename round_ctx into StepVotesRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk committed Oct 13, 2023
1 parent df539c1 commit f45e193
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 163 deletions.
18 changes: 13 additions & 5 deletions consensus/src/execution_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub struct IterationCtx<DB: Database> {
round: u64,
iter: u8,

// Stores any committee already generated in the execution of any iteration
// of current round
/// Stores any committee already generated in the execution of any
/// iteration of current round
committees: HashMap<u8, Committee>,
}

Expand Down Expand Up @@ -114,10 +114,12 @@ impl<D: Database> IterationCtx<D> {
pub(crate) fn get_committee(&mut self, step: u8) -> Option<&Committee> {
self.committees.get(&step)
}
}

impl<DB: Database> Drop for IterationCtx<DB> {
fn drop(&mut self) {
pub(crate) fn on_iteration_begin(&mut self, iter: u8) {
self.iter = iter;
}

pub(crate) fn on_iteration_end(&mut self) {
debug!(
event = "iter completed",
len = self.join_set.len(),
Expand All @@ -128,6 +130,12 @@ impl<DB: Database> Drop for IterationCtx<DB> {
}
}

impl<DB: Database> Drop for IterationCtx<DB> {
fn drop(&mut self) {
self.on_iteration_end();
}
}

/// ExecutionCtx encapsulates all data needed by a single step to be fully
/// executed.
pub struct ExecutionCtx<'a, DB: Database, T> {
Expand Down
23 changes: 13 additions & 10 deletions consensus/src/firststep/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Arc;
use crate::aggregator::Aggregator;
use crate::commons::{ConsensusError, Database, RoundUpdate};
use crate::msg_handler::{HandleMsgOutput, MsgHandler};
use crate::round_ctx::SafeRoundCtx;
use crate::step_votes_reg::{SafeStepVotesRegistry, SvType};
use async_trait::async_trait;
use node_data::ledger;
use node_data::ledger::{Block, StepVotes};
Expand Down Expand Up @@ -47,7 +47,7 @@ fn final_result_with_timeout(
}

pub struct Reduction<DB: Database> {
round_ctx: SafeRoundCtx,
sv_registry: SafeStepVotesRegistry,

pub(crate) db: Arc<Mutex<DB>>,
pub(crate) aggr: Aggregator,
Expand All @@ -56,9 +56,12 @@ pub struct Reduction<DB: Database> {
}

impl<DB: Database> Reduction<DB> {
pub(crate) fn new(db: Arc<Mutex<DB>>, round_ctx: SafeRoundCtx) -> Self {
pub(crate) fn new(
db: Arc<Mutex<DB>>,
sv_registry: SafeStepVotesRegistry,
) -> Self {
Self {
round_ctx,
sv_registry,
db,
aggr: Aggregator::default(),
candidate: Block::default(),
Expand Down Expand Up @@ -115,12 +118,12 @@ impl<D: Database> MsgHandler<Message> for Reduction<D> {
{
// Record result in global round registry of all non-Nil step_votes
if hash != [0u8; 32] {
if let Some(m) = self
.round_ctx
.lock()
.await
.add_step_votes(step, hash, sv, true)
{
if let Some(m) = self.sv_registry.lock().await.add_step_votes(
step,
hash,
sv,
SvType::FirstReduction,
) {
return Ok(HandleMsgOutput::FinalResult(m));
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ mod firststep;
mod msg_handler;
mod phase;
mod queue;
mod round_ctx;
mod secondstep;
mod selection;
mod step_votes_reg;

pub mod merkle;

Expand Down
137 changes: 0 additions & 137 deletions consensus/src/round_ctx.rs

This file was deleted.

12 changes: 6 additions & 6 deletions consensus/src/secondstep/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::commons::{ConsensusError, RoundUpdate};
use crate::msg_handler::{HandleMsgOutput, MsgHandler};
use crate::round_ctx::SafeRoundCtx;
use crate::step_votes_reg::{SafeStepVotesRegistry, SvType};
use async_trait::async_trait;
use node_data::ledger;
use node_data::ledger::{Hash, Signature, StepVotes};
Expand All @@ -18,7 +18,7 @@ use node_data::message::{payload, Message, Payload, Topics};
use crate::user::committee::Committee;

pub struct Reduction {
pub(crate) round_ctx: SafeRoundCtx,
pub(crate) sv_registry: SafeStepVotesRegistry,

pub(crate) aggregator: Aggregator,
pub(crate) first_step_votes: StepVotes,
Expand Down Expand Up @@ -69,11 +69,11 @@ impl MsgHandler<Message> for Reduction {
{
if block_hash != [0u8; 32] {
// Record result in global round results registry
if let Some(m) = self.round_ctx.lock().await.add_step_votes(
if let Some(m) = self.sv_registry.lock().await.add_step_votes(
step,
block_hash,
second_step_votes,
false,
SvType::SecondReduction,
) {
return Ok(HandleMsgOutput::FinalResult(m));
}
Expand Down Expand Up @@ -105,9 +105,9 @@ impl MsgHandler<Message> for Reduction {
}

impl Reduction {
pub(crate) fn new(round_ctx: SafeRoundCtx) -> Self {
pub(crate) fn new(sv_registry: SafeStepVotesRegistry) -> Self {
Self {
round_ctx,
sv_registry,
aggregator: Default::default(),
first_step_votes: Default::default(),
curr_step: 0,
Expand Down
11 changes: 7 additions & 4 deletions consensus/src/selection/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::commons::{ConsensusError, Database, RoundUpdate};
use crate::merkle::merkle_root;
use crate::msg_handler::{HandleMsgOutput, MsgHandler};
use crate::round_ctx::SafeRoundCtx;
use crate::step_votes_reg::SafeStepVotesRegistry;
use crate::user::committee::Committee;
use async_trait::async_trait;

Expand All @@ -17,7 +17,7 @@ use tokio::sync::Mutex;

pub struct Selection<D: Database> {
pub(crate) db: Arc<Mutex<D>>,
pub(crate) _round_ctx: SafeRoundCtx,
pub(crate) _sv_registry: SafeStepVotesRegistry,
}

#[async_trait]
Expand Down Expand Up @@ -67,10 +67,13 @@ impl<D: Database> MsgHandler<Message> for Selection<D> {
}

impl<D: Database> Selection<D> {
pub(crate) fn new(db: Arc<Mutex<D>>, round_ctx: SafeRoundCtx) -> Self {
pub(crate) fn new(
db: Arc<Mutex<D>>,
sv_registry: SafeStepVotesRegistry,
) -> Self {
Self {
db,
_round_ctx: round_ctx,
_sv_registry: sv_registry,
}
}

Expand Down
Loading

0 comments on commit f45e193

Please sign in to comment.