Skip to content

Commit

Permalink
feat(eth-sender): metrics for finalized and safe L1 block numbers (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanBrodetski committed Jan 31, 2024
1 parent dea6768 commit 32c1637
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
24 changes: 19 additions & 5 deletions core/lib/zksync_core/src/eth_sender/eth_tx_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::sync::watch;
use zksync_config::configs::eth_sender::SenderConfig;
use zksync_dal::{ConnectionPool, StorageProcessor};
use zksync_eth_client::{
BoundEthInterface, Error, ExecutedTxStatus, RawTransactionBytes, SignedCallResult,
BoundEthInterface, Error, EthInterface, ExecutedTxStatus, RawTransactionBytes, SignedCallResult,
};
use zksync_types::{
eth_sender::EthTx,
Expand Down Expand Up @@ -37,6 +37,7 @@ struct OperatorNonce {

#[derive(Debug, Clone, Copy)]
pub(super) struct L1BlockNumbers {
pub safe: L1BlockNumber,
pub finalized: L1BlockNumber,
pub latest: L1BlockNumber,
}
Expand Down Expand Up @@ -300,7 +301,22 @@ impl EthTxManager {
.await?
.as_u32()
.into();
Ok(L1BlockNumbers { finalized, latest })

let safe = self
.ethereum_gateway
.block(BlockId::Number(BlockNumber::Safe), "eth_tx_manager")
.await?
.expect("Safe block must be present on L1")
.number
.expect("Safe block must contain number")
.as_u32()
.into();

Ok(L1BlockNumbers {
finalized,
latest,
safe,
})
}

// Monitors the in-flight transactions, marks mined ones as confirmed,
Expand All @@ -310,9 +326,7 @@ impl EthTxManager {
storage: &mut StorageProcessor<'_>,
l1_block_numbers: L1BlockNumbers,
) -> Result<Option<(EthTx, u32)>, ETHSenderError> {
METRICS
.last_known_l1_block
.set(l1_block_numbers.latest.0.into());
METRICS.track_block_numbers(&l1_block_numbers);
let operator_nonce = self.get_operator_nonce(l1_block_numbers).await?;
let inflight_txs = storage.eth_sender_dal().get_inflight_txs().await.unwrap();
METRICS.number_of_inflight_txs.set(inflight_txs.len());
Expand Down
26 changes: 23 additions & 3 deletions core/lib/zksync_core/src/eth_sender/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use zksync_dal::StorageProcessor;
use zksync_types::{aggregated_operations::AggregatedActionType, eth_sender::EthTx};
use zksync_utils::time::seconds_since_epoch;

use crate::metrics::{BlockL1Stage, BlockStage, APP_METRICS};
use crate::{
eth_sender::eth_tx_manager::L1BlockNumbers,
metrics::{BlockL1Stage, BlockStage, APP_METRICS},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "kind", rename_all = "snake_case")]
Expand All @@ -18,6 +21,15 @@ pub(super) enum PubdataKind {
RepeatedWritesCompressed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "block_number_variant", rename_all = "snake_case")]
#[allow(clippy::enum_variant_names)]
pub(super) enum BlockNumberVariant {
Latest,
Finalized,
Safe,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "type")]
pub(super) struct ActionTypeLabel(AggregatedActionType);
Expand Down Expand Up @@ -76,20 +88,28 @@ pub(super) struct EthSenderMetrics {
#[metrics(buckets = FEE_BUCKETS)]
pub used_priority_fee_per_gas: Histogram<u64>,
/// Last L1 block observed by the Ethereum sender.
pub last_known_l1_block: Gauge<u64>,
pub last_known_l1_block: Family<BlockNumberVariant, Gauge<usize>>,
/// Number of in-flight txs produced by the Ethereum sender.
pub number_of_inflight_txs: Gauge<usize>,
#[metrics(buckets = GAS_BUCKETS)]
pub l1_gas_used: Family<ActionTypeLabel, Histogram<f64>>,
#[metrics(buckets = Buckets::LATENCIES)]
pub l1_tx_mined_latency: Family<ActionTypeLabel, Histogram<Duration>>,
#[metrics(buckets = &[1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 20.0, 30.0, 50.0])]
#[metrics(buckets = & [1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 20.0, 30.0, 50.0])]
pub l1_blocks_waited_in_mempool: Family<ActionTypeLabel, Histogram<u64>>,
/// Number of L1 batches aggregated for publishing with a specific reason.
pub block_aggregation_reason: Family<AggregationReasonLabels, Counter>,
}

impl EthSenderMetrics {
pub fn track_block_numbers(&self, l1_block_numbers: &L1BlockNumbers) {
self.last_known_l1_block[&BlockNumberVariant::Latest]
.set(l1_block_numbers.latest.0 as usize);
self.last_known_l1_block[&BlockNumberVariant::Finalized]
.set(l1_block_numbers.finalized.0 as usize);
self.last_known_l1_block[&BlockNumberVariant::Safe].set(l1_block_numbers.safe.0 as usize);
}

pub async fn track_eth_tx_metrics(
&self,
connection: &mut StorageProcessor<'_>,
Expand Down
6 changes: 5 additions & 1 deletion core/lib/zksync_core/src/eth_sender/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ impl EthSenderTester {
async fn get_block_numbers(&self) -> L1BlockNumbers {
let latest = self.gateway.block_number("").await.unwrap().as_u32().into();
let finalized = latest - Self::WAIT_CONFIRMATIONS as u32;
L1BlockNumbers { finalized, latest }
L1BlockNumbers {
finalized,
latest,
safe: finalized,
}
}
}

Expand Down

0 comments on commit 32c1637

Please sign in to comment.