Skip to content

Commit

Permalink
Improve logging for Tendermint State Update
Browse files Browse the repository at this point in the history
  • Loading branch information
paberr authored and jsdanielh committed Nov 26, 2024
1 parent 40fb803 commit cea095c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
13 changes: 13 additions & 0 deletions validator/src/aggregation/tendermint/contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ impl std::fmt::Debug for TendermintContribution {
}
}

impl std::fmt::Display for TendermintContribution {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut debug = f.debug_map();
// Use short string for hashes.
debug.entries(
self.contributions
.iter()
.map(|(v, m)| (v.as_ref().map(|hash| hash.to_short_str()), &m.signers)),
);
debug.finish()
}
}

impl TendermintContribution {
pub(crate) fn from_vote(
vote: TendermintVote,
Expand Down
46 changes: 43 additions & 3 deletions validator/src/aggregation/tendermint/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::BTreeMap, fmt::Debug};
use std::{
collections::BTreeMap,
fmt::{Debug, Display},
};

use nimiq_block::{MacroBody, MacroHeader};
use nimiq_database_value_derive::DbSerializable;
Expand Down Expand Up @@ -35,13 +38,50 @@ impl Debug for MacroState {
dbg.field("step", &self.step);
dbg.field("locked", &self.locked);
dbg.field("valid", &self.valid);
dbg.field("best_votes", &self.best_votes);
dbg.field("votes", &self.votes);
// Wrap the following fields to clean up the debug representation.
dbg.field("best_votes", &BestVotes(&self.best_votes));
dbg.field("votes", &Votes(&self.votes));

dbg.finish()
}
}

/// A simple wrapper to use `Display` instead of `Debug`.
struct DisplayWrapper<'a, T>(&'a T);
impl<'a, T> Debug for DisplayWrapper<'a, T>
where
T: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Use Display instead.
Display::fmt(&self.0, f)
}
}

struct BestVotes<'a>(&'a BTreeMap<(u32, Step), TendermintContribution>);
impl<'a> Debug for BestVotes<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Use the `Display` implementation for `TendermintContribution` instead.
f.debug_map()
.entries(self.0.iter().map(|(k, v)| (k, DisplayWrapper(v))))
.finish()
}
}

struct Votes<'a>(&'a BTreeMap<(u32, Step), Option<Blake2sHash>>);
impl<'a> Debug for Votes<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Use short representations for the hashes instead.
f.debug_map()
.entries(
self.0
.iter()
.map(|(k, v)| (k, v.as_ref().map(|hash| hash.to_short_str()))),
)
.finish()
}
}

impl MacroState {
pub fn from_tendermint_state<TValidatorNetwork>(
block_number: u32,
Expand Down
2 changes: 1 addition & 1 deletion validator/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ where
// In case of a new state update we need to store the new version of it disregarding
// any old state which potentially still lingers.
MappedReturn::Update(update) => {
trace!(?update, "Tendermint state update");
debug!(?update, "Tendermint state update");

let expected_block_number = self.blockchain.read().block_number() + 1;
if expected_block_number != update.block_number {
Expand Down

0 comments on commit cea095c

Please sign in to comment.