Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add metrics for transaction execution result in state keeper #2021

Merged
merged 19 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions core/lib/multivm/src/interface/types/errors/halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ pub enum Halt {
FailedToPublishCompressedBytecodes,
}

impl Halt {
pub fn to_metrics_friendly_string(&self) -> String {
AnastasiiaVashchuk marked this conversation as resolved.
Show resolved Hide resolved
let result = match self {
Halt::ValidationFailed(_) => "ValidationFailed",
Halt::PaymasterValidationFailed(_) => "PaymasterValidationFailed",
Halt::PrePaymasterPreparationFailed(_) => "PrePaymasterPreparationFailed",
Halt::PayForTxFailed(_) => "PayForTxFailed",
Halt::FailedToMarkFactoryDependencies(_) => "FailedToMarkFactoryDependencies",
Halt::FailedToChargeFee(_) => "FailedToChargeFee",
Halt::FromIsNotAnAccount => "FromIsNotAnAccount",
Halt::InnerTxError => "InnerTxError",
Halt::Unknown(_) => "Unknown",
Halt::UnexpectedVMBehavior(_) => "UnexpectedVMBehavior",
Halt::BootloaderOutOfGas => "BootloaderOutOfGas",
Halt::ValidationOutOfGas => "ValidationOutOfGas",
Halt::TooBigGasLimit => "TooBigGasLimit",
Halt::NotEnoughGasProvided => "NotEnoughGasProvided",
Halt::MissingInvocationLimitReached => "MissingInvocationLimitReached",
Halt::FailedToSetL2Block(_) => "FailedToSetL2Block",
Halt::FailedToAppendTransactionToL2Block(_) => "FailedToAppendTransactionToL2Block",
Halt::VMPanic => "VMPanic",
Halt::TracerCustom(_) => "TracerCustom",
Halt::FailedToPublishCompressedBytecodes => "FailedToPublishCompressedBytecodes",
};
result.to_string()
}
}

impl Display for Halt {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
11 changes: 11 additions & 0 deletions core/lib/multivm/src/interface/types/errors/vm_revert_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ impl VmRevertReason {
}
}

pub fn to_metrics_friendly_string(&self) -> String {
let result = match self {
VmRevertReason::General { .. } => "General",
VmRevertReason::InnerTxError => "InnerTxError",
VmRevertReason::VmError => "VmError",
VmRevertReason::Unknown { .. } => "Unknown",
};

result.to_string()
}

pub fn encoded_data(&self) -> Vec<u8> {
match self {
VmRevertReason::Unknown { data, .. } => data.clone(),
Expand Down
3 changes: 2 additions & 1 deletion core/node/state_keeper/src/io/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ impl StateKeeperIO for MempoolIO {

// Mark tx as rejected in the storage.
let mut storage = self.pool.connection_tagged("state_keeper").await?;
KEEPER_METRICS.rejected_transactions.inc();
KEEPER_METRICS.inc_rejected_txs(error.to_string());
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved

tracing::warn!(
"Transaction {} is rejected with error: {error}",
rejected.hash()
Expand Down
1 change: 1 addition & 0 deletions core/node/state_keeper/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ impl ZkSyncStateKeeper {
tx: Transaction,
) -> (SealResolution, TxExecutionResult) {
let exec_result = batch_executor.execute_tx(tx.clone()).await;

// All of `TxExecutionResult::BootloaderOutOfGasForTx`,
// `Halt::NotEnoughGasProvided` correspond to out-of-gas errors but of different nature.
// - `BootloaderOutOfGasForTx`: it is returned when bootloader stack frame run out of gas before tx execution finished.
Expand Down
44 changes: 42 additions & 2 deletions core/node/state_keeper/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ pub enum TxExecutionType {
L2,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue)]
#[metrics(rename_all = "snake_case")]
pub enum TxExecutionStatus {
Success,
Rejected,
Reverted,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, EncodeLabelSet)]
pub struct TxExecutionResult {
status: TxExecutionStatus,
reason: Option<String>,
}

impl TxExecutionType {
pub fn from_is_l1(is_l1: bool) -> TxExecutionType {
match is_l1 {
Expand Down Expand Up @@ -57,8 +71,8 @@ pub struct StateKeeperMetrics {
/// Latency of the state keeper getting a transaction from the mempool.
#[metrics(buckets = Buckets::LATENCIES)]
pub get_tx_from_mempool: Histogram<Duration>,
/// Number of transactions rejected by the state keeper.
pub rejected_transactions: Counter,
/// Number of transactions completed with a specific result.
pub tx_execution_result: Family<TxExecutionResult, Counter>,
/// Time spent waiting for the hash of a previous L1 batch.
#[metrics(buckets = Buckets::LATENCIES)]
pub wait_for_prev_hash_time: Histogram<Duration>,
Expand All @@ -77,6 +91,32 @@ pub struct StateKeeperMetrics {
pub blob_base_fee_too_high: Counter,
}

impl StateKeeperMetrics {
pub fn inc_rejected_txs(&self, reason: String) {
self.tx_execution_result[&TxExecutionResult {
status: TxExecutionStatus::Rejected,
reason: Some(reason),
}]
.inc();
AnastasiiaVashchuk marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn inc_succeeded_txs(&self) {
self.tx_execution_result[&TxExecutionResult {
status: TxExecutionStatus::Success,
reason: None,
}]
.inc();
}

pub fn inc_reverted_txs(&self, reason: String) {
self.tx_execution_result[&TxExecutionResult {
status: TxExecutionStatus::Reverted,
reason: Some(reason),
}]
.inc();
}
}

#[vise::register]
pub static KEEPER_METRICS: vise::Global<StateKeeperMetrics> = vise::Global::new();

Expand Down
16 changes: 13 additions & 3 deletions core/node/state_keeper/src/updates/l2_block_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use zksync_types::{
};
use zksync_utils::bytecode::{hash_bytecode, CompressedBytecodeInfo};

use crate::metrics::KEEPER_METRICS;

#[derive(Debug, Clone, PartialEq)]
pub struct L2BlockUpdates {
pub executed_transactions: Vec<TransactionExecutionResult>,
Expand Down Expand Up @@ -104,9 +106,17 @@ impl L2BlockUpdates {
};

let revert_reason = match &tx_execution_result.result {
ExecutionResult::Success { .. } => None,
ExecutionResult::Revert { output } => Some(output.to_string()),
ExecutionResult::Halt { reason } => Some(reason.to_string()),
ExecutionResult::Success { .. } => {
KEEPER_METRICS.inc_succeeded_txs();
None
}
ExecutionResult::Revert { output } => {
KEEPER_METRICS.inc_reverted_txs(output.to_metrics_friendly_string());
Some(output.to_string())
}
ExecutionResult::Halt { .. } => {
unreachable!("Tx that is added to `UpdatesManager` must not have Halted status")
}
};

// Get transaction factory deps
Expand Down
Loading