Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework0 #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions contracts/01-tamagotchi/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
#![no_std]

use codec::{Decode, Encode};
use gmeta::Metadata;
use gmeta::{Metadata,In,InOut,Out};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use cargo fmt before PR.

Copy link
Owner Author

@Jackliu-miaozi Jackliu-miaozi Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.
Thank you. You are the first who taught me coding step by step.

use gstd::prelude::*;
use scale_info::TypeInfo;

#[derive(Default, Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Tamagotchi {
// TODO: 1️⃣ Add `name` and `age` fields
pub name: String,
pub date_of_birth: u64,
}

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum TmgAction {
// TODO: 2️⃣ Add `Name` and `Age` actions that set the name and age
Name,
Age,
}

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum TmgEvent {
// TODO: 3️⃣ Add `Name` and `Age` events that return the name and age
Name(String),
Age(u64),
}

pub struct ProgramMetadata;

// TODO: 4️⃣ Fill `Init`, `Handle`, and `State` types
impl Metadata for ProgramMetadata {
type Init = ();
type Handle = ();
type State = ();
type Init = In<String>;
type Handle = InOut<TmgAction, TmgEvent>;
type State = Out<Tamagotchi>;
type Reply = ();
type Others = ();
type Signal = ();
Expand Down
29 changes: 27 additions & 2 deletions contracts/01-tamagotchi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
#![no_std]

#[allow(unused_imports)]
use gstd::prelude::*;
use gstd::{exec, msg, prelude::*};
use tamagotchi_io::*;

static mut TAMAGOTCHI: Option<Tamagotchi> = None;

#[no_mangle]
extern fn init() {
// TODO: 5️⃣ Initialize the Tamagotchi program
let initname = msg::load().expect("unable to load name");
let birthdate = exec::block_timestamp();
let tmg = Tamagotchi {
name: initname,
date_of_birth: birthdate,
};
unsafe {
TAMAGOTCHI = Some(tmg);
};
}

#[no_mangle]
extern fn handle() {
// TODO: 6️⃣ Add handling of `Name` and `Age` actions
let action: TmgAction = msg::load().expect("unable to load action");
let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) };
match action {
TmgAction::Name => {
msg::reply(TmgEvent::Name(tmg.name.clone()), 0)
.expect("Error in a reply'tamagotchi::name'");
}
TmgAction::Age => {
let age = exec::block_timestamp() - tmg.date_of_birth;
msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'");
}
}
}

#[no_mangle]
extern fn state() {
// TODO: 7️⃣ Return the Tamagotchi state
let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") };
msg::reply(tmg, 0).expect("Failed to share state");
}
35 changes: 32 additions & 3 deletions contracts/01-tamagotchi/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
use gtest::{Program, System};
use gtest::{Log, Program, System};
use tamagotchi_io::*;

#[test]
fn smoke_test() {
let sys = System::new();
sys.init_logger();
let _program = Program::current(&sys);
let program = Program::current(&sys);
let result = program.send(2, String::from("Goodmoring"));
assert!(!result.main_failed());
let result = program.send(2, TmgAction::Name);
let log = Log::builder()
.dest(2)
.payload(TmgEvent::Name(String::from("Goodmoring")));
assert!(result.contains(&log));
let _result = program.send(2, TmgAction::Age);
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp()));
// assert!(result.contains(&log));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use System::spend_blocks to go forward and emulate the changed timestamp.

// TODO: 8️⃣ Test the program initialization and message handling
//How to test the age?
}

#[test]
fn negative_smoke_test() {
let sys = System::new();
sys.init_logger();
let program = Program::current(&sys);
let payload = vec![1, 2, 3];
let _result = program.send(2, payload);
// assert!(result.main_failed());
// Why the assert is panic?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It panics because the init() function can't decode the payload as a UTF-8 string, I guess.

// let result = program.send(1, TmgAction::Name);
// let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string()));
// assert!(!result.contains(&log));
// let result = program.send(1, TmgAction::Age);
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp()));
// assert!(!result.contains(&log));
}
Loading