difftreelog
refactor Chage Owner storage value type H160 -> CrossAccountId BREAKING CHANGE: changed `fn allowed` signature
in: master
2 files changed
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth17use core::marker::PhantomData;17use core::marker::PhantomData;18use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};18use evm_coder::{19 abi::AbiWriter,20 execution::{Result, Error},21 generate_stubgen, solidity_interface,22 types::*,23};19use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};24use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};20use pallet_evm::{25use pallet_evm::{21 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,26 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,22 account::CrossAccountId,27 account::CrossAccountId,43#[solidity_interface(name = "ContractHelpers")]48#[solidity_interface(name = "ContractHelpers")]44impl<T: Config> ContractHelpers<T> {49impl<T: Config> ContractHelpers<T> {45 fn contract_owner(&self, contract_address: address) -> Result<address> {50 fn contract_owner(&self, contract_address: address) -> Result<address> {46 Ok(<Owner<T>>::get(contract_address))51 Ok(<Pallet<T>>::contract_owner(contract_address)52 .map_err(dispatch_to_evm::<T>)?53 .as_eth()54 .clone())47 }55 }485649 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {57 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {9710598 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {106 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {99 self.0.consume_sload()?;107 self.0.consume_sload()?;100 Ok(<Pallet<T>>::allowed(contract_address, user))108 Ok(<Pallet<T>>::allowed(contract_address, T::CrossAccountId::from_eth(user)))101 }109 }102110103 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {111 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {141 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {149 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {142 // TODO: Extract to another OnMethodCall handler150 // TODO: Extract to another OnMethodCall handler143 if <AllowlistEnabled<T>>::get(handle.code_address())151 if <AllowlistEnabled<T>>::get(handle.code_address())144 && !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)152 && !<Pallet<T>>::allowed(handle.code_address(), T::CrossAccountId::from_eth(handle.context().caller))145 {153 {146 return Some(Err(PrecompileFailure::Revert {154 return Some(Err(PrecompileFailure::Revert {147 exit_status: ExitRevert::Reverted,155 exit_status: ExitRevert::Reverted,170pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);178pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);171impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {179impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {172 fn on_create(owner: H160, contract: H160) {180 fn on_create(owner: H160, contract: H160) {173 <Owner<T>>::insert(contract, owner);181 <Owner<T>>::insert(contract, T::CrossAccountId::from_eth(owner));174 }182 }175}183}176184184 return None;192 return None;185 }193 }186194187 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who.as_eth()) {195 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, who.clone()) {188 return None;196 return None;189 }197 }190 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;198 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617#![cfg_attr(not(feature = "std"), no_std)]17#![cfg_attr(not(feature = "std"), no_std)]18#![feature(is_some_with)]181919use codec::{Decode, Encode, MaxEncodedLen};20use codec::{Decode, Encode, MaxEncodedLen};20pub use pallet::*;21pub use pallet::*;25#[frame_support::pallet]26#[frame_support::pallet]26pub mod pallet {27pub mod pallet {27 pub use super::*;28 pub use super::*;28 use evm_coder::execution::Result;29 use frame_support::pallet_prelude::*;29 use frame_support::pallet_prelude::*;30 use sp_core::H160;30 use sp_core::H160;31 use pallet_evm::account::CrossAccountId;32 use frame_system::pallet_prelude::BlockNumberFor;313332 #[pallet::config]34 #[pallet::config]33 pub trait Config:35 pub trait Config:394140 #[pallet::error]42 #[pallet::error]41 pub enum Error<T> {43 pub enum Error<T> {42 /// This method is only executable by owner44 /// This method is only executable by owner.43 NoPermission,45 NoPermission,4647 /// Contract has no owner.48 NoContractOwner,44 }49 }5051 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);455246 #[pallet::pallet]53 #[pallet::pallet]54 #[pallet::storage_version(STORAGE_VERSION)]47 #[pallet::generate_store(pub(super) trait Store)]55 #[pallet::generate_store(pub(super) trait Store)]48 pub struct Pallet<T>(_);56 pub struct Pallet<T>(_);495758 /// Store owner for contract.59 ///60 /// * **Key** - contract address.61 /// * **Value** - owner for contract.50 #[pallet::storage]62 #[pallet::storage]51 pub(super) type Owner<T: Config> =63 pub(super) type Owner<T: Config> = StorageMap<52 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;64 Hasher = Twox128,65 Key = H160,66 Value = T::CrossAccountId,67 QueryKind = OptionQuery,68 >;536954 #[pallet::storage]70 #[pallet::storage]57 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;73 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;587459 #[pallet::storage]75 #[pallet::storage]76 #[deprecated]60 pub(super) type SponsoringMode<T: Config> =77 pub(super) type SponsoringMode<T: Config> =61 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;78 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;627963 #[pallet::storage]80 #[pallet::storage]81 #[deprecated]64 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<82 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<65 Hasher = Twox128,83 Hasher = Twox128,66 Key = H160,84 Key = H160,70 >;88 >;718972 #[pallet::storage]90 #[pallet::storage]91 #[deprecated]73 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<92 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<74 Hasher1 = Twox128,93 Hasher1 = Twox128,75 Key1 = H160,94 Key1 = H160,80 >;99 >;8110082 #[pallet::storage]101 #[pallet::storage]102 #[deprecated]83 pub(super) type AllowlistEnabled<T: Config> =103 pub(super) type AllowlistEnabled<T: Config> =84 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;104 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;8510586 #[pallet::storage]106 #[pallet::storage]107 #[deprecated]87 pub(super) type Allowlist<T: Config> = StorageDoubleMap<108 pub(super) type Allowlist<T: Config> = StorageDoubleMap<88 Hasher1 = Twox128,109 Hasher1 = Twox128,89 Key1 = H160,110 Key1 = H160,93 QueryKind = ValueQuery,114 QueryKind = ValueQuery,94 >;115 >;116117 #[pallet::hooks]118 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {119 fn on_runtime_upgrade() -> Weight {120 let storage_version = StorageVersion::get::<Pallet<T>>();121 if storage_version < StorageVersion::new(1) {122 <Owner<T>>::translate_values::<H160, _>(|address| Some(T::CrossAccountId::from_eth(address)));123 }124125 0126 }127 }9512896 impl<T: Config> Pallet<T> {129 impl<T: Config> Pallet<T> {97 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {130 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {125 <SponsoringRateLimit<T>>::insert(contract, rate_limit);158 <SponsoringRateLimit<T>>::insert(contract, rate_limit);126 }159 }127160128 pub fn allowed(contract: H160, user: H160) -> bool {161 pub fn allowed(contract: H160, user: T::CrossAccountId) -> bool {129 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user162 <Allowlist<T>>::get(&contract, user.as_eth())163 || Pallet::<T>::contract_owner(contract).is_ok_and(|owner| *owner == user)130 }164 }131165132 pub fn toggle_allowlist(contract: H160, enabled: bool) {166 pub fn toggle_allowlist(contract: H160, enabled: bool) {137 <Allowlist<T>>::insert(contract, user, allowed);171 <Allowlist<T>>::insert(contract, user, allowed);138 }172 }139173140 pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {174 pub fn ensure_owner(contract: H160, user: H160) -> evm_coder::execution::Result<()> {141 ensure!(<Owner<T>>::get(&contract) == user, "no permission");175 ensure!(Pallet::<T>::contract_owner(contract).is_ok_and(|owner| *owner.as_eth() == user), "no permission");142 Ok(())176 Ok(())143 }177 }144 }178 }179180 impl<T: Config> Pallet<T> {181 pub fn contract_owner(contract: H160) -> Result<T::CrossAccountId, DispatchError> {182 Ok(<Owner<T>>::get(contract).ok_or::<Error<T>>(Error::NoContractOwner)?)183 }184 }145}185}146186147#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]187#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]