Skip to content

Commit

Permalink
feat(alpha): add alpha program (#14)
Browse files Browse the repository at this point in the history
* feat(alpha): alpha program

* fix(alpha): build problems

* feat(prog): state queries
  • Loading branch information
clearloop authored Feb 22, 2024
1 parent b111971 commit 0e5f489
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tokio.workspace = true
# It's necessary to include all metawasm crates in the workspace section, otherwise they'll be
# ignored by Cargo and won't be built.
[workspace]
members = ["state", "xtask", "domain", "domain/state"]
members = ["state", "xtask", "domain", "domain/state", "alpha", "alpha/state", "alpha/io"]

[workspace.dependencies]
gstd = "1.1.1"
Expand All @@ -37,6 +37,7 @@ gear-wasm-builder = "1.1.1"
gtest = "1.1.1"
gclient = "1.1.1"
template-io.path = "io"
alpha-io.path = "alpha/io"
tokio = "1"
xshell = "0.2"
anyhow = "1"
16 changes: 16 additions & 0 deletions alpha/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "vara-go-alpha"
version.workspace = true
edition.workspace = true
publish.workspace = true

[dependencies]
gstd.workspace = true
alpha-io.workspace = true

[dev-dependencies]
gtest.workspace = true

[build-dependencies]
alpha-io.workspace = true
gear-wasm-builder.workspace = true
5 changes: 5 additions & 0 deletions alpha/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use alpha_io::ContractMetadata;

fn main() {
gear_wasm_builder::build_with_metadata::<ContractMetadata>();
}
9 changes: 9 additions & 0 deletions alpha/io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "alpha-io"
version.workspace = true
edition.workspace = true
publish.workspace = true

[dependencies]
gmeta.workspace = true
gstd.workspace = true
68 changes: 68 additions & 0 deletions alpha/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![no_std]

use gmeta::{In, Metadata, Out};
use gstd::*;

/// The contract metadata. Used by frontend apps & for describing the types of messages that can be
/// sent in contract's entry points. See also [`Metadata`].
pub struct ContractMetadata;

pub type State = Vec<Resource>;

#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum Label {
Apple,
Banana,
Football,
Vara,
}

impl AsRef<str> for Label {
fn as_ref(&self) -> &str {
match self {
Label::Apple => "apple",
Label::Banana => "banana",
Label::Football => "football",
Label::Vara => "vara",
}
}
}

impl Label {
pub fn list() -> Vec<Label> {
vec![Label::Apple, Label::Banana, Label::Football, Label::Vara]
}
}

#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Resource {
pub labels: Vec<Label>,
pub domain: String,
pub description: String,
pub html: String,
pub styles: String,
}

/// `()` means the contract doesn't process & reply messages at the above written entry point or
/// doesn't implement it.
impl Metadata for ContractMetadata {
/// I/O types for the `init()` entry point.
type Init = In<()>;
/// I/O types for the `handle()` entry point.
type Handle = In<Resource>;
/// Types for miscellaneous scenarios.
type Others = ();
/// The input type for the `handle_reply()` entry point.
type Reply = ();
/// The output type for the `handle_signal()` entry point.
type Signal = ();
/// I/O types for the `state()` entry point.
///
/// You can also specify just an output ([`Out`]) or input ([`In`]) type, if both
/// ([`In`]) are expected like here.
type State = Out<State>;
}
30 changes: 30 additions & 0 deletions alpha/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![no_std]

use alpha_io::{Resource, State};
use gstd::{msg, prelude::*};

/// The resource of the domain.
static mut STATE: Option<State> = None;

// do nothing atm.
#[no_mangle]
extern fn init() {
unsafe {
STATE = Some(vec![]);
}
}

// The `handle()` entry point.
#[no_mangle]
extern fn handle() {
let res = msg::load::<Resource>().expect("Failed to load handle input");
let state = unsafe { STATE.as_mut().expect("State isn't initialized") };
state.push(res);
}

// The `state()` entry point.
#[no_mangle]
extern fn state() {
let state = unsafe { STATE.take().expect("State isn't initialized") };
msg::reply(state, 0).expect("Failed to reply from `state()`");
}
13 changes: 13 additions & 0 deletions alpha/state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "alpha-state"
version.workspace = true
edition.workspace = true
publish.workspace = true

[dependencies]
gstd.workspace = true
gmeta = { workspace = true, features = ["codegen"] }
alpha-io.workspace = true

[build-dependencies]
gear-wasm-builder = { workspace = true, features = ["metawasm"] }
3 changes: 3 additions & 0 deletions alpha/state/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
gear_wasm_builder::build_metawasm();
}
22 changes: 22 additions & 0 deletions alpha/state/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_std]

use alpha_io::{Label, Resource};
use gstd::prelude::*;

#[gmeta::metawasm]
pub mod metafns {
pub type State = alpha_io::State;

/// Returns all domains (pages) that matches the search input.
pub fn labels(_state: State) -> Vec<Label> {
Label::list()
}

/// Search all resouces matching the input label.
pub fn search(state: State, label: Label) -> Vec<Resource> {
state
.into_iter()
.filter(|s| s.labels.contains(&label))
.collect()
}
}
37 changes: 37 additions & 0 deletions alpha/tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use alpha_io::{Label, Resource};
use gtest::{state_args, Program, System};
use std::{fs, path::PathBuf};

fn get_state_binary() -> Vec<u8> {
let bin = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../target/wasm32-unknown-unknown/debug/alpha_state.meta.wasm");
fs::read(bin).unwrap()
}

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

let program = Program::current(&system);
let mut result = program.send_bytes(2, []);
assert!(!result.main_failed());

let input = Resource {
labels: Label::list(),
domain: "vara-go".into(),
description: "test test".into(),
html: "<div>hello, world<div>".into(),
styles: "".into(),
};
result = program.send(2, input);
assert!(!result.main_failed());

let state_bin = get_state_binary();
let search = Label::Vara;
let output: Vec<Resource> = program
.read_state_using_wasm(b"", "search", state_bin, state_args!(search))
.expect("Failed to search source");
// assert_eq!(output.get(&input.domain), Some(input.src).as_ref());
println!("{output:?}");
}
1 change: 0 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ fn test() {
assert_eq!(output.get(&input.domain), Some(input.src).as_ref());
}

#[allow(unused)]
fn get_state_binary() -> Vec<u8> {
fs::read("target/wasm32-unknown-unknown/debug/template_state.meta.wasm").unwrap()
}

0 comments on commit 0e5f489

Please sign in to comment.