git.delta.rocks / unique-network / refs/commits / 6fbb7219f087

difftreelog

misk: revert owners storage

Trubnikov Sergey2022-08-03parent: #d144cc6.patch.diff
in: master

2 files changed

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
48#[solidity_interface(name = "ContractHelpers")]48#[solidity_interface(name = "ContractHelpers")]
49impl<T: Config> ContractHelpers<T> {49impl<T: Config> ContractHelpers<T> {
50 fn contract_owner(&self, contract_address: address) -> Result<address> {50 fn contract_owner(&self, contract_address: address) -> Result<address> {
51 Ok(<Pallet<T>>::contract_owner(contract_address)51 Ok(<Owner<T>>::get(contract_address))
52 .map_err(dispatch_to_evm::<T>)?
53 .as_eth()
54 .clone())
55 }52 }
5653
57 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {54 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {
65 contract_address: address,62 contract_address: address,
66 enabled: bool,63 enabled: bool,
67 ) -> Result<void> {64 ) -> Result<void> {
68 <Pallet<T>>::ensure_owner(contract_address, caller)?;65 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
69 <Pallet<T>>::toggle_sponsoring(contract_address, enabled);66 <Pallet<T>>::toggle_sponsoring(contract_address, enabled);
70 Ok(())67 Ok(())
71 }68 }
76 contract_address: address,73 contract_address: address,
77 mode: uint8,74 mode: uint8,
78 ) -> Result<void> {75 ) -> Result<void> {
79 <Pallet<T>>::ensure_owner(contract_address, caller)?;76 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
80 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;77 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;
81 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);78 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);
82 Ok(())79 Ok(())
92 contract_address: address,89 contract_address: address,
93 rate_limit: uint32,90 rate_limit: uint32,
94 ) -> Result<void> {91 ) -> Result<void> {
95 <Pallet<T>>::ensure_owner(contract_address, caller)?;92 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
96 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());93 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());
97 Ok(())94 Ok(())
98 }95 }
105102
106 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {103 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {
107 self.0.consume_sload()?;104 self.0.consume_sload()?;
108 Ok(<Pallet<T>>::allowed(contract_address, T::CrossAccountId::from_eth(user)))105 Ok(<Pallet<T>>::allowed(contract_address, user))
109 }106 }
110107
111 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {108 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {
118 contract_address: address,115 contract_address: address,
119 enabled: bool,116 enabled: bool,
120 ) -> Result<void> {117 ) -> Result<void> {
121 <Pallet<T>>::ensure_owner(contract_address, caller)?;118 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
122 <Pallet<T>>::toggle_allowlist(contract_address, enabled);119 <Pallet<T>>::toggle_allowlist(contract_address, enabled);
123 Ok(())120 Ok(())
124 }121 }
130 user: address,127 user: address,
131 allowed: bool,128 allowed: bool,
132 ) -> Result<void> {129 ) -> Result<void> {
133 <Pallet<T>>::ensure_owner(contract_address, caller)?;130 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
134 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);131 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);
135 Ok(())132 Ok(())
136 }133 }
149 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {146 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
150 // TODO: Extract to another OnMethodCall handler147 // TODO: Extract to another OnMethodCall handler
151 if <AllowlistEnabled<T>>::get(handle.code_address())148 if <AllowlistEnabled<T>>::get(handle.code_address())
152 && !<Pallet<T>>::allowed(handle.code_address(), T::CrossAccountId::from_eth(handle.context().caller))149 && !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)
153 {150 {
154 return Some(Err(PrecompileFailure::Revert {151 return Some(Err(PrecompileFailure::Revert {
155 exit_status: ExitRevert::Reverted,152 exit_status: ExitRevert::Reverted,
178pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);175pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);
179impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {176impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {
180 fn on_create(owner: H160, contract: H160) {177 fn on_create(owner: H160, contract: H160) {
181 <Owner<T>>::insert(contract, T::CrossAccountId::from_eth(owner));178 <Owner<T>>::insert(contract, owner);
182 }179 }
183}180}
184181
187 for HelpersContractSponsoring<T>184 for HelpersContractSponsoring<T>
188{185{
189 fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {186 fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {
187 let (contract, _) = call;
190 let mode = <Pallet<T>>::sponsoring_mode(call.0);188 let mode = <Pallet<T>>::sponsoring_mode(*contract);
191 if mode == SponsoringModeT::Disabled {189 if mode == SponsoringModeT::Disabled {
192 return None;190 return None;
193 }191 }
194192
195 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, who.clone()) {193 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {
196 return None;194 return None;
197 }195 }
198 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;196 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
199197
200 if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who.as_eth()) {198 if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {
201 let limit = <SponsoringRateLimit<T>>::get(&call.0);199 let limit = <SponsoringRateLimit<T>>::get(contract);
202200
203 let timeout = last_tx_block + limit;201 let timeout = last_tx_block + limit;
204 if block_number < timeout {202 if block_number < timeout {
205 return None;203 return None;
206 }204 }
207 }205 }
208206
209 <SponsorBasket<T>>::insert(&call.0, who.as_eth(), block_number);207 <SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);
210208
211 let sponsor = T::CrossAccountId::from_eth(call.0);209 let sponsor = T::CrossAccountId::from_eth(*contract);
212 Some(sponsor)210 Some(sponsor)
213 }211 }
214}212}
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -60,12 +60,8 @@
 	/// * **Key** - contract address.
 	/// * **Value** - owner for contract.
 	#[pallet::storage]
-	pub(super) type Owner<T: Config> = StorageMap<
-		Hasher = Twox128,
-		Key = H160,
-		Value = T::CrossAccountId,
-		QueryKind = OptionQuery,
-	>;
+	pub(super) type Owner<T: Config> =
+		StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;
 
 	#[pallet::storage]
 	#[deprecated]
@@ -97,7 +93,6 @@
 	>;
 
 	#[pallet::storage]
-	#[deprecated]
 	pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<
 		Hasher1 = Twox128,
 		Key1 = H160,
@@ -119,7 +114,6 @@
 		StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
 
 	#[pallet::storage]
-	#[deprecated]
 	pub(super) type Allowlist<T: Config> = StorageDoubleMap<
 		Hasher1 = Twox128,
 		Key1 = H160,
@@ -134,7 +128,6 @@
 		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
@@ -173,9 +166,8 @@
 			<SponsoringRateLimit<T>>::insert(contract, rate_limit);
 		}
 
-		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 allowed(contract: H160, user: H160) -> bool {
+			<Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user
 		}
 
 		pub fn toggle_allowlist(contract: H160, enabled: bool) {
@@ -186,15 +178,15 @@
 			<Allowlist<T>>::insert(contract, user, allowed);
 		}
 
-		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");
+		pub fn ensure_owner(contract: H160, user: H160) -> DispatchResult {
+			ensure!(<Owner<T>>::get(&contract) == user, Error::<T>::NoPermission);
 			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)?)
+		pub fn contract_owner(contract: H160) -> H160 {
+			<Owner<T>>::get(contract)
 		}
 	}
 }