eosio_chaintester/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use lazy_static::lazy_static; // 1.4.0
19use std::sync::{
20    Mutex,
21    MutexGuard
22};
23
24pub mod interfaces;
25pub mod client;
26pub use client::{
27    new_vm_api_client,
28    ChainTester,
29    get_vm_api_client,
30    close_vm_api_client,
31    GetTableRowsPrams,
32};
33
34pub mod server;
35
36
37pub struct DebuggerConfig {
38    pub debugger_server_address: String,
39    pub debugger_server_port: u16,
40    pub vm_api_server_address: String,
41    pub vm_api_server_port: u16,
42    pub apply_request_server_address: String,
43    pub apply_request_server_port: u16,
44}
45
46impl DebuggerConfig {
47    fn new() -> Self {
48        Self { 
49            debugger_server_address: "127.0.0.1".into(), 
50            debugger_server_port: 9090, 
51            vm_api_server_address: "127.0.0.1".into(), 
52            vm_api_server_port: 9092,
53            apply_request_server_address: "127.0.0.1".into(), 
54            apply_request_server_port: 9091,
55        }
56    }
57}
58
59lazy_static! {
60    static ref DEBUGGER_CONFIG: Mutex<DebuggerConfig> = Mutex::new(DebuggerConfig::new());
61}
62
63pub fn get_debugger_config() -> MutexGuard<'static, DebuggerConfig> {
64    return DEBUGGER_CONFIG.lock().unwrap()
65}
66
67extern "Rust" {
68	pub fn __eosio_generate_abi() -> String;
69}
70
71pub fn generate_abi_file(package_name: &str) {
72	let abi = unsafe {
73		__eosio_generate_abi()
74	};
75
76    // let package_name = env!("CARGO_PKG_NAME");
77    let abi_file = format!("./target/{}.abi", package_name);
78	match std::fs::write(std::path::Path::new(&abi_file), abi) {
79        Ok(()) => {
80
81        }
82        Err(err) => {
83            panic!("{}", err);
84        }
85    }
86}
87
88lazy_static! {
89    static ref BUILD_CONTRACT_MUTEX: Mutex<std::collections::HashMap<String, String>> = Mutex::new(std::collections::HashMap::new());
90}
91
92pub fn build_contract(package_name: &str, project_dir: &str) {
93    println!("++++++building {package_name} at {project_dir}");
94    let mut build_contract = BUILD_CONTRACT_MUTEX.lock().unwrap();
95    if build_contract.get(package_name).is_some() {
96        return;
97    }
98
99    build_contract.insert(package_name.into(), project_dir.into());
100
101    // let mut cargo_path: String = which::which("cargo").unwrap().to_str().unwrap().into();
102    // if cargo_path.contains(".rustup") {
103    //     let pos = cargo_path.find(".rustup").unwrap();
104    //     cargo_path = format!("{}/{}", &cargo_path[0..pos], ".cargo/bin/cargo");
105    //     println!("++++++++++cargo path:{}", cargo_path);
106    // }
107    std::env::set_var("RUSTFLAGS", "-C link-arg=-zstack-size=8192  -Clinker-plugin-lto");
108    let mut cmd = std::process::Command::new("cargo");
109    cmd
110        .args([
111            "+nightly",
112            "build",
113            "--target=wasm32-wasi",
114            &format!("--target-dir={project_dir}/target"),
115            "-Zbuild-std",
116            "--no-default-features",
117            "--release",
118            "-Zbuild-std-features=panic_immediate_abort"
119            ]
120        );
121
122    let mut child = cmd
123        // capture the stdout to return from this function as bytes
124        .stdout(std::process::Stdio::null())
125        .spawn()
126        .expect("command failed to start");
127    let output = child.wait().unwrap();
128    if !output.success() {
129        panic!("build failed");
130    }
131
132    let in_wasm_file = format!("{project_dir}/target/wasm32-wasi/release/{}.wasm", package_name);
133    let out_wasm_file = format!("{project_dir}/target/{}.wasm", package_name);
134    let wasm = std::fs::read(in_wasm_file).unwrap();
135    std::fs::write(out_wasm_file, wasm).unwrap();
136
137    generate_abi_file(package_name);
138}
139