git.delta.rocks / unique-network / refs/commits / 44bd99aa7cbe

difftreelog

Merge remote-tracking branch 'origin/develop' into feature/CORE-164

str-mv2021-09-06parents: #82ad2e1 #abfdac1.patch.diff
in: master

5 files changed

modifiedMakefilediffbeforeafterboth
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,10 @@
-.PHONY: _eth_codegen
-_eth_codegen:
+.PHONY: _help
+_help:
+	@echo "regenerate_solidity - generate stubs/interfaces for contracts defined in native (via evm-coder)"
+	@echo "evm_stubs - recompile contract stubs"
+	@echo "bench - run frame-benchmarking"
+	@echo "  bench-evm-migration"
+	@echo "  bench-nft"
 
 .PHONY: regenerate_solidity
 regenerate_solidity:
modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
before · pallets/evm-contract-helpers/src/eth.rs
1use core::marker::PhantomData;2use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};3use pallet_evm_coder_substrate::SubstrateRecorder;4use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};5use sp_core::H160;6use crate::{7	AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,8};9use frame_support::traits::Get;10use up_sponsorship::SponsorshipHandler;11use sp_std::vec::Vec;1213struct ContractHelpers<T: Config>(SubstrateRecorder<T>);1415#[solidity_interface(name = "ContractHelpers")]16impl<T: Config> ContractHelpers<T> {17	fn contract_owner(&self, contract_address: address) -> Result<address> {18		self.0.consume_sload()?;19		Ok(<Owner<T>>::get(contract_address))20	}2122	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {23		self.0.consume_sload()?;24		Ok(<SelfSponsoring<T>>::get(contract_address))25	}2627	fn toggle_sponsoring(28		&mut self,29		caller: caller,30		contract_address: address,31		enabled: bool,32	) -> Result<void> {33		self.0.consume_sload()?;34		<Pallet<T>>::ensure_owner(contract_address, caller)?;35		self.0.consume_sstore()?;36		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);37		Ok(())38	}3940	fn set_sponsoring_rate_limit(41		&mut self,42		caller: caller,43		contract_address: address,44		rate_limit: uint32,45	) -> Result<void> {46		self.0.consume_sload()?;47		<Pallet<T>>::ensure_owner(contract_address, caller)?;48		self.0.consume_sstore()?;49		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());50		Ok(())51	}5253	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {54		self.0.consume_sload()?;55		Ok(<Pallet<T>>::allowed(contract_address, user, true))56	}5758	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {59		self.0.consume_sload()?;60		Ok(<AllowlistEnabled<T>>::get(contract_address))61	}6263	fn toggle_allowlist(64		&mut self,65		caller: caller,66		contract_address: address,67		enabled: bool,68	) -> Result<void> {69		self.0.consume_sload()?;70		<Pallet<T>>::ensure_owner(contract_address, caller)?;71		self.0.consume_sstore()?;72		<Pallet<T>>::toggle_allowlist(contract_address, enabled);73		Ok(())74	}7576	fn toggle_allowed(77		&mut self,78		caller: caller,79		contract_address: address,80		user: address,81		allowed: bool,82	) -> Result<void> {83		self.0.consume_sload()?;84		<Pallet<T>>::ensure_owner(contract_address, caller)?;85		self.0.consume_sstore()?;86		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);87		Ok(())88	}89}9091pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);92impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {93	fn is_reserved(contract: &sp_core::H160) -> bool {94		contract == &T::ContractAddress::get()95	}9697	fn is_used(contract: &sp_core::H160) -> bool {98		contract == &T::ContractAddress::get()99	}100101	fn call(102		source: &sp_core::H160,103		target: &sp_core::H160,104		gas_left: u64,105		input: &[u8],106		value: sp_core::U256,107	) -> Option<PrecompileOutput> {108		// TODO: Extract to another OnMethodCall handler109		if !<Pallet<T>>::allowed(*target, *source, true) {110			return Some(PrecompileOutput {111				exit_status: ExitReason::Revert(ExitRevert::Reverted),112				cost: 0,113				output: {114					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));115					writer.string("Target contract is allowlisted");116					writer.finish()117				},118				logs: sp_std::vec![],119			});120		}121122		if target != &T::ContractAddress::get() {123			return None;124		}125126		let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));127		let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);128		helpers.0.evm_to_precompile_output(result)129	}130131	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {132		(contract == &T::ContractAddress::get())133			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())134	}135}136137pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);138impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {139	fn on_create(owner: H160, contract: H160) {140		<Owner<T>>::insert(contract, owner);141	}142}143144pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);145impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {146	fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {147		if <SelfSponsoring<T>>::get(&call.0) && <Pallet<T>>::allowed(call.0, *who, false) {148			let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;149			if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {150				let rate_limit = <SponsoringRateLimit<T>>::get(&call.0);151				let limit_time = last_tx_block + rate_limit;152153				if block_number > limit_time {154					<SponsorBasket<T>>::insert(&call.0, who, block_number);155					return Some(call.0);156				}157			} else {158				<SponsorBasket<T>>::insert(&call.0, who, block_number);159				return Some(call.0);160			}161		}162		None163	}164}165166generate_stubgen!(contract_helpers_impl, ContractHelpersCall, true);167generate_stubgen!(contract_helpers_iface, ContractHelpersCall, false);
modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -52,6 +52,18 @@
 		dummy = 0;
 	}
 
+	// Selector: getSponsoringRateLimit(address) 610cfabd
+	function getSponsoringRateLimit(address contractAddress)
+		public
+		view
+		returns (uint32)
+	{
+		require(false, stub_error);
+		contractAddress;
+		dummy;
+		return 0;
+	}
+
 	// Selector: allowed(address,address) 5c658165
 	function allowed(address contractAddress, address user)
 		public
modifiedtests/src/eth/api/ContractHelpers.soldiffbeforeafterboth
--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -28,6 +28,12 @@
 	function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
 		external;
 
+	// Selector: getSponsoringRateLimit(address) 610cfabd
+	function getSponsoringRateLimit(address contractAddress)
+		external
+		view
+		returns (uint32);
+
 	// Selector: allowed(address,address) 5c658165
 	function allowed(address contractAddress, address user)
 		external