Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #75 from nervosnetwork/test-L2TX_Warning_CYCLES
Browse files Browse the repository at this point in the history
Add helper::check_cycles into test cases

Once unexpected cycles of l2tx come out, we will get too many cycles Warning.
  • Loading branch information
Flouse authored Aug 11, 2021
2 parents 2800e89 + b9b7c33 commit 67616ab
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion c/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down
17 changes: 15 additions & 2 deletions polyjuice-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
);
}
24 changes: 19 additions & 5 deletions polyjuice-tests/src/test_cases/call_selfdestruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 21 additions & 3 deletions polyjuice-tests/src/test_cases/contract_call_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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![
Expand Down
8 changes: 5 additions & 3 deletions polyjuice-tests/src/test_cases/contract_create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +30,7 @@ fn test_contract_create_contract() {
.unwrap();

// Deploy CreateContract
let _run_result = deploy(
let run_result = deploy(
&generator,
&store,
&mut state,
Expand All @@ -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()
Expand Down
17 changes: 10 additions & 7 deletions polyjuice-tests/src/test_cases/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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()
Expand Down Expand Up @@ -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
};
Expand Down
13 changes: 8 additions & 5 deletions polyjuice-tests/src/test_cases/delegatecall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -53,7 +53,7 @@ fn test_delegatecall() {
.unwrap();

// Deploy DelegateCall
let _run_result = deploy(
let run_result = deploy(
&generator,
&store,
&mut state,
Expand All @@ -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 {}",
Expand All @@ -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",
Expand Down Expand Up @@ -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 {}",
Expand Down
Loading

0 comments on commit 67616ab

Please sign in to comment.