diff --git a/polyjuice-tests/src/helper.rs b/polyjuice-tests/src/helper.rs index 25ac536a..a794204b 100644 --- a/polyjuice-tests/src/helper.rs +++ b/polyjuice-tests/src/helper.rs @@ -287,10 +287,10 @@ pub fn new_contract_account_script_with_nonce(from_addr: &[u8; 20], from_nonce: let mut stream = RlpStream::new_list(2); stream.append(&from_addr.to_vec()); stream.append(&from_nonce); - println!( - "rlp data of (eoa_address + nonce): {}", - hex::encode(stream.as_raw()) - ); + // println!( + // "rlp data of (eoa_address + nonce): {}", + // hex::encode(stream.as_raw()) + // ); let data_hash = tiny_keccak::keccak256(stream.as_raw()); let mut new_script_args = vec![0u8; 32 + 4 + 20]; @@ -799,3 +799,22 @@ pub(crate) fn eth_address_regiser( None, ) } + +pub(crate) fn print_gas_used(operation: &str, logs: &Vec) { + let mut gas_used: Option = None; + for log in logs { + gas_used = match parse_log(log) { + crate::helper::Log::PolyjuiceSystem { + gas_used, + cumulative_gas_used: _, + created_address: _, + status_code: _, + } => Some(gas_used), + _ => None + }; + if gas_used.is_some() { + break; + } + } + println!("{}: {} gas used", operation, gas_used.unwrap()); +} diff --git a/polyjuice-tests/src/test_cases/address.rs b/polyjuice-tests/src/test_cases/address.rs index ff394f0d..81c7d4c8 100644 --- a/polyjuice-tests/src/test_cases/address.rs +++ b/polyjuice-tests/src/test_cases/address.rs @@ -69,6 +69,7 @@ fn test_get_contract_code() { ) .expect("call createMemoryArray function"); let mut expect_result = [0u8; 32]; + #[allow(clippy::needless_range_loop)] for i in 0..32 { expect_result[i] = i as u8; } diff --git a/polyjuice-tests/src/test_cases/erc20.rs b/polyjuice-tests/src/test_cases/erc20.rs index 9b06fd7f..3466964a 100644 --- a/polyjuice-tests/src/test_cases/erc20.rs +++ b/polyjuice-tests/src/test_cases/erc20.rs @@ -3,7 +3,7 @@ use crate::helper::{ self, deploy, eth_addr_to_ethabi_addr, new_block_info, setup, MockContractInfo, - PolyjuiceArgsBuilder, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES, + PolyjuiceArgsBuilder, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES, print_gas_used, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -43,6 +43,7 @@ fn test_erc20() { block_producer_id.clone(), 1, ); + print_gas_used("Deploy ERC20 contract: ", &run_result.logs); // [Deploy ERC20] used cycles: 1018075 < 1020K helper::check_cycles("Deploy ERC20", run_result.used_cycles, 1_400_000); @@ -158,6 +159,8 @@ fn test_erc20() { None, ) .expect(operation); + print_gas_used(&format!("ERC20 {}: ", operation), &run_result.logs); + // [ERC20 contract method_x] used cycles: 942107 < 960K helper::check_cycles("ERC20 contract method_x", run_result.used_cycles, 1_400_000); state.apply_run_result(&run_result).expect("update state"); diff --git a/polyjuice-tests/src/test_cases/receive_ether.rs b/polyjuice-tests/src/test_cases/receive_ether.rs index 67ea2d13..c3babef9 100644 --- a/polyjuice-tests/src/test_cases/receive_ether.rs +++ b/polyjuice-tests/src/test_cases/receive_ether.rs @@ -11,7 +11,7 @@ use gw_types::{ use crate::helper::{ check_cycles, create_block_producer, deploy, eth_addr_to_ethabi_addr, new_block_info, - parse_log, setup, MockContractInfo, PolyjuiceArgsBuilder, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES, + parse_log, setup, MockContractInfo, PolyjuiceArgsBuilder, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES, print_gas_used, }; const INIT_CODE: &str = include_str!("./evm-contracts/EtherReceiverMock.bin"); @@ -46,7 +46,7 @@ fn receive_ether_test() -> anyhow::Result<()> { .get_account_id_by_script_hash(&contract_account.script_hash)? .unwrap(); - //call receive() + // call receive() let block_info = new_block_info(block_producer, 1, 0); let args = PolyjuiceArgsBuilder::default() .gas_limit(2100) @@ -73,8 +73,8 @@ fn receive_ether_test() -> anyhow::Result<()> { .expect("Call receive()"); check_cycles("receive()", run_result.used_cycles, 710_100); assert!(run_result.return_data.is_empty()); - let log = parse_log(&run_result.logs[1]); - let receive_data = match log { + + let receive_data = match parse_log(&run_result.logs[1]) { crate::helper::Log::PolyjuiceUser { address: _, data, @@ -85,6 +85,9 @@ fn receive_ether_test() -> anyhow::Result<()> { let mut expect = [7u8; 8]; expect[1..].copy_from_slice(b"receive"); assert_eq!(receive_data, Some(expect.to_vec())); + + print_gas_used("A simplest receive() call: ", &run_result.logs); + state.apply_run_result(&run_result).expect("update state"); Ok(()) } @@ -99,7 +102,7 @@ fn without_receive_fallback_test() -> anyhow::Result<()> { crate::helper::create_eth_eoa_account(&mut state, &from_eth_address, 200000000u64.into()); // Deploy SimpleTrasfer Contract - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -111,6 +114,8 @@ fn without_receive_fallback_test() -> anyhow::Result<()> { block_producer.clone(), 0, ); + print_gas_used("Deploy SimpleTrasfer Contract: ", &run_result.logs); + let st_contract_account = MockContractInfo::create(&from_eth_address, 0); let st_account_id = state .get_account_id_by_script_hash(&st_contract_account.script_hash)? @@ -129,12 +134,14 @@ fn without_receive_fallback_test() -> anyhow::Result<()> { block_producer.clone(), 0, ); + print_gas_used("Deploy RejectedSimpleStorage Contract: ", &run_result.logs); + let ss_contract_account = MockContractInfo::create(&from_eth_address, 1); let _ss_account_id = state .get_account_id_by_script_hash(&ss_contract_account.script_hash)? .unwrap(); - // SimpleTransfer.transferToSimpleStorage1(); + // SimpleTransfer.transferToSimpleStorage1() -> target_addr.transfer(1 wei); let block_info = new_block_info(block_producer, 1, 1); let input = hex::decode(format!( @@ -171,5 +178,9 @@ fn without_receive_fallback_test() -> anyhow::Result<()> { TransactionError::InvalidExitCode(2) ); + // TODO: read the log of a failed transaction + // print!("SimpleTransfer.transferToSimpleStorage1(): "); + // print_gas_used(&run_result.logs[0]); + Ok(()) } diff --git a/polyjuice-tests/src/test_cases/sudt_erc20_proxy.rs b/polyjuice-tests/src/test_cases/sudt_erc20_proxy.rs index ec6c53d4..def574df 100644 --- a/polyjuice-tests/src/test_cases/sudt_erc20_proxy.rs +++ b/polyjuice-tests/src/test_cases/sudt_erc20_proxy.rs @@ -5,7 +5,7 @@ use crate::helper::{ self, build_eth_l2_script, build_l2_sudt_script, deploy, eth_addr_to_ethabi_addr, new_block_info, new_contract_account_script, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, CREATOR_ACCOUNT_ID, FATAL_PRECOMPILED_CONTRACTS, L2TX_MAX_CYCLES, - SUDT_ERC20_PROXY_USER_DEFINED_DECIMALS_CODE, + SUDT_ERC20_PROXY_USER_DEFINED_DECIMALS_CODE, print_gas_used, }; use gw_common::builtins::ETH_REGISTRY_ACCOUNT_ID; use gw_common::registry_address::RegistryAddress; @@ -45,7 +45,7 @@ fn test_sudt_erc20_proxy_inner( // => 0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000024cb016ea00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e65726332305f646563696d616c7300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034445430000000000000000000000000000000000000000000000000000000000 let args = format!("00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000024cb016ea00000000000000000000000000000000000000000000000000000000000000{:02x}00000000000000000000000000000000000000000000000000000000000000{:02x}000000000000000000000000000000000000000000000000000000000000000e65726332305f646563696d616c7300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034445430000000000000000000000000000000000000000000000000000000000", new_sudt_id, decimals); let init_code = format!("{}{}", SUDT_ERC20_PROXY_USER_DEFINED_DECIMALS_CODE, args); - let _run_result = deploy( + let run_result = deploy( generator, store, state, @@ -58,10 +58,11 @@ fn test_sudt_erc20_proxy_inner( 1, ); print!("SudtERC20Proxy_UserDefinedDecimals.ContractCode.hex: 0x"); - for byte in _run_result.return_data { + for byte in run_result.return_data { print!("{:02x}", byte); } println!(); + print_gas_used("Deploy SUDT_ERC20_PROXY contract: ", &run_result.logs); let contract_account_script = new_contract_account_script(state, from_id1, &from_eth_address1, false); @@ -306,6 +307,8 @@ fn test_sudt_erc20_proxy_inner( L2TX_MAX_CYCLES, None, )?; + print_gas_used(&format!("SudtERC20Proxy {}: ", action), &run_result.logs); + println!( "[execute_transaction] {} {}ms", action,