Skip to content

Commit

Permalink
Merge pull request #21 from ston-fi/upstream-2024.4.0
Browse files Browse the repository at this point in the history
Upstream 2024.4.0
  • Loading branch information
Sild authored Jun 6, 2024
2 parents be698c3 + 8bdc64a commit d28a905
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tonlib-sys"
version = "2024.3.7"
version = "2024.4.0"
edition = "2021"
description = "Rust bindings for tonlibjson library"
license = "MIT"
Expand Down
78 changes: 28 additions & 50 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::path::Path;
use std::process::Command;
use std::thread::available_parallelism;
Expand All @@ -9,7 +10,7 @@ fn main() {
build();
}

const TON_MONOREPO_REVISION: &str = "25f61dff161b9c76dce0fc62dc51da911a208b68";
const TON_MONOREPO_REVISION: &str = "4cfe1d1a96acf956e28e2bbc696a143489e23631";
const TON_MONOREPO_DIR: &str = "./ton";

#[cfg(not(feature = "shared-tonlib"))]
Expand All @@ -19,7 +20,6 @@ fn build() {
env::set_var("TON_MONOREPO_REVISION", TON_MONOREPO_REVISION);
println!("cargo:rerun-if-env-changed=TON_MONOREPO_REVISION");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src");

// cleanup tonlib after previous build
if Path::new(TON_MONOREPO_DIR).exists() {
Expand All @@ -30,7 +30,7 @@ fn build() {
.args([
"clone",
"--branch",
"testnet",
"v2024.04",
"--depth",
"1", // get only the latest commit
"--recurse-submodules", // clone submodules as well
Expand Down Expand Up @@ -68,56 +68,34 @@ fn build() {
}

if cfg!(target_os = "macos") {
// OpenSSL
let openssl_installed = Command::new("brew")
.args(["--prefix", "openssl@3"])
.output()
.unwrap();
if !openssl_installed.status.success() {
panic!("OpenSSL is not installed. To install: `brew install openssl`");
}
let openssl = std::str::from_utf8(openssl_installed.stdout.as_slice())
.unwrap()
.trim();

// lz4
let lz4_installed = Command::new("brew").args(["list", "lz4"]).output().unwrap();
if !lz4_installed.status.success() {
panic!("liblz4 is not installed. To install: `brew install lz4`");
if Command::new("brew").args(["-h"]).output().is_err() {
panic!("brew is not available. Please install it to proceed");
}

// pkgconfig
let pkgconfig_installed = Command::new("brew")
.args(["list", "pkgconfig"])
.output()
.unwrap();
if !pkgconfig_installed.status.success() {
panic!("pkg-config is not installed. To install: `brew install pkgconfig`");
let mut dep_paths = HashMap::new();
for dep in &["openssl@3", "lz4", "pkgconfig", "libsodium", "secp256k1"] {
let dep_installed = Command::new("brew")
.args(["--prefix", dep])
.output()
.unwrap();
if !dep_installed.status.success() {
panic!(
"{} is not installed. To install: `brew install {}`",
dep, dep
);
} else {
dep_paths.insert(
dep.to_string(),
std::str::from_utf8(dep_installed.stdout.as_slice())
.unwrap()
.trim()
.to_string(),
);
}
}

// libsodium
let libsodium_installed = Command::new("brew")
.args(["--prefix", "libsodium"])
.output()
.unwrap();
if !libsodium_installed.status.success() {
panic!("libsodium is not installed. To install: `brew install libsodium`");
}
let libsodium = std::str::from_utf8(libsodium_installed.stdout.as_slice())
.unwrap()
.trim();

// secp256k1
let secp256k1_installed = Command::new("brew")
.args(["--prefix", "secp256k1"])
.output()
.unwrap();
if !secp256k1_installed.status.success() {
panic!("secp256k1 is not installed. To install: `brew install secp256k1`");
}
let secp256k1 = std::str::from_utf8(secp256k1_installed.stdout.as_slice())
.unwrap()
.trim();
let openssl = &dep_paths["openssl@3"];
let libsodium = &dep_paths["libsodium"];
let secp256k1 = &dep_paths["secp256k1"];

env::set_var("OPENSSL_ROOT_DIR", openssl);
env::set_var("OPENSSL_INCLUDE_DIR", format!("{openssl}/include"));
Expand Down
39 changes: 2 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
mod tonlibjson;
mod tvm_emulator;
mod tx_emulator;

pub use tonlibjson::*;
pub use tvm_emulator::*;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_creates_client() {
unsafe {
let client = tonlib_client_json_create();
tonlib_client_set_verbosity_level(4);
assert!(!client.is_null());
tonlib_client_json_send(client, "123\0".as_bytes().as_ptr() as *const i8);
tonlib_client_json_receive(client, 1.0);
tonlib_client_json_destroy(client);
}
}

#[test]
fn it_creates_emulator() {
let code = "te6cckECCwEAAe0AART/APSkE/S88sgLAQIBYgIDAgLMBAUCA3pgCQoD79mRDjgEit8GhpgYC42Eit8H0gGADpj+mf9qJofQB9IGpqGEAKqThdRxgamqiq44L5cCSA/SB9AGoYEGhAMGuQ/QAYEogaKCF4BFAqkGQoAn0BLGeLZmZk9qpwQQg97svvKThdcYEakuAB8YEYAmACcYEvgsIH+XhAYHCACT38FCIBuCoQCaoKAeQoAn0BLGeLAOeLZmSRZGWAiXoAegBlgGSQfIA4OmRlgWUD5f/k6DvADGRlgqxniygCfQEJ5bWJZmZkuP2AQA/jYD+gD6QPgoVBIIcFQgE1QUA8hQBPoCWM8WAc8WzMkiyMsBEvQA9ADLAMn5AHB0yMsCygfL/8nQUAjHBfLgShKhA1AkyFAE+gJYzxbMzMntVAH6QDAg1wsBwwCOH4IQ1TJ223CAEMjLBVADzxYi+gISy2rLH8s/yYBC+wCRW+IAMDUVxwXy4En6QDBZyFAE+gJYzxbMzMntVAAuUUPHBfLgSdQwAchQBPoCWM8WzMzJ7VQAfa289qJofQB9IGpqGDYY/BQAuCoQCaoKAeQoAn0BLGeLAOeLZmSRZGWAiXoAegBlgGT8gDg6ZGWBZQPl/+ToQAAfrxb2omh9AH0gamoYP6qQQFEAfwk=\0";

let data = "te6cckECFAEAA3wAAlFwOPUE4QoACAG/b+7lv/B/MjjfQ11sWK3b4LOpS7Bc7BSmJBVmyz5hdQECAEoBaHR0cHM6Ly90YXJhbnRpbmkuZGV2L3N0b24vbW9vbi5qc29uART/APSkE/S88sgLAwIBYgQFAgLMBgcAG6D2BdqJofQB9IH0gahhAgHUCAkCAUgKCwC7CDHAJJfBOAB0NMDAXGwlRNfA/AL4PpA+kAx+gAxcdch+gAx+gAwAtMfghAPin6lUiC6lTE0WfAI4IIQF41FGVIgupYxREQD8AngNYIQWV8HvLqTWfAK4F8EhA/y8IAARPpEMHC68uFNgAgEgDA0CASASEwH1APTP/oA+kAh8AHtRND6APpA+kDUMFE2oVIqxwXy4sEowv/y4sJUNEJwVCATVBQDyFAE+gJYzxYBzxbMySLIywES9AD0AMsAySD5AHB0yMsCygfL/8nQBPpA9AQx+gB3gBjIywVQCM8WcPoCF8trE8yCEBeNRRnIyx8ZgDgP3O1E0PoA+kD6QNQwCNM/+gBRUaAF+kD6QFNbxwVUc21wVCATVBQDyFAE+gJYzxYBzxbMySLIywES9AD0AMsAyfkAcHTIywLKB8v/ydBQDccFHLHy4sMK+gBRqKGCCJiWgIIImJaAErYIoYIImJaAoBihJ+MPJdcLAcMAI4A8QEQCayz9QB/oCIs8WUAbPFiX6AlADzxbJUAXMI5FykXHiUAioE6CCCJiWgKoAggiYloCgoBS88uLFBMmAQPsAECPIUAT6AljPFgHPFszJ7VQAcFJ5oBihghBzYtCcyMsfUjDLP1j6AlAHzxZQB88WyXGAGMjLBSTPFlAG+gIVy2oUzMlx+wAQJBAjAA4QSRA4N18EAHbCALCOIYIQ1TJ223CAEMjLBVAIzxZQBPoCFstqEssfEss/yXL7AJM1bCHiA8hQBPoCWM8WAc8WzMntVADbO1E0PoA+kD6QNQwB9M/+gD6QDBRUaFSSccF8uLBJ8L/8uLCggiYloCqABagFrzy4sOCEHvdl97Iyx8Vyz9QA/oCIs8WAc8WyXGAGMjLBSTPFnD6AstqzMmAQPsAQBPIUAT6AljPFgHPFszJ7VSAAgyAINch7UTQ+gD6QPpA1DAE0x+CEBeNRRlSILqCEHvdl94TuhKx8uLF0z8x+gAwE6BQI8hQBPoCWM8WAc8WzMntVIH++ZZY=\0";

let code_slice = code.as_bytes();
let data_slice = data.as_bytes();
let code_packed = code_slice.as_ptr();
let data_packed = data_slice.as_ptr();

unsafe {
let emulator =
tvm_emulator_create(code_packed as *const i8, data_packed as *const i8, 2);
tvm_emulator_run_get_method(emulator, 11111123, data_packed as *const i8);
assert!(!emulator.is_null());
tvm_emulator_destroy(emulator);
}
}
}
pub use tx_emulator::*;
32 changes: 22 additions & 10 deletions src/tonlibjson.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
extern "C" {
pub fn tonlib_client_json_create() -> *mut ::std::os::raw::c_void;
}
extern "C" {

pub fn tonlib_client_json_send(
client: *mut ::std::os::raw::c_void,
request: *const ::std::os::raw::c_char,
);
}
extern "C" {

pub fn tonlib_client_json_receive(
client: *mut ::std::os::raw::c_void,
timeout: f64,
) -> *const ::std::os::raw::c_char;
}
extern "C" {

pub fn tonlib_client_json_execute(
client: *mut ::std::os::raw::c_void,
request: *const ::std::os::raw::c_char,
) -> *const ::std::os::raw::c_char;
}
extern "C" {

pub fn tonlib_client_json_destroy(client: *mut ::std::os::raw::c_void);
}
extern "C" {

pub fn tonlib_client_set_verbosity_level(verbosity_level: u32);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_creates_client() {
unsafe {
let client = tonlib_client_json_create();
tonlib_client_set_verbosity_level(4);
assert!(!client.is_null());
tonlib_client_json_send(client, "123\0".as_bytes().as_ptr() as *const i8);
tonlib_client_json_receive(client, 1.0);
tonlib_client_json_destroy(client);
}
}
}
42 changes: 25 additions & 17 deletions src/tvm_emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ extern "C" {
data: *const ::std::os::raw::c_char,
vm_log_verbosity: u32,
) -> *mut ::std::os::raw::c_void;
}

extern "C" {
/**
* @brief Set libraries for TVM emulator
* @param libs_boc Base64 encoded BoC serialized libraries dictionary (HashmapE 256 ^Cell).
Expand All @@ -23,9 +21,7 @@ extern "C" {
tvm_emulator: *mut ::std::os::raw::c_void,
libs_boc: *const ::std::os::raw::c_char,
) -> bool;
}

extern "C" {
/**
* @brief Set c7 parameters
* @param tvm_emulator Pointer to TVM emulator
Expand All @@ -44,9 +40,7 @@ extern "C" {
rand_seed_hex: *const ::std::os::raw::c_char,
config: *const ::std::os::raw::c_char,
) -> bool;
}

extern "C" {
/**
* @brief Set TVM gas limit
* @param tvm_emulator Pointer to TVM emulator
Expand All @@ -57,9 +51,7 @@ extern "C" {
tvm_emulator: *mut ::std::os::raw::c_void,
gas_limit: u64,
) -> bool;
}

extern "C" {
/**
* @brief Enable or disable TVM debug primitives
* @param tvm_emulator Pointer to TVM emulator
Expand All @@ -71,9 +63,6 @@ extern "C" {
debug_enabled: ::std::os::raw::c_int,
) -> bool;

}

extern "C" {
/**
* @brief Run get method
* @param tvm_emulator Pointer to TVM emulator
Expand All @@ -99,9 +88,7 @@ extern "C" {
method_id: i32,
stack_boc: *const ::std::os::raw::c_char,
) -> *const ::std::os::raw::c_char;
}

extern "C" {
/**
* @brief Send external message
* @param tvm_emulator Pointer to TVM emulator
Expand All @@ -128,9 +115,7 @@ extern "C" {
tvm_emulator: *mut ::std::os::raw::c_void,
message_body_boc: *const ::std::os::raw::c_char,
) -> *const ::std::os::raw::c_char;
}

extern "C" {
/**
* @brief Send internal message
* @param tvm_emulator Pointer to TVM emulator
Expand Down Expand Up @@ -159,12 +144,35 @@ extern "C" {
message_body_boc: *const ::std::os::raw::c_char,
amount: u64,
) -> *const ::std::os::raw::c_char;
}

extern "C" {
/**
* @brief Destroy TVM emulator object
* @param tvm_emulator Pointer to TVM emulator object
*/
pub fn tvm_emulator_destroy(tvm_emulator: *mut ::std::os::raw::c_void);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_creates_tvm_emulator() {
let code = "te6cckECCwEAAe0AART/APSkE/S88sgLAQIBYgIDAgLMBAUCA3pgCQoD79mRDjgEit8GhpgYC42Eit8H0gGADpj+mf9qJofQB9IGpqGEAKqThdRxgamqiq44L5cCSA/SB9AGoYEGhAMGuQ/QAYEogaKCF4BFAqkGQoAn0BLGeLZmZk9qpwQQg97svvKThdcYEakuAB8YEYAmACcYEvgsIH+XhAYHCACT38FCIBuC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DccFHLHy4sMK+gBRqKGCCJiWgIIImJaAErYIoYIImJaAoBihJ+MPJdcLAcMAI4A8QEQCayz9QB/oCIs8WUAbPFiX6AlADzxbJUAXMI5FykXHiUAioE6CCCJiWgKoAggiYloCgoBS88uLFBMmAQPsAECPIUAT6AljPFgHPFszJ7VQAcFJ5oBihghBzYtCcyMsfUjDLP1j6AlAHzxZQB88WyXGAGMjLBSTPFlAG+gIVy2oUzMlx+wAQJBAjAA4QSRA4N18EAHbCALCOIYIQ1TJ223CAEMjLBVAIzxZQBPoCFstqEssfEss/yXL7AJM1bCHiA8hQBPoCWM8WAc8WzMntVADbO1E0PoA+kD6QNQwB9M/+gD6QDBRUaFSSccF8uLBJ8L/8uLCggiYloCqABagFrzy4sOCEHvdl97Iyx8Vyz9QA/oCIs8WAc8WyXGAGMjLBSTPFnD6AstqzMmAQPsAQBPIUAT6AljPFgHPFszJ7VSAAgyAINch7UTQ+gD6QPpA1DAE0x+CEBeNRRlSILqCEHvdl94TuhKx8uLF0z8x+gAwE6BQI8hQBPoCWM8WAc8WzMntVIH++ZZY=\0";

let code_slice = code.as_bytes();
let data_slice = data.as_bytes();
let code_packed = code_slice.as_ptr();
let data_packed = data_slice.as_ptr();

unsafe {
let emulator =
tvm_emulator_create(code_packed as *const i8, data_packed as *const i8, 2);
tvm_emulator_run_get_method(emulator, 11111123, data_packed as *const i8);
assert!(!emulator.is_null());
tvm_emulator_destroy(emulator);
}
}
}
Loading

0 comments on commit d28a905

Please sign in to comment.