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.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -15,6 +15,7 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]
+#![feature(is_some_with)]
use codec::{Decode, Encode, MaxEncodedLen};
pub use pallet::*;
@@ -25,9 +26,10 @@
#[frame_support::pallet]
pub mod pallet {
pub use super::*;
- use evm_coder::execution::Result;
use frame_support::pallet_prelude::*;
use sp_core::H160;
+ use pallet_evm::account::CrossAccountId;
+ use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config:
@@ -39,17 +41,31 @@
#[pallet::error]
pub enum Error<T> {
- /// This method is only executable by owner
+ /// This method is only executable by owner.
NoPermission,
+
+ /// Contract has no owner.
+ NoContractOwner,
}
+ const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
+
#[pallet::pallet]
+ #[pallet::storage_version(STORAGE_VERSION)]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
+ /// Store owner for contract.
+ ///
+ /// * **Key** - contract address.
+ /// * **Value** - owner for contract.
#[pallet::storage]
- pub(super) type Owner<T: Config> =
- StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;
+ pub(super) type Owner<T: Config> = StorageMap<
+ Hasher = Twox128,
+ Key = H160,
+ Value = T::CrossAccountId,
+ QueryKind = OptionQuery,
+ >;
#[pallet::storage]
#[deprecated]
@@ -57,10 +73,12 @@
StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
#[pallet::storage]
+ #[deprecated]
pub(super) type SponsoringMode<T: Config> =
StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;
#[pallet::storage]
+ #[deprecated]
pub(super) type SponsoringRateLimit<T: Config> = StorageMap<
Hasher = Twox128,
Key = H160,
@@ -70,6 +88,7 @@
>;
#[pallet::storage]
+ #[deprecated]
pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<
Hasher1 = Twox128,
Key1 = H160,
@@ -80,10 +99,12 @@
>;
#[pallet::storage]
+ #[deprecated]
pub(super) type AllowlistEnabled<T: Config> =
StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
#[pallet::storage]
+ #[deprecated]
pub(super) type Allowlist<T: Config> = StorageDoubleMap<
Hasher1 = Twox128,
Key1 = H160,
@@ -93,6 +114,18 @@
QueryKind = ValueQuery,
>;
+ #[pallet::hooks]
+ impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
+ fn on_runtime_upgrade() -> Weight {
+ let storage_version = StorageVersion::get::<Pallet<T>>();
+ if storage_version < StorageVersion::new(1) {
+ <Owner<T>>::translate_values::<H160, _>(|address| Some(T::CrossAccountId::from_eth(address)));
+ }
+
+ 0
+ }
+ }
+
impl<T: Config> Pallet<T> {
pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {
<SponsoringMode<T>>::get(contract)
@@ -125,8 +158,9 @@
<SponsoringRateLimit<T>>::insert(contract, rate_limit);
}
- pub fn allowed(contract: H160, user: H160) -> bool {
- <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user
+ pub fn allowed(contract: H160, user: T::CrossAccountId) -> bool {
+ <Allowlist<T>>::get(&contract, user.as_eth())
+ || Pallet::<T>::contract_owner(contract).is_ok_and(|owner| *owner == user)
}
pub fn toggle_allowlist(contract: H160, enabled: bool) {
@@ -137,11 +171,17 @@
<Allowlist<T>>::insert(contract, user, allowed);
}
- pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {
- ensure!(<Owner<T>>::get(&contract) == user, "no permission");
+ pub fn ensure_owner(contract: H160, user: H160) -> evm_coder::execution::Result<()> {
+ ensure!(Pallet::<T>::contract_owner(contract).is_ok_and(|owner| *owner.as_eth() == user), "no permission");
Ok(())
}
}
+
+ impl<T: Config> Pallet<T> {
+ pub fn contract_owner(contract: H160) -> Result<T::CrossAccountId, DispatchError> {
+ Ok(<Owner<T>>::get(contract).ok_or::<Error<T>>(Error::NoContractOwner)?)
+ }
+ }
}
#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]