Skip to main content

PING-PONG

Gear is very easy to write code for!

This is the minimal program for a classic ping-pong contract:

#![no_std]

use gstd::{debug, msg, prelude::*};

static mut MESSAGE_LOG: Vec<String> = vec![];

#[no_mangle]
pub unsafe extern "C" fn handle() {
let new_msg = String::from_utf8(msg::load_bytes().expect("Unable to load bytes"))
.expect("Invalid message");

if new_msg == "PING" {
msg::reply_bytes("PONG", 0).unwrap();
}

MESSAGE_LOG.push(new_msg);

debug!("{:?} total message(s) stored: ", MESSAGE_LOG.len());

for log in MESSAGE_LOG.iter() {
debug!(log);
}
}

/// and a simple unit test:

#[cfg(test)]
mod tests {
extern crate std;

use gtest::{Log, Program, System};

#[test]
fn it_works() {
let system = System::new();
system.init_logger();

let program = Program::current(&system);

let res = program.send_bytes(42, "INIT");
assert!(res.log().is_empty());

let res = program.send_bytes(42, "PING");
let log = Log::builder().source(1).dest(42).payload_bytes("PONG");
assert!(res.contains(&log));
}
}

The program just sends PONG back to the original sender who sent PING to it.

GitHub link: https://github.com/gear-dapps/ping