Skip to content

Commit caa3407

Browse files
authored
fix: disable metering since is problematic (#1472)
1 parent 9b6b71f commit caa3407

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

crates/core/src/wasm_runtime/runtime.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub struct RuntimeConfig {
105105
pub cpu_cycles_per_second: Option<u64>,
106106
/// Safety margin for CPU speed variations (0.0 to 1.0)
107107
pub safety_margin: f64,
108+
pub enable_metering: bool,
108109
}
109110

110111
impl Default for RuntimeConfig {
@@ -113,6 +114,7 @@ impl Default for RuntimeConfig {
113114
max_execution_seconds: 5.0,
114115
cpu_cycles_per_second: None,
115116
safety_margin: 0.2,
117+
enable_metering: false,
116118
}
117119
}
118120
}
@@ -134,6 +136,7 @@ pub struct Runtime {
134136
pub(crate) contract_store: ContractStore,
135137
/// loaded contract modules
136138
pub(super) contract_modules: HashMap<ContractKey, Module>,
139+
pub(crate) enabled_metering: bool,
137140
}
138141

139142
impl Runtime {
@@ -171,6 +174,7 @@ impl Runtime {
171174

172175
contract_store,
173176
delegate_modules: HashMap::new(),
177+
enabled_metering: config.enable_metering,
174178
})
175179
}
176180

@@ -338,7 +342,9 @@ impl Runtime {
338342

339343
let metering = Arc::new(Metering::new(max_cycles, operation_cost));
340344
let mut compiler_config = Singlepass::default();
341-
compiler_config.push_middleware(metering);
345+
if config.enable_metering {
346+
compiler_config.push_middleware(metering.clone());
347+
}
342348

343349
let engine = wasmer::EngineBuilder::new(compiler_config).engine();
344350

@@ -351,19 +357,24 @@ impl Runtime {
351357
instance: &wasmer::Instance,
352358
function_name: &str,
353359
) -> super::error::ContractError {
354-
let remaining_points = get_remaining_points(self.wasm_store.as_mut().unwrap(), instance);
355-
match remaining_points {
356-
MeteringPoints::Remaining(..) => {
357-
tracing::error!("Error while calling {}: {:?}", function_name, error);
358-
error.into()
359-
}
360-
MeteringPoints::Exhausted => {
361-
tracing::error!(
362-
"{} ran out of gas, not enough points remaining",
363-
function_name
364-
);
365-
ContractExecError::OutOfGas.into()
360+
if self.enabled_metering {
361+
let remaining_points =
362+
get_remaining_points(self.wasm_store.as_mut().unwrap(), instance);
363+
match remaining_points {
364+
MeteringPoints::Remaining(..) => {
365+
tracing::error!("Error while calling {}: {:?}", function_name, error);
366+
error.into()
367+
}
368+
MeteringPoints::Exhausted => {
369+
tracing::error!(
370+
"{} ran out of gas, not enough points remaining",
371+
function_name
372+
);
373+
ContractExecError::OutOfGas.into()
374+
}
366375
}
376+
} else {
377+
error.into()
367378
}
368379
}
369380
}

crates/core/src/wasm_runtime/tests/contract_metering.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn validate_state_metering() -> Result<(), Box<dyn std::error::Error>> {
3131
max_execution_seconds: 5.0,
3232
cpu_cycles_per_second: Some(1_000_000), // Lower limit to force gas error
3333
safety_margin: 0.1,
34+
enable_metering: true,
3435
};
3536

3637
let mut runtime =
@@ -81,6 +82,7 @@ fn test_update_state_metering() -> Result<(), Box<dyn std::error::Error>> {
8182
max_execution_seconds: 5.0,
8283
cpu_cycles_per_second: Some(2_000_000),
8384
safety_margin: 0.1,
85+
enable_metering: true,
8486
};
8587

8688
let mut runtime =
@@ -132,6 +134,7 @@ fn test_summarize_state_metering() -> Result<(), Box<dyn std::error::Error>> {
132134
max_execution_seconds: 5.0,
133135
cpu_cycles_per_second: Some(3_000_000),
134136
safety_margin: 0.1,
137+
enable_metering: true,
135138
};
136139

137140
let mut runtime =
@@ -178,6 +181,7 @@ fn test_get_state_delta_metering() -> Result<(), Box<dyn std::error::Error>> {
178181
max_execution_seconds: 5.0,
179182
cpu_cycles_per_second: Some(4_000_000),
180183
safety_margin: 0.1,
184+
enable_metering: true,
181185
};
182186

183187
let mut runtime =
@@ -229,6 +233,7 @@ fn test_timeout_metering() -> Result<(), Box<dyn std::error::Error>> {
229233
max_execution_seconds: 5.0,
230234
cpu_cycles_per_second: Some(u64::MAX),
231235
safety_margin: 0.1,
236+
enable_metering: true,
232237
};
233238

234239
let mut runtime =

0 commit comments

Comments
 (0)