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

difftreelog

feat(contract-helpers) generous sponsoring mode

Yaroslav Bolyukin2022-01-19parent: #fcf0631.patch.diff
in: master

7 files changed

modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -95,6 +95,10 @@
 		string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))
 	}
 
+	pub fn uint8(&mut self) -> Result<u8> {
+		Ok(self.read_padleft::<1>()?[0])
+	}
+
 	pub fn uint32(&mut self) -> Result<u32> {
 		Ok(u32::from_be_bytes(self.read_padleft()?))
 	}
@@ -243,6 +247,7 @@
 	};
 }
 
+impl_abi_readable!(u8, uint8);
 impl_abi_readable!(u32, uint32);
 impl_abi_readable!(u64, uint64);
 impl_abi_readable!(u128, uint128);
deletedpallets/evm-contract-helpers/exp.rsdiffbeforeafterboth

no changes

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -4,7 +4,8 @@
 use pallet_evm::{ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure};
 use sp_core::H160;
 use crate::{
-	AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,
+	AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsoringMode, SponsorBasket,
+	SponsoringRateLimit, SponsoringModeT,
 };
 use frame_support::traits::Get;
 use up_sponsorship::SponsorshipHandler;
@@ -31,6 +32,7 @@
 		Ok(<SelfSponsoring<T>>::get(contract_address))
 	}
 
+	/// Deprecated
 	fn toggle_sponsoring(
 		&mut self,
 		caller: caller,
@@ -42,6 +44,22 @@
 		Ok(())
 	}
 
+	fn set_sponsoring_mode(
+		&mut self,
+		caller: caller,
+		contract_address: address,
+		mode: uint8,
+	) -> Result<void> {
+		<Pallet<T>>::ensure_owner(contract_address, caller)?;
+		let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;
+		<Pallet<T>>::set_sponsoring_mode(contract_address, mode);
+		Ok(())
+	}
+
+	fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {
+		Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())
+	}
+
 	fn set_sponsoring_rate_limit(
 		&mut self,
 		caller: caller,
@@ -147,10 +165,11 @@
 pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);
 impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {
 	fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {
-		if !<SelfSponsoring<T>>::get(&call.0) {
+		let mode = <Pallet<T>>::sponsoring_mode(call.0);
+		if mode == SponsoringModeT::Disabled {
 			return None;
 		}
-		if !<Pallet<T>>::allowed(call.0, *who) {
+		if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who) {
 			return None;
 		}
 		let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -1,11 +1,14 @@
 #![cfg_attr(not(feature = "std"), no_std)]
 
+use codec::{Decode, Encode};
 pub use pallet::*;
 pub use eth::*;
+use scale_info::TypeInfo;
 pub mod eth;
 
 #[frame_support::pallet]
 pub mod pallet {
+	pub use super::*;
 	use evm_coder::execution::Result;
 	use frame_support::pallet_prelude::*;
 	use sp_core::H160;
@@ -31,10 +34,15 @@
 		StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;
 
 	#[pallet::storage]
+	#[deprecated]
 	pub(super) type SelfSponsoring<T: Config> =
 		StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
 
 	#[pallet::storage]
+	pub(super) type SponsoringMode<T: Config> =
+		StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;
+
+	#[pallet::storage]
 	pub(super) type SponsoringRateLimit<T: Config> = StorageMap<
 		Hasher = Twox128,
 		Key = H160,
@@ -68,8 +76,31 @@
 	>;
 
 	impl<T: Config> Pallet<T> {
+		pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {
+			<SponsoringMode<T>>::get(contract)
+				.or_else(|| {
+					<SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)
+				})
+				.unwrap_or_default()
+		}
+		pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {
+			if mode == SponsoringModeT::Disabled {
+				<SponsoringMode<T>>::remove(contract);
+			} else {
+				<SponsoringMode<T>>::insert(contract, mode);
+			}
+			<SelfSponsoring<T>>::remove(contract)
+		}
+
 		pub fn toggle_sponsoring(contract: H160, enabled: bool) {
-			<SelfSponsoring<T>>::insert(contract, enabled);
+			Self::set_sponsoring_mode(
+				contract,
+				if enabled {
+					SponsoringModeT::Allowlisted
+				} else {
+					SponsoringModeT::Disabled
+				},
+			)
 		}
 
 		pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {
@@ -94,3 +125,34 @@
 		}
 	}
 }
+
+#[derive(Encode, Decode, PartialEq, TypeInfo)]
+pub enum SponsoringModeT {
+	Disabled,
+	Allowlisted,
+	Generous,
+}
+
+impl SponsoringModeT {
+	fn from_eth(v: u8) -> Option<Self> {
+		Some(match v {
+			0 => Self::Disabled,
+			1 => Self::Allowlisted,
+			2 => Self::Generous,
+			_ => return None,
+		})
+	}
+	fn to_eth(self) -> u8 {
+		match self {
+			SponsoringModeT::Disabled => 0,
+			SponsoringModeT::Allowlisted => 1,
+			SponsoringModeT::Generous => 2,
+		}
+	}
+}
+
+impl Default for SponsoringModeT {
+	fn default() -> Self {
+		Self::Disabled
+	}
+}
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
@@ -21,7 +21,7 @@
 	}
 }
 
-// Selector: 31acb1fe
+// Selector: 7b4866f9
 contract ContractHelpers is Dummy, ERC165 {
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
@@ -47,6 +47,8 @@
 		return false;
 	}
 
+	// Deprecated
+	//
 	// Selector: toggleSponsoring(address,bool) fcac6d86
 	function toggleSponsoring(address contractAddress, bool enabled) public {
 		require(false, stub_error);
@@ -55,6 +57,26 @@
 		dummy = 0;
 	}
 
+	// Selector: setSponsoringMode(address,uint8) fde8a560
+	function setSponsoringMode(address contractAddress, uint8 mode) public {
+		require(false, stub_error);
+		contractAddress;
+		mode;
+		dummy = 0;
+	}
+
+	// Selector: sponsoringMode(address) b70c7267
+	function sponsoringMode(address contractAddress)
+		public
+		view
+		returns (uint8)
+	{
+		require(false, stub_error);
+		contractAddress;
+		dummy;
+		return 0;
+	}
+
 	// Selector: setSponsoringRateLimit(address,uint32) 77b6c908
 	function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
 		public
modifiedtests/src/eth/api/ContractHelpers.soldiffbeforeafterboth
--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -12,7 +12,7 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
-// Selector: 31acb1fe
+// Selector: 7b4866f9
 interface ContractHelpers is Dummy, ERC165 {
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
@@ -26,9 +26,20 @@
 		view
 		returns (bool);
 
+	// Deprecated
+	//
 	// Selector: toggleSponsoring(address,bool) fcac6d86
 	function toggleSponsoring(address contractAddress, bool enabled) external;
 
+	// Selector: setSponsoringMode(address,uint8) fde8a560
+	function setSponsoringMode(address contractAddress, uint8 mode) external;
+
+	// Selector: sponsoringMode(address) b70c7267
+	function sponsoringMode(address contractAddress)
+		external
+		view
+		returns (uint8);
+
 	// Selector: setSponsoringRateLimit(address,uint32) 77b6c908
 	function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
 		external;