Skip to content

Commit

Permalink
feat(gtest): Add send_with_gas (#4039)
Browse files Browse the repository at this point in the history
  • Loading branch information
holykol authored Jul 4, 2024
1 parent a2c41bb commit ceb1e15
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gsdk/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ async fn test_calculate_reply_for_handle_does_not_change_state() -> Result<()> {
);

// 5. read state after calculate
let calcualted_state = signer.api().read_state(pid_h256, vec![], None).await?;
let calculated_state = signer.api().read_state(pid_h256, vec![], None).await?;

// 6. assert that state hasn't changed
assert_eq!(initial_state, calcualted_state);
assert_eq!(initial_state, calculated_state);

// 7. make call
signer
Expand Down
73 changes: 71 additions & 2 deletions gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,21 @@ impl<'a> Program<'a> {
self.send_bytes_with_value(from, payload.encode(), value)
}

/// Send message to the program with gas limit and value.
pub fn send_with_gas<ID, P>(
&self,
from: ID,
payload: P,
gas_limit: u64,
value: u128,
) -> RunResult
where
ID: Into<ProgramIdWrapper>,
P: Encode,
{
self.send_bytes_with_gas_and_value(from, payload.encode(), gas_limit, value)
}

/// Send message to the program with bytes payload.
pub fn send_bytes<ID, T>(&self, from: ID, payload: T) -> RunResult
where
Expand All @@ -513,6 +528,35 @@ impl<'a> Program<'a> {
/// Send the message to the program with bytes payload and value.
#[track_caller]
pub fn send_bytes_with_value<ID, T>(&self, from: ID, payload: T, value: u128) -> RunResult
where
ID: Into<ProgramIdWrapper>,
T: Into<Vec<u8>>,
{
self.send_bytes_with_gas_and_value(from, payload, GAS_ALLOWANCE, value)
}

/// Send the message to the program with bytes payload, gas limit and value.
pub fn send_bytes_with_gas<ID, T>(
&self,
from: ID,
payload: T,
gas_limit: u64,
value: u128,
) -> RunResult
where
ID: Into<ProgramIdWrapper>,
T: Into<Vec<u8>>,
{
self.send_bytes_with_gas_and_value(from, payload, gas_limit, value)
}

fn send_bytes_with_gas_and_value<ID, T>(
&self,
from: ID,
payload: T,
gas_limit: u64,
value: u128,
) -> RunResult
where
ID: Into<ProgramIdWrapper>,
T: Into<Vec<u8>>,
Expand All @@ -530,7 +574,7 @@ impl<'a> Program<'a> {
source,
self.id,
payload.into().try_into().unwrap(),
Some(GAS_ALLOWANCE),
Some(gas_limit),
value,
None,
);
Expand Down Expand Up @@ -836,7 +880,8 @@ pub mod gbuild {
mod tests {
use super::Program;
use crate::{Log, System};
use gear_core_errors::ErrorReplyReason;
use gear_core::ids::ActorId;
use gear_core_errors::{ErrorReplyReason, ReplyCode, SimpleExecutionError};

#[test]
fn test_handle_messages_to_failing_program() {
Expand Down Expand Up @@ -1110,4 +1155,28 @@ mod tests {
let run_result = prog.send_bytes(user_id, []);
assert!(!run_result.main_failed());
}

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

let prog = Program::from_binary_with_id(&sys, 137, demo_ping::WASM_BINARY);

let user_id = ActorId::zero();

// set insufficient gas for execution
let res = prog.send_with_gas(user_id, "init".to_string(), 1, 0);

let expected_log =
Log::builder()
.source(prog.id())
.dest(user_id)
.reply_code(ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
)));

assert!(res.contains(&expected_log));
assert!(res.main_failed());
}
}

0 comments on commit ceb1e15

Please sign in to comment.