Skip to content

Commit

Permalink
feat(starknet_batcher): emit transaction metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Jan 20, 2025
1 parent e103c3c commit 9a73366
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,19 @@ impl Batcher {
.ok_or(BatcherError::ExecutedProposalNotFound { proposal_id })?
.map_err(|_| BatcherError::InternalError)?;
let state_diff = block_execution_artifacts.state_diff();
let n_txs = u64::try_from(block_execution_artifacts.tx_hashes().len())
.expect("Number of transactions should fit in u64");
let n_rejected_txs = u64::try_from(block_execution_artifacts.rejected_tx_hashes.len())
.expect("Number of rejected transactions should fit in u64");
self.commit_proposal_and_block(
height,
state_diff.clone(),
block_execution_artifacts.address_to_nonce(),
block_execution_artifacts.rejected_tx_hashes,
)
.await?;
counter!(crate::metrics::BATCHED_TRANSACTIONS.name).increment(n_txs);
counter!(crate::metrics::REJECTED_TRANSACTIONS.name).increment(n_rejected_txs);
Ok(DecisionReachedResponse {
state_diff,
l2_gas_used: block_execution_artifacts.l2_gas_used,
Expand Down
10 changes: 9 additions & 1 deletion crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ async fn decision_reached() {
.times(1)
.with(eq(CommitBlockArgs {
address_to_nonce: expected_artifacts.address_to_nonce(),
rejected_tx_hashes: [].into(),
rejected_tx_hashes: expected_artifacts.rejected_tx_hashes.clone(),
}))
.returning(|_| Ok(()));

Expand Down Expand Up @@ -782,6 +782,14 @@ async fn decision_reached() {
parse_numeric_metric::<u64>(&metrics, crate::metrics::STORAGE_HEIGHT.name),
Some(INITIAL_HEIGHT.unchecked_next().0)
);
assert_eq!(
parse_numeric_metric::<usize>(&metrics, crate::metrics::BATCHED_TRANSACTIONS.name),
Some(expected_artifacts.execution_infos.len())
);
assert_eq!(
parse_numeric_metric::<usize>(&metrics, crate::metrics::REJECTED_TRANSACTIONS.name),
Some(expected_artifacts.rejected_tx_hashes.len())
);
}

#[rstest]
Expand Down
17 changes: 17 additions & 0 deletions crates/starknet_batcher/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use metrics::{counter, describe_counter, describe_gauge, gauge};
use starknet_api::block::BlockNumber;

// Height metrics.
pub const STORAGE_HEIGHT: Metric =
Metric { name: "batcher_storage_height", description: "The height of the batcher's storage" };

// Proposal metrics.
pub const PROPOSAL_STARTED: Metric =
Metric { name: "batcher_proposal_started", description: "Counter of proposals started" };
pub const PROPOSAL_SUCCEEDED: Metric =
Expand All @@ -12,6 +15,14 @@ pub const PROPOSAL_FAILED: Metric =
pub const PROPOSAL_ABORTED: Metric =
Metric { name: "batcher_proposal_aborted", description: "Counter of aborted proposals" };

// Transaction metrics.
pub const BATCHED_TRANSACTIONS: Metric =
Metric { name: "batcher_batched_transactions", description: "Counter of batched transactions" };
pub const REJECTED_TRANSACTIONS: Metric = Metric {
name: "batcher_rejected_transactions",
description: "Counter of rejected transactions",
};

pub struct Metric {
pub name: &'static str,
pub description: &'static str,
Expand All @@ -31,6 +42,12 @@ pub fn register_metrics(storage_height: BlockNumber) {
describe_counter!(PROPOSAL_FAILED.name, PROPOSAL_FAILED.description);
counter!(PROPOSAL_ABORTED.name).absolute(0);
describe_counter!(PROPOSAL_ABORTED.name, PROPOSAL_ABORTED.description);

// In case of revert, consider calling `absolute`.
counter!(BATCHED_TRANSACTIONS.name).absolute(0);
describe_counter!(BATCHED_TRANSACTIONS.name, BATCHED_TRANSACTIONS.description);
counter!(REJECTED_TRANSACTIONS.name).absolute(0);
describe_counter!(REJECTED_TRANSACTIONS.name, REJECTED_TRANSACTIONS.description);
}

/// A handle to update the proposal metrics when the proposal is created and dropped.
Expand Down
9 changes: 6 additions & 3 deletions crates/starknet_batcher/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashSet;
use std::ops::Range;

use async_trait::async_trait;
use blockifier::bouncer::BouncerWeights;
use blockifier::state::cached_state::CommitmentStateDiff;
use blockifier::transaction::objects::TransactionExecutionInfo;
use indexmap::IndexMap;
use starknet_api::executable_transaction::Transaction;
use starknet_api::execution_resources::GasAmount;
Expand Down Expand Up @@ -74,8 +74,11 @@ impl BlockExecutionArtifacts {
pub fn create_for_testing() -> Self {
// Use a non-empty commitment_state_diff to make the tests more realistic.
Self {
execution_infos: IndexMap::default(),
rejected_tx_hashes: HashSet::default(),
execution_infos: test_txs(0..10)
.iter()
.map(|tx| (tx.tx_hash(), TransactionExecutionInfo::default()))
.collect(),
rejected_tx_hashes: test_txs(10..15).iter().map(|tx| tx.tx_hash()).collect(),
commitment_state_diff: CommitmentStateDiff {
address_to_class_hash: IndexMap::from_iter([(
contract_address!("0x7"),
Expand Down

0 comments on commit 9a73366

Please sign in to comment.