Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into al-backend-post-rem…
Browse files Browse the repository at this point in the history
…oval-refactoring
  • Loading branch information
ark0f committed Sep 19, 2023
2 parents 04a447b + 3e3254e commit 0325938
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion examples/autoreply/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#![no_std]

extern crate alloc;

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
Expand All @@ -31,6 +33,7 @@ mod wasm;

#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use gstd::ActorId;
use gtest::{Program, System};

Expand Down Expand Up @@ -58,7 +61,9 @@ mod tests {
assert!(!res.main_failed());

// Check whether the auto-reply was received
let reply_received: bool = prog2.read_state().expect("Failed to read state");
let reply_received: bool = prog2
.read_state(Vec::<u8>::default())
.expect("Failed to read state");
assert!(reply_received);
}
}
24 changes: 20 additions & 4 deletions examples/new-meta/tests/read_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn read_state_bytes_returns_full_state() {
let program = initialize_current_program(&system);

let actual_state = program
.read_state_bytes()
.read_state_bytes(Default::default())
.expect("Unable to read program state");

let expected_state = Wallet::test_sequence().encode();
Expand All @@ -26,7 +26,12 @@ fn read_state_bytes_with_wasm_func_returns_transformed_state() {
assert!(META_EXPORTS_V1.contains(&FUNC_NAME));

let actual_state = program
.read_state_bytes_using_wasm(FUNC_NAME, META_WASM_V1.to_vec(), state_args_encoded!())
.read_state_bytes_using_wasm(
Default::default(),
FUNC_NAME,
META_WASM_V1.to_vec(),
state_args_encoded!(),
)
.expect("Unable to read program state");

let expected_state = Wallet::test_sequence().first().encode();
Expand All @@ -47,6 +52,7 @@ fn read_state_bytes_with_parameterized_wasm_func_returns_transformed_state() {

let actual_state = program
.read_state_bytes_using_wasm(
Default::default(),
FUNC_NAME,
META_WASM_V2.to_vec(),
state_args_encoded!(&other_person),
Expand All @@ -73,6 +79,7 @@ fn read_state_bytes_with_two_args_wasm_func_returns_transformed_state() {

let actual_state = program
.read_state_bytes_using_wasm(
Default::default(),
FUNC_NAME,
META_WASM_V2.to_vec(),
state_args_encoded!(name.clone(), surname.clone()),
Expand All @@ -92,7 +99,9 @@ fn read_state_returns_full_state() {
let system = System::new();
let program = initialize_current_program(&system);

let actual_state: Vec<Wallet> = program.read_state().expect("Unable to read program state");
let actual_state: Vec<Wallet> = program
.read_state(Vec::<u8>::default())
.expect("Unable to read program state");

let expected_state = Wallet::test_sequence();

Expand All @@ -107,7 +116,12 @@ fn read_state_with_wasm_func_returns_transformed_state() {
assert!(META_EXPORTS_V1.contains(&FUNC_NAME));

let actual_state: Option<Wallet> = program
.read_state_using_wasm(FUNC_NAME, META_WASM_V1.to_vec(), state_args!())
.read_state_using_wasm(
Vec::<u8>::default(),
FUNC_NAME,
META_WASM_V1.to_vec(),
state_args!(),
)
.expect("Unable to read program state");

let expected_state = Wallet::test_sequence().first().cloned();
Expand All @@ -128,6 +142,7 @@ fn read_state_with_parameterized_wasm_func_returns_transformed_state() {

let actual_state: Option<Wallet> = program
.read_state_using_wasm(
Vec::<u8>::default(),
FUNC_NAME,
META_WASM_V2.to_vec(),
state_args!(other_person.clone()),
Expand All @@ -153,6 +168,7 @@ fn read_state_with_two_args_wasm_func_returns_transformed_state() {

let actual_state: Option<Wallet> = program
.read_state_using_wasm(
Vec::<u8>::default(),
FUNC_NAME,
META_WASM_V2.to_vec(),
state_args!(name.clone(), surname.clone()),
Expand Down
11 changes: 8 additions & 3 deletions gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,11 @@ impl ExtManager {

/// Call non-void meta function from actor stored in manager.
/// Warning! This is a static call that doesn't change actors pages data.
pub(crate) fn read_state_bytes(&mut self, program_id: &ProgramId) -> Result<Vec<u8>> {
pub(crate) fn read_state_bytes(
&mut self,
payload: Vec<u8>,
program_id: &ProgramId,
) -> Result<Vec<u8>> {
let (actor, _balance) = self
.actors
.get_mut(program_id)
Expand All @@ -467,7 +471,7 @@ impl ExtManager {
program.code().clone(),
Some(program.allocations().clone()),
Some(*program_id),
Default::default(),
payload,
u64::MAX,
self.block_info,
)
Expand All @@ -483,6 +487,7 @@ impl ExtManager {

pub(crate) fn read_state_bytes_using_wasm(
&mut self,
payload: Vec<u8>,
program_id: &ProgramId,
fn_name: &str,
wasm: Vec<u8>,
Expand All @@ -500,7 +505,7 @@ impl ExtManager {
.0;

let mut mapping_code_payload = args.unwrap_or_default();
mapping_code_payload.append(&mut self.read_state_bytes(program_id)?);
mapping_code_payload.append(&mut self.read_state_bytes(payload, program_id)?);

core_processor::informational::execute_for_reply::<Ext, _>(
String::from(fn_name),
Expand Down
41 changes: 22 additions & 19 deletions gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ impl<'a> Program<'a> {
}

/// Reads the program’s state as a byte vector.
pub fn read_state_bytes(&self) -> Result<Vec<u8>> {
pub fn read_state_bytes(&self, payload: Vec<u8>) -> Result<Vec<u8>> {
self.manager
.borrow_mut()
.with_externalities(|this| this.read_state_bytes(&self.id))
.with_externalities(|this| this.read_state_bytes(payload, &self.id))
}

/// Reads the program’s transformed state as a byte vector. The transformed
Expand Down Expand Up @@ -482,36 +482,37 @@ impl<'a> Program<'a> {
/// # let ARG_2 = 0u8;
/// //Read state bytes with no arguments passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, Option::<Vec<u8>>::None)?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, Option::<Vec<u8>>::None)?;
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, state_args_encoded!())?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, state_args_encoded!())?;
/// // Read state bytes with one argument passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, Some(ARG_1.encode()))?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, Some(ARG_1.encode()))?;
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, state_args_encoded!(ARG_1))?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, state_args_encoded!(ARG_1))?;
/// // Read state bytes with multiple arguments passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, Some((ARG_1, ARG_2).encode()))?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, Some((ARG_1, ARG_2).encode()))?;
/// # let WASM = vec![];
/// let _ = program.read_state_bytes_using_wasm("fn_name", WASM, state_args_encoded!(ARG_1, ARG_2))?;
/// let _ = program.read_state_bytes_using_wasm(Default::default(), "fn_name", WASM, state_args_encoded!(ARG_1, ARG_2))?;
/// # Ok(())
/// # }
/// ```
pub fn read_state_bytes_using_wasm(
&self,
payload: Vec<u8>,
fn_name: &str,
wasm: Vec<u8>,
args: Option<Vec<u8>>,
) -> Result<Vec<u8>> {
self.manager.borrow_mut().with_externalities(|this| {
this.read_state_bytes_using_wasm(&self.id, fn_name, wasm, args)
this.read_state_bytes_using_wasm(payload, &self.id, fn_name, wasm, args)
})
}

/// Reads and decodes the program's state .
pub fn read_state<D: Decode>(&self) -> Result<D> {
let state_bytes = self.read_state_bytes()?;
pub fn read_state<D: Decode, P: Encode>(&self, payload: P) -> Result<D> {
let state_bytes = self.read_state_bytes(payload.encode())?;
D::decode(&mut state_bytes.as_ref()).map_err(Into::into)
}

Expand Down Expand Up @@ -544,30 +545,32 @@ impl<'a> Program<'a> {
/// # let ARG_2 = 0u8;
/// //Read state bytes with no arguments passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, Option::<()>::None)?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, Option::<()>::None)?;
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, state_args!())?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, state_args!())?;
/// // Read state bytes with one argument passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, Some(ARG_1))?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, Some(ARG_1))?;
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, state_args!(ARG_1))?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, state_args!(ARG_1))?;
/// // Read state bytes with multiple arguments passed to wasm.
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, Some((ARG_1, ARG_2)))?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, Some((ARG_1, ARG_2)))?;
/// # let WASM = vec![];
/// let _ = program.read_state_using_wasm("fn_name", WASM, state_args!(ARG_1, ARG_2))?;
/// let _ = program.read_state_using_wasm(Vec::<u8>::default(), "fn_name", WASM, state_args!(ARG_1, ARG_2))?;
/// # Ok(())
/// # }
/// ```
pub fn read_state_using_wasm<E: Encode, D: Decode>(
pub fn read_state_using_wasm<E: Encode, P: Encode, D: Decode>(
&self,
payload: P,
fn_name: &str,
wasm: Vec<u8>,
argument: Option<E>,
) -> Result<D> {
let argument_bytes = argument.map(|arg| arg.encode());
let state_bytes = self.read_state_bytes_using_wasm(fn_name, wasm, argument_bytes)?;
let state_bytes =
self.read_state_bytes_using_wasm(payload.encode(), fn_name, wasm, argument_bytes)?;
D::decode(&mut state_bytes.as_ref()).map_err(Into::into)
}

Expand Down
4 changes: 3 additions & 1 deletion node/authorship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ frame-system = { workspace = true, features = ["std"] }
prometheus-endpoint.workspace = true

[dev-dependencies]
common = { workspace = true, features = ["std"] }
sc-transaction-pool.workspace = true
frame-support = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }
Expand All @@ -54,8 +55,9 @@ pallet-timestamp = { workspace = true, features = ["std"] }
pallet-balances = { workspace = true, features = ["std"] }
pallet-gear = { workspace = true, features = ["std"] }
pallet-gear-messenger = { workspace = true, features = ["std"] }
pallet-gear-program = { workspace = true, features = ["std"] }
testing = {workspace = true, features = ["vara-native"] }
vara-runtime = { workspace = true, features = ["std", "dev"] }
demo-mul-by-const.workspace = true
demo-mul-by-const = { workspace = true, features = ["debug"] }
env_logger.workspace = true
service = { workspace = true, features = ["dev", "vara-native"] }
40 changes: 30 additions & 10 deletions node/authorship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@

use crate::authorship::*;

use codec::Encode;
use codec::{Decode, Encode};
use common::Program;
use core::convert::TryFrom;
use frame_support::{storage::storage_prefix, traits::PalletInfoAccess};
use futures::executor::block_on;
use runtime_primitives::BlockNumber;
use sc_client_api::Backend;
use sc_transaction_pool::BasicPool;
use sc_transaction_pool_api::{
Expand All @@ -53,6 +55,7 @@ use testing::{
use vara_runtime::{AccountId, Runtime, RuntimeCall, UncheckedExtrinsic, SLOT_DURATION, VERSION};

const SOURCE: TransactionSource = TransactionSource::External;
const DEFAULT_GAS_LIMIT: u64 = 865_000_000;

fn chain_event<B: BlockT>(header: B::Header) -> ChainEvent<B>
where
Expand Down Expand Up @@ -91,7 +94,7 @@ fn checked_extrinsics(n: u32, signer: AccountId, nonce: &mut u32) -> Vec<Checked
code: WASM_BINARY.to_vec(),
salt: salt.as_bytes().to_vec(),
init_payload: (i as u64).encode(),
gas_limit: 500_000_000,
gas_limit: DEFAULT_GAS_LIMIT,
value: 0,
}),
};
Expand Down Expand Up @@ -513,8 +516,8 @@ fn block_max_gas_works() {

init_logger();

const INIT_MSG_GAS_LIMIT: u64 = 500_000_000;
const MAX_GAS: u64 = 2 * INIT_MSG_GAS_LIMIT + 25_000_100; // Enough to fit 2 messages
// Enough to fit 2 messages
const MAX_GAS: u64 = 2 * DEFAULT_GAS_LIMIT + 25_000_100;

let client_builder = TestClientBuilder::new()
.set_execution_strategy(sc_client_api::ExecutionStrategy::NativeWhenPossible);
Expand Down Expand Up @@ -621,15 +624,32 @@ fn block_max_gas_works() {
let mut queue_entry_args = IterArgs::default();
queue_entry_args.prefix = Some(&queue_entry_prefix);

let mut queue_len = 0_u32;

state
.keys(queue_entry_args)
.unwrap()
.for_each(|_k| queue_len += 1);
let queue_len = state.keys(queue_entry_args).unwrap().count();

// 2 out of 5 messages have been processed, 3 remain in the queue
assert_eq!(queue_len, 3);

let programs_prefix = storage_prefix(
pallet_gear_program::Pallet::<Runtime>::name().as_bytes(),
"ProgramStorage".as_bytes(),
);
let mut iter_args = IterArgs::default();
iter_args.prefix = Some(&programs_prefix);

// The fact that 2 init messages out of 5 have been processed means
// that there should be 2 inited programs.
let inited_count = state.pairs(iter_args).unwrap().fold(0u32, |count, pair| {
let value = match pair {
Ok((_key, value)) => value,
_ => return count,
};

match Program::<BlockNumber>::decode(&mut &value[..]) {
Ok(p) if p.is_initialized() => count + 1,
_ => count,
}
});
assert_eq!(inited_count, 2);
}

#[test]
Expand Down
Loading

0 comments on commit 0325938

Please sign in to comment.