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.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -15,8 +15,13 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
use core::marker::PhantomData;
-use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};
-use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
+use evm_coder::{
+ abi::AbiWriter,
+ execution::{Result, Error},
+ generate_stubgen, solidity_interface,
+ types::*,
+};
+use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};
use pallet_evm::{
ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,
account::CrossAccountId,
@@ -43,7 +48,10 @@
#[solidity_interface(name = "ContractHelpers")]
impl<T: Config> ContractHelpers<T> {
fn contract_owner(&self, contract_address: address) -> Result<address> {
- Ok(<Owner<T>>::get(contract_address))
+ Ok(<Pallet<T>>::contract_owner(contract_address)
+ .map_err(dispatch_to_evm::<T>)?
+ .as_eth()
+ .clone())
}
fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {
@@ -97,7 +105,7 @@
fn allowed(&self, contract_address: address, user: address) -> Result<bool> {
self.0.consume_sload()?;
- Ok(<Pallet<T>>::allowed(contract_address, user))
+ Ok(<Pallet<T>>::allowed(contract_address, T::CrossAccountId::from_eth(user)))
}
fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {
@@ -141,7 +149,7 @@
fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
// TODO: Extract to another OnMethodCall handler
if <AllowlistEnabled<T>>::get(handle.code_address())
- && !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)
+ && !<Pallet<T>>::allowed(handle.code_address(), T::CrossAccountId::from_eth(handle.context().caller))
{
return Some(Err(PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
@@ -170,7 +178,7 @@
pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);
impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {
fn on_create(owner: H160, contract: H160) {
- <Owner<T>>::insert(contract, owner);
+ <Owner<T>>::insert(contract, T::CrossAccountId::from_eth(owner));
}
}
@@ -184,7 +192,7 @@
return None;
}
- if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who.as_eth()) {
+ if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, who.clone()) {
return None;
}
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)]