use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResourceLimits {
pub max_fuel: u64,
pub max_memory_pages: u32,
pub max_table_elements: u32,
pub timeout_ms: u64,
pub max_output_bytes: usize,
}
impl ResourceLimits {
pub fn new(
max_fuel: u64,
max_memory_pages: u32,
timeout_ms: u64,
) -> Self {
Self {
max_fuel,
max_memory_pages,
max_table_elements: 1024,
timeout_ms,
max_output_bytes: 1024 * 1024, }
}
pub fn minimal() -> Self {
Self {
max_fuel: 10_000,
max_memory_pages: 1,
max_table_elements: 10,
timeout_ms: 100,
max_output_bytes: 1024,
}
}
pub fn generous() -> Self {
Self {
max_fuel: 10_000_000,
max_memory_pages: 64, max_table_elements: 4096,
timeout_ms: 30_000,
max_output_bytes: 10 * 1024 * 1024, }
}
pub fn max_memory_bytes(&self) -> usize {
(self.max_memory_pages as usize) * 65536
}
}
impl Default for ResourceLimits {
fn default() -> Self {
Self::new(1_000_000, 16, 5000)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FuelCosts {
pub base_opcost: u64,
pub memory_opcost: u64,
pub table_opcost: u64,
pub host_call_cost: u64,
}
impl FuelCosts {
pub fn standard() -> Self {
Self {
base_opcost: 1,
memory_opcost: 10,
table_opcost: 10,
host_call_cost: 100,
}
}
pub fn conservative() -> Self {
Self {
base_opcost: 2,
memory_opcost: 20,
table_opcost: 20,
host_call_cost: 200,
}
}
}
impl Default for FuelCosts {
fn default() -> Self {
Self::standard()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resource_limits() {
let limits = ResourceLimits::new(1000000, 16, 5000);
assert_eq!(limits.max_fuel, 1000000);
assert_eq!(limits.max_memory_pages, 16);
assert_eq!(limits.max_memory_bytes(), 16 * 65536);
assert_eq!(limits.timeout_ms, 5000);
}
#[test]
fn test_minimal_limits() {
let limits = ResourceLimits::minimal();
assert_eq!(limits.max_fuel, 10_000);
assert_eq!(limits.max_memory_pages, 1);
assert_eq!(limits.max_output_bytes, 1024);
}
#[test]
fn test_fuel_costs() {
let costs = FuelCosts::standard();
assert_eq!(costs.base_opcost, 1);
assert_eq!(costs.host_call_cost, 100);
}
}