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

difftreelog

feat(pallet) evm gas metering

Yaroslav Bolyukin2021-06-02parent: #f56d876.patch.diff
in: master

1 file changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
77
8#![cfg_attr(not(feature = "std"), no_std)]8#![cfg_attr(not(feature = "std"), no_std)]
99
10extern crate alloc;
1011
11pub use serde::{Serialize, Deserialize};12pub use serde::{Serialize, Deserialize};
1213
13use core::ops::{Deref, DerefMut};14use core::ops::{Deref, DerefMut};
15use core::cell::RefCell;
14use codec::{Decode, Encode};16use codec::{Decode, Encode};
15pub use frame_support::{17pub use frame_support::{
16 construct_runtime, decl_event, decl_module, decl_storage, decl_error,18 construct_runtime, decl_event, decl_module, decl_storage, decl_error,
30};32};
3133
32use frame_system::{self as system, ensure_signed, ensure_root};34use frame_system::{self as system, ensure_signed, ensure_root};
33use sp_core::{H160, H256};35use sp_core::H160;
34use sp_runtime::sp_std::prelude::Vec;36use sp_runtime::sp_std::prelude::Vec;
35use sp_runtime::{37use sp_runtime::{
36 traits::{38 traits::{
188 pub id: CollectionId,190 pub id: CollectionId,
189 collection: Collection<T>,191 collection: Collection<T>,
190 logs: eth::log::LogRecorder,192 logs: eth::log::LogRecorder,
193 evm_address: H160,
194 gas_limit: RefCell<u64>,
191}195}
192impl<T: Config> CollectionHandle<T> {196impl<T: Config> CollectionHandle<T> {
193 pub fn get(id: CollectionId) -> Option<Self> {197 pub fn get_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {
194 <CollectionById<T>>::get(id)198 <CollectionById<T>>::get(id)
195 .map(|collection| Self {199 .map(|collection| Self {
196 id,200 id,
197 collection,201 collection,
198 logs: eth::log::LogRecorder::default(),202 logs: eth::log::LogRecorder::default(),
203 evm_address: eth::collection_id_to_address(id),
204 gas_limit: RefCell::new(gas_limit),
199 })205 })
200 }206 }
207 pub fn get(id: CollectionId) -> Option<Self> {
208 Self::get_with_gas_limit(id, u64::MAX)
209 }
210 pub fn gas_left(&self) -> u64 {
211 *self.gas_limit.borrow()
212 }
213 pub fn consume_gas(&self, gas: u64) -> DispatchResult {
214 let mut gas_limit = self.gas_limit.borrow_mut();
215 if *gas_limit < gas {
216 fail!(Error::<T>::OutOfGas);
217 }
218 *gas_limit -= gas;
219 Ok(())
220 }
201 pub fn log(&self, log: impl evm_coder::ToLog) {221 pub fn log(&self, log: impl evm_coder::ToLog) {
202 self.logs.log(log.to_log(self.evm_address))222 self.logs.log(log.to_log(self.evm_address))
203 }223 }
473 WrongRefungiblePieces,493 WrongRefungiblePieces,
474 /// createRefungible should be called with one owner494 /// createRefungible should be called with one owner
475 BadCreateRefungibleCall,495 BadCreateRefungibleCall,
496 /// Gas limit exceeded
497 OutOfGas,
476 }498 }
477}499}
478500
1690 }1712 }
16911713
1692 pub fn transfer_internal(sender: &T::CrossAccountId, recipient: &T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {1714 pub fn transfer_internal(sender: &T::CrossAccountId, recipient: &T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {
1715 target_collection.consume_gas(2000000)?;
1693 // Limits check1716 // Limits check
1694 Self::is_correct_transfer(target_collection, &recipient)?;1717 Self::is_correct_transfer(target_collection, &recipient)?;
16951718
1723 item_id: TokenId,1746 item_id: TokenId,
1724 amount: u1281747 amount: u128
1725 ) -> DispatchResult {1748 ) -> DispatchResult {
1749 collection.consume_gas(2000000)?;
1726 Self::token_exists(&collection, item_id)?;1750 Self::token_exists(&collection, item_id)?;
17271751
1728 // Transfer permissions check1752 // Transfer permissions check
1787 item_id: TokenId,1811 item_id: TokenId,
1788 amount: u128,1812 amount: u128,
1789 ) -> DispatchResult {1813 ) -> DispatchResult {
1814 collection.consume_gas(2000000)?;
1790 // Check approval1815 // Check approval
1791 let approval: u128 = <Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));1816 let approval: u128 = <Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));
17921817