Skip to content

Commit

Permalink
Merge pull request #869 from everx-labs/IgorKoval/clippy
Browse files Browse the repository at this point in the history
upd deps, cargo clippy and fmt
  • Loading branch information
IgorKoval authored Aug 16, 2024
2 parents 8f2f0fb + edf73c3 commit 7a3298d
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 343 deletions.
269 changes: 183 additions & 86 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license-file = 'LICENSE.md'
name = 'tvm_linker'
readme = 'README.md'
repository = 'https://github.com/everx-labs/TVM-linker'
version = '0.21.5'
version = '0.21.6'

[[bin]]
name = 'tvm_linker'
Expand All @@ -30,10 +30,10 @@ serde_json = '1.0'
sha2 = '0.10'
simplelog = '0.6'
thiserror = '1.0'
ever_abi = { git = 'https://github.com/everx-labs/ever-abi.git', tag = '2.6.4' }
ever_assembler = { git = 'https://github.com/everx-labs/ever-assembler.git', tag = '1.6.6' }
ever_block = { git = 'https://github.com/everx-labs/ever-block.git', tag = '1.11.3' }
ever_vm = { git = 'https://github.com/everx-labs/ever-vm.git', tag = '2.2.4' }
ever_abi = { git = 'https://github.com/everx-labs/ever-abi.git', tag = '2.7.2' }
ever_assembler = { features = [ 'gosh' ], git = 'https://github.com/everx-labs/ever-assembler.git', tag = '1.6.14' }
ever_block = { features = [ 'gosh' ], git = 'https://github.com/everx-labs/ever-block.git', tag = '1.11.11' }
ever_vm = { features = [ 'gosh' ], git = 'https://github.com/everx-labs/ever-vm.git', tag = '2.2.12' }

[dev-dependencies]
assert_cmd = '2.0.5'
Expand Down
5 changes: 1 addition & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
use std::process::Command;

fn from_utf8(data: Vec<u8>) -> String {
match String::from_utf8(data) {
Ok(string) => string,
Err(_) => String::from("Unknown")
}
String::from_utf8(data).unwrap_or_else(|_| String::from("Unknown"))
}

fn main() {
Expand Down
9 changes: 5 additions & 4 deletions src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
* See the License for the specific EVERX DEV software governing permissions and
* limitations under the License.
*/
use ever_abi::{Contract, json_abi::{encode_function_call, decode_function_response}};
use anyhow::format_err;
use ever_abi::{
json_abi::{decode_function_response, encode_function_call},
Contract,
};

use ever_block::{BuilderData, Result, SliceData};
use crate::keyman::Keypair;
use ever_block::{BuilderData, Result, SliceData};

pub fn build_abi_body(
abi_file: &str,
Expand Down Expand Up @@ -60,5 +63,3 @@ pub fn decode_body(
false,
)
}


5 changes: 3 additions & 2 deletions src/keyman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* limitations under the License.
*/
use anyhow::format_err;
use ever_block::{Ed25519PrivateKey, Result, Ed25519PublicKey, ed25519_create_private_key};
use ever_block::{ed25519_create_private_key, Ed25519PrivateKey, Ed25519PublicKey, Result};
use serde::Deserialize;

pub struct Keypair {
Expand All @@ -35,7 +35,8 @@ impl Keypair {
let public = hex::decode(keys.public)
.map_err(|e| format_err!("failed to decode public key: {}", e))?;

let public_bytes = public.try_into()
let public_bytes = public
.try_into()
.map_err(|v: Vec<u8>| format_err!("failed to get public bytes, bad len {}", v.len()))?;
Ok(Self {
private: ed25519_create_private_key(&private)?,
Expand Down
159 changes: 84 additions & 75 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ mod printer;
mod program;
mod testcall;

use std::{env, io::Write, fs::File, str::FromStr};
use anyhow::{bail, format_err};
use clap::{clap_app, ArgMatches};
use anyhow::{format_err, bail};
use std::{env, fs::File, io::Write, str::FromStr};

use ever_block::{AccountId, BocWriter, Result, SliceData, Status, UInt256};
use ever_block::{
Deserializable, Message, StateInit, Serializable, MsgAddressInt,
ExternalInboundMessageHeader, InternalMessageHeader, MsgAddressIntOrNone, ConfigParams
ConfigParams, Deserializable, ExternalInboundMessageHeader, InternalMessageHeader, Message,
MsgAddressInt, MsgAddressIntOrNone, Serializable, StateInit,
};
use ever_block::{SliceData, Result, Status, AccountId, UInt256, BocWriter};

use abi::{build_abi_body, decode_body, load_abi_json_string, load_abi_contract};
use abi::{build_abi_body, decode_body, load_abi_contract, load_abi_json_string};
use keyman::Keypair;
use program::{get_now, save_to_file, load_from_file};
use program::{get_now, load_from_file, save_to_file};
use testcall::{call_contract, MsgInfo, TestCallParams, TraceLevel};

const DEFAULT_CAPABILITIES: u64 = 0x880116ae; // Default capabilities on the main network
Expand All @@ -47,7 +47,7 @@ fn linker_main() -> Status {
env!("CARGO_PKG_VERSION"),
env!("BUILD_GIT_COMMIT"),
env!("BUILD_GIT_DATE"),
env!("BUILD_TIME") ,
env!("BUILD_TIME"),
);
let matches = clap_app!(tvm_linker =>
(version: build_info.as_str())
Expand Down Expand Up @@ -104,7 +104,6 @@ fn linker_main() -> Status {
(@setting SubcommandRequired)
).get_matches();


//SUBCOMMAND TEST
if let Some(test_matches) = matches.subcommand_matches("test") {
return run_test_subcmd(test_matches);
Expand Down Expand Up @@ -132,14 +131,16 @@ fn linker_main() -> Status {

let msg_body = match msg_matches.value_of("DATA") {
Some(data) => {
let buf = hex::decode(data).map_err(|e| format_err!("data argument has invalid format: {}", e))?;
let buf = hex::decode(data)
.map_err(|e| format_err!("data argument has invalid format: {}", e))?;
let len = buf.len() * 8;
let body = SliceData::from_raw(buf, len);
Some(body)
},
None => {
build_body(msg_matches, msg_matches.value_of("ADDRESS").map(|s| s.to_string()))?
},
}
None => build_body(
msg_matches,
msg_matches.value_of("ADDRESS").map(|s| s.to_string()),
)?,
};

return build_message(
Expand All @@ -148,18 +149,18 @@ fn linker_main() -> Status {
msg_body,
msg_matches.is_present("INIT"),
&suffix,
msg_matches.is_present("INTERNAL")
)
msg_matches.is_present("INTERNAL"),
);
}

unreachable!()
}

fn parse_now(now: Option<&str>) -> Result<u32> {
let now = match now {
Some(now_str) => {
now_str.parse::<u32>().map_err(|e| format_err!("failed to parse \"now\" option: {}", e))?
},
Some(now_str) => now_str
.parse::<u32>()
.map_err(|e| format_err!("failed to parse \"now\" option: {}", e))?,
None => get_now(),
};
Ok(now)
Expand All @@ -168,7 +169,9 @@ fn parse_now(now: Option<&str>) -> Result<u32> {
fn parse_ticktock(ticktock: Option<&str>) -> Result<Option<i8>> {
let error = "invalid ticktock value: must be 0 for tick and -1 for tock.";
if let Some(tt) = ticktock {
let tt = tt.parse::<i8>().map_err(|e| format_err!("{}: {}", error, e))?;
let tt = tt
.parse::<i8>()
.map_err(|e| format_err!("{}: {}", error, e))?;
if tt != 0 && tt != -1 {
bail!(error)
} else {
Expand Down Expand Up @@ -219,7 +222,7 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {
let (buf, buf_bits) = decode_hex_string(hex_str.to_string())?;
let body = SliceData::from_raw(buf, buf_bits);
(Some(body), Some(matches.value_of("SIGN")))
},
}
None => (build_body(matches, Some(address.to_string()))?, None),
};

Expand All @@ -231,8 +234,7 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {
let method = matches.value_of("ABI_METHOD");
if let Some(abi_file) = abi_file {
if let Some(method) = method {
let result = decode_body(abi_file, method, body, is_internal)
.unwrap_or_default();
let result = decode_body(abi_file, method, body, is_internal).unwrap_or_default();
println!("{}", result);
}
}
Expand All @@ -242,25 +244,23 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {

let _abi_contract = match abi_json {
Some(abi_file) => Some(load_abi_contract(&load_abi_json_string(abi_file)?)?),
None => None
None => None,
};

let debug_map_filename = matches.value_of("DEBUG_MAP")
.map(|s| s.to_string())
.or({
let mut res = Some("debug_map.map.json".to_string());
if let Some(abi) = abi_json {
let abi_root = abi.trim_end_matches(".abi.json");
for extension in [".dbg.json", ".debug.json", ".map.json"] {
let dbg_path = format!("{abi_root}{extension}");
if std::path::Path::new(&dbg_path).exists() {
res = Some(dbg_path);
break;
}
let debug_map_filename = matches.value_of("DEBUG_MAP").map(|s| s.to_string()).or({
let mut res = Some("debug_map.map.json".to_string());
if let Some(abi) = abi_json {
let abi_root = abi.trim_end_matches(".abi.json");
for extension in [".dbg.json", ".debug.json", ".map.json"] {
let dbg_path = format!("{abi_root}{extension}");
if std::path::Path::new(&dbg_path).exists() {
res = Some(dbg_path);
break;
}
}
res
});
}
res
});
if let Some(map) = debug_map_filename.clone() {
println!("DEBUG_MAP: {map}");
}
Expand All @@ -281,7 +281,8 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {
msg_info.body = msg.body();
}

let gas_limit = matches.value_of("GASLIMIT")
let gas_limit = matches
.value_of("GASLIMIT")
.map(|v| v.parse::<i64>())
.transpose()?;

Expand All @@ -292,7 +293,6 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {
trace_level = TraceLevel::Minimal;
}


let input = if input.ends_with(".tvc") {
input.to_owned()
} else {
Expand All @@ -302,30 +302,36 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {
let state_init = load_from_file(&input)?;
let config_cell_opt = matches.value_of("CONFIG").and_then(testcall::load_config);

let capabilities =
match config_cell_opt {
Some(ref config_cell) => {
let config_params = ConfigParams::with_address_and_root(
UInt256::from_str(&"5".repeat(64)).unwrap(), // -1:5555...
config_cell.clone());
config_params.capabilities()
}
None => {
DEFAULT_CAPABILITIES
}
};
let (_, state_init, is_success) = call_contract(addr, state_init, TestCallParams {
balance: matches.value_of("BALANCE"),
msg_info,
config: config_cell_opt,
key_file: sign,
ticktock,
gas_limit,
action_decoder: if matches.is_present("DECODEC6") { Some(action_decoder) } else { None },
trace_level,
debug_info: testcall::load_debug_info(&debug_map_filename.unwrap_or("".to_string())),
capabilities
})?;
let capabilities = match config_cell_opt {
Some(ref config_cell) => {
let config_params = ConfigParams::with_address_and_root(
UInt256::from_str(&"5".repeat(64)).unwrap(), // -1:5555...
config_cell.clone(),
);
config_params.capabilities()
}
None => DEFAULT_CAPABILITIES,
};
let (_, state_init, is_success) = call_contract(
addr,
state_init,
TestCallParams {
balance: matches.value_of("BALANCE"),
msg_info,
config: config_cell_opt,
key_file: sign,
ticktock,
gas_limit,
action_decoder: if matches.is_present("DECODEC6") {
Some(action_decoder)
} else {
None
},
trace_level,
debug_info: testcall::load_debug_info(&debug_map_filename.unwrap_or("".to_string())),
capabilities,
},
)?;
if is_success {
save_to_file(state_init, Some(&input), 0, false)?;
println!("Contract persistent data updated");
Expand All @@ -337,22 +343,29 @@ fn run_test_subcmd(matches: &ArgMatches) -> Status {

fn build_body(matches: &ArgMatches, address: Option<String>) -> Result<Option<SliceData>> {
let mut mask = 0u8;
let abi_file = matches.value_of("ABI_JSON").map(|m| { mask |= 1; m });
let method_name = matches.value_of("ABI_METHOD").map(|m| { mask |= 2; m });
let abi_file = matches.value_of("ABI_JSON").map(|m| {
mask |= 1;
m
});
let method_name = matches.value_of("ABI_METHOD").map(|m| {
mask |= 2;
m
});
let params = matches.value_of("ABI_PARAMS");
let header = matches.value_of("ABI_HEADER");
if mask == 0x3 {
let key_file = matches.value_of("SIGN")
let key_file = matches
.value_of("SIGN")
.map(Keypair::from_file)
.transpose()?;
let params = params.map_or(Ok("{}".to_owned()), |params|
let params = params.map_or(Ok("{}".to_owned()), |params| {
if params.find('{').is_none() {
std::fs::read_to_string(params)
.map_err(|e| format_err!("failed to load params from file: {}", e))
} else {
Ok(params.to_owned())
}
)?;
})?;
let is_internal = matches.is_present("INTERNAL");
let body = build_abi_body(
abi_file.unwrap(),
Expand Down Expand Up @@ -385,17 +398,13 @@ fn build_message(
None => -1,
};
println!("contract address {}", address_str);
let dest_address = MsgAddressInt::with_standart(
None,
wc,
AccountId::from_str(address_str)?
)?;
let dest_address = MsgAddressInt::with_standart(None, wc, AccountId::from_str(address_str)?)?;

let mut msg = if internal {
let source_address = MsgAddressIntOrNone::Some(MsgAddressInt::with_standart(
None,
-1,
AccountId::from_str("55".repeat(32).as_str())?
AccountId::from_str("55".repeat(32).as_str())?,
)?);
Message::with_int_header(InternalMessageHeader {
ihr_disabled: true,
Expand Down
Loading

0 comments on commit 7a3298d

Please sign in to comment.