WASM Module Guide
Guest Check-In Module
This guide demonstrates how to build a simple but robust Guestbook module. It records the total number of physical visits to a location.
#Full Source Code
This module reads the alias (name) from the guest's Micro-UI, increments a global counter, and writes the guest's name to a specific Sled key.
src/lib.rs
#![no_std]
use qos_sdk::{state, input, output, log_msg, LogLevel};
#[no_mangle]
pub extern "C" fn run() -> i32 {
// 1. Read alias from input
let bytes = input::read().unwrap_or_default();
let alias = core::str::from_utf8(&bytes).unwrap_or("Anonymous");
// 2. Increment global counter atomically
let mut current_count: u32 = 1;
if let Some(val) = state::get("guestbook:total_visits") {
if val.len() == 4 {
let mut buf = [0u8; 4];
buf.copy_from_slice(&val);
current_count = u32::from_le_bytes(buf) + 1;
}
}
state::set("guestbook:total_visits", ¤t_count.to_le_bytes()).unwrap();
// 3. Save latest guest
state::set("guestbook:latest_guest", alias.as_bytes()).unwrap();
log_msg(LogLevel::Info, "New guest checked in!");
// 4. Return receipt
// In a real app, use a JSON serializer like microserde.
let receipt = "{\"status\":\"success\"}";
output::write(receipt.as_bytes());
0
}#Handling Concurrency
Because state::get and state::set run within the execution pipeline synchronously on the Edge Node, there is no risk of a race condition between reading the counter and writing the updated counter.