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

difftreelog

source

pallets/evm-contract-helpers/src/eth.rs6.3 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::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};19use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};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> {45	fn contract_owner(&self, contract_address: address) -> Result<address> {46		Ok(<Owner<T>>::get(contract_address))47	}4849	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {50		Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)51	}5253	/// Deprecated54	fn toggle_sponsoring(55		&mut self,56		caller: caller,57		contract_address: address,58		enabled: bool,59	) -> Result<void> {60		<Pallet<T>>::ensure_owner(contract_address, caller)?;61		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);62		Ok(())63	}6465	fn set_sponsoring_mode(66		&mut self,67		caller: caller,68		contract_address: address,69		mode: uint8,70	) -> Result<void> {71		<Pallet<T>>::ensure_owner(contract_address, caller)?;72		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;73		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);74		Ok(())75	}7677	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {78		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())79	}8081	fn set_sponsoring_rate_limit(82		&mut self,83		caller: caller,84		contract_address: address,85		rate_limit: uint32,86	) -> Result<void> {87		<Pallet<T>>::ensure_owner(contract_address, caller)?;88		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());89		Ok(())90	}9192	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {93		Ok(<SponsoringRateLimit<T>>::get(contract_address)94			.try_into()95			.map_err(|_| "rate limit > u32::MAX")?)96	}9798	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {99		self.0.consume_sload()?;100		Ok(<Pallet<T>>::allowed(contract_address, user))101	}102103	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {104		Ok(<AllowlistEnabled<T>>::get(contract_address))105	}106107	fn toggle_allowlist(108		&mut self,109		caller: caller,110		contract_address: address,111		enabled: bool,112	) -> Result<void> {113		<Pallet<T>>::ensure_owner(contract_address, caller)?;114		<Pallet<T>>::toggle_allowlist(contract_address, enabled);115		Ok(())116	}117118	fn toggle_allowed(119		&mut self,120		caller: caller,121		contract_address: address,122		user: address,123		allowed: bool,124	) -> Result<void> {125		<Pallet<T>>::ensure_owner(contract_address, caller)?;126		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);127		Ok(())128	}129}130131pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);132impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {133	fn is_reserved(contract: &sp_core::H160) -> bool {134		contract == &T::ContractAddress::get()135	}136137	fn is_used(contract: &sp_core::H160) -> bool {138		contract == &T::ContractAddress::get()139	}140141	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {142		// TODO: Extract to another OnMethodCall handler143		if <AllowlistEnabled<T>>::get(handle.code_address())144			&& !<Pallet<T>>::allowed(handle.code_address(), handle.context().caller)145		{146			return Some(Err(PrecompileFailure::Revert {147				exit_status: ExitRevert::Reverted,148				output: {149					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));150					writer.string("Target contract is allowlisted");151					writer.finish()152				},153			}));154		}155156		if handle.code_address() != T::ContractAddress::get() {157			return None;158		}159160		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));161		pallet_evm_coder_substrate::call(handle, helpers)162	}163164	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {165		(contract == &T::ContractAddress::get())166			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())167	}168}169170pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);171impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {172	fn on_create(owner: H160, contract: H160) {173		<Owner<T>>::insert(contract, owner);174	}175}176177pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);178impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>179	for HelpersContractSponsoring<T>180{181	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {182		let mode = <Pallet<T>>::sponsoring_mode(call.0);183		if mode == SponsoringModeT::Disabled {184			return None;185		}186187		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who.as_eth()) {188			return None;189		}190		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;191192		if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who.as_eth()) {193			let limit = <SponsoringRateLimit<T>>::get(&call.0);194195			let timeout = last_tx_block + limit;196			if block_number < timeout {197				return None;198			}199		}200201		<SponsorBasket<T>>::insert(&call.0, who.as_eth(), block_number);202203		let sponsor = T::CrossAccountId::from_eth(call.0);204		Some(sponsor)205	}206}207208generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);209generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);