c3f23d9452
- Reject a second running mod with an id already in use: two mods sharing an id would also share capability storage's data_dir, letting one read, overwrite or delete the other's state. - Cap storage to 64 keys per mod, not just 4 MiB: zero- or near-zero- byte values could otherwise flood the directory with files the byte quota never sees. - Add a 1 MiB per-call write budget, separate from the 4 MiB on-disk quota: a loop replacing one key stays under the quota on every check (the total on disk never grows) while still doing unbounded synchronous disk I/O per call, which fuel never meters. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
64 lines
2.2 KiB
Rust
64 lines
2.2 KiB
Rust
//! A mod for the runtime's tests: `/spin` loops forever, `/panic` panics,
|
|
//! `/grow` asks for a gigabyte, `/hello` chats, `/save` and `/load` exercise
|
|
//! capability `storage` on a fixed key, `/save_key <key> <text>` exercises
|
|
//! it on an arbitrary key (for the quota and key-count limits); `tick` and
|
|
//! `frame` do nothing, to measure the cost of a call.
|
|
use horizon_mod_sdk::{Mod, Registration, chat, storage};
|
|
|
|
struct Guest {
|
|
ticks: u64,
|
|
}
|
|
|
|
impl Mod for Guest {
|
|
fn init() -> Result<(Self, Registration), String> {
|
|
let hooks = ["spin", "panic", "grow", "hello", "save", "load", "save_key"]
|
|
.into_iter()
|
|
.fold(Registration::new(), Registration::command)
|
|
.on_tick()
|
|
.on_frame();
|
|
Ok((Guest { ticks: 0 }, hooks))
|
|
}
|
|
fn tick(&mut self, _tick: u64) {
|
|
self.ticks += 1;
|
|
}
|
|
fn command(&mut self, name: &str, args: &str) -> Result<Option<String>, String> {
|
|
match name {
|
|
"spin" => {
|
|
let mut n = 0u64;
|
|
while std::hint::black_box(n) != u64::MAX {
|
|
n = n.wrapping_add(2);
|
|
}
|
|
Ok(None)
|
|
}
|
|
"panic" => panic!("asked to"),
|
|
"grow" => {
|
|
let big = std::hint::black_box(vec![1u8; 1 << 30]);
|
|
Ok(Some(format!("{}", big.len())))
|
|
}
|
|
"save" => {
|
|
storage::set("note", args.as_bytes())?;
|
|
Ok(Some("saved".into()))
|
|
}
|
|
"load" => {
|
|
let text =
|
|
storage::get("note").map(|bytes| String::from_utf8_lossy(&bytes).into_owned());
|
|
chat::show(text.as_deref().unwrap_or("(empty)"));
|
|
Ok(None)
|
|
}
|
|
"save_key" => {
|
|
let (key, text) = args
|
|
.split_once(' ')
|
|
.ok_or("Usage: /save_key <key> <text>")?;
|
|
storage::set(key, text.as_bytes())?;
|
|
Ok(Some("saved".into()))
|
|
}
|
|
_ => {
|
|
chat::show(&format!("hello {args} after {} ticks", self.ticks));
|
|
Ok(None)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
horizon_mod_sdk::export_mod!(Guest);
|