Skip to content

Commit c4ebf40

Browse files
committed
Observe bytecode kinds in overrides
1 parent 8390a54 commit c4ebf40

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

core/node/api_server/src/execution_sandbox/vm_metrics.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use vise::{
55
Metrics,
66
};
77
use zksync_types::{
8-
api::state_override::{OverrideState, StateOverride},
8+
api::state_override::{BytecodeOverride, OverrideState, StateOverride},
9+
bytecode::BytecodeMarker,
910
H256,
1011
};
1112

@@ -100,6 +101,30 @@ struct StateOverrideLabels {
100101
kind: OverrideKind,
101102
}
102103

104+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue)]
105+
#[metrics(rename_all = "snake_case")]
106+
enum BytecodeMarkerLabel {
107+
EraVm,
108+
DetectedEraVm,
109+
Evm,
110+
DetectedEvm,
111+
}
112+
113+
impl BytecodeMarkerLabel {
114+
fn detected(kind: BytecodeMarker) -> Self {
115+
match kind {
116+
BytecodeMarker::EraVm => Self::DetectedEraVm,
117+
BytecodeMarker::Evm => Self::DetectedEvm,
118+
}
119+
}
120+
}
121+
122+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet)]
123+
struct BytecodeOverrideLabels {
124+
method: &'static str,
125+
kind: BytecodeMarkerLabel,
126+
}
127+
103128
#[derive(Debug, Metrics)]
104129
#[metrics(prefix = "api_web3")]
105130
pub(crate) struct SandboxMetrics {
@@ -123,6 +148,8 @@ pub(crate) struct SandboxMetrics {
123148
pub estimate_gas_optimistic_gas_limit_relative_diff: Histogram<f64>,
124149
/// Statistics on state overrides.
125150
state_overrides: Family<StateOverrideLabels, Counter>,
151+
/// Statistics on bytecode kinds supplied in overrides.
152+
bytecode_overrides: Family<BytecodeOverrideLabels, Counter>,
126153
}
127154

128155
impl SandboxMetrics {
@@ -140,8 +167,23 @@ impl SandboxMetrics {
140167

141168
pub fn observe_override_metrics(&self, method: &'static str, state_overrides: &StateOverride) {
142169
for (_, account_override) in state_overrides.iter() {
143-
if account_override.code.is_some() {
170+
if let Some(code_override) = &account_override.code {
144171
self.state_overrides[&OverrideKind::Code.for_method(method)].inc();
172+
173+
let bytecode_kind = match code_override {
174+
BytecodeOverride::Evm(_) => BytecodeMarkerLabel::Evm,
175+
BytecodeOverride::EraVm(_) => BytecodeMarkerLabel::EraVm,
176+
BytecodeOverride::Unspecified(bytes) => {
177+
// Bytecode kind detection is very cheap, so it's permissible to do it here
178+
let kind = BytecodeMarker::detect(&bytes.0);
179+
BytecodeMarkerLabel::detected(kind)
180+
}
181+
};
182+
let labels = BytecodeOverrideLabels {
183+
method,
184+
kind: bytecode_kind,
185+
};
186+
self.bytecode_overrides[&labels].inc();
145187
}
146188
if account_override.nonce.is_some() {
147189
self.state_overrides[&OverrideKind::Nonce.for_method(method)].inc();

0 commit comments

Comments
 (0)