Skip to content

Commit

Permalink
feat(gtest): Add payload to Program::read_state* (#3254)
Browse files Browse the repository at this point in the history
Co-authored-by: Sabaun Taraki <[email protected]>
  • Loading branch information
mqxf and techraed authored Sep 19, 2023
1 parent 5166948 commit ee5ce18
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
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 @@ -456,7 +456,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 @@ -468,7 +472,7 @@ impl ExtManager {
program.code().clone(),
Some(program.allocations().clone()),
Some(*program_id),
Default::default(),
payload,
u64::MAX,
self.block_info,
)
Expand All @@ -484,6 +488,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 @@ -501,7 +506,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::<SandboxEnvironment<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

0 comments on commit ee5ce18

Please sign in to comment.