From f41fe66214812b89bfc99bfcf6f51cf5e4f81118 Mon Sep 17 00:00:00 2001 From: Flouse Date: Tue, 10 Aug 2021 06:31:55 +0000 Subject: [PATCH 1/3] Update repo URL of evmone submodule --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index d8125188..af59707c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -9,7 +9,7 @@ url = https://github.com/chfast/intx [submodule "deps/evmone"] path = deps/evmone - url = https://github.com/TheWaWaR/evmone + url = https://github.com/nervosnetwork/evmone [submodule "deps/secp256k1"] path = deps/secp256k1 url = https://github.com/nervosnetwork/secp256k1 From 1cc979fd3e8dd906d2ac14fef467e6a96fdead97 Mon Sep 17 00:00:00 2001 From: Flouse Date: Tue, 10 Aug 2021 11:54:47 +0000 Subject: [PATCH 2/3] chore: fix docs spelling --- c/generator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/generator.c b/c/generator.c index 70fcd61c..195d6d61 100644 --- a/c/generator.c +++ b/c/generator.c @@ -5,7 +5,7 @@ * and provides layer2 syscalls. * * A program should be able to generate a post state after run the generator, - * and should be able to use the states to construct a transaction that satifies + * and should be able to use the states to construct a transaction that satisfies * the validator. */ From b9b7c33502da7e466b8f79252d1b7eb13fda7e4f Mon Sep 17 00:00:00 2001 From: Flouse Date: Tue, 10 Aug 2021 12:04:30 +0000 Subject: [PATCH 3/3] Add helper::check_cycles(...) into test cases Once unexpected cycles of l2tx come out, we will get `too many cycles` Warning. --- polyjuice-tests/src/helper.rs | 17 +++++++- .../src/test_cases/call_selfdestruct.rs | 24 ++++++++--- .../src/test_cases/contract_call_contract.rs | 24 +++++++++-- .../test_cases/contract_create_contract.rs | 8 ++-- polyjuice-tests/src/test_cases/create2.rs | 17 ++++---- .../src/test_cases/delegatecall.rs | 13 +++--- polyjuice-tests/src/test_cases/ecrecover.rs | 22 ++++++---- polyjuice-tests/src/test_cases/erc20.rs | 10 +++-- .../src/test_cases/eth_to_godwoken_addr.rs | 18 +++++++-- .../src/test_cases/fallback_function.rs | 9 ++++- polyjuice-tests/src/test_cases/heap_memory.rs | 8 ++-- .../test_cases/invalid_sudt_erc20_proxy.rs | 19 +++++++-- .../src/test_cases/recover_account.rs | 22 +++++++--- .../src/test_cases/selfdestruct.rs | 10 +++-- .../src/test_cases/simple_storage.rs | 40 +++---------------- .../src/test_cases/simple_transfer.rs | 20 +++++++++- .../src/test_cases/simple_wallet.rs | 7 +++- 17 files changed, 196 insertions(+), 92 deletions(-) diff --git a/polyjuice-tests/src/helper.rs b/polyjuice-tests/src/helper.rs index 6dd0cd10..196a5550 100644 --- a/polyjuice-tests/src/helper.rs +++ b/polyjuice-tests/src/helper.rs @@ -505,6 +505,7 @@ pub fn deploy( ) .expect("construct"); state.apply_run_result(&run_result).expect("update state"); + // println!("[deploy contract] used cycles: {}", run_result.used_cycles); run_result } @@ -563,14 +564,17 @@ pub fn simple_storage_get( .build(); let db = store.begin_transaction(); let tip_block_hash = store.get_tip_block_hash().unwrap(); - generator + let run_result = generator .execute_transaction( &ChainView::new(&db, tip_block_hash), state, &block_info, &raw_tx, ) - .expect("construct") + .expect("construct"); + // 491894, 571661 < 580K + check_cycles("simple_storage_get", run_result.used_cycles, 580_000); + run_result } pub fn build_l2_sudt_script(args: [u8; 32]) -> Script { @@ -594,3 +598,12 @@ pub fn build_eth_l2_script(args: [u8; 20]) -> Script { .hash_type(ScriptHashType::Type.into()) .build() } + +pub fn check_cycles(l2_tx_label: &str, used_cycles: u64, warning_cycles: u64) { + assert!( + used_cycles < warning_cycles, + "[Warning: {} used too many cycles = {}]", + l2_tx_label, + used_cycles + ); +} diff --git a/polyjuice-tests/src/test_cases/call_selfdestruct.rs b/polyjuice-tests/src/test_cases/call_selfdestruct.rs index 5a3de7e1..976baedd 100644 --- a/polyjuice-tests/src/test_cases/call_selfdestruct.rs +++ b/polyjuice-tests/src/test_cases/call_selfdestruct.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/SelfDestruct.sol use crate::helper::{ - account_id_to_eth_address, build_eth_l2_script, contract_script_to_eth_address, deploy, + self, account_id_to_eth_address, build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script_with_nonce, new_block_info, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; @@ -12,7 +12,7 @@ use gw_store::chain_view::ChainView; use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*}; const SD_INIT_CODE: &str = include_str!("./evm-contracts/SelfDestruct.bin"); -const INIT_CODE: &str = include_str!("./evm-contracts/CallSelfDestruct.bin"); +const CALL_SD_INIT_CODE: &str = include_str!("./evm-contracts/CallSelfDestruct.bin"); #[test] fn test_selfdestruct() { @@ -44,12 +44,13 @@ fn test_selfdestruct() { 0 ); + // deploy SelfDestruct let input = format!( "{}{}", SD_INIT_CODE, hex::encode(account_id_to_eth_address(&state, beneficiary_id, true)) ); - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -61,6 +62,9 @@ fn test_selfdestruct() { block_producer_id, block_number, ); + // 571282 < 580K + helper::check_cycles("deploy SelfDestruct", run_result.used_cycles, 580_000); + block_number += 1; let sd_account_script = new_account_script_with_nonce(&state, creator_account_id, from_id, 0); let sd_script_hash = sd_account_script.hash(); @@ -82,18 +86,22 @@ fn test_selfdestruct() { 0 ); - let _run_result = deploy( + // deploy CallSelfDestruct + let run_result = deploy( &generator, &store, &mut state, creator_account_id, from_id, - INIT_CODE, + CALL_SD_INIT_CODE, 122000, 0, block_producer_id, block_number, ); + // [deploy CallSelfDestruct] used cycles: 551984 < 560K + helper::check_cycles("deploy CallSelfDestruct", run_result.used_cycles, 560_000); + block_number += 1; let new_account_script = new_account_script_with_nonce(&state, creator_account_id, from_id, 1); let new_account_id = state @@ -135,6 +143,12 @@ fn test_selfdestruct() { ) .expect("construct"); state.apply_run_result(&run_result).expect("update state"); + // [call CallSelfDestruct.proxyDone(sd_account_id)] used cycles: 1043108 < 1100K + helper::check_cycles( + "CallSelfDestruct.proxyDone(sd_account_id)", + run_result.used_cycles, + 1100_000, + ); } assert_eq!(state.get_nonce(from_id).unwrap(), 3); diff --git a/polyjuice-tests/src/test_cases/contract_call_contract.rs b/polyjuice-tests/src/test_cases/contract_call_contract.rs index dd8860f4..027f47f9 100644 --- a/polyjuice-tests/src/test_cases/contract_call_contract.rs +++ b/polyjuice-tests/src/test_cases/contract_call_contract.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/CallContract.sol use crate::helper::{ - build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script, + self, build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script, new_block_info, setup, simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; @@ -58,7 +58,7 @@ fn test_contract_call_contract() { INIT_CODE, hex::encode(contract_script_to_eth_address(&ss_account_script, true)), ); - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -71,6 +71,9 @@ fn test_contract_call_contract() { block_number, ); block_number += 1; + // [Deploy CreateContract] used cycles: 600288 < 610K + helper::check_cycles("Deploy CreateContract", run_result.used_cycles, 610_000); + // println!( // "result {}", // serde_json::to_string_pretty(&RunResult::from(run_result)).unwrap() @@ -123,6 +126,8 @@ fn test_contract_call_contract() { ) .expect("construct"); state.apply_run_result(&run_result).expect("update state"); + // [CallContract.proxySet(222)] used cycles: 961599 < 970K + helper::check_cycles("CallContract.proxySet()", run_result.used_cycles, 970_000); } let run_result = simple_storage_get( @@ -161,7 +166,7 @@ fn test_contract_call_non_exists_contract() { let block_number = 1; // Deploy CallNonExistsContract - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -173,6 +178,12 @@ fn test_contract_call_non_exists_contract() { block_producer_id, block_number, ); + // [Deploy CallNonExistsContract] used cycles: 657243 < 670K + helper::check_cycles( + "Deploy CallNonExistsContract", + run_result.used_cycles, + 670_000, + ); let contract_account_script = new_account_script(&mut state, creator_account_id, from_id, false); @@ -207,6 +218,13 @@ fn test_contract_call_non_exists_contract() { &raw_tx, ) .expect("construct"); + // [CallNonExistsContract.rawCall(addr)] used cycles: 862060 < 870K + helper::check_cycles( + "CallNonExistsContract.rawCall(addr)", + run_result.used_cycles, + 870_000, + ); + assert_eq!( run_result.return_data, vec![ diff --git a/polyjuice-tests/src/test_cases/contract_create_contract.rs b/polyjuice-tests/src/test_cases/contract_create_contract.rs index 6c426b3b..61ad2e5b 100644 --- a/polyjuice-tests/src/test_cases/contract_create_contract.rs +++ b/polyjuice-tests/src/test_cases/contract_create_contract.rs @@ -2,8 +2,8 @@ //! See ./evm-contracts/CreateContract.sol use crate::helper::{ - build_eth_l2_script, deploy, new_account_script, new_account_script_with_nonce, new_block_info, - setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, + self, build_eth_l2_script, deploy, new_account_script, new_account_script_with_nonce, + new_block_info, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -30,7 +30,7 @@ fn test_contract_create_contract() { .unwrap(); // Deploy CreateContract - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -42,6 +42,8 @@ fn test_contract_create_contract() { block_producer_id, 1, ); + // [Deploy CreateContract] used cycles: 2109521 < 2120K + helper::check_cycles("Deploy CreateContract", run_result.used_cycles, 2120_000); // println!( // "result {}", // serde_json::to_string_pretty(&RunResult::from(run_result)).unwrap() diff --git a/polyjuice-tests/src/test_cases/create2.rs b/polyjuice-tests/src/test_cases/create2.rs index 738d00c4..4411b707 100644 --- a/polyjuice-tests/src/test_cases/create2.rs +++ b/polyjuice-tests/src/test_cases/create2.rs @@ -2,8 +2,8 @@ //! See ./evm-contracts/CallContract.sol use crate::helper::{ - build_eth_l2_script, compute_create2_script, deploy, new_account_script, new_block_info, setup, - simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, + self, build_eth_l2_script, compute_create2_script, deploy, new_account_script, new_block_info, + setup, simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -12,7 +12,7 @@ use gw_store::chain_view::ChainView; use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*}; const SS_INIT_CODE: &str = include_str!("./evm-contracts/SimpleStorage.bin"); -const INIT_CODE: &str = include_str!("./evm-contracts/Create2Impl.bin"); +const CREATE2_IMPL_CODE: &str = include_str!("./evm-contracts/Create2Impl.bin"); #[test] fn test_create2() { @@ -31,20 +31,22 @@ fn test_create2() { .unwrap(); let mut block_number = 1; - // Deploy CreateContract - let _run_result = deploy( + // Deploy CREATE2_IMPL_CODE + let run_result = deploy( &generator, &store, &mut state, creator_account_id, from_id, - INIT_CODE, + CREATE2_IMPL_CODE, 122000, 0, block_producer_id, block_number, ); block_number += 1; + // [Deploy Create2Impl] used cycles: 805376 < 810K + helper::check_cycles("Deploy Create2Impl", run_result.used_cycles, 810_000); // println!( // "result {}", // serde_json::to_string_pretty(&RunResult::from(run_result)).unwrap() @@ -98,7 +100,8 @@ fn test_create2() { &raw_tx, ) .expect("construct"); - // println!("run_result: {:?}", run_result); + // [Create2Impl.deploy(...)] used cycles: 1197555 < 1210K + helper::check_cycles("Create2Impl.deploy(...)", run_result.used_cycles, 1210_000); state.apply_run_result(&run_result).expect("update state"); run_result }; diff --git a/polyjuice-tests/src/test_cases/delegatecall.rs b/polyjuice-tests/src/test_cases/delegatecall.rs index 26383d29..31663aec 100644 --- a/polyjuice-tests/src/test_cases/delegatecall.rs +++ b/polyjuice-tests/src/test_cases/delegatecall.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/CallContract.sol use crate::helper::{ - build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script, + self, build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script, new_account_script_with_nonce, new_block_info, setup, simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; @@ -53,7 +53,7 @@ fn test_delegatecall() { .unwrap(); // Deploy DelegateCall - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -65,6 +65,8 @@ fn test_delegatecall() { block_producer_id, block_number, ); + // [Deploy DelegateCall] used cycles: 742217 < 750K + helper::check_cycles("Deploy DelegateCall", run_result.used_cycles, 750_000); block_number += 1; // println!( // "result {}", @@ -82,17 +84,17 @@ fn test_delegatecall() { assert_eq!(state.get_nonce(new_account_id).unwrap(), 0); for (fn_sighash, expected_return_value) in [ - // DelegateCall.set(address, uint) + // DelegateCall.set(address, uint) => used cycles: 1002251 ( "3825d828", "0000000000000000000000000000000000000000000000000000000000000022", ), - // DelegateCall.overwrite(address, uint) + // DelegateCall.overwrite(address, uint) => used cycles: 1002099 ( "3144564b", "0000000000000000000000000000000000000000000000000000000000000023", ), - // DelegateCall.multiCall(address, uint) + // DelegateCall.multiCall(address, uint) => used cycles: 1422033 ( "c6c211e9", "0000000000000000000000000000000000000000000000000000000000000024", @@ -129,6 +131,7 @@ fn test_delegatecall() { &raw_tx, ) .expect("construct"); + helper::check_cycles("DelegateCall", run_result.used_cycles, 1_430_000); state.apply_run_result(&run_result).expect("update state"); // println!( // "result {}", diff --git a/polyjuice-tests/src/test_cases/ecrecover.rs b/polyjuice-tests/src/test_cases/ecrecover.rs index f7f04ef1..c739ab59 100644 --- a/polyjuice-tests/src/test_cases/ecrecover.rs +++ b/polyjuice-tests/src/test_cases/ecrecover.rs @@ -1,9 +1,9 @@ -//! Test SimpleStorage -//! See ./evm-contracts/SimpleStorage.sol +//! Test ecrecover +//! See ./evm-contracts/HeadTail.sol use crate::helper::{ - build_eth_l2_script, deploy, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, - CKB_SUDT_ACCOUNT_ID, + self, build_eth_l2_script, deploy, new_account_script, new_block_info, setup, + PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -28,8 +28,8 @@ fn test_ecrecover() { .mint_sudt(CKB_SUDT_ACCOUNT_ID, from_short_address, 200000) .unwrap(); - // Deploy CreateContract - let _run_result = deploy( + // Deploy HeadTail Contract + let run_result = deploy( &generator, &store, &mut state, @@ -41,6 +41,12 @@ fn test_ecrecover() { block_producer_id, 1, ); + // [Deploy HeadTail Contract] used cycles: 1590800 < 1600K + helper::check_cycles( + "Deploy HeadTail Contract", + run_result.used_cycles, + 1_600_000, + ); // println!( // "result {}", // serde_json::to_string_pretty(&RunResult::from(run_result)).unwrap() @@ -56,7 +62,7 @@ fn test_ecrecover() { println!("\n==============================================\n"); { - // SimpleStorage.get(); + // verify|recover(bytes32 hash, bytes memory signature) let block_info = new_block_info(0, 2, 0); let hash = "8ab0890f028c9502cc20d441b4c4bb116f48ea632f522ac84e965d1dadf918e1"; let signed_hash = "aaa99f644a5c4447314c5b7fcfac80deb186218aca1edaa63711aa75eb36585b47743901ce20f32768c7108bf85457ee0f16020f9bebc2bf456d6094c1c923c11c"; @@ -86,6 +92,8 @@ fn test_ecrecover() { &raw_tx, ) .expect("construct"); + // [recover] used cycles: 2604412 < 2610K + helper::check_cycles("verify|recover", run_result.used_cycles, 2_610_000); state.apply_run_result(&run_result).expect("update state"); assert_eq!( run_result.return_data, diff --git a/polyjuice-tests/src/test_cases/erc20.rs b/polyjuice-tests/src/test_cases/erc20.rs index 569d38ef..7968123d 100644 --- a/polyjuice-tests/src/test_cases/erc20.rs +++ b/polyjuice-tests/src/test_cases/erc20.rs @@ -2,8 +2,8 @@ //! See ./evm-contracts/ERC20.bin use crate::helper::{ - account_id_to_eth_address, build_eth_l2_script, deploy, new_account_script, new_block_info, - setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, + self, account_id_to_eth_address, build_eth_l2_script, deploy, new_account_script, + new_block_info, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -41,7 +41,7 @@ fn test_erc20() { .unwrap(); // Deploy ERC20 - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -53,6 +53,8 @@ fn test_erc20() { block_producer_id, 1, ); + // [Deploy ERC20] used cycles: 1004236 < 1010K + helper::check_cycles("Deploy ERC20", run_result.used_cycles, 1_010_000); let contract_account_script = new_account_script(&mut state, creator_account_id, from_id1, false); @@ -166,6 +168,8 @@ fn test_erc20() { &raw_tx, ) .expect("construct"); + // [ERC20 contract method_x] used cycles: 942107 < 950K + helper::check_cycles("ERC20 contract method_x", run_result.used_cycles, 950_000); state.apply_run_result(&run_result).expect("update state"); assert_eq!( run_result.return_data, diff --git a/polyjuice-tests/src/test_cases/eth_to_godwoken_addr.rs b/polyjuice-tests/src/test_cases/eth_to_godwoken_addr.rs index b2754b35..482f6abb 100644 --- a/polyjuice-tests/src/test_cases/eth_to_godwoken_addr.rs +++ b/polyjuice-tests/src/test_cases/eth_to_godwoken_addr.rs @@ -1,8 +1,8 @@ //! See ./evm-contracts/EthToGodwokenAddr.sol use crate::helper::{ - build_eth_l2_script, deploy, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, - CKB_SUDT_ACCOUNT_ID, PROGRAM_CODE_HASH, ROLLUP_SCRIPT_HASH, + self, build_eth_l2_script, deploy, new_account_script, new_block_info, setup, + PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, PROGRAM_CODE_HASH, ROLLUP_SCRIPT_HASH, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -33,7 +33,7 @@ fn test_eth_to_godwoken_addr() { .mint_sudt(CKB_SUDT_ACCOUNT_ID, from_short_address, 200000) .unwrap(); - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -45,6 +45,12 @@ fn test_eth_to_godwoken_addr() { block_producer_id, 0, ); + // [Deploy EthToGodwokenAddr Contract] used cycles: 593775 < 600K + helper::check_cycles( + "Deploy EthToGodwokenAddr Contract", + run_result.used_cycles, + 600_000, + ); let contract_account_script = new_account_script(&mut state, creator_account_id, from_id, false); @@ -83,6 +89,12 @@ fn test_eth_to_godwoken_addr() { &raw_tx, ) .expect("construct"); + // [EthToGodwokenAddr.convert(addr)] used cycles: 573228 < 580K + helper::check_cycles( + "EthToGodwokenAddr.convert(addr)", + run_result.used_cycles, + 580_000, + ); state.apply_run_result(&run_result).expect("update state"); let mut script_args = vec![0u8; 32 + 4 + 20]; script_args[0..32].copy_from_slice(&ROLLUP_SCRIPT_HASH); diff --git a/polyjuice-tests/src/test_cases/fallback_function.rs b/polyjuice-tests/src/test_cases/fallback_function.rs index 60f7d47a..ed1603ec 100644 --- a/polyjuice-tests/src/test_cases/fallback_function.rs +++ b/polyjuice-tests/src/test_cases/fallback_function.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/FallbackFunction.sol use crate::helper::{ - build_eth_l2_script, new_account_script, new_block_info, setup, simple_storage_get, + self, build_eth_l2_script, new_account_script, new_block_info, setup, simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; @@ -29,7 +29,7 @@ fn test_fallback_function() { .unwrap(); { - // Deploy SimpleStorage + // Deploy FallbackFunction Contract let block_info = new_block_info(0, 1, 0); let input = hex::decode(INIT_CODE).unwrap(); let args = PolyjuiceArgsBuilder::default() @@ -54,6 +54,8 @@ fn test_fallback_function() { &raw_tx, ) .expect("construct"); + // [Deploy FallbackFunction] used cycles: 587271 < 590K + helper::check_cycles("Deploy FallbackFunction", run_result.used_cycles, 590_000); state.apply_run_result(&run_result).expect("update state"); } @@ -70,6 +72,7 @@ fn test_fallback_function() { ); { + // Call fallback() let block_info = new_block_info(0, 2, 0); let input = hex::decode("3333").unwrap(); let args = PolyjuiceArgsBuilder::default() @@ -93,6 +96,8 @@ fn test_fallback_function() { &raw_tx, ) .expect("construct"); + // [Call fallback()] used cycles: 504210 < 510K + helper::check_cycles("Call fallback()", run_result.used_cycles, 510_000); assert!(run_result.return_data.is_empty()); state.apply_run_result(&run_result).expect("update state"); } diff --git a/polyjuice-tests/src/test_cases/heap_memory.rs b/polyjuice-tests/src/test_cases/heap_memory.rs index 8302e0d4..9d846952 100644 --- a/polyjuice-tests/src/test_cases/heap_memory.rs +++ b/polyjuice-tests/src/test_cases/heap_memory.rs @@ -2,12 +2,12 @@ //! See ./evm-contracts/Memory.sol use crate::helper::{ - build_eth_l2_script, deploy, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, - CKB_SUDT_ACCOUNT_ID, + self, build_eth_l2_script, deploy, new_account_script, new_block_info, setup, + PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; -use gw_generator::{traits::StateExt}; // error::TransactionError, +use gw_generator::traits::StateExt; // error::TransactionError, use gw_store::chain_view::ChainView; use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*}; @@ -77,6 +77,8 @@ fn test_heap_momory() { &raw_tx, ) .expect("success to malloc memory"); + // [newMemory less than 512K] used cycles: 3454814 < 3465K + helper::check_cycles("new Memory", run_result.used_cycles, 3_465_000); println!( "\t new byte(about {}K) => call result {:?}", 16 * 32, diff --git a/polyjuice-tests/src/test_cases/invalid_sudt_erc20_proxy.rs b/polyjuice-tests/src/test_cases/invalid_sudt_erc20_proxy.rs index f3c7daa9..3205cebf 100644 --- a/polyjuice-tests/src/test_cases/invalid_sudt_erc20_proxy.rs +++ b/polyjuice-tests/src/test_cases/invalid_sudt_erc20_proxy.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/ERC20.bin use crate::helper::{ - account_id_to_eth_address, build_eth_l2_script, build_l2_sudt_script, deploy, + self, account_id_to_eth_address, build_eth_l2_script, build_l2_sudt_script, deploy, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; @@ -49,11 +49,11 @@ fn test_invalid_sudt_erc20_proxy() { .unwrap(); assert_eq!(CKB_SUDT_ACCOUNT_ID, 1); - // Deploy ERC20 + // Deploy InvalidSudtERC20Proxy // ethabi encode params -v string "test" -v string "tt" -v uint256 000000000000000000000000000000000000000204fce5e3e250261100000000 -v uint256 0000000000000000000000000000000000000000000000000000000000000001 let args = format!("000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000204fce5e3e25026110000000000000000000000000000000000000000000000000000000000000000000000{:02x}0000000000000000000000000000000000000000000000000000000000000004746573740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000027474000000000000000000000000000000000000000000000000000000000000", new_sudt_id); let init_code = format!("{}{}", INIT_CODE, args); - let _run_result = deploy( + let run_result = deploy( &generator, &store, &mut state, @@ -65,6 +65,12 @@ fn test_invalid_sudt_erc20_proxy() { block_producer_id, 1, ); + // [Deploy InvalidSudtERC20Proxy] used cycles: 1421789 < 1430K + helper::check_cycles( + "Deploy InvalidSudtERC20Proxy", + run_result.used_cycles, + 1_430_000, + ); let contract_account_script = new_account_script(&mut state, creator_account_id, from_id1, false); @@ -157,8 +163,15 @@ fn test_invalid_sudt_erc20_proxy() { &block_info, &raw_tx, ); + if *success { let run_result = result.expect("execute"); + // used cycles: 844202 < 850K + helper::check_cycles( + "ERC20.{balanceOf|transfer}", + run_result.used_cycles, + 850_000, + ); state.apply_run_result(&run_result).expect("update state"); assert_eq!( run_result.return_data, diff --git a/polyjuice-tests/src/test_cases/recover_account.rs b/polyjuice-tests/src/test_cases/recover_account.rs index 5dfbe5e8..1a081915 100644 --- a/polyjuice-tests/src/test_cases/recover_account.rs +++ b/polyjuice-tests/src/test_cases/recover_account.rs @@ -2,9 +2,9 @@ //! See ./evm-contracts/RecoverAccount.sol use crate::helper::{ - build_eth_l2_script, deploy, new_account_script, new_block_info, setup, simple_storage_get, - PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, FATAL_PRECOMPILED_CONTRACTS, ROLLUP_SCRIPT_HASH, - SECP_LOCK_CODE_HASH, + self, build_eth_l2_script, deploy, new_account_script, new_block_info, setup, + simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, FATAL_PRECOMPILED_CONTRACTS, + ROLLUP_SCRIPT_HASH, SECP_LOCK_CODE_HASH, }; use gw_common::state::State; use gw_generator::{error::TransactionError, traits::StateExt}; @@ -35,7 +35,8 @@ fn test_recover_account() { .mint_sudt(CKB_SUDT_ACCOUNT_ID, from_short_address, 200000) .unwrap(); - let _run_result = deploy( + // Deploy RecoverAccount Contract + let run_result = deploy( &generator, &store, &mut state, @@ -47,7 +48,12 @@ fn test_recover_account() { block_producer_id, 0, ); - + // Deploy RecoverAccount Contract used cycles = 682404 < 690K + helper::check_cycles( + "Deploy RecoverAccount Contract", + run_result.used_cycles, + 690_000, + ); let contract_account_script = new_account_script(&mut state, creator_account_id, from_id, false); let new_account_id = state @@ -93,6 +99,12 @@ fn test_recover_account() { &raw_tx, ) .expect("construct"); + // [RecoverAccount.recover(message, signature, code_hash)] used cycles: 648630 < 660K + helper::check_cycles( + "RecoverAccount.recover(message, signature, code_hash)", + run_result.used_cycles, + 660_000, + ); state.apply_run_result(&run_result).expect("update state"); let mut script_args = vec![0u8; 32 + 20]; script_args[0..32].copy_from_slice(&ROLLUP_SCRIPT_HASH); diff --git a/polyjuice-tests/src/test_cases/selfdestruct.rs b/polyjuice-tests/src/test_cases/selfdestruct.rs index 8ea14218..3e2e0bcb 100644 --- a/polyjuice-tests/src/test_cases/selfdestruct.rs +++ b/polyjuice-tests/src/test_cases/selfdestruct.rs @@ -2,8 +2,8 @@ //! See ./evm-contracts/SelfDestruct.sol use crate::helper::{ - account_id_to_eth_address, build_eth_l2_script, new_account_script, new_block_info, setup, - PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, + self, account_id_to_eth_address, build_eth_l2_script, new_account_script, new_block_info, + setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; use gw_generator::traits::StateExt; @@ -68,8 +68,9 @@ fn test_selfdestruct() { &raw_tx, ) .expect("construct"); + // [Deploy SelfDestruct] used cycles: 570570 < 580K + helper::check_cycles("Deploy SelfDestruct", run_result.used_cycles, 580_000); state.apply_run_result(&run_result).expect("update state"); - // println!("result {:?}", run_result); } let contract_account_script = @@ -117,8 +118,9 @@ fn test_selfdestruct() { &raw_tx, ) .expect("construct"); + // [call SelfDestruct.done()] used cycles: 589657 < 600K + helper::check_cycles("call SelfDestruct.done()", run_result.used_cycles, 600_000); state.apply_run_result(&run_result).expect("update state"); - // println!("result {:?}", run_result); } assert_eq!( state diff --git a/polyjuice-tests/src/test_cases/simple_storage.rs b/polyjuice-tests/src/test_cases/simple_storage.rs index bca31c2a..c0db85c5 100644 --- a/polyjuice-tests/src/test_cases/simple_storage.rs +++ b/polyjuice-tests/src/test_cases/simple_storage.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/SimpleStorage.sol use crate::helper::{ - build_eth_l2_script, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, + self, build_eth_l2_script, new_account_script, new_block_info, setup, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; use gw_common::state::State; @@ -59,8 +59,9 @@ fn test_simple_storage() { ) .expect("construct"); state.apply_run_result(&run_result).expect("update state"); - // println!("result {:?}", run_result); println!("return_data: {}", hex::encode(&run_result.return_data[..])); + // 557534 < 560K + helper::check_cycles("Deploy SimpleStorage", run_result.used_cycles, 560_000); } let contract_account_script = @@ -109,7 +110,8 @@ fn test_simple_storage() { ) .expect("construct"); state.apply_run_result(&run_result).expect("update state"); - // println!("result {:?}", run_result); + // 489767 < 500K + helper::check_cycles("SimpleStorage.set", run_result.used_cycles, 500_000); } { @@ -142,37 +144,7 @@ fn test_simple_storage() { expected_return_data[30] = 0x0d; expected_return_data[31] = 0x10; assert_eq!(run_result.return_data, expected_return_data); - // println!("result {:?}", run_result); } - { - // SimpleStorage.get(); - let block_info = new_block_info(0, 3, 0); - let input = hex::decode("6d4ce63c").unwrap(); - let args = PolyjuiceArgsBuilder::default() - .gas_limit(21000) - .gas_price(0) - .value(0) - .input(&input) - .build(); - let raw_tx = RawL2Transaction::new_builder() - .from_id(from_id.pack()) - .to_id(new_account_id.pack()) - .args(Bytes::from(args).pack()) - .build(); - let db = store.begin_transaction(); - let tip_block_hash = store.get_tip_block_hash().unwrap(); - let run_result = generator - .execute_transaction( - &ChainView::new(&db, tip_block_hash), - &state, - &block_info, - &raw_tx, - ) - .expect("construct"); - let mut expected_return_data = vec![0u8; 32]; - expected_return_data[30] = 0x0d; - expected_return_data[31] = 0x10; - assert_eq!(run_result.return_data, expected_return_data); - } + helper::simple_storage_get(&store, &state, &generator, 4, from_id, new_account_id); } diff --git a/polyjuice-tests/src/test_cases/simple_transfer.rs b/polyjuice-tests/src/test_cases/simple_transfer.rs index c8848d20..3d8d0700 100644 --- a/polyjuice-tests/src/test_cases/simple_transfer.rs +++ b/polyjuice-tests/src/test_cases/simple_transfer.rs @@ -2,7 +2,7 @@ //! See ./evm-contracts/SimpleTransfer.sol use crate::helper::{ - account_id_to_eth_address, build_eth_l2_script, contract_script_to_eth_address, deploy, + self, account_id_to_eth_address, build_eth_l2_script, contract_script_to_eth_address, deploy, new_account_script, new_block_info, setup, simple_storage_get, PolyjuiceArgsBuilder, CKB_SUDT_ACCOUNT_ID, }; @@ -106,6 +106,9 @@ fn test_simple_transfer() { block_producer_id, block_number, ); + // [Deploy SimpleTransfer] used cycles: 491894 < 500K + helper::check_cycles("Deploy SimpleTransfer", run_result.used_cycles, 500_000); + block_number += 1; let contract_account_script = new_account_script(&mut state, creator_account_id, from_id, false); @@ -158,6 +161,8 @@ fn test_simple_transfer() { &raw_tx, ) .expect("construct"); + // [SimpleTransfer to EoA] used cycles: 725217 < 736K + helper::check_cycles("SimpleTransfer to EoA", run_result.used_cycles, 736_000); state.apply_run_result(&run_result).expect("update state"); let new_balance = state @@ -203,6 +208,13 @@ fn test_simple_transfer() { &raw_tx, ) .expect("construct"); + // [SimpleTransfer to zero address] used cycles: 699554 < 710K + helper::check_cycles( + "SimpleTransfer to zero address", + run_result.used_cycles, + 710_000, + ); + state.apply_run_result(&run_result).expect("update state"); let new_balance = state @@ -249,6 +261,12 @@ fn test_simple_transfer() { &raw_tx, ) .expect("construct"); + // [SimpleTransfer.transferToSimpleStorage1] used cycles: 1203332 < 1210K + helper::check_cycles( + "SimpleTransfer.transferToSimpleStorage1()", + run_result.used_cycles, + 1_210_000, + ); state.apply_run_result(&run_result).expect("update state"); let new_balance = state diff --git a/polyjuice-tests/src/test_cases/simple_wallet.rs b/polyjuice-tests/src/test_cases/simple_wallet.rs index f517441a..b521b301 100644 --- a/polyjuice-tests/src/test_cases/simple_wallet.rs +++ b/polyjuice-tests/src/test_cases/simple_wallet.rs @@ -24,9 +24,9 @@ fn test_simple_wallet() { .unwrap(); let mut block_number = 0; - // Deploy Contract + // Deploy SimpleWallet Contract block_number += 1; - let _run_result = helper::deploy( + let run_result = helper::deploy( &generator, &store, &mut state, @@ -38,6 +38,9 @@ fn test_simple_wallet() { block_producer_id, block_number, ); + // [Deploy SimpleWallet Contract] used cycles: 1741377 < 1750K + helper::check_cycles("Deploy SimpleWallet", run_result.used_cycles, 1_750_000); + let account_script = helper::new_account_script(&mut state, creator_account_id, from_id, false); let _contract_account_id = state .get_account_id_by_script_hash(&account_script.hash().into())