git.delta.rocks / unique-network / refs/commits / e50c7257388c

difftreelog

path: Return sponsor as a tuple.

Trubnikov Sergey2022-08-04parent: #14eb87c.patch.diff
in: master

3 files changed

modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -425,7 +425,7 @@
 macro_rules! impl_tuples_abi_writer {
 	($($ident:ident)+) => {
 		#[allow(non_snake_case)]
-		impl<$($ident),+> AbiWrite for &($($ident,)+) 
+		impl<$($ident),+> AbiWrite for &($($ident,)+)
 		where
 			$($ident: AbiWrite,)+
 		{
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -52,6 +52,16 @@
 	address[0..16] == ETH_COLLECTION_PREFIX
 }
 
+/// Convert `CrossAccountId` to `uint256`.
+pub fn convert_cross_account_to_eth_uint256<T: Config>(from: &T::CrossAccountId) -> uint256
+where
+	T::AccountId: AsRef<[u8]>,
+{
+	use pallet_evm::account::CrossAccountId;
+	let slice = from.as_sub().as_ref();
+	uint256::from_big_endian(slice)
+}
+
 /// Converts Substrate address to CrossAccountId
 pub fn convert_substrate_address_to_cross_account_id<T: Config>(
 	address: uint256,
modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
before · pallets/evm-contract-helpers/src/eth.rs
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::{19	abi::AbiWriter,20	execution::{Result, Error},21	generate_stubgen, solidity_interface,22	types::*,23};24use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};25use pallet_evm::{26	ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,27	account::CrossAccountId,28};29use sp_core::H160;30use crate::{31	AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringRateLimit, SponsoringModeT,32};33use frame_support::traits::Get;34use up_sponsorship::SponsorshipHandler;35use sp_std::vec::Vec;3637struct ContractHelpers<T: Config>(SubstrateRecorder<T>);38impl<T: Config> WithRecorder<T> for ContractHelpers<T> {39	fn recorder(&self) -> &SubstrateRecorder<T> {40		&self.041	}4243	fn into_recorder(self) -> SubstrateRecorder<T> {44		self.045	}46}4748#[solidity_interface(name = "ContractHelpers")]49impl<T: Config> ContractHelpers<T> {50	fn contract_owner(&self, contract_address: address) -> Result<address> {51		Ok(<Owner<T>>::get(contract_address))52	}5354	fn set_sponsor(55		&mut self,56		caller: caller,57		contract_address: address,58		sponsor: address,59	) -> Result<void> {60		Pallet::<T>::set_sponsor(61			&T::CrossAccountId::from_eth(caller),62			contract_address,63			&T::CrossAccountId::from_eth(sponsor),64		)65		.map_err(dispatch_to_evm::<T>)?;66		Ok(())67	}6869	fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {70		Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)71			.map_err(dispatch_to_evm::<T>)?;72		Ok(())73	}7475	fn get_sponsor(&self, contract_address: address) -> Result<address> {76		let sponsor =77			Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?;78		Ok(*sponsor.as_eth())79	}8081	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {82		Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)83	}8485	/// Deprecated86	fn toggle_sponsoring(87		&mut self,88		caller: caller,89		contract_address: address,90		enabled: bool,91	) -> Result<void> {92		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;93		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);94		Ok(())95	}9697	fn set_sponsoring_mode(98		&mut self,99		caller: caller,100		contract_address: address,101		mode: uint8,102	) -> Result<void> {103		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;104		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;105		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);106		Ok(())107	}108109	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {110		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())111	}112113	fn set_sponsoring_rate_limit(114		&mut self,115		caller: caller,116		contract_address: address,117		rate_limit: uint32,118	) -> Result<void> {119		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;120		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());121		Ok(())122	}123124	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {125		Ok(<SponsoringRateLimit<T>>::get(contract_address)126			.try_into()127			.map_err(|_| "rate limit > u32::MAX")?)128	}129130	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {131		self.0.consume_sload()?;132		Ok(<Pallet<T>>::allowed(contract_address, user))133	}134135	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {136		Ok(<AllowlistEnabled<T>>::get(contract_address))137	}138139	fn toggle_allowlist(140		&mut self,141		caller: caller,142		contract_address: address,143		enabled: bool,144	) -> Result<void> {145		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;146		<Pallet<T>>::toggle_allowlist(contract_address, enabled);147		Ok(())148	}149150	fn toggle_allowed(151		&mut self,152		caller: caller,153		contract_address: address,154		user: address,155		allowed: bool,156	) -> Result<void> {157		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;158		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);159		Ok(())160	}161}162163pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);164impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {165	fn is_reserved(contract: &sp_core::H160) -> bool {166		contract == &T::ContractAddress::get()167	}168169	fn is_used(contract: &sp_core::H160) -> bool {170		contract == &T::ContractAddress::get()171	}172173	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {174		// TODO: Extract to another OnMethodCall handler175		if <AllowlistEnabled<T>>::get(handle.code_address())176			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)177		{178			return Some(Err(PrecompileFailure::Revert {179				exit_status: ExitRevert::Reverted,180				output: {181					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));182					writer.string("Target contract is allowlisted");183					writer.finish()184				},185			}));186		}187188		if handle.code_address() != T::ContractAddress::get() {189			return None;190		}191192		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));193		pallet_evm_coder_substrate::call(handle, helpers)194	}195196	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {197		(contract == &T::ContractAddress::get())198			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())199	}200}201202pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);203impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {204	fn on_create(owner: H160, contract: H160) {205		<Owner<T>>::insert(contract, owner);206	}207}208209pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);210impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>211	for HelpersContractSponsoring<T>212{213	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {214		let (contract, _) = call;215		let mode = <Pallet<T>>::sponsoring_mode(*contract);216		if mode == SponsoringModeT::Disabled {217			return None;218		}219220		let sponsor = match <Pallet<T>>::get_sponsor(*contract) {221			Some(sponsor) => sponsor,222			None => return None,223		};224225		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {226			return None;227		}228		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;229230		if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {231			let limit = <SponsoringRateLimit<T>>::get(contract);232233			let timeout = last_tx_block + limit;234			if block_number < timeout {235				return None;236			}237		}238239		<SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);240241		Some(sponsor)242	}243}244245generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);246generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);
after · pallets/evm-contract-helpers/src/eth.rs
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 crate::{26	AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringRateLimit, SponsoringModeT,27};28use frame_support::traits::Get;29use up_sponsorship::SponsorshipHandler;30use sp_std::vec::Vec;3132struct ContractHelpers<T: Config>(SubstrateRecorder<T>);33impl<T: Config> WithRecorder<T> for ContractHelpers<T> {34	fn recorder(&self) -> &SubstrateRecorder<T> {35		&self.036	}3738	fn into_recorder(self) -> SubstrateRecorder<T> {39		self.040	}41}4243#[solidity_interface(name = "ContractHelpers")]44impl<T: Config> ContractHelpers<T>45where46	T::AccountId: AsRef<[u8]>,47{48	fn contract_owner(&self, contract_address: address) -> Result<address> {49		Ok(<Owner<T>>::get(contract_address))50	}5152	fn set_sponsor(53		&mut self,54		caller: caller,55		contract_address: address,56		sponsor: address,57	) -> Result<void> {58		Pallet::<T>::set_sponsor(59			&T::CrossAccountId::from_eth(caller),60			contract_address,61			&T::CrossAccountId::from_eth(sponsor),62		)63		.map_err(dispatch_to_evm::<T>)?;64		Ok(())65	}6667	fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {68		Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)69			.map_err(dispatch_to_evm::<T>)?;70		Ok(())71	}7273	fn get_sponsor(&self, contract_address: address) -> Result<(address, uint256)> {74		let sponsor =75			Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?;76		let sponsor_sub =77			pallet_common::eth::convert_cross_account_to_eth_uint256::<T>(&sponsor);78		Ok((*sponsor.as_eth(), sponsor_sub))79	}8081	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {82		Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)83	}8485	/// Deprecated 86	fn toggle_sponsoring(87		&mut self,88		caller: caller,89		contract_address: address,90		enabled: bool,91	) -> Result<void> {92		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;93		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);94		Ok(())95	}9697	fn set_sponsoring_mode(98		&mut self,99		caller: caller,100		contract_address: address,101		mode: uint8,102	) -> Result<void> {103		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;104		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;105		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);106		Ok(())107	}108109	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {110		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())111	}112113	fn set_sponsoring_rate_limit(114		&mut self,115		caller: caller,116		contract_address: address,117		rate_limit: uint32,118	) -> Result<void> {119		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;120		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());121		Ok(())122	}123124	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {125		Ok(<SponsoringRateLimit<T>>::get(contract_address)126			.try_into()127			.map_err(|_| "rate limit > u32::MAX")?)128	}129130	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {131		self.0.consume_sload()?;132		Ok(<Pallet<T>>::allowed(contract_address, user))133	}134135	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {136		Ok(<AllowlistEnabled<T>>::get(contract_address))137	}138139	fn toggle_allowlist(140		&mut self,141		caller: caller,142		contract_address: address,143		enabled: bool,144	) -> Result<void> {145		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;146		<Pallet<T>>::toggle_allowlist(contract_address, enabled);147		Ok(())148	}149150	fn toggle_allowed(151		&mut self,152		caller: caller,153		contract_address: address,154		user: address,155		allowed: bool,156	) -> Result<void> {157		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;158		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);159		Ok(())160	}161}162163pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);164impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T>165where166	T::AccountId: AsRef<[u8]>,167{168	fn is_reserved(contract: &sp_core::H160) -> bool {169		contract == &T::ContractAddress::get()170	}171172	fn is_used(contract: &sp_core::H160) -> bool {173		contract == &T::ContractAddress::get()174	}175176	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {177		// TODO: Extract to another OnMethodCall handler178		if <AllowlistEnabled<T>>::get(handle.code_address())179			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)180		{181			return Some(Err(PrecompileFailure::Revert {182				exit_status: ExitRevert::Reverted,183				output: {184					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));185					writer.string("Target contract is allowlisted");186					writer.finish()187				},188			}));189		}190191		if handle.code_address() != T::ContractAddress::get() {192			return None;193		}194195		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));196		pallet_evm_coder_substrate::call(handle, helpers)197	}198199	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {200		(contract == &T::ContractAddress::get())201			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())202	}203}204205pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);206impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {207	fn on_create(owner: H160, contract: H160) {208		<Owner<T>>::insert(contract, owner);209	}210}211212pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);213impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>214	for HelpersContractSponsoring<T>215{216	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {217		let (contract, _) = call;218		let mode = <Pallet<T>>::sponsoring_mode(*contract);219		if mode == SponsoringModeT::Disabled {220			return None;221		}222223		let sponsor = match <Pallet<T>>::get_sponsor(*contract) {224			Some(sponsor) => sponsor,225			None => return None,226		};227228		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {229			return None;230		}231		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;232233		if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {234			let limit = <SponsoringRateLimit<T>>::get(contract);235236			let timeout = last_tx_block + limit;237			if block_number < timeout {238				return None;239			}240		}241242		<SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);243244		Some(sponsor)245	}246}247248generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);249generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);