Coding Verifiable Components
Proven Network uses a virtual machine which can run code targeting the WASM Component Model. Let's look at an example component!
package component:rps-example;
world rps-example {
import @proven-network:storage/identity-kv;
enum move {
rock,
paper,
scissors,
}
enum outcome {
win,
loss,
draw,
}
record metrics {
played: u32,
won: u32,
lost: u32,
drawn: u32,
}
export play: func(move: move) -> outcome;
export score: func() -> metrics;
}
Here we're defining a simple rock-paper-scissors games which allows player to call a play function with their selected move and get an outcome in return.
WIT files are used by language-specific build tools to automatically handle all required codegen. Let's implement the play function in Rust using the cargo-component tooling.
mod bindings; // This module is automatically generated by cargo-component
use bindings::{
Guest, Metrics, Move,
Move::{Paper, Rock, Scissors},
Outcome,
Outcome::{Draw, Loss, Win}
};
use rand::Rng;
struct Component;
impl Guest for Component {
fn play(input: Move) -> Outcome {
let server_selection = [Rock, Paper, Scissors][rand::thread_rng().gen_range(0..3)];
match (input, server_selection) {
(Rock, Scissors) | (Scissors, Paper) | (Paper, Rock) => Win,
(Rock, Rock) | (Scissors, Scissors) | (Paper, Paper) => Draw,
_ => Loss,
}
}
}
bindings::export!(Component with_types_in bindings);
Accessing persistent storage
Coming soon
Further reading on the Webassembly Component Model
Further documentation on the WA Component Model can be found on the Bytecode Alliance documentation site: https://component-model.bytecodealliance.org/
Last updated