git.delta.rocks / unique-network / refs/commits / 82ca9c1be6da

difftreelog

Merge pull request #25 from usetech-llc/feature/NFTPAR-110_contract_spam

str-mv2020-12-02parents: #61a5f4d #33c31e8.patch.diff
in: master
Feature/nftpar 110 contract spam protection

4 files changed

modifiedCargo.lockdiffbeforeafterboth
3737 "frame-support",3737 "frame-support",
3738 "frame-system",3738 "frame-system",
3739 "log",3739 "log",
3740 "pallet-balances",
3740 "pallet-contracts",3741 "pallet-contracts",
3742 "pallet-randomness-collective-flip",
3743 "pallet-timestamp",
3741 "pallet-transaction-payment",3744 "pallet-transaction-payment",
3742 "parity-scale-codec",3745 "parity-scale-codec",
3743 "serde",3746 "serde",
modifiedpallets/nft/src/default_weights.rsdiffbeforeafterboth
--- a/pallets/nft/src/default_weights.rs
+++ b/pallets/nft/src/default_weights.rs
@@ -107,9 +107,19 @@
             .saturating_add(DbWeight::get().reads(2 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
+    // fn set_chain_limits() -> Weight {
+    //     (0 as Weight)
+    //         .saturating_add(DbWeight::get().reads(1 as Weight))
+    //         .saturating_add(DbWeight::get().writes(1 as Weight))
+    // }
     // fn enable_contract_sponsoring() -> Weight {
     //     (0 as Weight)
     //         .saturating_add(DbWeight::get().reads(1 as Weight))
     //         .saturating_add(DbWeight::get().writes(1 as Weight))
     // }
+    // fn set_contract_sponsoring_rate_limit() -> Weight {
+    //     (0 as Weight)
+    //         .saturating_add(DbWeight::get().reads(1 as Weight))
+    //         .saturating_add(DbWeight::get().writes(1 as Weight))
+    // }
 }
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -366,8 +366,10 @@
         pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => T::BlockNumber;
 
         // Contract Sponsorship and Ownership
-        pub ContractOwner get(fn contract_owner): map hasher(identity) T::AccountId => T::AccountId;
-        pub ContractSelfSponsoring get(fn contract_self_sponsoring): map hasher(identity) T::AccountId => bool;
+        pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;
+        pub ContractSelfSponsoring get(fn contract_self_sponsoring): map hasher(twox_64_concat) T::AccountId => bool;
+        pub ContractSponsorBasket get(fn contract_sponsor_basket): map hasher(twox_64_concat) T::AccountId => T::BlockNumber;
+        pub ContractSponsoringRateLimit get(fn contract_sponsoring_rate_limit): map hasher(twox_64_concat) T::AccountId => T::BlockNumber;
     }
     add_extra_genesis {
         build(|config: &GenesisConfig<T>| {
@@ -1323,6 +1325,41 @@
             Ok(())
         }
 
+        /// Set the rate limit for contract sponsoring to specified number of blocks.
+        /// 
+        /// If not set (has the default value of 0 blocks), the sponsoring will be disabled. 
+        /// If set to the number B (for blocks), the transactions will be sponsored with a rate 
+        /// limit of B, i.e. fees for every transaction sent to this smart contract will be paid 
+        /// from contract endowment if there are at least B blocks between such transactions. 
+        /// Nonetheless, if transactions are sent more frequently, the fees are paid by the sender.
+        /// 
+        /// # Permissions
+        /// 
+        /// * Contract Owner
+        /// 
+        /// # Arguments
+        /// 
+        /// -`contract_address`: Address of the contract to sponsor
+        /// -`rate_limit`: Number of blocks to wait until the next sponsored transaction is allowed
+        /// 
+        #[weight = 0]
+        pub fn set_contract_sponsoring_rate_limit(
+            origin,
+            contract_address: T::AccountId,
+            rate_limit: T::BlockNumber
+        ) -> DispatchResult {
+            let sender = ensure_signed(origin)?;
+            let mut is_owner = false;
+            if <ContractOwner<T>>::contains_key(contract_address.clone()) {
+                let owner = <ContractOwner<T>>::get(&contract_address);
+                is_owner = sender == owner;
+            }
+            ensure!(is_owner, Error::<T>::NoPermission);
+
+            <ContractSponsoringRateLimit<T>>::insert(contract_address, rate_limit);
+            Ok(())
+        }
+
     }
 }
 
@@ -2230,11 +2267,30 @@
             // When the contract is called, check if the sponsoring is enabled and pay fees from contract endowment if it is
             Some(pallet_contracts::Call::call(dest, _value, _gas_limit, _data)) => {
 
+                let called_contract: T::AccountId = T::Lookup::lookup((*dest).clone()).unwrap_or(T::AccountId::default());
+
+                let mut sponsor_transfer = false;
+                if <ContractSponsoringRateLimit<T>>::contains_key(called_contract.clone()) {
+                    let last_tx_block = <ContractSponsorBasket<T>>::get(&called_contract);
+                    let block_number = <system::Module<T>>::block_number() as T::BlockNumber;
+                    let rate_limit = <ContractSponsoringRateLimit<T>>::get(&called_contract);
+                    let limit_time = last_tx_block + rate_limit;
+
+                    if block_number >= limit_time {
+                        <ContractSponsorBasket<T>>::insert(called_contract.clone(), block_number);
+                        sponsor_transfer = true;
+                    }
+                } else {
+                    sponsor_transfer = false;
+                }
+               
+                
                 let mut sp = T::AccountId::default();
-                let called_contract: T::AccountId = T::Lookup::lookup((*dest).clone()).unwrap_or(T::AccountId::default());
-                if <ContractSelfSponsoring<T>>::contains_key(called_contract.clone()) {
-                    if <ContractSelfSponsoring<T>>::get(called_contract.clone()) {
-                        sp = called_contract;
+                if sponsor_transfer {
+                    if <ContractSelfSponsoring<T>>::contains_key(called_contract.clone()) {
+                        if <ContractSelfSponsoring<T>>::get(called_contract.clone()) {
+                            sp = called_contract;
+                        }
                     }
                 }
 
modifiedruntime/src/nft_weights.rsdiffbeforeafterboth
--- a/runtime/src/nft_weights.rs
+++ b/runtime/src/nft_weights.rs
@@ -108,4 +108,19 @@
             .saturating_add(DbWeight::get().reads(2 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
+    // fn set_chain_limits() -> Weight {
+    //     (0 as Weight)
+    //         .saturating_add(DbWeight::get().reads(1 as Weight))
+    //         .saturating_add(DbWeight::get().writes(1 as Weight))
+    // }
+    // fn enable_contract_sponsoring() -> Weight {
+    //     (0 as Weight)
+    //         .saturating_add(DbWeight::get().reads(1 as Weight))
+    //         .saturating_add(DbWeight::get().writes(1 as Weight))
+    // }
+    // fn set_contract_sponsoring_rate_limit() -> Weight {
+    //     (0 as Weight)
+    //         .saturating_add(DbWeight::get().reads(1 as Weight))
+    //         .saturating_add(DbWeight::get().writes(1 as Weight))
+    // }
 }