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

Commit

Permalink
chore: cleanup duplicate methods and add failed test case
Browse files Browse the repository at this point in the history
  • Loading branch information
magicalne committed Aug 1, 2022
1 parent 02f11b4 commit a8342d3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 54 deletions.
5 changes: 3 additions & 2 deletions devtools/ci/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ make all-via-docker

cd $TESTS_DIR
export RUST_BACKTRACE=full
cargo test -- --nocapture
cargo test --lib -- --nocapture
# TODO: cargo bench | egrep -v debug

# run ethereum test
cargo test -- ethereum_test --nocapture
RUST_LOG=info,gw_generator=debug cargo test --test ethereum_test -- ethereum_test --nocapture

53 changes: 2 additions & 51 deletions polyjuice-tests/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::{
collections::HashMap,
convert::TryInto,
fs,
io::Read,
path::{Path, PathBuf},
time::SystemTime,
};
Expand Down Expand Up @@ -40,6 +37,7 @@ use gw_types::{
};

use crate::helper::{
build_eth_l2_script, build_l2_sudt_script, create_block_producer, load_program,
PolyjuiceArgsBuilder, CHAIN_ID, CREATOR_ACCOUNT_ID, ETH_ACCOUNT_LOCK_CODE_HASH,
L2TX_MAX_CYCLES, META_VALIDATOR_SCRIPT_TYPE_HASH, POLYJUICE_PROGRAM_CODE_HASH,
ROLLUP_SCRIPT_HASH, SECP_LOCK_CODE_HASH, SUDT_VALIDATOR_SCRIPT_TYPE_HASH,
Expand All @@ -62,14 +60,6 @@ pub const ETH_ADDRESS_REGISTRY_GENERATOR_NAME: &str =
"build/godwoken-scripts/eth-addr-reg-generator";
pub const ETH_ADDRESS_REGISTRY_VALIDATOR_NAME: &str =
"build/godwoken-scripts/eth-addr-reg-validator";
fn load_program(program_name: &str) -> Bytes {
let mut buf = Vec::new();
let mut path = PathBuf::new();
path.push(program_name);
let mut f = fs::File::open(&path).unwrap_or_else(|_| panic!("load program {}", program_name));
f.read_to_end(&mut buf).expect("read program");
Bytes::from(buf.to_vec())
}

fn load_code_hash(path: &Path) -> [u8; 32] {
let mut buf = [0u8; 32];
Expand All @@ -86,23 +76,6 @@ fn set_mapping(state: &mut DummyState, eth_address: &[u8; 20], script_hash: &[u8
.expect("map reg addr to script hash");
}

fn create_block_producer(state: &mut DummyState) -> anyhow::Result<RegistryAddress> {
// This eth_address is hardcoded in src/test_cases/evm-contracts/BlockInfo.sol
let eth_address: [u8; 20] = hex::decode("a1ad227Ad369f593B5f3d0Cc934A681a50811CB2")?
.try_into()
.expect("decode");
let block_producer_script = build_eth_l2_script(&eth_address);
let block_producer_script_hash = block_producer_script.hash();
let _block_producer_id = state
.create_account_from_script(block_producer_script)
.expect("create_block_producer");
set_mapping(state, &eth_address, &block_producer_script_hash);
Ok(RegistryAddress::new(
ETH_REGISTRY_ACCOUNT_ID,
eth_address.to_vec(),
))
}

pub struct MockChain {
ctx: Context,
block_producer: RegistryAddress,
Expand All @@ -117,7 +90,7 @@ impl MockChain {
*/
pub fn setup(base_path: &str) -> anyhow::Result<Self> {
let mut ctx = Context::setup(base_path)?;
let block_producer = create_block_producer(&mut ctx.state)?;
let block_producer = create_block_producer(&mut ctx.state);
let timestamp = SystemTime::now();
Ok(Self {
ctx,
Expand Down Expand Up @@ -529,28 +502,6 @@ impl Config {
}
}

pub fn build_l2_sudt_script(args: [u8; 32]) -> Script {
let mut script_args = Vec::with_capacity(64);
script_args.extend(&ROLLUP_SCRIPT_HASH);
script_args.extend(&args[..]);
Script::new_builder()
.args(Bytes::from(script_args).pack())
.code_hash(SUDT_VALIDATOR_SCRIPT_TYPE_HASH.clone().pack())
.hash_type(ScriptHashType::Type.into())
.build()
}

pub fn build_eth_l2_script(args: &[u8; 20]) -> Script {
let mut script_args = Vec::with_capacity(32 + 20);
script_args.extend(&ROLLUP_SCRIPT_HASH);
script_args.extend(&args[..]);
Script::new_builder()
.args(Bytes::from(script_args).pack())
.code_hash(ETH_ACCOUNT_LOCK_CODE_HASH.clone().pack())
.hash_type(ScriptHashType::Type.into())
.build()
}

// port from poolyjuice.h#polyjuice_build_system_key
pub fn build_contract_code_key(account_id: u32) -> [u8; 32] {
let mut key = [0u8; 32];
Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub const FATAL_PRECOMPILED_CONTRACTS: i8 = -51;
pub(crate) const SUDT_ERC20_PROXY_USER_DEFINED_DECIMALS_CODE: &str =
include_str!("../../solidity/erc20/SudtERC20Proxy_UserDefinedDecimals.bin");

fn load_program(program_name: &str) -> Bytes {
pub fn load_program(program_name: &str) -> Bytes {
let mut buf = Vec::new();
let mut path = PathBuf::new();
path.push(program_name);
Expand Down
26 changes: 26 additions & 0 deletions polyjuice-tests/tests/ethereum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,29 @@ fn ethereum_test() -> anyhow::Result<()> {
}
Ok(())
}

#[test]
fn ethereum_failure_test() -> anyhow::Result<()> {
let mut paths = Vec::new();
read_all_files(Path::new(TEST_CASE_DIR), &mut paths)?;
let mut err_cases = Vec::new();
for path in paths {
if let Some(filename) = path.file_name() {
if let Some(filename) = filename.to_str() {
if EXCLUDE_TEST_FILES.contains(&filename) {
println!("Starting test with: {:?}", &path);
let content = fs::read_to_string(&path)?;
let test_cases: HashMap<String, TestCase> = serde_json::from_str(&content)?;
for (testname, testcase) in test_cases {
println!("test name: {}", testname);
let runner = VMTestRunner::new(testcase)?;
if let Err(_) = runner.run() {
err_cases.push(path.clone());
}
}
}
}
}
}
Ok(())
}

0 comments on commit a8342d3

Please sign in to comment.