git.delta.rocks / unique-network / refs/commits / 7a21c0bed6bb

difftreelog

path: Add has_sponsor function to solidity. Fix confirm_sponsorship.

Trubnikov Sergey2022-08-05parent: #f5f392c.patch.diff
in: master

2 files changed

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::{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	fn set_sponsoring_mode(86		&mut self,87		caller: caller,88		contract_address: address,89		mode: uint8,90	) -> Result<void> {91		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;92		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;93		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);94		Ok(())95	}9697	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {98		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())99	}100101	fn set_sponsoring_rate_limit(102		&mut self,103		caller: caller,104		contract_address: address,105		rate_limit: uint32,106	) -> Result<void> {107		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;108		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());109		Ok(())110	}111112	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {113		Ok(<SponsoringRateLimit<T>>::get(contract_address)114			.try_into()115			.map_err(|_| "rate limit > u32::MAX")?)116	}117118	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {119		self.0.consume_sload()?;120		Ok(<Pallet<T>>::allowed(contract_address, user))121	}122123	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {124		Ok(<AllowlistEnabled<T>>::get(contract_address))125	}126127	fn toggle_allowlist(128		&mut self,129		caller: caller,130		contract_address: address,131		enabled: bool,132	) -> Result<void> {133		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;134		<Pallet<T>>::toggle_allowlist(contract_address, enabled);135		Ok(())136	}137138	fn toggle_allowed(139		&mut self,140		caller: caller,141		contract_address: address,142		user: address,143		allowed: bool,144	) -> Result<void> {145		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;146		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);147		Ok(())148	}149}150151pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);152impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T>153where154	T::AccountId: AsRef<[u8]>,155{156	fn is_reserved(contract: &sp_core::H160) -> bool {157		contract == &T::ContractAddress::get()158	}159160	fn is_used(contract: &sp_core::H160) -> bool {161		contract == &T::ContractAddress::get()162	}163164	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {165		// TODO: Extract to another OnMethodCall handler166		if <AllowlistEnabled<T>>::get(handle.code_address())167			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)168		{169			return Some(Err(PrecompileFailure::Revert {170				exit_status: ExitRevert::Reverted,171				output: {172					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));173					writer.string("Target contract is allowlisted");174					writer.finish()175				},176			}));177		}178179		if handle.code_address() != T::ContractAddress::get() {180			return None;181		}182183		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));184		pallet_evm_coder_substrate::call(handle, helpers)185	}186187	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {188		(contract == &T::ContractAddress::get())189			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())190	}191}192193pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);194impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {195	fn on_create(owner: H160, contract: H160) {196		<Owner<T>>::insert(contract, owner);197	}198}199200pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);201impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>202	for HelpersContractSponsoring<T>203{204	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {205		let (contract, _) = call;206		let mode = <Pallet<T>>::sponsoring_mode(*contract);207		if mode == SponsoringModeT::Disabled {208			return None;209		}210211		let sponsor = match <Pallet<T>>::get_sponsor(*contract) {212			Some(sponsor) => sponsor,213			None => return None,214		};215216		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {217			return None;218		}219		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;220221		if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {222			let limit = <SponsoringRateLimit<T>>::get(contract);223224			let timeout = last_tx_block + limit;225			if block_number < timeout {226				return None;227			}228		}229230		<SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);231232		Some(sponsor)233	}234}235236generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);237generate_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 = pallet_common::eth::convert_cross_account_to_eth_uint256::<T>(&sponsor);77		Ok((*sponsor.as_eth(), sponsor_sub))78	}7980	fn has_sponsor(&self, contract_address: address) -> Result<bool> {81		Ok(Pallet::<T>::get_sponsor(contract_address).is_some())82	}8384	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {85		Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)86	}8788	fn set_sponsoring_mode(89		&mut self,90		caller: caller,91		contract_address: address,92		mode: uint8,93	) -> Result<void> {94		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;95		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;96		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);97		Ok(())98	}99100	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {101		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())102	}103104	fn set_sponsoring_rate_limit(105		&mut self,106		caller: caller,107		contract_address: address,108		rate_limit: uint32,109	) -> Result<void> {110		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;111		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());112		Ok(())113	}114115	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {116		Ok(<SponsoringRateLimit<T>>::get(contract_address)117			.try_into()118			.map_err(|_| "rate limit > u32::MAX")?)119	}120121	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {122		self.0.consume_sload()?;123		Ok(<Pallet<T>>::allowed(contract_address, user))124	}125126	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {127		Ok(<AllowlistEnabled<T>>::get(contract_address))128	}129130	fn toggle_allowlist(131		&mut self,132		caller: caller,133		contract_address: address,134		enabled: bool,135	) -> Result<void> {136		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;137		<Pallet<T>>::toggle_allowlist(contract_address, enabled);138		Ok(())139	}140141	fn toggle_allowed(142		&mut self,143		caller: caller,144		contract_address: address,145		user: address,146		allowed: bool,147	) -> Result<void> {148		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;149		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);150		Ok(())151	}152}153154pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);155impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T>156where157	T::AccountId: AsRef<[u8]>,158{159	fn is_reserved(contract: &sp_core::H160) -> bool {160		contract == &T::ContractAddress::get()161	}162163	fn is_used(contract: &sp_core::H160) -> bool {164		contract == &T::ContractAddress::get()165	}166167	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {168		// TODO: Extract to another OnMethodCall handler169		if <AllowlistEnabled<T>>::get(handle.code_address())170			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)171		{172			return Some(Err(PrecompileFailure::Revert {173				exit_status: ExitRevert::Reverted,174				output: {175					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));176					writer.string("Target contract is allowlisted");177					writer.finish()178				},179			}));180		}181182		if handle.code_address() != T::ContractAddress::get() {183			return None;184		}185186		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));187		pallet_evm_coder_substrate::call(handle, helpers)188	}189190	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {191		(contract == &T::ContractAddress::get())192			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())193	}194}195196pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);197impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {198	fn on_create(owner: H160, contract: H160) {199		<Owner<T>>::insert(contract, owner);200	}201}202203pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);204impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>205	for HelpersContractSponsoring<T>206{207	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {208		let (contract, _) = call;209		let mode = <Pallet<T>>::sponsoring_mode(*contract);210		if mode == SponsoringModeT::Disabled {211			return None;212		}213214		let sponsor = match <Pallet<T>>::get_sponsor(*contract) {215			Some(sponsor) => sponsor,216			None => return None,217		};218219		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {220			return None;221		}222		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;223224		if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {225			let limit = <SponsoringRateLimit<T>>::get(contract);226227			let timeout = last_tx_block + limit;228			if block_number < timeout {229				return None;230			}231		}232233		<SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);234235		Some(sponsor)236	}237}238239generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);240generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -168,6 +168,11 @@
 			match Sponsoring::<T>::get(contract) {
 				SponsorshipState::Unconfirmed(sponsor) => {
 					ensure!(sponsor == *sender, Error::<T>::NoPermission);
+					Sponsoring::<T>::mutate_exists(contract, |state| {
+						*state = Some(SponsorshipState::<T::CrossAccountId>::Confirmed(
+							sponsor.clone(),
+						))
+					});
 					Ok(())
 				}
 				SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => {