From 326c9b419cb990e6d92f0bdb1d0f6a02c20e788e Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 12 Aug 2021 10:07:00 +0000 Subject: [PATCH] feat: evm migration pallet --- --- /dev/null +++ b/pallets/evm-migration/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "pallet-evm-migration" +version = "0.1.0" +edition = "2018" + +[dependencies] +frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +frame-benchmarking = { default-features = false, version = '3.0.0', optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +sp-io = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +sp-core = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' } +pallet-evm = { default-features = false, version = "5.0.0-dev", git = "https://github.com/uniquenetwork/frontier.git", branch = "injected-transactions-parachain" } +fp-evm = { default-features = false, version = "2.0.0", git = "https://github.com/uniquenetwork/frontier.git", branch = "injected-transactions-parachain" } + +[dependencies.codec] +default-features = false +features = ['derive'] +package = 'parity-scale-codec' +version = '2.0.0' + +[features] +default = ["std", "runtime-benchmarks"] +std = [ + "frame-support/std", + "frame-system/std", + "frame-benchmarking/std", + "sp-runtime/std", + "sp-std/std", + "sp-io/std", + "sp-core/std", + "pallet-evm/std", + "fp-evm/std", +] +runtime-benchmarks = ["frame-benchmarking"] --- /dev/null +++ b/pallets/evm-migration/src/benchmarking.rs @@ -0,0 +1,30 @@ +use super::{Call, Config, Pallet}; +use frame_benchmarking::benchmarks; +use frame_system::RawOrigin; +use sp_core::{H160, H256}; +use sp_std::vec::Vec; + +benchmarks! { + begin { + }: _(RawOrigin::Root, H160::default()) + + set_data { + let b in 0..80; + let address = H160::from_low_u64_be(b as u64); + let mut data = Vec::new(); + for i in 0..b { + data.push(( + H256::from_low_u64_be(i as u64), + H256::from_low_u64_be(i as u64), + )); + } + >::begin(RawOrigin::Root.into(), address)?; + }: _(RawOrigin::Root, address, data) + + finish { + let b in 0..80; + let address = H160::from_low_u64_be(b as u64); + let data: Vec = (0..b as u8).collect(); + >::begin(RawOrigin::Root.into(), address)?; + }: _(RawOrigin::Root, address, data) +} --- /dev/null +++ b/pallets/evm-migration/src/lib.rs @@ -0,0 +1,111 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +pub use pallet::*; +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; +pub mod weights; + +#[frame_support::pallet] +pub mod pallet { + use frame_support::{pallet_prelude::*, transactional}; + use frame_system::pallet_prelude::*; + use sp_core::{H160, H256}; + use sp_std::vec::Vec; + use super::weights::WeightInfo; + + #[pallet::config] + pub trait Config: frame_system::Config + pallet_evm::Config { + type WeightInfo: WeightInfo; + } + + type SelfWeightOf = ::WeightInfo; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::error] + pub enum Error { + AccountNotEmpty, + AccountIsNotMigrating, + } + + #[pallet::storage] + pub(super) type MigrationPending = + StorageMap; + + #[pallet::call] + impl Pallet { + #[pallet::weight(>::begin())] + pub fn begin(origin: OriginFor, address: H160) -> DispatchResult { + ensure_root(origin)?; + ensure!( + >::is_account_empty(&address) + && !>::get(&address), + >::AccountNotEmpty, + ); + + >::insert(address, true); + Ok(()) + } + + #[pallet::weight(>::set_data(data.len() as u32))] + pub fn set_data( + origin: OriginFor, + address: H160, + data: Vec<(H256, H256)>, + ) -> DispatchResult { + use frame_support::StorageDoubleMap; + ensure_root(origin)?; + ensure!( + >::get(&address), + >::AccountIsNotMigrating, + ); + + for (k, v) in data { + pallet_evm::AccountStorages::insert(&address, k, v); + } + Ok(()) + } + + #[pallet::weight(>::finish(code.len() as u32))] + #[transactional] + pub fn finish(origin: OriginFor, address: H160, code: Vec) -> DispatchResult { + use frame_support::StorageMap; + ensure_root(origin)?; + ensure!( + >::get(&address), + >::AccountIsNotMigrating, + ); + + pallet_evm::AccountCodes::insert(&address, code); + >::remove(address); + Ok(()) + } + } + + pub struct OnMethodCall(PhantomData); + impl pallet_evm::OnMethodCall for OnMethodCall { + fn is_reserved(contract: &H160) -> bool { + >::get(&contract) + } + + fn is_used(_contract: &H160) -> bool { + false + } + + fn call( + _source: &H160, + _arget: &H160, + _gas_left: u64, + _input: &[u8], + _value: sp_core::U256, + ) -> Option { + None + } + + fn get_code(_contract: &H160) -> Option> { + None + } + } +} --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -23,6 +23,7 @@ 'frame-support/runtime-benchmarks', 'frame-system-benchmarking', 'frame-system/runtime-benchmarks', + 'pallet-evm-migration/runtime-benchmarks', 'pallet-balances/runtime-benchmarks', 'pallet-timestamp/runtime-benchmarks', 'pallet-nft/runtime-benchmarks', @@ -56,6 +57,7 @@ 'pallet-treasury/std', 'pallet-vesting/std', 'pallet-evm/std', + 'pallet-evm-migration/std', 'pallet-evm-contract-helpers/std', 'pallet-evm-transaction-payment/std', 'pallet-evm-coder-substrate/std', @@ -392,6 +394,7 @@ # pallet-contract-helpers = { path = '../pallets/contract-helpers', default-features = false, version = '0.1.0' } pallet-nft-transaction-payment = { path = '../pallets/nft-transaction-payment', default-features = false, version = '3.0.0' } pallet-nft-charge-transaction = { path = '../pallets/nft-charge-transaction', default-features = false, version = '3.0.0' } +pallet-evm-migration = { path = '../pallets/evm-migration', default-features = false } pallet-evm-contract-helpers = { path = '../pallets/evm-contract-helpers', default-features = false } pallet-evm-transaction-payment = { path = '../pallets/evm-transaction-payment', default-features = false } pallet-evm-coder-substrate = { default-features = false, path = "../pallets/evm-coder-substrate" } --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -252,6 +252,7 @@ type Currency = Balances; type Event = Event; type OnMethodCall = ( + pallet_evm_migration::OnMethodCall, pallet_nft::NftErcSupport, pallet_evm_contract_helpers::HelpersOnMethodCall, ); @@ -263,6 +264,10 @@ type FindAuthor = EthereumFindAuthor; } +impl pallet_evm_migration::Config for Runtime { + type WeightInfo = pallet_evm_migration::weights::SubstrateWeight; +} + pub struct EthereumFindAuthor(core::marker::PhantomData); impl> FindAuthor for EthereumFindAuthor { fn find_author<'a, I>(digests: I) -> Option @@ -819,6 +824,7 @@ EvmCoderSubstrate: pallet_evm_coder_substrate::{Pallet, Storage} = 150, EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage} = 151, EvmTransactionPayment: pallet_evm_transaction_payment::{Pallet} = 152, + EvmMigration: pallet_evm_migration::{Pallet, Call, Storage} = 153, } ); @@ -1180,6 +1186,7 @@ let mut batches = Vec::::new(); let params = (&config, &whitelist); + add_benchmark!(params, batches, pallet_evm_migration, EvmMigration); add_benchmark!(params, batches, pallet_nft, Nft); add_benchmark!(params, batches, pallet_inflation, Inflation); -- gitstuff