Skip to content

Commit

Permalink
Feature-gated malachitebft-metrics in malachitebft-core-consensus crate
Browse files Browse the repository at this point in the history
  • Loading branch information
OakenKnight committed Jan 14, 2025
1 parent d10e840 commit 9d1ea03
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 45 deletions.
4 changes: 2 additions & 2 deletions code/crates/core-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ readme = "../../../README.md"
all-features = true

[features]
std = []
std = ["malachitebft-metrics"]
debug = ["std", "malachitebft-core-driver/debug"]

[dependencies]
malachitebft-core-types.workspace = true
malachitebft-core-driver.workspace = true
malachitebft-metrics.workspace = true
malachitebft-metrics = { workspace = true, optional = true }
malachitebft-peer.workspace = true

async-recursion = { workspace = true }
Expand Down
15 changes: 12 additions & 3 deletions code/crates/core-consensus/src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;

#[cfg(not(feature = "std"))]
use crate::types::Metrics;
mod decide;
mod driver;
mod proposal;
Expand Down Expand Up @@ -33,14 +34,22 @@ pub async fn handle<Ctx>(
where
Ctx: Context,
{
handle_input(&co, state, metrics, input).await
#[cfg(feature = "std")]
{
handle_input(&co, state, Some(metrics), input).await
}

#[cfg(not(feature = "std"))]
{
handle_input(&co, state, None, input).await
}
}

#[async_recursion]
async fn handle_input<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
input: Input<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand Down
14 changes: 9 additions & 5 deletions code/crates/core-consensus/src/handle/decide.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::prelude::*;

#[cfg(not(feature = "std"))]
use crate::types::Metrics;
pub async fn decide<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
consensus_round: Round,
proposal: SignedProposal<Ctx>,
) -> Result<(), Error<Ctx>>
Expand All @@ -21,20 +22,23 @@ where
state.remove_full_proposals(height);

// Update metrics
#[cfg(feature = "std")]
{
// We are only interested in consensus time for round 0, ie. in the happy path.
if consensus_round == Round::new(0) {
metrics.consensus_end();
metrics.unwrap().consensus_end();
}

metrics.block_end();
metrics.finalized_blocks.inc();
metrics.unwrap().block_end();
metrics.unwrap().finalized_blocks.inc();

metrics
.unwrap()
.consensus_round
.observe(consensus_round.as_i64() as f64);

metrics
.unwrap()
.proposal_round
.observe(proposal_round.as_i64() as f64);
}
Expand Down
21 changes: 12 additions & 9 deletions code/crates/core-consensus/src/handle/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ use crate::handle::signature::sign_proposal;
use crate::handle::signature::sign_vote;
use crate::handle::vote::on_vote;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::types::SignedConsensusMsg;
use crate::util::pretty::PrettyVal;
use malachitebft_core_driver::Input as DriverInput;
use malachitebft_core_driver::Output as DriverOutput;

#[async_recursion]
pub async fn apply_driver_input<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
input: DriverInput<Ctx>,
) -> Result<(), Error<Ctx>>
where
Ctx: Context,
{
match &input {
DriverInput::NewRound(height, round, proposer) => {
metrics.round.set(round.as_i64());
#[cfg(feature = "std")]
metrics.unwrap().round.set(round.as_i64());

info!(%height, %round, %proposer, "Starting new round");
perform!(co, Effect::CancelAllTimeouts(Default::default()));
Expand Down Expand Up @@ -103,8 +105,11 @@ where
);
}
}
metrics.step_end(prev_step);
metrics.step_start(new_step);
#[cfg(feature = "std")]
{
metrics.unwrap().step_end(prev_step);
metrics.unwrap().step_start(new_step);
}
}

if prev_step != new_step {
Expand Down Expand Up @@ -152,7 +157,7 @@ where
async fn process_driver_outputs<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
outputs: Vec<DriverOutput<Ctx>>,
) -> Result<(), Error<Ctx>>
where
Expand All @@ -168,7 +173,7 @@ where
async fn process_driver_output<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
output: DriverOutput<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand All @@ -177,7 +182,6 @@ where
match output {
DriverOutput::NewRound(height, round) => {
let proposer = state.get_proposer(height, round);

apply_driver_input(
co,
state,
Expand Down Expand Up @@ -209,7 +213,6 @@ where
)
);
}

on_proposal(co, state, metrics, signed_proposal.clone()).await?;

// Proposal messages should not be broadcasted if they are implicit,
Expand Down
5 changes: 3 additions & 2 deletions code/crates/core-consensus/src/handle/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::handle::signature::verify_signature;
use crate::handle::validator_set::get_validator_set;
use crate::input::Input;
use crate::types::ConsensusMsg;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::util::pretty::PrettyProposal;
use crate::ProposedValue;
use crate::{prelude::*, SignedConsensusMsg};

pub async fn on_proposal<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
signed_proposal: SignedProposal<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand Down
8 changes: 5 additions & 3 deletions code/crates/core-consensus/src/handle/propose.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::types::{ProposedValue, ValueToPropose};

pub async fn on_propose<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
value: ValueToPropose<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand Down Expand Up @@ -37,8 +39,8 @@ where

return Ok(());
}

metrics.consensus_start();
#[cfg(feature = "std")]
metrics.unwrap().consensus_start();

state.store_value(&ProposedValue {
height,
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/handle/proposed_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::types::ProposedValue;
Expand All @@ -17,7 +19,7 @@ use super::signature::sign_proposal;
pub async fn on_proposed_value<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
proposed_value: ProposedValue<Ctx>,
origin: ValueOrigin,
) -> Result<(), Error<Ctx>>
Expand Down
23 changes: 13 additions & 10 deletions code/crates/core-consensus/src/handle/start_height.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::handle::handle_input;

pub async fn reset_and_start_height<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
height: Ctx::Height,
validator_set: Ctx::ValidatorSet,
) -> Result<(), Error<Ctx>>
Expand All @@ -15,8 +17,8 @@ where
{
perform!(co, Effect::CancelAllTimeouts(Default::default()));
perform!(co, Effect::ResetTimeouts(Default::default()));

metrics.step_end(state.driver.step());
#[cfg(feature = "std")]
metrics.unwrap().step_end(state.driver.step());

state.driver.move_to_height(height, validator_set);

Expand All @@ -29,19 +31,20 @@ where
pub async fn start_height<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
height: Ctx::Height,
) -> Result<(), Error<Ctx>>
where
Ctx: Context,
{
let round = Round::new(0);
info!(%height, "Starting new height");

metrics.block_start();
metrics.height.set(height.as_u64() as i64);
metrics.round.set(round.as_i64());

#[cfg(feature = "std")]
{
metrics.unwrap().block_start();
metrics.unwrap().height.set(height.as_u64() as i64);
metrics.unwrap().round.set(round.as_i64());
}
let proposer = state.get_proposer(height, round);

apply_driver_input(
Expand All @@ -60,7 +63,7 @@ where
async fn replay_pending_msgs<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
) -> Result<(), Error<Ctx>>
where
Ctx: Context,
Expand Down
7 changes: 5 additions & 2 deletions code/crates/core-consensus/src/handle/step_timeout.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_step_limit_timeout<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
round: Round,
) -> Result<(), Error<Ctx>>
where
Expand All @@ -17,7 +19,8 @@ where
co,
Effect::GetVoteSet(state.driver.height(), round, Default::default())
);
metrics.step_timeouts.inc();
#[cfg(feature = "std")]
metrics.unwrap().step_timeouts.inc();

if state.driver.step_is_prevote() {
perform!(
Expand Down
5 changes: 3 additions & 2 deletions code/crates/core-consensus/src/handle/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use crate::handle::driver::apply_driver_input;
use crate::handle::signature::verify_certificate;
use crate::handle::validator_set::get_validator_set;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_commit_certificate<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
certificate: CommitCertificate<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand All @@ -33,7 +35,6 @@ where
return Err(Error::InvalidCertificate(certificate, e));
}

// Go to Commit step via L49
apply_driver_input(
co,
state,
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/handle/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use crate::handle::decide::decide;
use crate::handle::driver::apply_driver_input;
use crate::handle::step_timeout::on_step_limit_timeout;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_timeout_elapsed<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
timeout: Timeout,
) -> Result<(), Error<Ctx>>
where
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/handle/vote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::{prelude::*, SignedConsensusMsg};

use crate::handle::driver::apply_driver_input;
Expand All @@ -10,7 +12,7 @@ use crate::util::pretty::PrettyVote;
pub async fn on_vote<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
signed_vote: SignedVote<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand Down
6 changes: 4 additions & 2 deletions code/crates/core-consensus/src/handle/vote_set.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::handle::vote::on_vote;
use crate::input::RequestId;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_vote_set_request<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
_metrics: &Metrics,
_metrics: Option<&Metrics>,
request_id: RequestId,
height: Ctx::Height,
round: Round,
Expand All @@ -32,7 +34,7 @@ where
pub async fn on_vote_set_response<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
metrics: &Metrics,
metrics: Option<&Metrics>,
response: VoteSet<Ctx>,
) -> Result<(), Error<Ctx>>
where
Expand Down
1 change: 0 additions & 1 deletion code/crates/core-consensus/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
macro_rules! process {
(input: $input:expr, state: $state:expr, metrics: $metrics:expr, with: $effect:ident => $handle:expr) => {{
let mut gen = $crate::gen::Gen::new(|co| $crate::handle(co, $state, $metrics, $input));

let mut co_result = gen.resume_with($crate::Resume::Start);

loop {
Expand Down
Loading

0 comments on commit 9d1ea03

Please sign in to comment.