Skip to content

Commit

Permalink
add origin to events (#1461)
Browse files Browse the repository at this point in the history
* add origin to events

* Update runtime/integration-tests/src/evm_tests.rs

Co-authored-by: zjb0807 <zjb0807@qq.com>

* fix tests

* fix test

* fix test

Co-authored-by: zjb0807 <zjb0807@qq.com>
  • Loading branch information
xlc and zjb0807 committed Oct 8, 2021
1 parent 732e3cb commit c1d8450
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 52 deletions.
6 changes: 2 additions & 4 deletions modules/currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub fn deploy_contracts() {
));

System::assert_last_event(Event::EVM(module_evm::Event::Created(
alice_evm_addr(),
erc20_address(),
vec![module_evm::Log {
address: H160::from_str("0x0000000000000000000000000000000002000000").unwrap(),
Expand All @@ -261,10 +262,7 @@ pub fn deploy_contracts() {
H256::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap(),
],
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 16,
]
.to_vec(),
data: H256::from_low_u64_be(10000).as_bytes().to_vec(),
}],
)));

Expand Down
4 changes: 4 additions & 0 deletions modules/evm-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ std = [
"orml-traits/std",
"module-support/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
2 changes: 2 additions & 0 deletions modules/evm-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ impl<T: Config> Pallet<T> {
secp256k1::PublicKey::from_secret_key(secret)
}

#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
// Returns an Etherum address derived from an Ethereum secret key.
// Only for tests
pub fn eth_address(secret: &secp256k1::SecretKey) -> EvmAddress {
EvmAddress::from_slice(&keccak_256(&Self::eth_public(secret).serialize()[1..65])[12..])
}
Expand Down
6 changes: 2 additions & 4 deletions modules/evm-bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub fn deploy_contracts() {
assert_ok!(EVM::create(Origin::signed(alice()), code, 0, 2_100_000, 10000));

System::assert_last_event(Event::EVM(module_evm::Event::Created(
alice_evm_addr(),
erc20_address(),
vec![module_evm::Log {
address: erc20_address(),
Expand All @@ -197,10 +198,7 @@ pub fn deploy_contracts() {
H256::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap(),
],
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 16,
]
.to_vec(),
data: H256::from_low_u64_be(10000).as_bytes().to_vec(),
}],
)));

Expand Down
6 changes: 2 additions & 4 deletions modules/evm-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub fn deploy_contracts() {
));

System::assert_last_event(Event::EVM(module_evm::Event::Created(
alice_evm_addr(),
erc20_address(),
vec![module_evm::Log {
address: H160::from_str("0x0000000000000000000000000000000002000000").unwrap(),
Expand All @@ -226,10 +227,7 @@ pub fn deploy_contracts() {
H256::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap(),
],
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 16,
]
.to_vec(),
data: H256::from_low_u64_be(10000).as_bytes().to_vec(),
}],
)));

Expand Down
16 changes: 8 additions & 8 deletions modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,16 @@ pub mod module {
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
#[pallet::metadata(T::AccountId = "AccountId")]
pub enum Event<T: Config> {
/// A contract has been created at given \[address, logs\].
Created(EvmAddress, Vec<Log>),
/// A contract has been created at given \[from, address, logs\].
Created(EvmAddress, EvmAddress, Vec<Log>),
/// A contract was attempted to be created, but the execution failed.
/// \[contract, exit_reason, logs\]
CreatedFailed(EvmAddress, ExitReason, Vec<Log>),
/// A contract has been executed successfully with states applied. \[contract, logs]\
Executed(EvmAddress, Vec<Log>),
/// \[from, contract, exit_reason, logs\]
CreatedFailed(EvmAddress, EvmAddress, ExitReason, Vec<Log>),
/// A contract has been executed successfully with states applied. \[from, contract, logs]\
Executed(EvmAddress, EvmAddress, Vec<Log>),
/// A contract has been executed with errors. States are reverted with
/// only gas fees applied. \[contract, exit_reason, output, logs\]
ExecutedFailed(EvmAddress, ExitReason, Vec<u8>, Vec<Log>),
/// only gas fees applied. \[from, contract, exit_reason, output, logs\]
ExecutedFailed(EvmAddress, EvmAddress, ExitReason, Vec<u8>, Vec<Log>),
/// Transferred maintainer. \[contract, address\]
TransferredMaintainer(EvmAddress, EvmAddress),
/// Enabled contract development. \[who\]
Expand Down
12 changes: 8 additions & 4 deletions modules/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ impl<T: Config> RunnerT<T> for Runner<T> {
})?;

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Executed(target, info.logs.clone()));
Pallet::<T>::deposit_event(Event::<T>::Executed(source, target, info.logs.clone()));
} else {
Pallet::<T>::deposit_event(Event::<T>::ExecutedFailed(
source,
target,
info.exit_reason.clone(),
info.value.clone(),
Expand Down Expand Up @@ -241,9 +242,10 @@ impl<T: Config> RunnerT<T> for Runner<T> {
})?;

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created(info.value, info.logs.clone()));
Pallet::<T>::deposit_event(Event::<T>::Created(source, info.value, info.logs.clone()));
} else {
Pallet::<T>::deposit_event(Event::<T>::CreatedFailed(
source,
info.value,
info.exit_reason.clone(),
info.logs.clone(),
Expand Down Expand Up @@ -276,9 +278,10 @@ impl<T: Config> RunnerT<T> for Runner<T> {
})?;

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created(info.value, info.logs.clone()));
Pallet::<T>::deposit_event(Event::<T>::Created(source, info.value, info.logs.clone()));
} else {
Pallet::<T>::deposit_event(Event::<T>::CreatedFailed(
source,
info.value,
info.exit_reason.clone(),
info.logs.clone(),
Expand Down Expand Up @@ -306,9 +309,10 @@ impl<T: Config> RunnerT<T> for Runner<T> {
})?;

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created(info.value, info.logs.clone()));
Pallet::<T>::deposit_event(Event::<T>::Created(source, info.value, info.logs.clone()));
} else {
Pallet::<T>::deposit_event(Event::<T>::CreatedFailed(
source,
info.value,
info.exit_reason.clone(),
info.logs.clone(),
Expand Down
7 changes: 6 additions & 1 deletion modules/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ fn create_network_contract_works() {
2.into()
);
System::assert_last_event(Event::EVM(crate::Event::Created(
NetworkContractSource::get(),
H160::from_low_u64_be(MIRRORED_NFT_ADDRESS_START),
vec![],
)));
Expand Down Expand Up @@ -709,7 +710,11 @@ fn create_predeploy_contract_works() {

assert_eq!(Pallet::<Runtime>::is_account_empty(&addr), false);

System::assert_last_event(Event::EVM(crate::Event::Created(addr, vec![])));
System::assert_last_event(Event::EVM(crate::Event::Created(
NetworkContractSource::get(),
addr,
vec![],
)));

assert_noop!(
EVM::create_predeploy_contract(
Expand Down
24 changes: 10 additions & 14 deletions runtime/integration-tests/src/evm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn deploy_erc20_contracts() {
));

System::assert_last_event(Event::EVM(module_evm::Event::Created(
Default::default(),
erc20_address_0(),
vec![module_evm::Log {
address: erc20_address_0(),
Expand All @@ -75,10 +76,7 @@ pub fn deploy_erc20_contracts() {
H256::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap(),
],
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 16,
]
.to_vec(),
data: H256::from_low_u64_be(10000).as_bytes().to_vec(),
}],
)));

Expand All @@ -87,6 +85,7 @@ pub fn deploy_erc20_contracts() {
assert_ok!(EVM::create_network_contract(Origin::root(), code, 0, 2100_000, 100000));

System::assert_last_event(Event::EVM(module_evm::Event::Created(
Default::default(),
erc20_address_1(),
vec![module_evm::Log {
address: erc20_address_1(),
Expand All @@ -95,10 +94,7 @@ pub fn deploy_erc20_contracts() {
H256::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap(),
],
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 16,
]
.to_vec(),
data: H256::from_low_u64_be(10000).as_bytes().to_vec(),
}],
)));

Expand All @@ -122,8 +118,7 @@ fn deploy_contract(account: AccountId) -> Result<H160, DispatchError> {

EVM::create(Origin::signed(account), contract, 0, 1000000000, 100000).map_or_else(|e| Err(e.error), |_| Ok(()))?;

if let Event::EVM(module_evm::Event::<Runtime>::Created(address, _)) = System::events().iter().last().unwrap().event
{
if let Event::EVM(module_evm::Event::<Runtime>::Created(_, address, _)) = System::events().last().unwrap().event {
Ok(address)
} else {
Err("deploy_contract failed".into())
Expand Down Expand Up @@ -314,11 +309,11 @@ fn test_evm_module() {
assert_eq!(Balances::free_balance(alice()), 1_000 * dollar(NATIVE_CURRENCY));
assert_eq!(Balances::free_balance(bob()), 1_000 * dollar(NATIVE_CURRENCY));

let _alice_address = EvmAccounts::eth_address(&alice_key());
let alice_address = EvmAccounts::eth_address(&alice_key());
let bob_address = EvmAccounts::eth_address(&bob_key());

let contract = deploy_contract(alice()).unwrap();
System::assert_last_event(Event::EVM(module_evm::Event::Created(contract, vec![])));
System::assert_last_event(Event::EVM(module_evm::Event::Created(alice_address, contract, vec![])));

assert_ok!(EVM::transfer_maintainer(Origin::signed(alice()), contract, bob_address));
System::assert_last_event(Event::EVM(module_evm::Event::TransferredMaintainer(
Expand Down Expand Up @@ -504,7 +499,7 @@ fn should_not_kill_contract_on_transfer_all() {

assert_ok!(EVM::create(Origin::signed(alice()), code, 2 * dollar(NATIVE_CURRENCY), 1000000000, 100000));

let contract = if let Event::EVM(module_evm::Event::Created(address, _)) = System::events().iter().last().unwrap().event {
let contract = if let Event::EVM(module_evm::Event::Created(_, address, _)) = System::events().last().unwrap().event {
address
} else {
panic!("deploy contract failed");
Expand Down Expand Up @@ -564,7 +559,8 @@ fn should_not_kill_contract_on_transfer_all_tokens() {
// }
let code = hex_literal::hex!("608060405260848060116000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806341c0e1b514602d575b600080fd5b60336035565b005b600073ffffffffffffffffffffffffffffffffffffffff16fffea265627a7a72315820ed64a7551098c4afc823bee1663309079d9cb8798a6bdd71be2cd3ccee52d98e64736f6c63430005110032").to_vec();
assert_ok!(EVM::create(Origin::signed(alice()), code, 0, 1000000000, 100000));
let contract = if let Event::EVM(module_evm::Event::Created(address, _)) = System::events().iter().last().unwrap().event {

let contract = if let Event::EVM(module_evm::Event::Created(_, address, _)) = System::events().last().unwrap().event {
address
} else {
panic!("deploy contract failed");
Expand Down
1 change: 1 addition & 0 deletions runtime/karura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ runtime-benchmarks = [

"module-nft/runtime-benchmarks",
"module-homa-lite/runtime-benchmarks",
"module-evm-accounts/runtime-benchmarks",

"sp-api/disable-logging",
]
Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ runtime-benchmarks = [

"module-nft/runtime-benchmarks",
"module-homa-lite/runtime-benchmarks",
"module-evm-accounts/runtime-benchmarks",

"sp-api/disable-logging",
]
Expand Down
9 changes: 7 additions & 2 deletions runtime/mandala/src/benchmarking/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{dollar, AccountId, CurrencyId, Event, EvmAccounts, GetNativeCurrency
use super::utils::set_balance;
use frame_support::dispatch::DispatchError;
use frame_system::RawOrigin;
use module_support::AddressMapping;
use orml_benchmarking::{runtime_benchmarks, whitelist_account};
use sp_core::H160;
use sp_io::hashing::keccak_256;
Expand Down Expand Up @@ -56,10 +57,14 @@ fn deploy_contract(caller: AccountId) -> Result<H160, DispatchError> {
let contract = hex_literal::hex!("608060405234801561001057600080fd5b5061016f806100206000396000f3fe608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063412a5a6d14610046575b600080fd5b61004e610050565b005b600061005a6100e2565b604051809103906000f080158015610076573d6000803e3d6000fd5b50905060008190806001815401808255809150509060018203906000526020600020016000909192909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050565b6040516052806100f28339019056fe6080604052348015600f57600080fd5b50603580601d6000396000f3fe6080604052600080fdfea165627a7a7230582092dc1966a8880ddf11e067f9dd56a632c11a78a4afd4a9f05924d427367958cc0029a165627a7a723058202b2cc7384e11c452cdbf39b68dada2d5e10a632cc0174a354b8b8c83237e28a40029").to_vec();

System::set_block_number(1);
EVM::create(Origin::signed(caller), contract, 0, 1000000000, 1000000000)
EVM::create(Origin::signed(caller.clone()), contract, 0, 1000000000, 1000000000)
.map_or_else(|e| Err(e.error), |_| Ok(()))?;

System::assert_last_event(Event::EVM(module_evm::Event::Created(contract_addr(), vec![])));
System::assert_last_event(Event::EVM(module_evm::Event::Created(
module_evm_accounts::EvmAddressMapping::<Runtime>::get_evm_address(&caller).unwrap(),
contract_addr(),
vec![],
)));
Ok(contract_addr())
}

Expand Down
2 changes: 1 addition & 1 deletion ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@acala-network/api": "^2.2.2-1",
"@acala-network/bodhi": "1.0.2-14",
"@acala-network/bodhi": "^1.0.4-0",
"@openzeppelin/contracts": "4.3.2",
"@polkadot/api": "^5.2.1",
"@types/chai": "^4.2.11",
Expand Down
7 changes: 1 addition & 6 deletions ts-tests/tests/test-transaction-cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ describeWithAcala("Acala RPC (Transaction cost)", (context) => {
const contract = await deployContract(alice as any, Erc20DemoContract, [1000000000]);
const to = await ethers.Wallet.createRandom().getAddress();

try {
await contract.transfer(to, 1000, { gasLimit: 0 });
}
catch(err) {
expect(err).to.equal('{"error":{"outofgas":null}} ');
}
await expect(contract.transfer(to, 1000, { gasLimit: 0 })).to.be.rejectedWith('{"error":{"outofgas":null}} ');
});
});
8 changes: 4 additions & 4 deletions ts-tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"@polkadot/api" "^5.9.1"
"@polkadot/rpc-core" "^5.9.1"

"@acala-network/bodhi@1.0.2-14":
version "1.0.2-14"
resolved "https://registry.yarnpkg.com/@acala-network/bodhi/-/bodhi-1.0.2-14.tgz#52fd7774887b886b10b23b63e405e003705ce3ae"
integrity sha512-lW4sK0ahyf+bcjmDkgK2uwZ1kgNNRq/xhuwtBS+TOeC5Tt+48L0/JEORFXjpDHg5Rcz+2nL75Q/3ZkmEGRMG9A==
"@acala-network/bodhi@^1.0.4-0":
version "1.0.4-0"
resolved "https://registry.yarnpkg.com/@acala-network/bodhi/-/bodhi-1.0.4-0.tgz#7b1635343f002c98f96879c2cf9a06126816caae"
integrity sha512-x12AlZC5x2epEvr6SetTybnkKbW4U/KCn4R0QFBBC06bNAIEwTfWHSO0omVXbil3Rs6EXhb1oDxy53IGWcloTA==
dependencies:
"@open-web3/scanner" "^0.9.4-19"
blakejs "^1.1.0"
Expand Down

0 comments on commit c1d8450

Please sign in to comment.