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

difftreelog

source

pallets/evm-contract-helpers/src/eth.rs6.6 KiBsourcehistory
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 sponsoring_enabled(&self, contract_address: address) -> Result<bool> {55		Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)56	}5758	/// Deprecated59	fn toggle_sponsoring(60		&mut self,61		caller: caller,62		contract_address: address,63		enabled: bool,64	) -> Result<void> {65		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;66		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);67		Ok(())68	}6970	fn set_sponsoring_mode(71		&mut self,72		caller: caller,73		contract_address: address,74		mode: uint8,75	) -> Result<void> {76		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;77		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;78		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);79		Ok(())80	}8182	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {83		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())84	}8586	fn set_sponsoring_rate_limit(87		&mut self,88		caller: caller,89		contract_address: address,90		rate_limit: uint32,91	) -> Result<void> {92		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;93		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());94		Ok(())95	}9697	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {98		Ok(<SponsoringRateLimit<T>>::get(contract_address)99			.try_into()100			.map_err(|_| "rate limit > u32::MAX")?)101	}102103	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {104		self.0.consume_sload()?;105		Ok(<Pallet<T>>::allowed(contract_address, user))106	}107108	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {109		Ok(<AllowlistEnabled<T>>::get(contract_address))110	}111112	fn toggle_allowlist(113		&mut self,114		caller: caller,115		contract_address: address,116		enabled: bool,117	) -> Result<void> {118		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;119		<Pallet<T>>::toggle_allowlist(contract_address, enabled);120		Ok(())121	}122123	fn toggle_allowed(124		&mut self,125		caller: caller,126		contract_address: address,127		user: address,128		allowed: bool,129	) -> Result<void> {130		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;131		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);132		Ok(())133	}134}135136pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);137impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {138	fn is_reserved(contract: &sp_core::H160) -> bool {139		contract == &T::ContractAddress::get()140	}141142	fn is_used(contract: &sp_core::H160) -> bool {143		contract == &T::ContractAddress::get()144	}145146	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {147		// TODO: Extract to another OnMethodCall handler148		if <AllowlistEnabled<T>>::get(handle.code_address())149			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)150		{151			return Some(Err(PrecompileFailure::Revert {152				exit_status: ExitRevert::Reverted,153				output: {154					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));155					writer.string("Target contract is allowlisted");156					writer.finish()157				},158			}));159		}160161		if handle.code_address() != T::ContractAddress::get() {162			return None;163		}164165		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));166		pallet_evm_coder_substrate::call(handle, helpers)167	}168169	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {170		(contract == &T::ContractAddress::get())171			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())172	}173}174175pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);176impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {177	fn on_create(owner: H160, contract: H160) {178		<Owner<T>>::insert(contract, owner);179	}180}181182pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);183impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>184	for HelpersContractSponsoring<T>185{186	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {187		let (contract, _) = call;188		let mode = <Pallet<T>>::sponsoring_mode(*contract);189		if mode == SponsoringModeT::Disabled {190			return None;191		}192193		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {194			return None;195		}196		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;197198		if let Some(last_tx_block) = <SponsorBasket<T>>::get(contract, who.as_eth()) {199			let limit = <SponsoringRateLimit<T>>::get(contract);200201			let timeout = last_tx_block + limit;202			if block_number < timeout {203				return None;204			}205		}206207		<SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);208209		let sponsor = T::CrossAccountId::from_eth(*contract);210		Some(sponsor)211	}212}213214generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);215generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);