difftreelog
path: Add 'self_sponsored_enable' and 'remove_sponsor'.
in: master
2 files changed
pallets/evm-contract-helpers/src/eth.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/>.1617use core::marker::PhantomData;18use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};19use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};20use pallet_evm::{21 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,22 account::CrossAccountId,23};24use sp_core::H160;25use up_data_structs::SponsorshipState;26use crate::{27 AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringRateLimit, SponsoringModeT, Sponsoring,28};29use frame_support::traits::Get;30use up_sponsorship::SponsorshipHandler;31use sp_std::vec::Vec;3233struct ContractHelpers<T: Config>(SubstrateRecorder<T>);34impl<T: Config> WithRecorder<T> for ContractHelpers<T> {35 fn recorder(&self) -> &SubstrateRecorder<T> {36 &self.037 }3839 fn into_recorder(self) -> SubstrateRecorder<T> {40 self.041 }42}4344#[solidity_interface(name = "ContractHelpers")]45impl<T: Config> ContractHelpers<T>46where47 T::AccountId: AsRef<[u8]>,48{49 /// Get contract ovner50 /// 51 /// @param Contract_address contract for which the owner is being determined.52 /// @return Contract owner.53 fn contract_owner(&self, contract_address: address) -> Result<address> {54 Ok(<Owner<T>>::get(contract_address))55 }5657 /// Set sponsor.58 /// 59 /// @param contract_address Contract for which a sponsor is being established.60 /// @param sponsor User address who set as pending sponsor.61 fn set_sponsor(62 &mut self,63 caller: caller,64 contract_address: address,65 sponsor: address,66 ) -> Result<void> {67 Pallet::<T>::set_sponsor(68 &T::CrossAccountId::from_eth(caller),69 contract_address,70 &T::CrossAccountId::from_eth(sponsor),71 )72 .map_err(dispatch_to_evm::<T>)?;73 Ok(())74 }7576 /// Confirm sponsorship.77 /// 78 /// @dev Caller must be same that set via [`set_sponsor`].79 /// 80 /// @param contract_address Сontract for which need to confirm sponsorship.81 fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {82 Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)83 .map_err(dispatch_to_evm::<T>)?;84 Ok(())85 }8687 /// Get current sponsor.88 /// 89 /// @param contract_address The contract for which a sponsor is requested.90 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.91 fn get_sponsor(&self, contract_address: address) -> Result<(address, uint256)> {92 let sponsor =93 Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?;94 let sponsor_sub = pallet_common::eth::convert_cross_account_to_eth_uint256::<T>(&sponsor);95 Ok((*sponsor.as_eth(), sponsor_sub))96 }9798 /// Check tat contract has confirmed sponsor.99 /// 100 /// @param contract_address The contract for which the presence of a confirmed sponsor is checked.101 /// @return **true** if contract has confirmed sponsor.102 fn has_sponsor(&self, contract_address: address) -> Result<bool> {103 Ok(Pallet::<T>::get_sponsor(contract_address).is_some())104 }105106 /// Check tat contract has pending sponsor.107 /// 108 /// @param contract_address The contract for which the presence of a pending sponsor is checked.109 /// @return **true** if contract has pending sponsor.110 fn has_pending_sponsor(&self, contract_address: address) -> Result<bool> {111 Ok(match Sponsoring::<T>::get(contract_address) {112 SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => false,113 SponsorshipState::Unconfirmed(_) => true,114 })115 }116117 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {118 Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)119 }120121 fn set_sponsoring_mode(122 &mut self,123 caller: caller,124 contract_address: address,125 mode: uint8,126 ) -> Result<void> {127 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;128 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;129 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);130 Ok(())131 }132133 fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {134 Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())135 }136137 fn set_sponsoring_rate_limit(138 &mut self,139 caller: caller,140 contract_address: address,141 rate_limit: uint32,142 ) -> Result<void> {143 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;144 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());145 Ok(())146 }147148 fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {149 Ok(<SponsoringRateLimit<T>>::get(contract_address)150 .try_into()151 .map_err(|_| "rate limit > u32::MAX")?)152 }153154 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {155 self.0.consume_sload()?;156 Ok(<Pallet<T>>::allowed(contract_address, user))157 }158159 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {160 Ok(<AllowlistEnabled<T>>::get(contract_address))161 }162163 fn toggle_allowlist(164 &mut self,165 caller: caller,166 contract_address: address,167 enabled: bool,168 ) -> Result<void> {169 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;170 <Pallet<T>>::toggle_allowlist(contract_address, enabled);171 Ok(())172 }173174 fn toggle_allowed(175 &mut self,176 caller: caller,177 contract_address: address,178 user: address,179 allowed: bool,180 ) -> Result<void> {181 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;182 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);183 Ok(())184 }185}186187pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);188impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T>189where190 T::AccountId: AsRef<[u8]>,191{192 fn is_reserved(contract: &sp_core::H160) -> bool {193 contract == &T::ContractAddress::get()194 }195196 fn is_used(contract: &sp_core::H160) -> bool {197 contract == &T::ContractAddress::get()198 }199200 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {201 // TODO: Extract to another OnMethodCall handler202 if <AllowlistEnabled<T>>::get(handle.code_address())203 && !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)204 {205 return Some(Err(PrecompileFailure::Revert {206 exit_status: ExitRevert::Reverted,207 output: {208 let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));209 writer.string("Target contract is allowlisted");210 writer.finish()211 },212 }));213 }214215 if handle.code_address() != T::ContractAddress::get() {216 return None;217 }218219 let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));220 pallet_evm_coder_substrate::call(handle, helpers)221 }222223 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {224 (contract == &T::ContractAddress::get())225 .then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())226 }227}228229pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);230impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {231 fn on_create(owner: H160, contract: H160) {232 <Owner<T>>::insert(contract, owner);233 }234}235236pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);237impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>238 for HelpersContractSponsoring<T>239{240 fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {241 let (contract, _) = call;242 let mode = <Pallet<T>>::sponsoring_mode(*contract);243 if mode == SponsoringModeT::Disabled {244 return None;245 }246247 let sponsor = match <Pallet<T>>::get_sponsor(*contract) {248 Some(sponsor) => sponsor,249 None => return None,250 };251252 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {253 return None;254 }255 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;256257 if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {258 let limit = <SponsoringRateLimit<T>>::get(contract);259260 let timeout = last_tx_block + limit;261 if block_number < timeout {262 return None;263 }264 }265266 <SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);267268 Some(sponsor)269 }270}271272generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);273generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);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/>.1617use core::marker::PhantomData;18use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};19use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};20use pallet_evm::{21 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,22 account::CrossAccountId,23};24use sp_core::H160;25use up_data_structs::SponsorshipState;26use crate::{27 AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringRateLimit, SponsoringModeT,28 Sponsoring,29};30use frame_support::traits::Get;31use up_sponsorship::SponsorshipHandler;32use sp_std::vec::Vec;3334struct ContractHelpers<T: Config>(SubstrateRecorder<T>);35impl<T: Config> WithRecorder<T> for ContractHelpers<T> {36 fn recorder(&self) -> &SubstrateRecorder<T> {37 &self.038 }3940 fn into_recorder(self) -> SubstrateRecorder<T> {41 self.042 }43}4445#[solidity_interface(name = "ContractHelpers")]46impl<T: Config> ContractHelpers<T>47where48 T::AccountId: AsRef<[u8]>,49{50 /// Get contract ovner51 ///52 /// @param Contract_address contract for which the owner is being determined.53 /// @return Contract owner.54 fn contract_owner(&self, contract_address: address) -> Result<address> {55 Ok(<Owner<T>>::get(contract_address))56 }5758 /// Set sponsor.59 ///60 /// @param contract_address Contract for which a sponsor is being established.61 /// @param sponsor User address who set as pending sponsor.62 fn set_sponsor(63 &mut self,64 caller: caller,65 contract_address: address,66 sponsor: address,67 ) -> Result<void> {68 Pallet::<T>::set_sponsor(69 &T::CrossAccountId::from_eth(caller),70 contract_address,71 &T::CrossAccountId::from_eth(sponsor),72 )73 .map_err(dispatch_to_evm::<T>)?;74 Ok(())75 }7677 /// Set contract as self sponsored.78 ///79 /// @param contract_address Contract for which a self sponsoring is being enabled.80 fn self_sponsored_enable(&mut self, caller: caller, contract_address: address) -> Result<void> {81 Pallet::<T>::self_sponsored_enable(&T::CrossAccountId::from_eth(caller), contract_address)82 .map_err(dispatch_to_evm::<T>)?;83 Ok(())84 }8586 /// Remove sponsor.87 ///88 /// @param contract_address Contract for which a sponsorship is being removed.89 fn remove_sponsor(&mut self, caller: caller, contract_address: address) -> Result<void> {90 Pallet::<T>::remove_sponsor(&T::CrossAccountId::from_eth(caller), contract_address)91 .map_err(dispatch_to_evm::<T>)?;92 Ok(())93 }9495 /// Confirm sponsorship.96 ///97 /// @dev Caller must be same that set via [`set_sponsor`].98 ///99 /// @param contract_address Сontract for which need to confirm sponsorship.100 fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {101 Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)102 .map_err(dispatch_to_evm::<T>)?;103 Ok(())104 }105106 /// Get current sponsor.107 ///108 /// @param contract_address The contract for which a sponsor is requested.109 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.110 fn get_sponsor(&self, contract_address: address) -> Result<(address, uint256)> {111 let sponsor =112 Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?;113 let sponsor_sub = pallet_common::eth::convert_cross_account_to_eth_uint256::<T>(&sponsor);114 Ok((*sponsor.as_eth(), sponsor_sub))115 }116117 /// Check tat contract has confirmed sponsor.118 ///119 /// @param contract_address The contract for which the presence of a confirmed sponsor is checked.120 /// @return **true** if contract has confirmed sponsor.121 fn has_sponsor(&self, contract_address: address) -> Result<bool> {122 Ok(Pallet::<T>::get_sponsor(contract_address).is_some())123 }124125 /// Check tat contract has pending sponsor.126 ///127 /// @param contract_address The contract for which the presence of a pending sponsor is checked.128 /// @return **true** if contract has pending sponsor.129 fn has_pending_sponsor(&self, contract_address: address) -> Result<bool> {130 Ok(match Sponsoring::<T>::get(contract_address) {131 SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => false,132 SponsorshipState::Unconfirmed(_) => true,133 })134 }135136 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {137 Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)138 }139140 fn set_sponsoring_mode(141 &mut self,142 caller: caller,143 contract_address: address,144 mode: uint8,145 ) -> Result<void> {146 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;147 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;148 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);149 Ok(())150 }151152 fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {153 Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())154 }155156 fn set_sponsoring_rate_limit(157 &mut self,158 caller: caller,159 contract_address: address,160 rate_limit: uint32,161 ) -> Result<void> {162 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;163 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());164 Ok(())165 }166167 fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {168 Ok(<SponsoringRateLimit<T>>::get(contract_address)169 .try_into()170 .map_err(|_| "rate limit > u32::MAX")?)171 }172173 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {174 self.0.consume_sload()?;175 Ok(<Pallet<T>>::allowed(contract_address, user))176 }177178 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {179 Ok(<AllowlistEnabled<T>>::get(contract_address))180 }181182 fn toggle_allowlist(183 &mut self,184 caller: caller,185 contract_address: address,186 enabled: bool,187 ) -> Result<void> {188 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;189 <Pallet<T>>::toggle_allowlist(contract_address, enabled);190 Ok(())191 }192193 fn toggle_allowed(194 &mut self,195 caller: caller,196 contract_address: address,197 user: address,198 allowed: bool,199 ) -> Result<void> {200 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;201 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);202 Ok(())203 }204}205206pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);207impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T>208where209 T::AccountId: AsRef<[u8]>,210{211 fn is_reserved(contract: &sp_core::H160) -> bool {212 contract == &T::ContractAddress::get()213 }214215 fn is_used(contract: &sp_core::H160) -> bool {216 contract == &T::ContractAddress::get()217 }218219 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {220 // TODO: Extract to another OnMethodCall handler221 if <AllowlistEnabled<T>>::get(handle.code_address())222 && !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)223 {224 return Some(Err(PrecompileFailure::Revert {225 exit_status: ExitRevert::Reverted,226 output: {227 let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));228 writer.string("Target contract is allowlisted");229 writer.finish()230 },231 }));232 }233234 if handle.code_address() != T::ContractAddress::get() {235 return None;236 }237238 let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));239 pallet_evm_coder_substrate::call(handle, helpers)240 }241242 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {243 (contract == &T::ContractAddress::get())244 .then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())245 }246}247248pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);249impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {250 fn on_create(owner: H160, contract: H160) {251 <Owner<T>>::insert(contract, owner);252 }253}254255pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);256impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>257 for HelpersContractSponsoring<T>258{259 fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {260 let (contract, _) = call;261 let mode = <Pallet<T>>::sponsoring_mode(*contract);262 if mode == SponsoringModeT::Disabled {263 return None;264 }265266 let sponsor = match <Pallet<T>>::get_sponsor(*contract) {267 Some(sponsor) => sponsor,268 None => return None,269 };270271 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {272 return None;273 }274 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;275276 if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {277 let limit = <SponsoringRateLimit<T>>::get(contract);278279 let timeout = last_tx_block + limit;280 if block_number < timeout {281 return None;282 }283 }284285 <SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);286287 Some(sponsor)288 }289}290291generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);292generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -49,10 +49,7 @@
NoPendingSponsor,
}
- 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>(_);
@@ -102,7 +99,7 @@
>;
/// Storage for last sponsored block.
- ///
+ ///
/// * **Key1** - contract address.
/// * **Key2** - sponsored user address.
/// * **Value** - last sponsored block number.
@@ -144,25 +141,15 @@
Value = bool,
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) {}
- 0
- }
- }
-
impl<T: Config> Pallet<T> {
/// Get contract owner.
pub fn contract_owner(contract: H160) -> H160 {
<Owner<T>>::get(contract)
}
-
- /// Set `sponsor` for `contract`.
- ///
+
+ /// Set `sponsor` for `contract`.
+ ///
/// `sender` must be owner of contract.
pub fn set_sponsor(
sender: &T::CrossAccountId,
@@ -177,8 +164,31 @@
Ok(())
}
+ /// Set `contract` as self sponsored.
+ ///
+ /// `sender` must be owner of contract.
+ pub fn self_sponsored_enable(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
+ Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;
+ Sponsoring::<T>::insert(
+ contract,
+ SponsorshipState::<T::CrossAccountId>::Confirmed(T::CrossAccountId::from_eth(
+ contract,
+ )),
+ );
+ Ok(())
+ }
+
+ /// Remove sponsor for `contract`.
+ ///
+ /// `sender` must be owner of contract.
+ pub fn remove_sponsor(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
+ Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;
+ Sponsoring::<T>::remove(contract);
+ Ok(())
+ }
+
/// Confirm sponsorship.
- ///
+ ///
/// `sender` must be same that set via [`set_sponsor`].
pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
match Sponsoring::<T>::get(contract) {