1234567891011121314151617#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]19#![deny(missing_docs)]2021pub use pallet::*;22#[cfg(feature = "runtime-benchmarks")]23pub mod benchmarking;24pub mod weights;2526#[frame_support::pallet]27pub mod pallet {28 use frame_support::{29 pallet_prelude::{*, DispatchResult},30 traits::IsType,31 };32 use frame_system::pallet_prelude::{*, OriginFor};33 use sp_core::{H160, H256};34 use sp_std::vec::Vec;35 use super::weights::WeightInfo;36 use pallet_evm::{PrecompileHandle, Pallet as PalletEvm};3738 #[pallet::config]39 pub trait Config: frame_system::Config + pallet_evm::Config {40 41 type WeightInfo: WeightInfo;42 43 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;44 }4546 type SelfWeightOf<T> = <T as Config>::WeightInfo;4748 #[pallet::pallet]49 #[pallet::generate_store(pub(super) trait Store)]50 pub struct Pallet<T>(_);5152 #[pallet::event]53 pub enum Event<T: Config> {54 55 TestEvent,56 }5758 #[pallet::error]59 pub enum Error<T> {60 61 AccountNotEmpty,62 63 AccountIsNotMigrating,64 65 BadEvent,66 }6768 #[pallet::storage]69 pub(super) type MigrationPending<T: Config> =70 StorageMap<Hasher = Twox64Concat, Key = H160, Value = bool, QueryKind = ValueQuery>;7172 #[pallet::call]73 impl<T: Config> Pallet<T> {74 75 76 #[pallet::weight(<SelfWeightOf<T>>::begin())]77 pub fn begin(origin: OriginFor<T>, address: H160) -> DispatchResult {78 ensure_root(origin)?;79 ensure!(80 <PalletEvm<T>>::is_account_empty(&address) && !<MigrationPending<T>>::get(&address),81 <Error<T>>::AccountNotEmpty,82 );8384 <MigrationPending<T>>::insert(address, true);85 Ok(())86 }8788 89 90 #[pallet::weight(<SelfWeightOf<T>>::set_data(data.len() as u32))]91 pub fn set_data(92 origin: OriginFor<T>,93 address: H160,94 data: Vec<(H256, H256)>,95 ) -> DispatchResult {96 ensure_root(origin)?;97 ensure!(98 <MigrationPending<T>>::get(&address),99 <Error<T>>::AccountIsNotMigrating,100 );101102 for (k, v) in data {103 <pallet_evm::AccountStorages<T>>::insert(&address, k, v);104 }105 Ok(())106 }107108 109 110 111 #[pallet::weight(<SelfWeightOf<T>>::finish(code.len() as u32))]112 pub fn finish(origin: OriginFor<T>, address: H160, code: Vec<u8>) -> DispatchResult {113 ensure_root(origin)?;114 ensure!(115 <MigrationPending<T>>::get(&address),116 <Error<T>>::AccountIsNotMigrating,117 );118119 <pallet_evm::AccountCodes<T>>::insert(&address, code);120 <MigrationPending<T>>::remove(address);121 Ok(())122 }123124 125 #[pallet::weight(<SelfWeightOf<T>>::insert_eth_logs(logs.len() as u32))]126 pub fn insert_eth_logs(origin: OriginFor<T>, logs: Vec<ethereum::Log>) -> DispatchResult {127 ensure_root(origin)?;128 for log in logs {129 <pallet_evm::Pallet<T>>::deposit_log(log);130 }131 132 Ok(())133 }134135 136 #[pallet::weight(<SelfWeightOf<T>>::insert_events(events.len() as u32))]137 pub fn insert_events(origin: OriginFor<T>, events: Vec<Vec<u8>>) -> DispatchResult {138 ensure_root(origin)?;139 for event in events {140 <frame_system::Pallet<T>>::deposit_event(141 <T as frame_system::Config>::RuntimeEvent::decode(&mut event.as_slice())142 .map_err(|_| <Error<T>>::BadEvent)?,143 );144 }145 Ok(())146 }147 }148149 150 pub struct OnMethodCall<T>(PhantomData<T>);151 impl<T: Config> pallet_evm::OnMethodCall<T> for OnMethodCall<T> {152 fn is_reserved(contract: &H160) -> bool {153 <MigrationPending<T>>::get(&contract)154 }155156 fn is_used(_contract: &H160) -> bool {157 false158 }159160 fn call(_handle: &mut impl PrecompileHandle) -> Option<pallet_evm::PrecompileResult> {161 None162 }163164 fn get_code(_contract: &H160) -> Option<Vec<u8>> {165 None166 }167 }168}