difftreelog
feat fake event injection
in: master
8 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6038,6 +6038,7 @@
name = "pallet-evm-migration"
version = "0.1.1"
dependencies = [
+ "ethereum",
"fp-evm",
"frame-benchmarking",
"frame-support",
pallets/evm-migration/Cargo.tomldiffbeforeafterboth--- a/pallets/evm-migration/Cargo.toml
+++ b/pallets/evm-migration/Cargo.toml
@@ -8,6 +8,7 @@
scale-info = { version = "2.0.1", default-features = false, features = [
"derive",
] }
+ethereum = { version = "0.12.0", default-features = false }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallets/evm-migration/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/evm-migration/src/benchmarking.rs
+++ b/pallets/evm-migration/src/benchmarking.rs
@@ -20,9 +20,11 @@
use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;
use sp_core::{H160, H256};
-use sp_std::vec::Vec;
+use sp_std::{vec::Vec, vec};
benchmarks! {
+ where_clause { where <T as Config>::RuntimeEvent: codec::Encode }
+
begin {
}: _(RawOrigin::Root, H160::default())
@@ -45,4 +47,19 @@
let data: Vec<u8> = (0..b as u8).collect();
<Pallet<T>>::begin(RawOrigin::Root.into(), address)?;
}: _(RawOrigin::Root, address, data)
+
+ insert_eth_logs {
+ let b in 0..200;
+ let logs = (0..b).map(|_| ethereum::Log {
+ address: H160([b as u8; 20]),
+ data: vec![b as u8; 128],
+ topics: vec![H256([b as u8; 32]); 6],
+ }).collect::<Vec<_>>();
+ }: _(RawOrigin::Root, logs)
+
+ insert_events {
+ let b in 0..200;
+ use codec::Encode;
+ let logs = (0..b).map(|_| <T as Config>::RuntimeEvent::from(crate::Event::<T>::TestEvent).encode()).collect::<Vec<_>>();
+ }: _(RawOrigin::Root, logs)
}
pallets/evm-migration/src/lib.rsdiffbeforeafterboth--- a/pallets/evm-migration/src/lib.rs
+++ b/pallets/evm-migration/src/lib.rs
@@ -25,8 +25,11 @@
#[frame_support::pallet]
pub mod pallet {
- use frame_support::pallet_prelude::*;
- use frame_system::pallet_prelude::*;
+ use frame_support::{
+ pallet_prelude::{*, DispatchResult},
+ traits::IsType,
+ };
+ use frame_system::pallet_prelude::{*, OriginFor};
use sp_core::{H160, H256};
use sp_std::vec::Vec;
use super::weights::WeightInfo;
@@ -36,6 +39,8 @@
pub trait Config: frame_system::Config + pallet_evm::Config {
/// Weights
type WeightInfo: WeightInfo;
+ /// The overarching event type.
+ type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
type SelfWeightOf<T> = <T as Config>::WeightInfo;
@@ -44,12 +49,20 @@
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
+ #[pallet::event]
+ pub enum Event<T: Config> {
+ /// This event is used in benchmarking and can be used for tests
+ TestEvent,
+ }
+
#[pallet::error]
pub enum Error<T> {
/// Can only migrate to empty address.
AccountNotEmpty,
/// Migration of this account is not yet started, or already finished.
AccountIsNotMigrating,
+ /// Failed to decode event bytes
+ BadEvent,
}
#[pallet::storage]
@@ -107,6 +120,30 @@
<MigrationPending<T>>::remove(address);
Ok(())
}
+
+ /// Create ethereum events attached to the fake transaction
+ #[pallet::weight(<SelfWeightOf<T>>::insert_eth_logs(logs.len() as u32))]
+ pub fn insert_eth_logs(origin: OriginFor<T>, logs: Vec<ethereum::Log>) -> DispatchResult {
+ ensure_root(origin)?;
+ for log in logs {
+ <pallet_evm::Pallet<T>>::deposit_log(log);
+ }
+ // Transactions is created by FakeTransactionFinalizer
+ Ok(())
+ }
+
+ /// Create substrate events
+ #[pallet::weight(<SelfWeightOf<T>>::insert_events(events.len() as u32))]
+ pub fn insert_events(origin: OriginFor<T>, events: Vec<Vec<u8>>) -> DispatchResult {
+ ensure_root(origin)?;
+ for event in events {
+ <frame_system::Pallet<T>>::deposit_event(
+ <T as frame_system::Config>::RuntimeEvent::decode(&mut event.as_slice())
+ .map_err(|_| <Error<T>>::BadEvent)?,
+ );
+ }
+ Ok(())
+ }
}
/// Implements [`pallet_evm::OnMethodCall`], which reserves accounts with pending migration
pallets/evm-migration/src/weights.rsdiffbeforeafterboth--- a/pallets/evm-migration/src/weights.rs
+++ b/pallets/evm-migration/src/weights.rs
@@ -3,7 +3,7 @@
//! Autogenerated weights for pallet_evm_migration
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-08-15, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-11-23, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
// Executed Command:
@@ -37,6 +37,8 @@
fn begin() -> Weight;
fn set_data(b: u32, ) -> Weight;
fn finish(b: u32, ) -> Weight;
+ fn insert_eth_logs(b: u32, ) -> Weight;
+ fn insert_events(b: u32, ) -> Weight;
}
/// Weights for pallet_evm_migration using the Substrate node and recommended hardware.
@@ -46,26 +48,38 @@
// Storage: System Account (r:1 w:0)
// Storage: EVM AccountCodes (r:1 w:0)
fn begin() -> Weight {
- Weight::from_ref_time(8_035_000)
+ Weight::from_ref_time(16_080_000 as u64)
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
// Storage: EvmMigration MigrationPending (r:1 w:0)
// Storage: EVM AccountStorages (r:0 w:1)
fn set_data(b: u32, ) -> Weight {
- Weight::from_ref_time(3_076_000)
- // Standard Error: 0
- .saturating_add(Weight::from_ref_time(828_000).saturating_mul(b as u64))
+ Weight::from_ref_time(7_945_000 as u64)
+ // Standard Error: 1_272
+ .saturating_add(Weight::from_ref_time(1_056_832 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(b as u64)))
}
// Storage: EvmMigration MigrationPending (r:1 w:1)
// Storage: EVM AccountCodes (r:0 w:1)
- fn finish(_b: u32, ) -> Weight {
- Weight::from_ref_time(6_591_000)
+ fn finish(b: u32, ) -> Weight {
+ Weight::from_ref_time(8_336_000 as u64)
+ // Standard Error: 89
+ .saturating_add(Weight::from_ref_time(6_411 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(2 as u64))
}
+ fn insert_eth_logs(b: u32, ) -> Weight {
+ Weight::from_ref_time(3_447_000 as u64)
+ // Standard Error: 843
+ .saturating_add(Weight::from_ref_time(901_039 as u64).saturating_mul(b as u64))
+ }
+ fn insert_events(b: u32, ) -> Weight {
+ Weight::from_ref_time(3_457_000 as u64)
+ // Standard Error: 1_460
+ .saturating_add(Weight::from_ref_time(1_491_611 as u64).saturating_mul(b as u64))
+ }
}
// For backwards compatibility and tests
@@ -74,24 +88,36 @@
// Storage: System Account (r:1 w:0)
// Storage: EVM AccountCodes (r:1 w:0)
fn begin() -> Weight {
- Weight::from_ref_time(8_035_000)
+ Weight::from_ref_time(16_080_000 as u64)
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
// Storage: EvmMigration MigrationPending (r:1 w:0)
// Storage: EVM AccountStorages (r:0 w:1)
fn set_data(b: u32, ) -> Weight {
- Weight::from_ref_time(3_076_000)
- // Standard Error: 0
- .saturating_add(Weight::from_ref_time(828_000).saturating_mul(b as u64))
+ Weight::from_ref_time(7_945_000 as u64)
+ // Standard Error: 1_272
+ .saturating_add(Weight::from_ref_time(1_056_832 as u64).saturating_mul(b as u64))
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(b as u64)))
}
// Storage: EvmMigration MigrationPending (r:1 w:1)
// Storage: EVM AccountCodes (r:0 w:1)
- fn finish(_b: u32, ) -> Weight {
- Weight::from_ref_time(6_591_000)
+ fn finish(b: u32, ) -> Weight {
+ Weight::from_ref_time(8_336_000 as u64)
+ // Standard Error: 89
+ .saturating_add(Weight::from_ref_time(6_411 as u64).saturating_mul(b as u64))
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(2 as u64))
}
+ fn insert_eth_logs(b: u32, ) -> Weight {
+ Weight::from_ref_time(3_447_000 as u64)
+ // Standard Error: 843
+ .saturating_add(Weight::from_ref_time(901_039 as u64).saturating_mul(b as u64))
+ }
+ fn insert_events(b: u32, ) -> Weight {
+ Weight::from_ref_time(3_457_000 as u64)
+ // Standard Error: 1_460
+ .saturating_add(Weight::from_ref_time(1_491_611 as u64).saturating_mul(b as u64))
+ }
}
runtime/common/config/ethereum.rsdiffbeforeafterboth--- a/runtime/common/config/ethereum.rs
+++ b/runtime/common/config/ethereum.rs
@@ -91,6 +91,7 @@
}
impl pallet_evm_migration::Config for Runtime {
+ type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_evm_migration::weights::SubstrateWeight<Self>;
}
runtime/common/construct_runtime/mod.rsdiffbeforeafterboth--- a/runtime/common/construct_runtime/mod.rs
+++ b/runtime/common/construct_runtime/mod.rs
@@ -92,7 +92,7 @@
EvmCoderSubstrate: pallet_evm_coder_substrate::{Pallet, Storage} = 150,
EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage, Event<T>} = 151,
EvmTransactionPayment: pallet_evm_transaction_payment::{Pallet} = 152,
- EvmMigration: pallet_evm_migration::{Pallet, Call, Storage} = 153,
+ EvmMigration: pallet_evm_migration::{Pallet, Call, Storage, Event<T>} = 153,
Maintenance: pallet_maintenance::{Pallet, Call, Storage, Event<T>} = 154,
tests/src/eth/migration.seqtest.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('EVM Migrations', () => {21 let superuser: IKeyringPair;2223 before(async function() {24 await usingEthPlaygrounds(async (_helper, privateKey) => {25 superuser = await privateKey('//Alice');26 });27 });28 29 // todo:playgrounds requires sudo, look into later30 itEth('Deploy contract saved state', async ({helper}) => {31 /*32 contract StatefulContract {33 uint counter;34 mapping (uint => uint) kv;3536 function inc() public {37 counter = counter + 1;38 }39 function counterValue() public view returns (uint) {40 return counter;41 }4243 function set(uint key, uint value) public {44 kv[key] = value;45 }4647 function get(uint key) public view returns (uint) {48 return kv[key];49 }50 }51 */52 const ADDRESS = '0x4956bf52ef9ed8789f21bc600e915e0d961079f6';53 const CODE = '0x608060405234801561001057600080fd5b506004361061004c5760003560e01c80631ab06ee514610051578063371303c01461006d5780637bfdec3b146100775780639507d39a14610095575b600080fd5b61006b60048036038101906100669190610160565b6100c5565b005b6100756100e1565b005b61007f6100f8565b60405161008c91906101af565b60405180910390f35b6100af60048036038101906100aa9190610133565b610101565b6040516100bc91906101af565b60405180910390f35b8060016000848152602001908152602001600020819055505050565b60016000546100f091906101ca565b600081905550565b60008054905090565b600060016000838152602001908152602001600020549050919050565b60008135905061012d8161025e565b92915050565b60006020828403121561014957610148610259565b5b60006101578482850161011e565b91505092915050565b6000806040838503121561017757610176610259565b5b60006101858582860161011e565b92505060206101968582860161011e565b9150509250929050565b6101a981610220565b82525050565b60006020820190506101c460008301846101a0565b92915050565b60006101d582610220565b91506101e083610220565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156102155761021461022a565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b61026781610220565b811461027257600080fd5b5056fea26469706673582212206a02d2fb5c244105ab884961479c1aee3b4c1011e4b5530ab483eb22344a865664736f6c63430008060033';54 const DATA = [55 // counter = 1056 ['0x0000000000000000000000000000000000000000000000000000000000000000', '0x000000000000000000000000000000000000000000000000000000000000000a'],57 // kv = {1: 1, 2: 2, 3: 3, 4: 4},58 ['0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f', '0x0000000000000000000000000000000000000000000000000000000000000001'],59 ['0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f', '0x0000000000000000000000000000000000000000000000000000000000000002'],60 ['0x7dfe757ecd65cbd7922a9c0161e935dd7fdbcc0e999689c7d31633896b1fc60b', '0x0000000000000000000000000000000000000000000000000000000000000003'],61 ['0xedc95719e9a3b28dd8e80877cb5880a9be7de1a13fc8b05e7999683b6b567643', '0x0000000000000000000000000000000000000000000000000000000000000004'],62 ];6364 const caller = await helper.eth.createAccountWithBalance(superuser);6566 const txBegin = helper.constructApiCall('api.tx.evmMigration.begin', [ADDRESS]);67 const txSetData = helper.constructApiCall('api.tx.evmMigration.setData', [ADDRESS, DATA]);68 const txFinish = helper.constructApiCall('api.tx.evmMigration.finish', [ADDRESS, CODE]);69 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txBegin])).to.be.fulfilled;70 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txSetData])).to.be.fulfilled;71 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txFinish])).to.be.fulfilled;7273 const web3 = helper.getWeb3();74 const contract = new web3.eth.Contract([75 {76 inputs: [],77 name: 'counterValue',78 outputs: [{79 internalType: 'uint256',80 name: '',81 type: 'uint256',82 }],83 stateMutability: 'view',84 type: 'function',85 },86 {87 inputs: [{88 internalType: 'uint256',89 name: 'key',90 type: 'uint256',91 }],92 name: 'get',93 outputs: [{94 internalType: 'uint256',95 name: '',96 type: 'uint256',97 }],98 stateMutability: 'view',99 type: 'function',100 },101 ], ADDRESS, {from: caller, gas: helper.eth.DEFAULT_GAS});102103 expect(await contract.methods.counterValue().call()).to.be.equal('10');104 for (let i = 1; i <= 4; i++) {105 expect(await contract.methods.get(i).call()).to.be.equal(i.toString());106 }107 });108});