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

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroqn committed Apr 25, 2022
1 parent 645dbfd commit 99997e6
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 102 deletions.
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
const BINARIES: &[(&str, &str)] = &[
(
"generator",
"b1284ae3e3268611368b60c4e7f08b5086cd2d5c5f20c56c94051e689b658f7f",
"2dedb79afb0bffb21e01a832866cb32bd49bbde0af4b76e3c1fe8d7e1943cf52",
),
(
"validator",
"9996e062253cd32ebf1b820c391b0103781ff943e66fc937e03cfc817759655f",
"30e56ee291fd106f752253654966e6789bf247d73ae731df5650361f26eb36cf",
),
(
"generator_log",
"235e1c7a126c5e8954a4008a57301f45ad60074ac71e0ccaa730391afb2fddf3",
"a9ea7e5940b0c2cf21dd8a07bd9bce20aca3edc007ffb70a19f50ed6f4e9c418",
),
(
"validator_log",
"8b25482e9c3a7c5070fe356aac24c999b7b60dc142a78f9134f0c58d99b56df4",
"3998335dc17c056512e51c8ad1e08ed147e54eb6fb78372b8203cda40c573bb3",
),
];

Expand Down
2 changes: 1 addition & 1 deletion devtools/ci/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ else
fi
cd $GODWOKEN_DIR
# checkout https://github.com/nervosnetwork/godwoken/pull/659/head
git fetch origin 0dd1500455ca81c23addadff05399925921aa352
git fetch origin ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8
git checkout FETCH_HEAD
git submodule update --init --recursive --depth=1

Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 26 additions & 49 deletions polyjuice-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use gw_types::{
offchain::RunResult,
packed::{
AllowedTypeHash, BatchSetMapping, BlockInfo, Fee, LogItem, RawL2Transaction, RollupConfig,
Script, Uint64,
Script, SetMapping, Uint64,
},
prelude::*,
U256,
};
use gw_types::{
offchain::RollupContext,
Expand Down Expand Up @@ -126,13 +127,13 @@ pub enum Log {
sudt_id: u32,
from_addr: RegistryAddress,
to_addr: RegistryAddress,
amount: u128,
amount: U256,
},
SudtPayFee {
sudt_id: u32,
from_addr: RegistryAddress,
block_producer_addr: RegistryAddress,
amount: u128,
amount: U256,
},
PolyjuiceSystem {
gas_used: u64,
Expand All @@ -147,13 +148,13 @@ pub enum Log {
},
}

fn parse_sudt_log_data(data: &[u8]) -> (RegistryAddress, RegistryAddress, u128) {
fn parse_sudt_log_data(data: &[u8]) -> (RegistryAddress, RegistryAddress, U256) {
let from_addr = RegistryAddress::from_slice(&data[0..28]).expect("parse from_addr");
let to_addr = RegistryAddress::from_slice(&data[28..56]).expect("parse to_addr");

let mut u128_bytes = [0u8; 16];
u128_bytes.copy_from_slice(&data[56..56 + 16]);
let amount = u128::from_le_bytes(u128_bytes);
let mut u256_bytes = [0u8; 32];
u256_bytes.copy_from_slice(&data[56..56 + 32]);
let amount = U256::from_little_endian(&mut u256_bytes);
(from_addr, to_addr, amount)
}

Expand All @@ -164,7 +165,7 @@ pub fn parse_log(item: &LogItem) -> Log {
match service_flag {
GW_LOG_SUDT_TRANSFER => {
let sudt_id: u32 = item.account_id().unpack();
if data.len() != (28 + 28 + 16) {
if data.len() != (28 + 28 + 32) {
panic!("Invalid data length: {}", data.len());
}
let (from_addr, to_addr, amount) = parse_sudt_log_data(data);
Expand All @@ -177,7 +178,7 @@ pub fn parse_log(item: &LogItem) -> Log {
}
GW_LOG_SUDT_PAY_FEE => {
let sudt_id: u32 = item.account_id().unpack();
if data.len() != (28 + 28 + 16) {
if data.len() != (28 + 28 + 32) {
panic!("Invalid data length: {}", data.len());
}
let (from_addr, block_producer_addr, amount) = parse_sudt_log_data(data);
Expand Down Expand Up @@ -685,7 +686,7 @@ pub(crate) fn create_block_producer(state: &mut DummyState) -> RegistryAddress {
pub(crate) fn create_eth_eoa_account(
state: &mut DummyState,
eth_address: &[u8; 20],
mint_ckb: u128,
mint_ckb: impl Into<U256>,
) -> (u32, [u8; 32]) {
let script = build_eth_l2_script(eth_address);
let script_hash = script.hash();
Expand Down Expand Up @@ -735,38 +736,6 @@ pub(crate) fn register_eoa_account(
.expect("map reg addr to script hash");
}

#[derive(Default)]
struct SetMappingArgsBuilder {
method: u32,
gw_script_hash: [u8; 32],
fee: u64,
}
impl SetMappingArgsBuilder {
/// Set the SetMappingArgs builder's method.
fn method(mut self, method: u32) -> Self {
self.method = method;
self
}
/// Set the SetMappingArgs builder's gw script hash.
fn gw_script_hash(mut self, gw_script_hash: [u8; 32]) -> Self {
self.gw_script_hash = gw_script_hash;
self
}
/// Set the set mapping args‘s fee.
fn set_fee(mut self, fee: u64) -> Self {
self.fee = fee;
self
}
fn build(self) -> Vec<u8> {
let mut output: Vec<u8> = vec![0u8; 4];
output[0..4].copy_from_slice(&self.method.to_le_bytes()[..]);
output.extend(self.gw_script_hash);
output.extend(ETH_REGISTRY_ACCOUNT_ID.to_le_bytes());
output.extend(&self.fee.to_le_bytes()[..]);
output
}
}

pub enum SetMappingArgs {
One(H256),
Batch(Vec<H256>),
Expand All @@ -783,16 +752,24 @@ pub(crate) fn eth_address_regiser(
set_mapping_args: SetMappingArgs,
) -> Result<RunResult, TransactionError> {
let args = match set_mapping_args {
SetMappingArgs::One(gw_script_hash) => SetMappingArgsBuilder::default()
.method(2u32)
.gw_script_hash(gw_script_hash.into())
.set_fee(1000)
.build()
.pack(),
SetMappingArgs::One(gw_script_hash) => {
let fee = Fee::new_builder()
.registry_id(ETH_REGISTRY_ACCOUNT_ID.pack())
.amount(U256::from(1000u64).pack())
.build();
let set_mapping = SetMapping::new_builder()
.fee(fee)
.gw_script_hash(gw_script_hash.pack())
.build();
let args = ETHAddrRegArgs::new_builder()
.set(ETHAddrRegArgsUnion::SetMapping(set_mapping))
.build();
args.as_bytes().pack()
}
SetMappingArgs::Batch(gw_script_hashes) => {
let fee = Fee::new_builder()
.registry_id(ETH_REGISTRY_ACCOUNT_ID.pack())
.amount(1000u64.pack())
.amount(U256::from(1000u64).pack())
.build();
let batch_set_mapping = BatchSetMapping::new_builder()
.fee(fee)
Expand Down
12 changes: 6 additions & 6 deletions polyjuice-tests/src/test_cases/call_selfdestruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use gw_common::{
use gw_generator::traits::StateExt;
use gw_store::chain_view::ChainView;
use gw_store::traits::chain_store::ChainStore;
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*};
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*, U256};

const SD_INIT_CODE: &str = include_str!("./evm-contracts/SelfDestruct.bin");
const CALL_SD_INIT_CODE: &str = include_str!("./evm-contracts/CallSelfDestruct.bin");
Expand All @@ -33,7 +33,7 @@ fn test_selfdestruct() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &beneficiary_address)
.unwrap(),
0
U256::zero()
);

// deploy SelfDestruct
Expand Down Expand Up @@ -71,13 +71,13 @@ fn test_selfdestruct() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &sd_address)
.unwrap(),
200
U256::from(200u64)
);
assert_eq!(
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &beneficiary_address)
.unwrap(),
0
U256::zero()
);

// deploy CallSelfDestruct
Expand Down Expand Up @@ -152,13 +152,13 @@ fn test_selfdestruct() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &sd_address)
.unwrap(),
0
U256::zero()
);
assert_eq!(
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &beneficiary_address)
.unwrap(),
200
U256::from(200u64)
);

block_number += 1;
Expand Down
6 changes: 3 additions & 3 deletions polyjuice-tests/src/test_cases/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use gw_common::{builtins::ETH_REGISTRY_ACCOUNT_ID, state::State};
use gw_generator::traits::StateExt;
use gw_store::chain_view::ChainView;
use gw_store::traits::chain_store::ChainStore;
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*};
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*, U256};

const SS_INIT_CODE: &str = include_str!("./evm-contracts/SimpleStorage.bin");
const CREATE2_IMPL_CODE: &str = include_str!("./evm-contracts/Create2Impl.bin");
Expand Down Expand Up @@ -59,7 +59,7 @@ fn test_create2() {
let create2_contract_balance = state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap();
assert_eq!(create2_contract_balance, 0);
assert_eq!(create2_contract_balance, U256::zero());

let input_value_u128: u128 = 0x9a;
// bytes32 salt
Expand Down Expand Up @@ -128,7 +128,7 @@ fn test_create2() {
let create2_account_balance = state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap();
assert_eq!(create2_account_balance, input_value_u128);
assert_eq!(create2_account_balance, U256::from(input_value_u128));

let run_result = simple_storage_get(
&store,
Expand Down
16 changes: 8 additions & 8 deletions polyjuice-tests/src/test_cases/eth_addr_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::helper::{
use gw_common::{registry_address::RegistryAddress, state::State};
use gw_generator::{error::TransactionError, traits::StateExt};
use gw_store::{chain_view::ChainView, traits::chain_store::ChainStore};
use gw_types::{packed::RawL2Transaction, prelude::*};
use gw_types::{packed::RawL2Transaction, prelude::*, U256};

const SS_INIT_CODE: &str = include_str!("./evm-contracts/SimpleStorage.bin");

Expand Down Expand Up @@ -79,7 +79,7 @@ fn test_update_eth_addr_reg_by_contract() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap(),
0u128
U256::zero()
);
state /* mint CKB to pay fee */
.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, 52000)
Expand All @@ -88,7 +88,7 @@ fn test_update_eth_addr_reg_by_contract() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap(),
52000u128
U256::from(52000u128)
);

// update_eth_address_registry by `ETH Address Registry` layer2 contract
Expand All @@ -110,7 +110,7 @@ fn test_update_eth_addr_reg_by_contract() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap(),
51000u128
U256::from(51000u128)
);

// try to register the same account again
Expand Down Expand Up @@ -203,7 +203,7 @@ fn test_batch_set_mapping_by_contract() {
// init accounts
let from_eth_address = [1u8; 20];
let (from_id, _from_script_hash) =
helper::create_eth_eoa_account(&mut state, &from_eth_address, 400000);
helper::create_eth_eoa_account(&mut state, &from_eth_address, 400000u64);

// create new EOAs which is not registered
let eth_eoa_addresses = vec![[0xeeu8; 20], [0xefu8; 20]];
Expand All @@ -219,16 +219,16 @@ fn test_batch_set_mapping_by_contract() {
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap(),
0u128
U256::zero()
);
state /* mint CKB to pay fee */
.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, 200000)
.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, 200000u64)
.unwrap();
assert_eq!(
state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap(),
200000u128
U256::from(200000u128)
);
}

Expand Down
14 changes: 10 additions & 4 deletions polyjuice-tests/src/test_cases/invalid_sudt_erc20_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use gw_common::{builtins::ETH_REGISTRY_ACCOUNT_ID, state::State};
use gw_generator::{error::TransactionError, traits::StateExt};
use gw_store::chain_view::ChainView;
use gw_store::traits::chain_store::ChainStore;
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*};
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*, U256};

const INVALID_SUDT_ERC20_PROXY_CODE: &str =
include_str!("./evm-contracts/InvalidSudtERC20Proxy.bin");
Expand Down Expand Up @@ -82,10 +82,16 @@ fn test_invalid_sudt_erc20_proxy() {

assert_eq!(
state.get_sudt_balance(new_sudt_id, &address1).unwrap(),
160000000000000000000000000000u128
U256::from(160000000000000000000000000000u128)
);
assert_eq!(
state.get_sudt_balance(new_sudt_id, &address2).unwrap(),
U256::zero()
);
assert_eq!(
state.get_sudt_balance(new_sudt_id, &address2).unwrap(),
U256::zero()
);
assert_eq!(state.get_sudt_balance(new_sudt_id, &address2).unwrap(), 0);
assert_eq!(state.get_sudt_balance(new_sudt_id, &address2).unwrap(), 0);
for (_idx, (from_id, args_str, success, return_data_str)) in [
// balanceOf(eoa1)
(
Expand Down
8 changes: 4 additions & 4 deletions polyjuice-tests/src/test_cases/parse_log_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use gw_common::{builtins::ETH_REGISTRY_ACCOUNT_ID, state::State};
use gw_generator::traits::StateExt;
use gw_store::chain_view::ChainView;
use gw_store::traits::chain_store::ChainStore;
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*};
use gw_types::{bytes::Bytes, packed::RawL2Transaction, prelude::*, U256};

const INIT_CODE: &str = include_str!("./evm-contracts/LogEvents.bin");

Expand All @@ -29,7 +29,7 @@ fn test_parse_log_event() {
let from_balance1 = state
.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)
.unwrap();
assert_eq!(from_balance1, 200000);
assert_eq!(from_balance1, U256::from(200000u64));

let mut block_number = 0;
let deploy_value = 0xfa;
Expand Down Expand Up @@ -75,7 +75,7 @@ fn test_parse_log_event() {
{
assert_eq!(&the_from_addr, &address);
assert_eq!(&the_to_addr, &contract_addr);
assert_eq!(amount, deploy_value);
assert_eq!(amount, U256::from(deploy_value));
} else {
panic!("unexpected polyjuice log");
}
Expand Down Expand Up @@ -142,7 +142,7 @@ fn test_parse_log_event() {
assert_eq!(&the_from_addr, &address);
// The block producer id is `0`
assert_eq!(&the_to_addr, &block_producer);
assert_eq!(amount, 1814);
assert_eq!(amount, U256::from(1814u64));
} else {
panic!("unexpected polyjuice log");
}
Expand Down
Loading

0 comments on commit 99997e6

Please sign in to comment.