diff --git a/api/programs/Cargo.lock b/api/programs/Cargo.lock index cbe16271d2..b49dcfd260 100644 --- a/api/programs/Cargo.lock +++ b/api/programs/Cargo.lock @@ -301,14 +301,6 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" -[[package]] -name = "empty" -version = "0.1.0" -dependencies = [ - "gear-wasm-builder", - "gstd", -] - [[package]] name = "enum-iterator" version = "1.5.0" @@ -1130,23 +1122,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] -name = "test-gas" +name = "test" version = "0.1.0" dependencies = [ "gear-wasm-builder", "gstd", - "parity-scale-codec", - "scale-info", - "test-gas-io", ] [[package]] -name = "test-gas-io" +name = "test-io" version = "0.1.0" dependencies = [ "gmeta", "gstd", "parity-scale-codec", + "primitive-types", "scale-info", ] @@ -1157,23 +1147,11 @@ dependencies = [ "gear-wasm-builder", "gstd", "parity-scale-codec", - "scale-info", - "test-meta-io", + "test-io", "test-meta-state-v1", "test-meta-state-v2", ] -[[package]] -name = "test-meta-io" -version = "0.1.0" -dependencies = [ - "gmeta", - "gstd", - "parity-scale-codec", - "primitive-types", - "scale-info", -] - [[package]] name = "test-meta-state-v1" version = "0.1.0" @@ -1181,7 +1159,7 @@ dependencies = [ "gear-wasm-builder", "gmeta", "gstd", - "test-meta-io", + "test-io", ] [[package]] @@ -1191,17 +1169,7 @@ dependencies = [ "gear-wasm-builder", "gmeta", "gstd", - "test-meta-io", -] - -[[package]] -name = "test-waitlist" -version = "0.1.0" -dependencies = [ - "gear-wasm-builder", - "gstd", - "parity-scale-codec", - "scale-info", + "test-io", ] [[package]] diff --git a/api/programs/Cargo.toml b/api/programs/Cargo.toml index 3d74c1e7e3..6c8539c277 100644 --- a/api/programs/Cargo.toml +++ b/api/programs/Cargo.toml @@ -1,13 +1,10 @@ [workspace] resolver = "2" -members = ["test-gas", "test-waitlist", "test-meta", "empty"] - -[profile.release] -lto = true -opt-level = 's' +members = ["test-meta", 'test'] [workspace.dependencies] +gmeta = { git = "https://github.com/gear-tech/gear.git" } gstd = { git = "https://github.com/gear-tech/gear.git" } scale-info = { version = "2.11.0", default-features = false, features = [ "derive", @@ -19,5 +16,3 @@ codec = { package = "parity-scale-codec", version = "3.6.9", default-features = gear-wasm-builder = { git = "https://github.com/gear-tech/gear.git", features = [ "metawasm", ] } -gmeta = { git = "https://github.com/gear-tech/gear.git" } -primitive-types = { version = "0.12.2", default-features = false } diff --git a/api/programs/empty/src/lib.rs b/api/programs/empty/src/lib.rs deleted file mode 100644 index b437ee0b16..0000000000 --- a/api/programs/empty/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![no_std] - -use gstd::msg; - -#[no_mangle] -unsafe extern "C" fn init() { - msg::reply("ok", 0).unwrap(); -} diff --git a/api/programs/test-gas/Cargo.toml b/api/programs/test-gas/Cargo.toml deleted file mode 100644 index 62fcf6259b..0000000000 --- a/api/programs/test-gas/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "test-gas" -version = "0.1.0" -authors = ["Gear Technologies"] -edition = "2021" - -[dependencies] -gstd.workspace = true -scale-info.workspace = true -codec.workspace = true -test-gas-io = { path = "io" } - -[build-dependencies] -gear-wasm-builder.workspace = true -test-gas-io = { path = "io" } diff --git a/api/programs/test-gas/build.rs b/api/programs/test-gas/build.rs deleted file mode 100644 index 0127ed9ea0..0000000000 --- a/api/programs/test-gas/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use test_gas_io::ProgramMetadata; - -fn main() { - gear_wasm_builder::build_with_metadata::(); -} diff --git a/api/programs/test-gas/io/Cargo.toml b/api/programs/test-gas/io/Cargo.toml deleted file mode 100644 index 23a30efc95..0000000000 --- a/api/programs/test-gas/io/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "test-gas-io" -version = "0.1.0" -edition = "2021" - -[dependencies] -gmeta = { git = "https://github.com/gear-tech/gear.git" } -scale-info = { version = "2.11.0", default-features = false, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "3.6.9", default-features = false, features = ["derive", "full"] } -gstd = { git = "https://github.com/gear-tech/gear.git" } diff --git a/api/programs/test-gas/io/src/lib.rs b/api/programs/test-gas/io/src/lib.rs deleted file mode 100644 index 9fe208b8ff..0000000000 --- a/api/programs/test-gas/io/src/lib.rs +++ /dev/null @@ -1,25 +0,0 @@ -#![no_std] - -extern crate alloc; - -use alloc::string::String; -use codec::{Decode, Encode}; -use gmeta::{InOut, Metadata}; -use gstd::prelude::*; -use scale_info::TypeInfo; - -pub struct ProgramMetadata; - -impl Metadata for ProgramMetadata { - type Init = InOut; - type Handle = InOut; - type Reply = InputStruct; - type Others = InOut<(), ()>; - type Signal = (); - type State = InOut<(), ()>; -} - -#[derive(TypeInfo, Decode, Encode)] -pub struct InputStruct { - pub input: String, -} diff --git a/api/programs/test-gas/src/lib.rs b/api/programs/test-gas/src/lib.rs deleted file mode 100644 index de5eb4b3d7..0000000000 --- a/api/programs/test-gas/src/lib.rs +++ /dev/null @@ -1,30 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2022 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -#![no_std] - -// Reexport of types -pub use test_gas_io::*; - -mod wasm; - -mod exports { - mod code { - include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); - } -} diff --git a/api/programs/test-gas/src/wasm.rs b/api/programs/test-gas/src/wasm.rs deleted file mode 100644 index eea6600a3d..0000000000 --- a/api/programs/test-gas/src/wasm.rs +++ /dev/null @@ -1,30 +0,0 @@ -use gstd::msg; -use test_gas_io::InputStruct; - -#[no_mangle] -pub unsafe extern "C" fn init() { - let payload: InputStruct = msg::load().expect("Unable to decode message"); - if payload.input != "Init" { - panic!("Wrong input"); - } - msg::reply("Init", 0).expect("Unable to send relpy"); -} - -#[gstd::async_main] -async fn main() { - let payload: InputStruct = msg::load().expect("Unable to decode message"); - - if payload.input != "Handle" { - panic!("Wrong input"); - } - - let response: InputStruct = msg::send_bytes_for_reply_as(msg::source(), b"PING", 0, 0) - .expect("Unable to send message for reply") - .await - .expect("Error in async"); - - if response.input != "Reply" { - panic!("Wrong input"); - } - msg::reply_bytes(b"ok", 0).unwrap(); -} diff --git a/api/programs/test-meta/Cargo.toml b/api/programs/test-meta/Cargo.toml index 77f1e44b34..e64d286015 100644 --- a/api/programs/test-meta/Cargo.toml +++ b/api/programs/test-meta/Cargo.toml @@ -3,19 +3,16 @@ name = "test-meta" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] gstd.workspace = true -scale-info.workspace = true codec.workspace = true -test-meta-io = { path = "io" } +test-io = { path = "io" } test-meta-state-v1 = { path = "state-v1", default-features = false, optional = true } test-meta-state-v2 = { path = "state-v2", default-features = false, optional = true } [build-dependencies] -test-meta-io = { path = "io" } gear-wasm-builder.workspace = true +test-io = { path = "io" } [features] default = ["std"] diff --git a/api/programs/test-meta/build.rs b/api/programs/test-meta/build.rs index 85e08a1d5c..1af17d6e84 100644 --- a/api/programs/test-meta/build.rs +++ b/api/programs/test-meta/build.rs @@ -1,22 +1,4 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2022 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -use test_meta_io::ProgramMetadata; +use test_io::ProgramMetadata; fn main() { gear_wasm_builder::build_with_metadata::(); diff --git a/api/programs/test-meta/io/Cargo.toml b/api/programs/test-meta/io/Cargo.toml index 2705aa974b..c95848031b 100644 --- a/api/programs/test-meta/io/Cargo.toml +++ b/api/programs/test-meta/io/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "test-meta-io" +name = "test-io" version = "0.1.0" edition = "2021" [dependencies] +primitive-types = { version = "0.12.2", default-features = false } +gmeta = { workspace = true, features = ["codegen"] } +gstd.workspace = true scale-info.workspace = true codec.workspace = true -gstd.workspace = true -gmeta.workspace = true -primitive-types.workspace = true diff --git a/api/programs/test-meta/io/src/lib.rs b/api/programs/test-meta/io/src/lib.rs index 9e35987709..6d85ba3162 100644 --- a/api/programs/test-meta/io/src/lib.rs +++ b/api/programs/test-meta/io/src/lib.rs @@ -2,7 +2,7 @@ extern crate alloc; -use alloc::string::String; +use alloc::{string::String, vec::Vec}; use codec::{Decode, Encode}; use gmeta::{InOut, Metadata}; use gstd::{ @@ -18,8 +18,8 @@ pub struct ProgramMetadata; impl Metadata for ProgramMetadata { type Init = InOut, BTreeMap>; type Handle = InOut; + type Reply = ReplyType; type Others = InOut<(), ()>; - type Reply = String; type Signal = H256; type State = InOut, Vec>; } @@ -40,8 +40,7 @@ pub enum Action { Two(Vec), Three { field1: Result<(u8, String), i32> }, Four(SomeStruct), - Five(SomeStruct), - Six(ActorId, EmptyStruct), + Input(String), } #[derive(TypeInfo, Encode, Decode, Clone)] @@ -72,3 +71,14 @@ pub enum State { DappMeta(String), Data(Vec), } + +#[derive(TypeInfo, Decode, Encode)] +pub struct InputStruct { + pub input: String, +} + +#[derive(TypeInfo, Decode, Encode)] +pub enum ReplyType { + TextReply(String), + StructReply(InputStruct), +} diff --git a/api/programs/test-meta/src/lib.rs b/api/programs/test-meta/src/lib.rs index e547325613..80f92006fe 100644 --- a/api/programs/test-meta/src/lib.rs +++ b/api/programs/test-meta/src/lib.rs @@ -19,7 +19,7 @@ #![no_std] // Reexport of types -pub use test_meta_io::*; +pub use test_io::*; // For wasm compilation #[cfg(not(feature = "std"))] diff --git a/api/programs/test-meta/src/wasm.rs b/api/programs/test-meta/src/wasm.rs index 13e70e3419..85212c86f6 100644 --- a/api/programs/test-meta/src/wasm.rs +++ b/api/programs/test-meta/src/wasm.rs @@ -1,16 +1,11 @@ -use crate::{Action, Id, Person, Wallet}; -use gstd::{ - collections::{BTreeMap, BTreeSet}, - debug, msg, - prelude::*, -}; -use test_meta_io::EmptyStruct; +use gstd::{collections::{BTreeMap, BTreeSet}, debug, msg, prelude::*}; +use test_io::{Action, EmptyStruct, Id, InputStruct, Person, Wallet}; static mut STATE: Vec = Vec::new(); #[no_mangle] -unsafe extern "C" fn init() { - let _: BTreeSet = msg::load().expect("Unable to decode message"); +pub unsafe extern "C" fn init() { + let _: BTreeSet = msg::load().expect("Failed to load init arguments"); let mut res: BTreeMap = BTreeMap::new(); @@ -49,12 +44,16 @@ unsafe extern "C" fn init() { #[gstd::async_main] async fn main() { - let action: Action = msg::load().unwrap(); + let action: Action = msg::load().expect("Failed to load Action"); match action { - Action::One(_) => { - msg::send_with_gas(msg::source(), EmptyStruct { empty: () }, 10000000, 10_000_000_000_000).unwrap() - } + Action::One(_) => msg::send_with_gas( + msg::source(), + EmptyStruct { empty: () }, + 10000000, + 10_000_000_000_000, + ) + .unwrap(), Action::Four(_) => { let response: String = msg::send_for_reply_as(msg::source(), "reply", 0, 0) .expect("Unable to send msg for reply") @@ -62,12 +61,27 @@ async fn main() { .expect("Error in async"); msg::reply(response, 0).unwrap() } + Action::Input(input) => { + if input != "Handle" { + panic!("Wrong input"); + } + + let response: InputStruct = msg::send_bytes_for_reply_as(msg::source(), b"PING", 0, 0) + .expect("Unable to send message for reply") + .await + .expect("Error in async"); + + if response.input != "Reply" { + panic!("Wrong input"); + } + msg::reply_bytes(b"ok", 0).unwrap() + } _ => msg::reply("ok", 0).unwrap(), }; } #[no_mangle] -extern "C" fn state() { +pub unsafe extern "C" fn state() { debug!("{:?}", msg::load_bytes()); let input: Option = msg::load().expect("Unable to load input"); let mut result: Vec = Vec::new(); diff --git a/api/programs/test-meta/state-v1/Cargo.toml b/api/programs/test-meta/state-v1/Cargo.toml index 3efc3700c0..4eb97195c4 100644 --- a/api/programs/test-meta/state-v1/Cargo.toml +++ b/api/programs/test-meta/state-v1/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] gmeta = { workspace = true, features = ["codegen"] } gstd.workspace = true -test-meta-io = { path = "../io" } +test-io = { path = "../io" } [build-dependencies] gear-wasm-builder = { workspace = true, features = ["metawasm"] } diff --git a/api/programs/test-meta/state-v1/src/wasm.rs b/api/programs/test-meta/state-v1/src/wasm.rs index 73b2b5dae4..b31d4cc7ae 100644 --- a/api/programs/test-meta/state-v1/src/wasm.rs +++ b/api/programs/test-meta/state-v1/src/wasm.rs @@ -1,6 +1,6 @@ use gmeta::metawasm; use gstd::prelude::*; -use test_meta_io::Wallet; +use test_io::Wallet; #[metawasm] pub mod metafns { diff --git a/api/programs/test-meta/state-v2/Cargo.toml b/api/programs/test-meta/state-v2/Cargo.toml index 1cb14f9bc9..259016af9d 100644 --- a/api/programs/test-meta/state-v2/Cargo.toml +++ b/api/programs/test-meta/state-v2/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] gstd.workspace = true gmeta = { workspace = true, features = ["codegen"] } -test-meta-io = { path = "../io" } +test-io = { path = "../io" } [build-dependencies] gear-wasm-builder = { workspace = true, features = ["metawasm"] } diff --git a/api/programs/test-meta/state-v2/src/wasm.rs b/api/programs/test-meta/state-v2/src/wasm.rs index 339b851349..f1224e0e71 100644 --- a/api/programs/test-meta/state-v2/src/wasm.rs +++ b/api/programs/test-meta/state-v2/src/wasm.rs @@ -1,6 +1,6 @@ use gmeta::metawasm; use gstd::prelude::*; -use test_meta_io::{Id, Person, Wallet}; +use test_io::{Id, Person, Wallet}; #[metawasm] pub mod metafns { diff --git a/api/programs/test-waitlist/Cargo.toml b/api/programs/test-waitlist/Cargo.toml deleted file mode 100644 index b2a1e90224..0000000000 --- a/api/programs/test-waitlist/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "test-waitlist" -version = "0.1.0" -authors = ["Gear Technologies"] -edition = "2021" - -[dependencies] -gstd.workspace = true -scale-info.workspace = true -codec.workspace = true - -[build-dependencies] -gear-wasm-builder.workspace = true diff --git a/api/programs/test-waitlist/build.rs b/api/programs/test-waitlist/build.rs deleted file mode 100644 index 6836d02c06..0000000000 --- a/api/programs/test-waitlist/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - gear_wasm_builder::build(); -} diff --git a/api/programs/empty/Cargo.toml b/api/programs/test/Cargo.toml similarity index 91% rename from api/programs/empty/Cargo.toml rename to api/programs/test/Cargo.toml index f6aa5deea5..4213a15adc 100644 --- a/api/programs/empty/Cargo.toml +++ b/api/programs/test/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "empty" +name = "test" version = "0.1.0" authors = ["Gear Technologies"] edition = "2021" diff --git a/api/programs/empty/build.rs b/api/programs/test/build.rs similarity index 100% rename from api/programs/empty/build.rs rename to api/programs/test/build.rs diff --git a/api/programs/test-waitlist/src/lib.rs b/api/programs/test/src/lib.rs similarity index 52% rename from api/programs/test-waitlist/src/lib.rs rename to api/programs/test/src/lib.rs index d5de6a72ca..89c4dea826 100644 --- a/api/programs/test-waitlist/src/lib.rs +++ b/api/programs/test/src/lib.rs @@ -2,6 +2,12 @@ extern crate gstd; +#[no_mangle] +unsafe extern "C" fn init() { + gstd::msg::reply("ok", 0).unwrap(); +} + + #[gstd::async_main] async fn main() { gstd::exec::wait(); diff --git a/api/test/Code.test.ts b/api/test/Code.test.ts index 01c0b73c06..029bf69395 100644 --- a/api/test/Code.test.ts +++ b/api/test/Code.test.ts @@ -1,14 +1,13 @@ import { HexString } from '@polkadot/util/types'; -import { join } from 'path'; import { readFileSync } from 'fs'; -import { TARGET } from './config'; +import { TEST_CODE } from './config'; import { getAccount, sendTransaction, sleep } from './utilsFunctions'; import { getApi } from './common'; const api = getApi(); const accounts = {}; -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_waitlist.opt.wasm'))); +const code = Uint8Array.from(readFileSync(TEST_CODE)); let codeId: HexString; beforeAll(async () => { diff --git a/api/test/Gas.test.ts b/api/test/Gas.test.ts index dac9b0063a..0a7b83e087 100644 --- a/api/test/Gas.test.ts +++ b/api/test/Gas.test.ts @@ -1,12 +1,10 @@ import { HexString } from '@polkadot/util/types'; -import { bufferToU8a } from '@polkadot/util'; import { KeyringPair } from '@polkadot/keyring/types'; -import { join } from 'path'; import { readFileSync } from 'fs'; import { u64 } from '@polkadot/types-codec'; +import { TEST_META, TEST_META_CODE } from './config'; import { ProgramMetadata } from '../src'; -import { TARGET, TEST_GAS_META } from './config'; import { checkInit, getAccount, sendTransaction, sleep } from './utilsFunctions'; import { GasInfo } from '../src/types'; import { decodeAddress } from '../src/utils'; @@ -19,8 +17,8 @@ let programId: HexString; let codeId: HexString; let messageId: HexString; -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_gas.opt.wasm'))); -const meta = ProgramMetadata.from(`0x${readFileSync(TEST_GAS_META, 'utf-8')}`); +const code = Uint8Array.from(readFileSync(TEST_META_CODE)); +const meta = ProgramMetadata.from(`0x${readFileSync(TEST_META, 'utf-8')}`); const gasLimits: { init?: u64; handle?: u64; reply?: u64 } = { init: undefined, @@ -41,7 +39,7 @@ afterAll(async () => { describe('Calculate gas', () => { test('Get init gas spent (upload)', async () => { - const gas: GasInfo = await api.program.calculateGas.initUpload(aliceRaw, code, { input: 'Init' }, 0, true, meta); + const gas: GasInfo = await api.program.calculateGas.initUpload(aliceRaw, code, [1, 2, 3], 0, true, meta); expect(gas).toBeDefined(); expect(gas.toHuman()).toHaveProperty('min_limit'); expect(gas.min_limit.gtn(0)).toBeTruthy(); @@ -54,7 +52,7 @@ describe('Calculate gas', () => { test('Upload program', async () => { expect(gasLimits.init).toBeDefined(); - const program = api.program.upload({ code, gasLimit: gasLimits.init as u64, initPayload: { input: 'Init' } }, meta); + const program = api.program.upload({ code, gasLimit: gasLimits.init as u64, initPayload: [1, 2, 3] }, meta); programId = program.programId; codeId = program.codeId; const initStatus = checkInit(api, programId); @@ -63,7 +61,7 @@ describe('Calculate gas', () => { }); test('Get init gas spent (create)', async () => { - const gas: GasInfo = await api.program.calculateGas.initCreate(aliceRaw, codeId, { input: 'Init' }, 0, true, meta); + const gas: GasInfo = await api.program.calculateGas.initCreate(aliceRaw, codeId, [1, 2, 3], 0, true, meta); expect(gas).toBeDefined(); expect(gas.toHuman()).toHaveProperty('min_limit'); expect(gas.min_limit.gtn(0)).toBeTruthy(); @@ -76,10 +74,7 @@ describe('Calculate gas', () => { test('Create program', async () => { expect(gasLimits.init).toBeDefined(); - const program = api.program.create( - { codeId, gasLimit: gasLimits.init as u64, initPayload: { input: 'Init' } }, - meta, - ); + const program = api.program.create({ codeId, gasLimit: gasLimits.init as u64, initPayload: [1, 2, 3] }, meta); programId = program.programId; const initStatus = checkInit(api, programId); await sendTransaction(program.extrinsic, alice, ['MessageQueued']); @@ -111,7 +106,7 @@ describe('Calculate gas', () => { const tx = await api.message.send( { destination: programId, - payload: { input: 'Handle' }, + payload: { Input: 'Handle' }, gasLimit: (gasLimits.handle as u64).muln(2), value: 10_000_000_000_000, }, @@ -131,7 +126,14 @@ describe('Calculate gas', () => { test('Calculate reply gas', async () => { expect(messageId).toBeDefined(); - const gas = await api.program.calculateGas.reply(aliceRaw, messageId, { input: 'Reply' }, 0, true, meta); + const gas = await api.program.calculateGas.reply( + aliceRaw, + messageId, + { StructReply: { input: 'Reply' } }, + 0, + true, + meta, + ); expect(gas).toBeDefined(); expect(gas.toHuman()).toHaveProperty('min_limit'); gasLimits.reply = gas.min_limit; @@ -146,7 +148,7 @@ describe('Calculate gas', () => { const tx = await api.message.sendReply( { replyToId: messageId, - payload: { input: 'Reply' }, + payload: { StructReply: { input: 'Reply' } }, gasLimit: gasLimits.reply!, }, meta, diff --git a/api/test/Message.test.ts b/api/test/Message.test.ts index 30b92955e6..7f731c891c 100644 --- a/api/test/Message.test.ts +++ b/api/test/Message.test.ts @@ -1,10 +1,9 @@ import { HexString } from '@polkadot/util/types'; import { KeyringPair } from '@polkadot/keyring/types'; -import { join } from 'path'; import { readFileSync } from 'fs'; +import { TEST_META, TEST_META_CODE } from './config'; import { ProgramMetadata } from '../src'; -import { TARGET, TEST_META_META } from './config'; import { checkInit, getAccount, sendTransaction, sleep } from './utilsFunctions'; import { decodeAddress } from '../src/utils'; import { getApi } from './common'; @@ -14,8 +13,8 @@ let alice: KeyringPair; let programId: HexString; let messageToClaim: HexString; -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_meta.opt.wasm'))); -const metaHex: HexString = `0x${readFileSync(TEST_META_META, 'utf-8')}`; +const code = Uint8Array.from(readFileSync(TEST_META_CODE)); +const metaHex: HexString = `0x${readFileSync(TEST_META, 'utf-8')}`; const metadata = ProgramMetadata.from(metaHex); beforeAll(async () => { diff --git a/api/test/Meta.test.ts b/api/test/Meta.test.ts index 6880c88600..d0d6e98bdf 100644 --- a/api/test/Meta.test.ts +++ b/api/test/Meta.test.ts @@ -1,4 +1,4 @@ -import { TEST_META_META } from './config'; +import { TEST_META } from './config'; import fs from 'fs'; import { ProgramMetadata, encodePayload } from '../src'; @@ -6,7 +6,7 @@ import { ProgramMetadata, encodePayload } from '../src'; let meta: ProgramMetadata; beforeAll(() => { - const hex = fs.readFileSync(TEST_META_META, 'utf-8'); + const hex = fs.readFileSync(TEST_META, 'utf-8'); meta = ProgramMetadata.from(`0x${hex}`); }); @@ -15,10 +15,10 @@ describe('Get type definitions', () => { expect(meta.types).toEqual({ init: { input: 0, output: 3 }, handle: { input: 7, output: 4 }, - reply: 4, + reply: 21, others: { input: null, output: null }, - signal: 26, - state: { input: 27, output: 29 }, + signal: 23, + state: { input: 24, output: 26 }, }); }); @@ -129,23 +129,22 @@ describe('Get type definitions', () => { array32: ['U8', 32], actor: 'ActorId', }, - Five: { - array8: ['Str', 8], - array32: [['U8', 'U16'], 32], - actor: 'ActorId', - }, - Six: ['ActorId', { empty: null }], + Input: 'Str', }, }); expect(meta.getTypeDef(7, true)).toEqual({ - name: 'TestMetaIoAction', + name: 'TestIoAction', kind: 'variant', type: { One: { - kind: 'option', name: 'Option', + kind: 'option', type: { - None: { name: 'None', kind: 'none', type: null }, + None: { + name: 'None', + kind: 'none', + type: null, + }, Some: { name: 'Str', kind: 'primitive', @@ -160,14 +159,22 @@ describe('Get type definitions', () => { name: 'X', kind: 'tuple', type: [ - { name: 'U8', kind: 'primitive', type: 'U8' }, - { name: 'U16', kind: 'primitive', type: 'U16' }, + { + name: 'U8', + kind: 'primitive', + type: 'U8', + }, + { + name: 'U16', + kind: 'primitive', + type: 'U16', + }, ], }, }, Three: { - kind: 'composite', name: null, + kind: 'composite', type: { field1: { name: 'Result<(U8, Str), I32>', @@ -177,8 +184,16 @@ describe('Get type definitions', () => { name: '(U8, Str)', kind: 'tuple', type: [ - { name: 'U8', kind: 'primitive', type: 'U8' }, - { name: 'Str', kind: 'primitive', type: 'Str' }, + { + name: 'U8', + kind: 'primitive', + type: 'U8', + }, + { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, ], }, Err: { @@ -197,55 +212,34 @@ describe('Get type definitions', () => { array8: { name: '[U128;8]', kind: 'array', - type: { type: 'U128', name: 'U128', kind: 'primitive' }, + type: { + name: 'U128', + kind: 'primitive', + type: 'U128', + }, len: 8, }, - array32: { name: '[U8;32]', kind: 'array', type: { type: 'U8', name: 'U8', kind: 'primitive' }, len: 32 }, - actor: { name: 'ActorId', kind: 'actorid', type: 'actorid' }, - }, - }, - Five: { - name: 'SomeStruct', - kind: 'composite', - type: { - array8: { name: '[Str;8]', kind: 'array', type: { type: 'Str', name: 'Str', kind: 'primitive' }, len: 8 }, array32: { - name: '[X;32]', + name: '[U8;32]', kind: 'array', type: { - name: 'X', - kind: 'tuple', - type: [ - { name: 'U8', kind: 'primitive', type: 'U8' }, - { name: 'U16', kind: 'primitive', type: 'U16' }, - ], + name: 'U8', + kind: 'primitive', + type: 'U8', }, len: 32, }, - actor: { name: 'ActorId', kind: 'actorid', type: 'actorid' }, - }, - }, - Six: { - kind: 'tuple', - name: null, - type: [ - { - kind: 'actorid', + actor: { name: 'ActorId', + kind: 'actorid', type: 'actorid', }, - { - kind: 'composite', - name: 'EmptyStruct', - type: { - empty: { - kind: 'empty', - name: '()', - type: null, - }, - }, - }, - ], + }, + }, + Input: { + name: 'Str', + kind: 'primitive', + type: 'Str', }, }, }); @@ -420,77 +414,261 @@ describe('Get type definitions', () => { test('Get type structure 21', () => { expect(meta.getTypeDef(21)).toEqual({ - array8: ['Str', 8], - array32: [['U8', 'U16'], 32], - actor: 'ActorId', + _variants: { + TextReply: 'Str', + StructReply: { + input: 'Str', + }, + }, }); expect(meta.getTypeDef(21, true)).toEqual({ - name: 'SomeStruct', - kind: 'composite', + name: 'TestIoReplyType', + kind: 'variant', type: { - array8: { name: '[Str;8]', kind: 'array', type: { type: 'Str', name: 'Str', kind: 'primitive' }, len: 8 }, - array32: { - name: '[X;32]', - kind: 'array', + TextReply: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + StructReply: { + name: 'InputStruct', + kind: 'composite', type: { - name: 'X', - kind: 'tuple', - type: [ - { name: 'U8', kind: 'primitive', type: 'U8' }, - { name: 'U16', kind: 'primitive', type: 'U16' }, - ], + input: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, }, - len: 32, }, - actor: { name: 'ActorId', kind: 'actorid', type: 'actorid' }, }, }); }); test('Get type structure 22', () => { - expect(meta.getTypeDef(22)).toEqual(['Str', 8]); + expect(meta.getTypeDef(22)).toEqual({ + input: 'Str', + }); expect(meta.getTypeDef(22, true)).toEqual({ - name: '[Str;8]', - kind: 'array', - type: { type: 'Str', name: 'Str', kind: 'primitive' }, - len: 8, + name: 'InputStruct', + kind: 'composite', + type: { + input: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + }, }); }); test('Get type structure 23', () => { - expect(meta.getTypeDef(23)).toEqual([['U8', 'U16'], 32]); + expect(meta.getTypeDef(23)).toEqual('H256'); expect(meta.getTypeDef(23, true)).toEqual({ - name: '[X;32]', - kind: 'array', - type: { - name: 'X', - kind: 'tuple', - type: [ - { name: 'U8', kind: 'primitive', type: 'U8' }, - { name: 'U16', kind: 'primitive', type: 'U16' }, - ], - }, - len: 32, + name: 'H256', + kind: 'primitive', + type: 'H256', }); }); test('Get type structure 24', () => { - expect(meta.getTypeDef(24)).toEqual({ empty: null }); + expect(meta.getTypeDef(24)).toEqual({ + _variants: { + None: null, + Some: 'U32', + }, + }); expect(meta.getTypeDef(24, true)).toEqual({ - name: 'EmptyStruct', - kind: 'composite', - type: { empty: { name: '()', kind: 'empty', type: null } }, + name: 'Option', + kind: 'option', + type: { + None: { + name: 'None', + kind: 'none', + type: null, + }, + Some: { + name: 'U32', + kind: 'primitive', + type: 'U32', + }, + }, }); }); test('Get type structure 25', () => { - expect(meta.getTypeDef(25)).toEqual(null); - expect(meta.getTypeDef(25, true)).toEqual({ name: '()', kind: 'empty', type: null }); + expect(meta.getTypeDef(25)).toEqual('U32'); + expect(meta.getTypeDef(25, true)).toEqual({ + name: 'U32', + kind: 'primitive', + type: 'U32', + }); }); test('Get type structure 26', () => { - expect(meta.getTypeDef(26)).toEqual('H256'); - expect(meta.getTypeDef(26, true)).toEqual({ kind: 'primitive', name: 'H256', type: 'H256' }); + expect(meta.getTypeDef(26)).toEqual([ + { + id: { + decimal: 'U128', + hex: ['U8'], + }, + person: { + surname: 'Str', + name: 'Str', + }, + }, + ]); + expect(meta.getTypeDef(26, true)).toEqual({ + name: 'Vec', + kind: 'sequence', + type: { + name: 'Wallet', + kind: 'composite', + type: { + id: { + name: 'Id', + kind: 'composite', + type: { + decimal: { + name: 'U128', + kind: 'primitive', + type: 'U128', + }, + hex: { + name: 'Vec', + kind: 'sequence', + type: { + name: 'U8', + kind: 'primitive', + type: 'U8', + }, + }, + }, + }, + person: { + name: 'Person', + kind: 'composite', + type: { + surname: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + name: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + }, + }, + }, + }, + }); + }); + + test('Get type structure 27', () => { + expect(meta.getTypeDef(27)).toEqual({ + id: { + decimal: 'U128', + hex: ['U8'], + }, + person: { + surname: 'Str', + name: 'Str', + }, + }); + expect(meta.getTypeDef(27, true)).toEqual({ + name: 'Wallet', + kind: 'composite', + type: { + id: { + name: 'Id', + kind: 'composite', + type: { + decimal: { + name: 'U128', + kind: 'primitive', + type: 'U128', + }, + hex: { + name: 'Vec', + kind: 'sequence', + type: { + name: 'U8', + kind: 'primitive', + type: 'U8', + }, + }, + }, + }, + person: { + name: 'Person', + kind: 'composite', + type: { + surname: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + name: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + }, + }, + }, + }); + }); + + test('Get type structure 28', () => { + expect(meta.getTypeDef(28)).toEqual({ + decimal: 'U128', + hex: ['U8'], + }); + expect(meta.getTypeDef(28, true)).toEqual({ + name: 'Id', + kind: 'composite', + type: { + decimal: { + name: 'U128', + kind: 'primitive', + type: 'U128', + }, + hex: { + name: 'Vec', + kind: 'sequence', + type: { + name: 'U8', + kind: 'primitive', + type: 'U8', + }, + }, + }, + }); + }); + + test('Get type structure 29', () => { + expect(meta.getTypeDef(29)).toEqual({ + surname: 'Str', + name: 'Str', + }); + expect(meta.getTypeDef(29, true)).toEqual({ + name: 'Person', + kind: 'composite', + type: { + surname: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + name: { + name: 'Str', + kind: 'primitive', + type: 'Str', + }, + }, + }); }); }); diff --git a/api/test/Program.test.ts b/api/test/Program.test.ts index 3bba871d6c..138327b586 100644 --- a/api/test/Program.test.ts +++ b/api/test/Program.test.ts @@ -2,11 +2,10 @@ import { HexString } from '@polkadot/util/types'; import { KeyringPair } from '@polkadot/keyring/types'; import { blake2AsHex } from '@polkadot/util-crypto'; import { bufferToU8a } from '@polkadot/util'; -import { join } from 'path'; import { readFileSync } from 'fs'; +import { TEST_META, TEST_META_CODE } from './config'; import { ProgramMetadata } from '../src'; -import { TARGET, TEST_META_META } from './config'; import { checkInit, getAccount, sendTransaction, sleep, waitForPausedProgram } from './utilsFunctions'; import { getApi } from './common'; @@ -19,8 +18,8 @@ let metaHash: HexString; let expiredBN: number; let pausedBlockHash: HexString; -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_meta.opt.wasm'))); -const metaHex: HexString = `0x${readFileSync(TEST_META_META, 'utf-8')}`; +const code = Uint8Array.from(readFileSync(TEST_META_CODE)); +const metaHex: HexString = `0x${readFileSync(TEST_META, 'utf-8')}`; beforeAll(async () => { await api.isReadyOrError; @@ -92,7 +91,7 @@ describe('New Program', () => { pausedBlockHash = blockHash; }); - test('Сreate program', async () => { + test('Create program', async () => { expect(codeId).toBeDefined(); const metadata = ProgramMetadata.from(metaHex); diff --git a/api/test/State.test.ts b/api/test/State.test.ts index 75c94efbee..1084f83aa5 100644 --- a/api/test/State.test.ts +++ b/api/test/State.test.ts @@ -3,15 +3,15 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { join } from 'path'; import { readFileSync } from 'fs'; +import { TARGET, TEST_META, TEST_META_CODE } from './config'; import { CreateType, HumanTypesRepr, ProgramMetadata, StateMetadata, getStateMetadata } from '../src'; -import { TARGET, TEST_META_META } from './config'; import { checkInit, getAccount, sleep } from './utilsFunctions'; import { getApi } from './common'; const api = getApi(); let alice: KeyringPair; -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_meta.opt.wasm'))); +const code = Uint8Array.from(readFileSync(TEST_META_CODE)); const stateV1 = Uint8Array.from(readFileSync(join(TARGET, 'test_meta_state_v1.meta.wasm'))); @@ -20,7 +20,7 @@ let stateV1Meta: StateMetadata; const stateV2 = Uint8Array.from(readFileSync(join(TARGET, 'test_meta_state_v2.meta.wasm'))); let stateV2Meta: StateMetadata; -const metaHex = `0x${readFileSync(TEST_META_META, 'utf-8')}` as HexString; +const metaHex = `0x${readFileSync(TEST_META, 'utf-8')}` as HexString; const meta = ProgramMetadata.from(metaHex); diff --git a/api/test/Voucher.test.ts b/api/test/Voucher.test.ts index d9f6e38047..12bb7fcbb9 100644 --- a/api/test/Voucher.test.ts +++ b/api/test/Voucher.test.ts @@ -3,8 +3,8 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { join } from 'path'; import { readFileSync } from 'fs'; +import { TARGET, TEST_META, TEST_META_CODE } from './config'; import { ProgramMetadata, decodeAddress } from '../src'; -import { TARGET, TEST_META_META } from './config'; import { checkInit, getAccount, sendTransaction, sleep } from './utilsFunctions'; import { getApi } from './common'; @@ -19,8 +19,8 @@ let voucher: string; let validUpTo: number; const api = getApi(); -const code = Uint8Array.from(readFileSync(join(TARGET, 'test_meta.opt.wasm'))); -const metaHex: HexString = `0x${readFileSync(TEST_META_META, 'utf-8')}`; +const code = Uint8Array.from(readFileSync(TEST_META_CODE)); +const metaHex: HexString = `0x${readFileSync(TEST_META, 'utf-8')}`; const metadata = ProgramMetadata.from(metaHex); beforeAll(async () => { @@ -189,7 +189,7 @@ describe('Voucher', () => { account: charlieRaw, gasLimit: 20_000_000_000, value: 0, - payload: 'Charlie', + payload: { TextReply: 'Charlie' }, }, metadata, metadata.types.reply!, @@ -225,7 +225,7 @@ describe('Voucher', () => { test('Upload code with voucher', async () => { expect(voucher).toBeDefined(); - const code = new Uint8Array(readFileSync(join(TARGET, 'empty.opt.wasm')).buffer); + const code = new Uint8Array(readFileSync(join(TARGET, 'test.wasm')).buffer); const { extrinsic, codeHash } = await api.code.upload(code); diff --git a/api/test/Waitlist.test.ts b/api/test/Waitlist.test.ts index 6cbe0c228b..e045e68be0 100644 --- a/api/test/Waitlist.test.ts +++ b/api/test/Waitlist.test.ts @@ -1,16 +1,14 @@ import { HexString } from '@polkadot/util/types'; import { KeyringPair } from '@polkadot/keyring/types'; -import { join } from 'path'; import { CreateType, MessageWaitedData } from '../src'; -import { TARGET } from './config'; +import { TEST_CODE } from './config'; import { checkInit, getAccount, listenToMessageWaited, sendTransaction, sleep } from './utilsFunctions'; import { readFileSync } from 'fs'; import { getApi } from './common'; const api = getApi(); -const CODE_PATH = join(TARGET, 'test_waitlist.opt.wasm'); let alice: KeyringPair; let programId: HexString; let messageId: HexString; @@ -18,7 +16,7 @@ let messageWaited: (messageId: HexString) => Promise; beforeAll(async () => { await api.isReadyOrError; - const code = Uint8Array.from(readFileSync(CODE_PATH)); + const code = Uint8Array.from(readFileSync(TEST_CODE)); alice = await getAccount('//Alice'); programId = api.program.upload({ code, gasLimit: 20_000_000_000 }).programId; const init = checkInit(api, programId); diff --git a/api/test/config.ts b/api/test/config.ts index dc01a36d79..0afea4a4c9 100644 --- a/api/test/config.ts +++ b/api/test/config.ts @@ -2,6 +2,7 @@ export const TEST_WASM_DIR = 'test/wasm'; export const TARGET = 'programs/target/wasm32-unknown-unknown/release'; export const GEAR_EXAMPLES_WASM_DIR = 'test/wasm/examples'; export const PROGRAMS_DIR = 'programs'; -export const TEST_META_META = `${TARGET}/test_meta.meta.txt`; -export const TEST_GAS_META = `${TARGET}/test_gas.meta.txt`; +export const TEST_META = `${TARGET}/test_meta.meta.txt`; +export const TEST_CODE = `${TARGET}/test.opt.wasm`; +export const TEST_META_CODE = `${TARGET}/test_meta.opt.wasm`; export const WS_ADDRESS = 'ws://127.0.0.1:9944';