difftreelog
doc(pallet-evm-migration): document public api
in: master
4 files changed
pallets/evm-migration/README.mddiffbeforeafterboth--- /dev/null
+++ b/pallets/evm-migration/README.md
@@ -0,0 +1,18 @@
+# EVM contract migration pallet
+
+This pallet is only callable by root, it has functionality to migrate contract
+from other ethereum chain to pallet-evm
+
+Contract data includes contract code, and contract storage,
+where contract storage is a mapping from evm word to evm word (evm word = 32 byte)
+
+To import contract data into pallet-evm admin should call this pallet multiple times:
+1. Start migration via `begin`
+2. Insert all contract data using single or
+ multiple (If data can't be fit into single extrinsic) calls
+ to `set_data`
+3. Finish migration using `finish`, providing contract code
+
+During migration no one can insert code at address of this contract,
+as [`pallet::OnMethodCall`] prevents this, and no one can call this contract,
+as code is only supplied at final stage of contract deployment
\ No newline at end of file
pallets/evm-migration/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/evm-migration/src/benchmarking.rs
+++ b/pallets/evm-migration/src/benchmarking.rs
@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+#![allow(missing_docs)]
+
use super::{Call, Config, Pallet};
use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;
pallets/evm-migration/src/lib.rsdiffbeforeafterboth1// 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/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;20#[cfg(feature = "runtime-benchmarks")]21pub mod benchmarking;22pub mod weights;2324#[frame_support::pallet]25pub mod pallet {26 use frame_support::pallet_prelude::*;27 use frame_system::pallet_prelude::*;28 use sp_core::{H160, H256};29 use sp_std::vec::Vec;30 use super::weights::WeightInfo;31 use pallet_evm::{PrecompileHandle, Pallet as PalletEvm};3233 #[pallet::config]34 pub trait Config: frame_system::Config + pallet_evm::Config {35 type WeightInfo: WeightInfo;36 }3738 type SelfWeightOf<T> = <T as Config>::WeightInfo;3940 #[pallet::pallet]41 #[pallet::generate_store(pub(super) trait Store)]42 pub struct Pallet<T>(_);4344 #[pallet::error]45 pub enum Error<T> {46 AccountNotEmpty,47 AccountIsNotMigrating,48 }4950 #[pallet::storage]51 pub(super) type MigrationPending<T: Config> =52 StorageMap<Hasher = Twox64Concat, Key = H160, Value = bool, QueryKind = ValueQuery>;5354 #[pallet::call]55 impl<T: Config> Pallet<T> {56 #[pallet::weight(<SelfWeightOf<T>>::begin())]57 pub fn begin(origin: OriginFor<T>, address: H160) -> DispatchResult {58 ensure_root(origin)?;59 ensure!(60 <PalletEvm<T>>::is_account_empty(&address) && !<MigrationPending<T>>::get(&address),61 <Error<T>>::AccountNotEmpty,62 );6364 <MigrationPending<T>>::insert(address, true);65 Ok(())66 }6768 #[pallet::weight(<SelfWeightOf<T>>::set_data(data.len() as u32))]69 pub fn set_data(70 origin: OriginFor<T>,71 address: H160,72 data: Vec<(H256, H256)>,73 ) -> DispatchResult {74 ensure_root(origin)?;75 ensure!(76 <MigrationPending<T>>::get(&address),77 <Error<T>>::AccountIsNotMigrating,78 );7980 for (k, v) in data {81 <pallet_evm::AccountStorages<T>>::insert(&address, k, v);82 }83 Ok(())84 }8586 #[pallet::weight(<SelfWeightOf<T>>::finish(code.len() as u32))]87 pub fn finish(origin: OriginFor<T>, address: H160, code: Vec<u8>) -> DispatchResult {88 ensure_root(origin)?;89 ensure!(90 <MigrationPending<T>>::get(&address),91 <Error<T>>::AccountIsNotMigrating,92 );9394 <pallet_evm::AccountCodes<T>>::insert(&address, code);95 <MigrationPending<T>>::remove(address);96 Ok(())97 }98 }99100 pub struct OnMethodCall<T>(PhantomData<T>);101 impl<T: Config> pallet_evm::OnMethodCall<T> for OnMethodCall<T> {102 fn is_reserved(contract: &H160) -> bool {103 <MigrationPending<T>>::get(&contract)104 }105106 fn is_used(_contract: &H160) -> bool {107 false108 }109110 fn call(_handle: &mut impl PrecompileHandle) -> Option<pallet_evm::PrecompileResult> {111 None112 }113114 fn get_code(_contract: &H160) -> Option<Vec<u8>> {115 None116 }117 }118}1// 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/>.1617#![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::pallet_prelude::*;29 use frame_system::pallet_prelude::*;30 use sp_core::{H160, H256};31 use sp_std::vec::Vec;32 use super::weights::WeightInfo;33 use pallet_evm::{PrecompileHandle, Pallet as PalletEvm};3435 #[pallet::config]36 pub trait Config: frame_system::Config + pallet_evm::Config {37 /// Weights38 type WeightInfo: WeightInfo;39 }4041 type SelfWeightOf<T> = <T as Config>::WeightInfo;4243 #[pallet::pallet]44 #[pallet::generate_store(pub(super) trait Store)]45 pub struct Pallet<T>(_);4647 #[pallet::error]48 pub enum Error<T> {49 /// Can only migrate to empty address.50 AccountNotEmpty,51 /// Migration of this account is not yet started, or already finished.52 AccountIsNotMigrating,53 }5455 #[pallet::storage]56 pub(super) type MigrationPending<T: Config> =57 StorageMap<Hasher = Twox64Concat, Key = H160, Value = bool, QueryKind = ValueQuery>;5859 #[pallet::call]60 impl<T: Config> Pallet<T> {61 /// Start contract migration, inserts contract stub at target address,62 /// and marks account as pending, allowing to insert storage63 #[pallet::weight(<SelfWeightOf<T>>::begin())]64 pub fn begin(origin: OriginFor<T>, address: H160) -> DispatchResult {65 ensure_root(origin)?;66 ensure!(67 <PalletEvm<T>>::is_account_empty(&address) && !<MigrationPending<T>>::get(&address),68 <Error<T>>::AccountNotEmpty,69 );7071 <MigrationPending<T>>::insert(address, true);72 Ok(())73 }7475 /// Insert items into contract storage, this method can be called76 /// multiple times77 #[pallet::weight(<SelfWeightOf<T>>::set_data(data.len() as u32))]78 pub fn set_data(79 origin: OriginFor<T>,80 address: H160,81 data: Vec<(H256, H256)>,82 ) -> DispatchResult {83 ensure_root(origin)?;84 ensure!(85 <MigrationPending<T>>::get(&address),86 <Error<T>>::AccountIsNotMigrating,87 );8889 for (k, v) in data {90 <pallet_evm::AccountStorages<T>>::insert(&address, k, v);91 }92 Ok(())93 }9495 /// Finish contract migration, allows it to be called.96 /// It is not possible to alter contract storage via [`Self::set_data`]97 /// after this call.98 #[pallet::weight(<SelfWeightOf<T>>::finish(code.len() as u32))]99 pub fn finish(origin: OriginFor<T>, address: H160, code: Vec<u8>) -> DispatchResult {100 ensure_root(origin)?;101 ensure!(102 <MigrationPending<T>>::get(&address),103 <Error<T>>::AccountIsNotMigrating,104 );105106 <pallet_evm::AccountCodes<T>>::insert(&address, code);107 <MigrationPending<T>>::remove(address);108 Ok(())109 }110 }111112 /// Implements [`pallet_evm::OnMethodCall`], which reserves accounts with pending migration113 pub struct OnMethodCall<T>(PhantomData<T>);114 impl<T: Config> pallet_evm::OnMethodCall<T> for OnMethodCall<T> {115 fn is_reserved(contract: &H160) -> bool {116 <MigrationPending<T>>::get(&contract)117 }118119 fn is_used(_contract: &H160) -> bool {120 false121 }122123 fn call(_handle: &mut impl PrecompileHandle) -> Option<pallet_evm::PrecompileResult> {124 None125 }126127 fn get_code(_contract: &H160) -> Option<Vec<u8>> {128 None129 }130 }131}pallets/evm-migration/src/weights.rsdiffbeforeafterboth--- a/pallets/evm-migration/src/weights.rs
+++ b/pallets/evm-migration/src/weights.rs
@@ -26,6 +26,7 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
+#![allow(missing_docs)]
#![allow(clippy::unnecessary_cast)]
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};