Skip to content

Commit

Permalink
Tiny refactoring by clippy suggestions (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss authored and birchmd committed Nov 15, 2022
1 parent 991660b commit 42d8bc7
Show file tree
Hide file tree
Showing 42 changed files with 1,090 additions and 918 deletions.
950 changes: 549 additions & 401 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ args = [
[tasks.udeps]
category = "Check"
env = { "CARGO_MAKE_RUST_CHANNEL" = "nightly", "CARGO_MAKE_CRATE_INSTALLATION_LOCKED" = "true" }
install_crate = { crate_name = "cargo-udeps", binary = "cargo", min_version = "0.1.30", test_arg = ["udeps", "-h"], force = true }
install_crate = { crate_name = "cargo-udeps", binary = "cargo", min_version = "0.1.34", test_arg = ["udeps", "-h"], force = true }
command = "${CARGO}"
args = [
"udeps",
Expand All @@ -81,9 +81,7 @@ category = "Check"
command = "${CARGO}"
args = [
"clippy",
"--no-default-features",
"--features",
"contract",
"--all-targets",
"--",
"-D",
"warnings",
Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ mod tests {
#[test]
fn test_make_address() {
for i in 0..u8::MAX {
assert_eq!(super::make_address(0, i as u128), u8_to_address(i));
assert_eq!(super::make_address(0, i.into()), u8_to_address(i));
}

let mut rng = rand::thread_rng();
Expand Down
8 changes: 4 additions & 4 deletions engine-precompiles/src/modexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,21 @@ mod tests {
#[test]
fn test_modexp() {
for (test, test_gas) in TESTS.iter().zip(BYZANTIUM_GAS.iter()) {
let input = hex::decode(&test.input).unwrap();
let input = hex::decode(test.input).unwrap();

let res = ModExp::<Byzantium>::new()
.run(&input, Some(*test_gas), &new_context(), false)
.unwrap()
.output;
let expected = hex::decode(&test.expected).unwrap();
let expected = hex::decode(test.expected).unwrap();
assert_eq!(res, expected, "{}", test.name);
}
}

#[test]
fn test_byzantium_modexp_gas() {
for (test, test_gas) in TESTS.iter().zip(BYZANTIUM_GAS.iter()) {
let input = hex::decode(&test.input).unwrap();
let input = hex::decode(test.input).unwrap();

let gas = ModExp::<Byzantium>::required_gas(&input).unwrap();
assert_eq!(gas, *test_gas, "{} gas", test.name);
Expand All @@ -443,7 +443,7 @@ mod tests {
#[test]
fn test_berlin_modexp_gas() {
for (test, test_gas) in TESTS.iter().zip(BERLIN_GAS.iter()) {
let input = hex::decode(&test.input).unwrap();
let input = hex::decode(test.input).unwrap();

let gas = ModExp::<Berlin>::required_gas(&input).unwrap();
assert_eq!(gas, *test_gas, "{} gas", test.name);
Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod tests {
&hex::decode("1563915e194D8CfBA1943570603F7606A3115508").unwrap(),
)
.unwrap();
assert!(ecverify(hash, &signature, signer));
assert!(ecverify(hash, signature, signer));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion engine-standalone-storage/src/relayer_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod test {
.set_block_data(block_hash, block_height, block_metadata)
.unwrap();
let result = storage.with_engine_access(block_height, 0, &[], |io| {
let mut local_io = io.clone();
let mut local_io = io;
engine::set_state(&mut local_io, engine_state.clone());
connector::EthConnectorContract::create_contract(
io,
Expand Down
6 changes: 3 additions & 3 deletions engine-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ evm-runtime = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "
evm-gasometer = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.36.0-aurora", default-features = false, features = ["std", "tracing"] }
rlp = { version = "0.5.0", default-features = false }
base64 = "0.13.0"
bstr = "0.2"
bstr = "1.0.1"
byte-slice-cast = { version = "1.0", default-features = false }
ethabi = "17.1"
serde = { version = "1", features = ["derive"] }
Expand All @@ -46,8 +46,8 @@ near-primitives-core = { git = "https://github.com/birchmd/nearcore.git", rev =
near-primitives = { git = "https://github.com/birchmd/nearcore.git", rev = "6033903be2037d67510188450f289b2d6e033f04", default-features = false, features = [ "nightly_protocol" ] }
libsecp256k1 = { version = "0.7.0", default-features = false }
rand = "0.8.5"
criterion = "0.3.4"
git2 = "0.13"
criterion = "0.4.0"
git2 = "0.15"
tempfile = "3.2.0"
walrus = "0.19"

Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/benches/eth_standard_precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn eth_standard_precompiles_benchmark(c: &mut Criterion) {
));

let test_names = PrecompilesContract::all_method_names();
let bench_ids: Vec<_> = test_names.iter().map(BenchmarkId::from_parameter).collect();
let bench_ids = test_names.iter().map(BenchmarkId::from_parameter);

// create testing transactions
let transactions: Vec<_> = test_names
Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/benches/nft_pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn measure_gas_usage(
));

// mint NFTs
let data: String = std::iter::repeat('0').take(data_size).collect();
let data = "0".repeat(data_size);
for i in 0..total_tokens {
let result = runner
.submit_with_signer(&mut source_account, |nonce| {
Expand Down
12 changes: 6 additions & 6 deletions engine-tests/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<'a> OneShotAuroraRunner<'a> {
impl AuroraRunner {
pub fn one_shot(&self) -> OneShotAuroraRunner {
OneShotAuroraRunner {
base: &self,
base: self,
ext: self.ext.clone(),
context: self.context.clone(),
}
Expand Down Expand Up @@ -309,7 +309,7 @@ impl AuroraRunner {
let ft_value = {
let mut current_ft: FungibleToken = trie
.get(&ft_key)
.map(|bytes| FungibleToken::try_from_slice(&bytes).unwrap())
.map(|bytes| FungibleToken::try_from_slice(bytes).unwrap())
.unwrap_or_default();
current_ft.total_eth_supply_on_near =
current_ft.total_eth_supply_on_near + NEP141Wei::new(init_balance.raw().as_u128());
Expand All @@ -326,7 +326,7 @@ impl AuroraRunner {
let aurora_balance_value = {
let mut current_balance: u128 = trie
.get(&aurora_balance_key)
.map(|bytes| u128::try_from_slice(&bytes).unwrap())
.map(|bytes| u128::try_from_slice(bytes).unwrap())
.unwrap_or_default();
current_balance += init_balance.raw().as_u128();
current_balance
Expand Down Expand Up @@ -811,8 +811,8 @@ pub(crate) fn validate_address_balance_and_nonce(
}

pub(crate) fn address_from_hex(address: &str) -> Address {
let bytes = if address.starts_with("0x") {
hex::decode(&address[2..]).unwrap()
let bytes = if let Some(address) = address.strip_prefix("0x") {
hex::decode(address).unwrap()
} else {
hex::decode(address).unwrap()
};
Expand All @@ -838,7 +838,7 @@ pub fn unwrap_success(result: SubmitResult) -> Vec<u8> {

pub fn unwrap_success_slice(result: &SubmitResult) -> &[u8] {
match &result.status {
TransactionStatus::Succeed(ret) => &ret,
TransactionStatus::Succeed(ret) => ret,
other => panic!("Unexpected status: {:?}", other),
}
}
Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/test_utils/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn compile<P: AsRef<Path>>(source_path: P) {
let output = Command::new("cargo")
.current_dir(source_path)
.env("RUSTFLAGS", "-C link-arg=-s")
.args(&["build", "--target", "wasm32-unknown-unknown", "--release"])
.args(["build", "--target", "wasm32-unknown-unknown", "--release"])
.output()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/test_utils/self_destruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl SelfDestruct {
.unwrap();

let input = CallArgs::V2(FunctionCallArgsV2 {
contract: self.contract.address.into(),
contract: self.contract.address,
value: WeiU256::default(),
input: data.to_vec(),
})
Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/test_utils/solidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
let output_mount_arg = format!("{}:/output", output_path.to_str().unwrap());
let contract_arg = format!("/contracts/{}", contract_file.as_ref().to_str().unwrap());
let output = Command::new("/usr/bin/env")
.args(&[
.args([
"docker",
"run",
"-v",
Expand Down
6 changes: 2 additions & 4 deletions engine-tests/src/test_utils/standalone/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,15 @@ pub fn mint_evm_account<I: IO + Copy, E: Env>(
.unwrap();

let transfer_args = NEP141FtOnTransferArgs {
sender_id: aurora_account_id.clone(),
sender_id: aurora_account_id,
amount: Balance::new(balance.raw().as_u128()),
msg: format!(
"aurora:{}{}",
hex::encode(Wei::zero().to_bytes()),
hex::encode(address.as_bytes())
),
};
connector
.ft_on_transfer(&mut engine, &transfer_args)
.unwrap();
connector.ft_on_transfer(&engine, &transfer_args).unwrap();

engine.apply(std::iter::once(state_change), std::iter::empty(), false);
}
Expand Down
19 changes: 10 additions & 9 deletions engine-tests/src/test_utils/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl StandaloneRunner {
.unwrap();
env.block_height += 1;
let transaction_hash = H256::zero();
let tx_msg = Self::template_tx_msg(storage, &env, 0, transaction_hash, &[]);
let tx_msg = Self::template_tx_msg(storage, env, 0, transaction_hash, &[]);
let result = storage.with_engine_access(env.block_height, 0, &[], |io| {
mocks::init_evm(io, env, chain_id);
});
Expand Down Expand Up @@ -79,7 +79,7 @@ impl StandaloneRunner {
};

env.block_height += 1;
let tx_msg = Self::template_tx_msg(storage, &env, 0, transaction_hash, &[]);
let tx_msg = Self::template_tx_msg(storage, env, 0, transaction_hash, &[]);

let result = storage.with_engine_access(env.block_height, 0, &[], |io| {
mocks::mint_evm_account(address, balance, nonce, code, io, env)
Expand Down Expand Up @@ -142,7 +142,7 @@ impl StandaloneRunner {
env.block_height += 1;

Self::internal_submit_transaction(
&transaction_bytes,
transaction_bytes,
0,
storage,
env,
Expand All @@ -165,17 +165,18 @@ impl StandaloneRunner {
let transaction_bytes = rlp::encode(signed_tx).to_vec();
let transaction_hash = aurora_engine_sdk::keccak(&transaction_bytes);

let mut tx_msg = Self::template_tx_msg(storage, &env, 0, transaction_hash, &[]);
let mut tx_msg = Self::template_tx_msg(storage, env, 0, transaction_hash, &[]);
tx_msg.position = transaction_position;
tx_msg.transaction =
TransactionKind::Submit(transaction_bytes.as_slice().try_into().unwrap());
let outcome = sync::execute_transaction_message(storage, tx_msg).unwrap();

match outcome.maybe_result.as_ref().unwrap().as_ref().unwrap() {
sync::TransactionExecutionResult::Submit(result) => match result.as_ref() {
Err(e) => return Err(e.clone()),
Ok(_) => (),
},
sync::TransactionExecutionResult::Submit(result) => {
if let Err(e) = result.as_ref() {
return Err(e.clone());
}
}
_ => unreachable!(),
};

Expand Down Expand Up @@ -359,7 +360,7 @@ impl StandaloneRunner {
cumulative_diff: &mut Diff,
promise_results: &[PromiseResult],
) -> Result<SubmitResult, engine::EngineError> {
let transaction_hash = aurora_engine_sdk::keccak(&transaction_bytes);
let transaction_hash = aurora_engine_sdk::keccak(transaction_bytes);
let mut tx_msg = Self::template_tx_msg(
storage,
env,
Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/test_utils/uniswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ fn download_uniswap_artifacts() {
DOWNLOAD_ONCE.call_once(|| {
let output = Command::new("/usr/bin/env")
.current_dir(&uniswap_root_path())
.args(&["yarn", "install"])
.args(["yarn", "install"])
.output()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/tests/account_id_precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ fn test_account_id_precompiles() {

fn unwrap_ethabi_string(result: &SubmitResult) -> String {
let bytes = test_utils::unwrap_success_slice(result);
let mut tokens = ethabi::decode(&[ethabi::ParamType::String], &bytes).unwrap();
let mut tokens = ethabi::decode(&[ethabi::ParamType::String], bytes).unwrap();
tokens.pop().unwrap().into_string().unwrap()
}
11 changes: 3 additions & 8 deletions engine-tests/src/tests/contract_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::test_utils::exit_precompile::{Tester, TesterConstructor, DEST_ACCOUNT

fn setup_test() -> (AuroraRunner, Signer, Address, Tester) {
let mut runner = AuroraRunner::new();
let token = runner.deploy_erc20_token(&"tt.testnet".to_string());
let token = runner.deploy_erc20_token("tt.testnet");
let mut signer = test_utils::Signer::random();
runner.create_address(
test_utils::address_from_secret_key(&signer.secret_key),
Expand All @@ -20,17 +20,12 @@ fn setup_test() -> (AuroraRunner, Signer, Address, Tester) {
let tester: Tester = runner
.deploy_contract(
&signer.secret_key,
|ctr| ctr.deploy(nonce, token.into()),
|ctr| ctr.deploy(nonce, token),
tester_ctr,
)
.into();

runner.mint(
token,
tester.contract.address.into(),
1_000_000_000,
origin(),
);
runner.mint(token, tester.contract.address, 1_000_000_000, origin());

(runner, signer, token, tester)
}
Expand Down
4 changes: 2 additions & 2 deletions engine-tests/src/tests/ecrecover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ fn check_wasm_ecrecover(

fn construct_input(hash: &[u8], sig: &[u8]) -> Vec<u8> {
let mut buf = [0u8; 128];
(&mut buf[0..32]).copy_from_slice(hash);
(buf[0..32]).copy_from_slice(hash);
buf[63] = sig[64];
(&mut buf[64..128]).copy_from_slice(&sig[0..64]);
(buf[64..128]).copy_from_slice(&sig[0..64]);

buf.to_vec()
}
2 changes: 1 addition & 1 deletion engine-tests/src/tests/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn profile_erc20_get_balance() {
// at least 70% of the cost is spent on wasm computation (as opposed to host functions)
let wasm_fraction = (100 * profile.wasm_gas()) / profile.all_gas();
assert!(
20 <= wasm_fraction && wasm_fraction <= 30,
(20..=30).contains(&wasm_fraction),
"{}% is not between 20% and 30%",
wasm_fraction
);
Expand Down
Loading

0 comments on commit 42d8bc7

Please sign in to comment.