diff --git a/src/blockchain/fork_choice.cpp b/src/blockchain/fork_choice.cpp index 4def0d6c..3d795486 100644 --- a/src/blockchain/fork_choice.cpp +++ b/src/blockchain/fork_choice.cpp @@ -225,10 +225,14 @@ namespace lean { // Sign proposer attestation auto payload = sszHash(proposer_attestation); + auto timer = + metrics_->crypto_pq_signature_attestation_signing_time_seconds() + ->timer(); crypto::xmss::XmssSignature signature = xmss_provider_->sign( validator_keys_manifest_->currentNodeXmssKeypair().private_key, slot, payload); + timer.stop(); SignedBlockWithAttestation signed_block_with_attestation{ .message = { @@ -459,8 +463,15 @@ namespace lean { // - The signature was created at the correct epoch (slot) auto message = sszHash(attestation); Epoch epoch = attestation.data.slot; - if (not xmss_provider_->verify( - validator.pubkey, message, epoch, signature)) { + + auto timer = + metrics_->crypto_pq_signature_attestation_verification_time_seconds() + ->timer(); + bool verify_result = + xmss_provider_->verify(validator.pubkey, message, epoch, signature); + timer.stop(); + + if (not verify_result) { SL_WARN(logger_, "Attestation signature verification failed for validator {}", validator_id); @@ -633,8 +644,12 @@ namespace lean { auto payload = sszHash(attestation); crypto::xmss::XmssKeypair keypair = validator_keys_manifest_->currentNodeXmssKeypair(); + auto timer = + metrics_->crypto_pq_signature_attestation_signing_time_seconds() + ->timer(); crypto::xmss::XmssSignature signature = xmss_provider_->sign(keypair.private_key, current_slot, payload); + timer.stop(); SignedAttestation signed_attestation{.message = attestation, .signature = signature}; diff --git a/src/crypto/crypto_metrics.def b/src/crypto/crypto_metrics.def new file mode 100644 index 00000000..d7a401d0 --- /dev/null +++ b/src/crypto/crypto_metrics.def @@ -0,0 +1,49 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @brief Cryptography metrics definitions + * + * Format: + * METRIC_GAUGE(field_name, + * "prometheus_metric_name", + * "help_text") + * + * METRIC_GAUGE_LABELS(field_name, + * "prometheus_metric_name", + * "help_text", + * {"label1", "label2", ...}) + * + * METRIC_COUNTER(field_name, + * "prometheus_metric_name", + * "help_text") + * + * METRIC_COUNTER_LABELS(field_name, + * "prometheus_metric_name", + * "help_text", + * {"label1", "label2", ...}) + * + * METRIC_HISTOGRAM(field_name, + * "prometheus_metric_name", + * "help_text", + * {bucket1, bucket2, ...}) + * + * METRIC_HISTOGRAM_LABELS(field_name, + * "prometheus_metric_name", + * "help_text", + * {bucket1, bucket2, ...}, + * {"label1", "label2", ...}) + */ + +// PQ signatures +METRIC_HISTOGRAM(crypto_pq_signature_attestation_signing_time_seconds, + "lean_pq_signature_attestation_signing_time_seconds", + "Time taken to sign an attestation", + (0.005, 0.01, 0.025, 0.05, 0.1, 1)) +METRIC_HISTOGRAM(crypto_pq_signature_attestation_verification_time_seconds, + "lean_pq_signature_attestation_verification_time_seconds", + "Time taken to verify an attestation signature", + (0.005, 0.01, 0.025, 0.05, 0.1, 1)) diff --git a/src/metrics/all_metrics.def b/src/metrics/all_metrics.def index 2e1f5854..c6dffee3 100644 --- a/src/metrics/all_metrics.def +++ b/src/metrics/all_metrics.def @@ -2,3 +2,4 @@ #include "blockchain/fork_choice_metrics.def" #include "blockchain/impl/validator_metrics.def" #include "blockchain/state_transition_metrics.def" +#include "crypto/crypto_metrics.def" diff --git a/src/metrics/metrics.hpp b/src/metrics/metrics.hpp index 7dc90a38..68abce51 100644 --- a/src/metrics/metrics.hpp +++ b/src/metrics/metrics.hpp @@ -171,10 +171,7 @@ namespace lean::metrics { */ ~HistogramTimer() { if (running_) { - const auto elapsed_time = stop(); - if (histogram_) { - histogram_->observe(elapsed_time); - } + stop(); } } @@ -193,10 +190,7 @@ namespace lean::metrics { HistogramTimer &operator=(HistogramTimer &&other) noexcept { if (this != &other) { if (running_) { - const auto elapsed_time = stop(); - if (histogram_) { - histogram_->observe(elapsed_time); - } + stop(); } histogram_ = other.histogram_; start_time_ = other.start_time_; @@ -207,17 +201,36 @@ namespace lean::metrics { } /** - * @brief Manually stop the timer + * @brief Stop the timer and record the elapsed time to the histogram * @return Elapsed time in seconds * - * After calling stop(), the timer is considered stopped and the destructor - * will not record the time to the histogram. */ double stop() { if (!running_) { return 0.0; } + const auto elapsed_time = elapsed(); + running_ = false; + + // Automatically observe the elapsed time + if (histogram_) { + histogram_->observe(elapsed_time); + } + + return elapsed_time; + } + + /** + * @brief Stop the timer without recording to the histogram + * @return Elapsed time in seconds + * + */ + double stopWithoutObserving() { + if (!running_) { + return 0.0; + } + const auto elapsed_time = elapsed(); running_ = false; return elapsed_time;