Skip to content

Commit

Permalink
Add some metrics for random beacon (MystenLabs#15450)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschran authored Dec 20, 2023
1 parent d719500 commit 499802d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion narwhal/.clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cognitive-complexity-threshold = 100
# types are used for safety encoding
type-complexity-threshold = 10000
# big constructors
too-many-arguments-threshold = 15
too-many-arguments-threshold = 20

disallowed-methods = [
{path = "anyhow", reason = "we prefer to use eyre"},
Expand Down
14 changes: 14 additions & 0 deletions narwhal/primary/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ pub struct PrimaryMetrics {
pub fetched_certificates_verified_directly: IntCounter,
// Total number of fetched certificates verified indirectly.
pub fetched_certificates_verified_indirectly: IntCounter,
/// The number of shares held by this node after the random beacon DKG protocol completed.
pub state_handler_random_beacon_dkg_num_shares: IntGauge,
/// The randomness round that currently in progress.
pub state_handler_current_randomness_round: IntGauge,
}

impl PrimaryMetrics {
Expand Down Expand Up @@ -555,6 +559,16 @@ impl PrimaryMetrics {
"Total number of fetched certificates verified indirectly.",
registry
).unwrap(),
state_handler_random_beacon_dkg_num_shares: register_int_gauge_with_registry!(
"state_handler_random_beacon_dkg_num_shares",
"The number of shares held by this node after the random beacon DKG protocol completed.",
registry
).unwrap(),
state_handler_current_randomness_round: register_int_gauge_with_registry!(
"state_handler_current_randomness_round",
"The randomness round that currently in progress.",
registry
).unwrap(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion narwhal/primary/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl Primary {
tx_headers,
tx_narwhal_round_updates,
rx_committed_own_headers,
node_metrics,
node_metrics.clone(),
leader_schedule.clone(),
);

Expand Down Expand Up @@ -544,6 +544,7 @@ impl Primary {
leader_schedule,
network,
randomness_store,
node_metrics,
);
handles.push(state_handler_handle);

Expand Down
22 changes: 22 additions & 0 deletions narwhal/primary/src/state_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::consensus::LeaderSchedule;
use crate::metrics::PrimaryMetrics;
use config::{AuthorityIdentifier, ChainIdentifier, Committee};
use crypto::{RandomnessPartialSignature, RandomnessPrivateKey};
use fastcrypto::encoding::{Encoding, Hex};
Expand Down Expand Up @@ -82,6 +83,7 @@ pub struct StateHandler {
// randomness round is incremented.
struct RandomnessState {
store: RandomnessStore,
metrics: Arc<PrimaryMetrics>,

// A channel to send system messages to the proposer.
tx_system_messages: Sender<SystemMessage>,
Expand Down Expand Up @@ -124,6 +126,7 @@ impl RandomnessState {
vss_key_output: Arc<OnceLock<PublicVssKey>>,
tx_system_messages: Sender<SystemMessage>,
store: RandomnessStore,
metrics: Arc<PrimaryMetrics>,
) -> Option<Self> {
if !protocol_config.random_beacon() {
info!("random beacon: disabled");
Expand Down Expand Up @@ -184,9 +187,20 @@ impl RandomnessState {
"random beacon: state initialized with authority_id={authority_id}, total_weight={total_weight}, t={t}, num_nodes={num_nodes}, oracle initial_prefix={prefix_str:?}",
);

// Load existing data from store.
let dkg_output = store.dkg_output();
if let Some(dkg_output) = &dkg_output {
metrics
.state_handler_random_beacon_dkg_num_shares
.set(dkg_output.shares.as_ref().map_or(0, |shares| shares.len()) as i64);
}
metrics
.state_handler_current_randomness_round
.set(store.randomness_round().0 as i64);

Some(Self {
store,
metrics,
tx_system_messages,
party,
has_sent_confirmation: false,
Expand All @@ -205,6 +219,9 @@ impl RandomnessState {
}

fn set_dkg_output(&mut self, output: dkg::Output<PkG, EncG>) {
self.metrics
.state_handler_random_beacon_dkg_num_shares
.set(output.shares.as_ref().map_or(0, |shares| shares.len()) as i64);
self.store.set_dkg_output(&output);
self.dkg_output = Some(output);
}
Expand Down Expand Up @@ -320,6 +337,9 @@ impl RandomnessState {
return;
}
debug!("random beacon: updating local randomness round to {round:?}");
self.metrics
.state_handler_current_randomness_round
.set(round.0 as i64);
self.store.set_randomness_round(round);
self.partial_sigs.retain(|&(r, _), _| r >= round);
self.send_partial_signatures().await;
Expand Down Expand Up @@ -525,6 +545,7 @@ impl StateHandler {
leader_schedule: LeaderSchedule,
network: anemo::Network,
randomness_store: RandomnessStore,
metrics: Arc<PrimaryMetrics>,
) -> JoinHandle<()> {
let state_handler = Self {
authority_id,
Expand All @@ -544,6 +565,7 @@ impl StateHandler {
vss_key_output,
tx_system_messages,
randomness_store,
metrics,
),
network,
};
Expand Down
7 changes: 7 additions & 0 deletions narwhal/primary/src/tests/state_handler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fastcrypto::{
traits::{KeyPair, ToFromBytes},
};

use prometheus::Registry;
use test_utils::CommitteeFixture;

#[tokio::test]
Expand All @@ -30,6 +31,7 @@ async fn dkg() {
for authority in fixture.authorities() {
let network = test_utils::test_network(authority.network_keypair(), authority.address());
let randomness_store = RandomnessStore::new_for_tests();
let metrics = Arc::new(PrimaryMetrics::new(&Registry::new()));
let vss_key_output = Arc::new(OnceLock::new());

let (tx_system_messages, rx_system_messages) = test_utils::test_channel!(1);
Expand Down Expand Up @@ -57,6 +59,7 @@ async fn dkg() {
vss_key_output.clone(),
tx_system_messages,
randomness_store,
metrics,
)
.unwrap();

Expand Down Expand Up @@ -139,6 +142,7 @@ async fn resume_from_store() {
let randomness_store = RandomnessStore::new_for_tests();
randomness_stores.push(randomness_store.clone());
let vss_key_output = Arc::new(OnceLock::new());
let metrics = Arc::new(PrimaryMetrics::new(&Registry::new()));

let (tx_system_messages, rx_system_messages) = test_utils::test_channel!(2);

Expand All @@ -165,6 +169,7 @@ async fn resume_from_store() {
vss_key_output.clone(),
tx_system_messages,
randomness_store,
metrics,
)
.unwrap();

Expand Down Expand Up @@ -218,6 +223,7 @@ async fn resume_from_store() {
for (i, authority) in fixture.authorities().enumerate() {
let network = test_utils::test_network(authority.network_keypair(), authority.address());
let (tx_system_messages, rx_system_messages) = test_utils::test_channel!(2);
let metrics = Arc::new(PrimaryMetrics::new(&Registry::new()));

let randomness_private_key = RandomnessPrivateKey::from(
fastcrypto::groups::bls12381::Scalar::from_byte_array(
Expand All @@ -242,6 +248,7 @@ async fn resume_from_store() {
vss_key_outputs[i].clone(),
tx_system_messages,
randomness_stores[i].clone(),
metrics,
)
.unwrap();
// called by StateHandler::run on restart
Expand Down

0 comments on commit 499802d

Please sign in to comment.