git.delta.rocks / unique-network / refs/commits / 294c410fd86d

difftreelog

fix Add weights to common collection eth methods.

Trubnikov Sergey2022-09-08parent: #bee4854.patch.diff
in: master

2 files changed

modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -78,6 +78,8 @@
 		key: string,
 		value: bytes,
 	) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let key = <Vec<u8>>::from(key)
 			.try_into()
@@ -92,6 +94,8 @@
 	///
 	/// @param key Property key.
 	fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let key = <Vec<u8>>::from(key)
 			.try_into()
@@ -123,6 +127,8 @@
 	///
 	/// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.
 	fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 
 		let sponsor = T::CrossAccountId::from_eth(sponsor);
@@ -141,6 +147,8 @@
 		caller: caller,
 		sponsor: uint256,
 	) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 
 		let sponsor = convert_uint256_to_cross_account::<T>(sponsor);
@@ -161,6 +169,8 @@
 	///
 	/// @dev After setting the sponsor for the collection, it must be confirmed with this function.
 	fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		if !self
 			.confirm_sponsorship(caller.as_sub())
@@ -173,6 +183,7 @@
 
 	/// Remove collection sponsor.
 	fn remove_collection_sponsor(&mut self, caller: caller) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
 		check_is_owner_or_admin(caller, self)?;
 		self.remove_sponsor().map_err(dispatch_to_evm::<T>)?;
 		save(self)
@@ -209,6 +220,8 @@
 	/// @param value Value of the limit.
 	#[solidity(rename_selector = "setCollectionLimit")]
 	fn set_int_limit(&mut self, caller: caller, limit: string, value: uint32) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 		let mut limits = self.limits.clone();
 
@@ -252,6 +265,8 @@
 	/// @param value Value of the limit.
 	#[solidity(rename_selector = "setCollectionLimit")]
 	fn set_bool_limit(&mut self, caller: caller, limit: string, value: bool) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 		let mut limits = self.limits.clone();
 
@@ -289,6 +304,8 @@
 		caller: caller,
 		new_admin: uint256,
 	) -> Result<void> {
+		self.consume_store_writes(2)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let new_admin = convert_uint256_to_cross_account::<T>(new_admin);
 		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;
@@ -302,6 +319,8 @@
 		caller: caller,
 		admin: uint256,
 	) -> Result<void> {
+		self.consume_store_writes(2)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let admin = convert_uint256_to_cross_account::<T>(admin);
 		<Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;
@@ -311,6 +330,8 @@
 	/// Add collection admin.
 	/// @param newAdmin Address of the added administrator.
 	fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<void> {
+		self.consume_store_writes(2)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let new_admin = T::CrossAccountId::from_eth(new_admin);
 		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;
@@ -321,6 +342,8 @@
 	///
 	/// @param admin Address of the removed administrator.
 	fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<void> {
+		self.consume_store_writes(2)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let admin = T::CrossAccountId::from_eth(admin);
 		<Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;
@@ -332,6 +355,8 @@
 	/// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'
 	#[solidity(rename_selector = "setCollectionNesting")]
 	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 
 		let mut permissions = self.collection.permissions.clone();
@@ -361,6 +386,8 @@
 		enable: bool,
 		collections: Vec<address>,
 	) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		if collections.is_empty() {
 			return Err("no addresses provided".into());
 		}
@@ -404,6 +431,8 @@
 	/// 	0 for Normal
 	/// 	1 for AllowList
 	fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 		let permissions = CollectionPermissions {
 			access: Some(match mode {
@@ -437,6 +466,8 @@
 	///
 	/// @param user Address of a trusted user.
 	fn add_to_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let user = T::CrossAccountId::from_eth(user);
 		<Pallet<T>>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;
@@ -451,6 +482,8 @@
 		caller: caller,
 		user: uint256,
 	) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let user = convert_uint256_to_cross_account::<T>(user);
 		Pallet::<T>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;
@@ -461,6 +494,8 @@
 	///
 	/// @param user Address of a removed user.
 	fn remove_from_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let user = T::CrossAccountId::from_eth(user);
 		<Pallet<T>>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;
@@ -475,6 +510,8 @@
 		caller: caller,
 		user: uint256,
 	) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let user = convert_uint256_to_cross_account::<T>(user);
 		Pallet::<T>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;
@@ -485,6 +522,8 @@
 	///
 	/// @param mode Enable if "true".
 	fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {
+		self.consume_store_reads_and_writes(1, 1)?;
+
 		check_is_owner_or_admin(caller, self)?;
 		let permissions = CollectionPermissions {
 			mint_mode: Some(mode),
@@ -546,6 +585,8 @@
 	/// @dev Owner can be changed only by current owner
 	/// @param newOwner new owner account
 	fn set_owner(&mut self, caller: caller, new_owner: address) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let new_owner = T::CrossAccountId::from_eth(new_owner);
 		self.set_owner_internal(caller, new_owner)
@@ -557,6 +598,8 @@
 	/// @dev Owner can be changed only by current owner
 	/// @param newOwner new owner substrate account
 	fn set_owner_substrate(&mut self, caller: caller, new_owner: uint256) -> Result<void> {
+		self.consume_store_writes(1)?;
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let new_owner = convert_uint256_to_cross_account::<T>(new_owner);
 		self.set_owner_internal(caller, new_owner)
@@ -572,6 +615,8 @@
 	// }
 }
 
+/// ### Note
+/// Do not forget to add: `self.consume_store_reads(1)?;`
 fn check_is_owner_or_admin<T: Config>(
 	caller: caller,
 	collection: &CollectionHandle<T>,
@@ -583,9 +628,9 @@
 	Ok(caller)
 }
 
+/// ### Note
+/// Do not forget to add: `self.consume_store_writes(1)?;`
 fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {
-	// TODO possibly delete for the lack of transaction
-	collection.consume_store_writes(1)?;
 	collection
 		.check_is_internal()
 		.map_err(dispatch_to_evm::<T>)?;
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
112 RmrkBoundedTheme,112 RmrkBoundedTheme,
113 RmrkNftChild,113 RmrkNftChild,
114 CollectionPermissions,114 CollectionPermissions,
115 SchemaVersion,
116};115};
117116
118pub use pallet::*;117pub use pallet::*;
202 ))201 ))
203 }202 }
203
204 /// Consume gas for reading and writing.
205 pub fn consume_store_reads_and_writes(
206 &self,
207 reads: u64,
208 writes: u64,
209 ) -> evm_coder::execution::Result<()> {
210 let weight = <T as frame_system::Config>::DbWeight::get();
211 let reads = weight.read.saturating_mul(reads);
212 let writes = weight.read.saturating_mul(writes);
213 self.recorder
214 .consume_gas(T::GasWeightMapping::weight_to_gas(
215 reads.saturating_add(writes),
216 ))
217 }
204218
205 /// Save collection to storage.219 /// Save collection to storage.
206 pub fn save(&self) -> DispatchResult {220 pub fn save(&self) -> DispatchResult {
310 }324 }
311325
312 /// Changes collection owner to another account326 /// Changes collection owner to another account
327 /// #### Store read/writes
328 /// 1 writes
313 fn set_owner_internal(329 fn set_owner_internal(
314 &mut self,330 &mut self,
315 caller: T::CrossAccountId,331 caller: T::CrossAccountId,
1292 }1308 }
12931309
1294 /// Toggle `user` participation in the `collection`'s allow list.1310 /// Toggle `user` participation in the `collection`'s allow list.
1311 /// #### Store read/writes
1312 /// 1 writes
1295 pub fn toggle_allowlist(1313 pub fn toggle_allowlist(
1296 collection: &CollectionHandle<T>,1314 collection: &CollectionHandle<T>,
1297 sender: &T::CrossAccountId,1315 sender: &T::CrossAccountId,
1312 }1330 }
13131331
1314 /// Toggle `user` participation in the `collection`'s admin list.1332 /// Toggle `user` participation in the `collection`'s admin list.
1333 /// #### Store read/writes
1334 /// 2 writes
1315 pub fn toggle_admin(1335 pub fn toggle_admin(
1316 collection: &CollectionHandle<T>,1336 collection: &CollectionHandle<T>,
1317 sender: &T::CrossAccountId,1337 sender: &T::CrossAccountId,