Skip to content

Commit

Permalink
feat(tvm): add tvm_emulator_emulate_run_method
Browse files Browse the repository at this point in the history
  • Loading branch information
akostylev0 committed Apr 5, 2024
1 parent f8f7861 commit 9a87893
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion tonlibjson-sys/src/tonemulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extern {
fn tvm_emulator_send_internal_message(p: *mut c_void, message_body_boc: *const c_char, amount: c_ulong) -> *const c_char;

fn tvm_emulator_destroy(p: *mut c_void);

fn tvm_emulator_emulate_run_method(len: u32, params_boc: *const c_char, gas_limit: i64) -> *const c_char;

Check warning on line 44 in tonlibjson-sys/src/tonemulator.rs

View workflow job for this annotation

GitHub Actions / clippy

function `tvm_emulator_emulate_run_method` is never used

warning: function `tvm_emulator_emulate_run_method` is never used --> tonlibjson-sys/src/tonemulator.rs:44:8 | 44 | fn tvm_emulator_emulate_run_method(len: u32, params_boc: *const c_char, gas_limit: i64) -> *const c_char; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

#[derive(Debug)]
Expand Down Expand Up @@ -227,10 +229,27 @@ impl Drop for TvmEmulator {
}
}

pub fn emulate_run_method(params_boc: &str, gas_limit: i64) -> Result<String> {

Check warning on line 232 in tonlibjson-sys/src/tonemulator.rs

View workflow job for this annotation

GitHub Actions / clippy

function `emulate_run_method` is never used

warning: function `emulate_run_method` is never used --> tonlibjson-sys/src/tonemulator.rs:232:8 | 232 | pub fn emulate_run_method(params_boc: &str, gas_limit: i64) -> Result<String> { | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
let len = params_boc.len() as u32;
let params_boc = CString::new(params_boc)?;

unsafe {
let ptr = tvm_emulator_emulate_run_method(len, params_boc.as_ptr(), gas_limit);
if ptr.is_null() {
return Err(anyhow!("pointer is null"));
}

let response = CStr::from_ptr(ptr).to_string_lossy().to_string();
libc::free(ptr as *mut c_void);

Ok(response)
}
}


#[cfg(test)]
pub mod tests {
use crate::tonemulator::{TransactionEmulator, TvmEmulator};
use crate::tonemulator::{emulate_run_method, TransactionEmulator, TvmEmulator};

#[test]
fn transaction_emulator_test() {
Expand Down Expand Up @@ -311,4 +330,15 @@ pub mod tests {
println!("{:?}", result);
assert!(result.is_ok());
}

#[test]
fn emulate_run_method_fail_test() {
let params = "invalid boc";
let gas_limit = 1000000;

let result = emulate_run_method(params, gas_limit);

println!("{:?}", result);
assert!(result.is_err());
}
}

0 comments on commit 9a87893

Please sign in to comment.