warp_runtime/fuel.rs
1use std::time::Duration;
2
3/// A run stops after this much computing (the CPU time of the thread running it: sleeping and waiting for input don't
4/// count) with `out of time`, instead of hanging (decision fuel-metering). Change it with `WARP_TIME_LIMIT=<seconds>`.
5pub const DEFAULT_TIME_LIMIT: Duration = Duration::from_secs(60);
6/// Environment variable that overrides `DEFAULT_TIME_LIMIT`, in seconds
7pub const TIME_LIMIT_VARIABLE: &str = "WARP_TIME_LIMIT";
8
9/// Fuel for a run that asks for fuel instead of a time limit (`use fuel`, `warp fuel <steps>`, `WARP_FUEL`): wasmtime
10/// burns about one unit per executed WASM instruction, so this is roughly a hundred billion steps before a run stops
11/// with `out of fuel after N steps`. Counting steps makes calls about four times slower (probes/fast_int/fuel_cost.sh),
12/// so only such runs count them.
13pub const DEFAULT_FUEL: u64 = 100_000_000_000;
14/// Environment variable that asks for fuel, its value the steps (overrides `DEFAULT_FUEL`)
15pub const FUEL_VARIABLE: &str = "WARP_FUEL";
16
17/// The steps `WARP_FUEL` gives (underscores allowed: 1_000_000), if it is set to a number
18pub fn fuel_from_environment() -> Option<u64> {
19 std::env::var(FUEL_VARIABLE).ok().and_then(|steps| steps.trim().replace('_', "").parse().ok())
20}
21
22/// The time `WARP_TIME_LIMIT` gives (seconds, fractions allowed), if it is set to a positive number
23pub fn time_limit_from_environment() -> Option<Duration> {
24 let seconds: f64 = std::env::var(TIME_LIMIT_VARIABLE).ok()?.trim().parse().ok()?;
25 (seconds > 0.0).then(|| Duration::from_secs_f64(seconds))
26}