Browse Source

proposal: move funding request extrinsic to council

conectado 4 years ago
parent
commit
6a075cd15d

+ 29 - 2
runtime-modules/council/src/lib.rs

@@ -371,6 +371,9 @@ decl_event! {
 
         /// Councilor reward has been updated.
         CouncilorRewardUpdated(Balance),
+
+        /// Request has been funded
+        RequestFunded(AccountId, Balance),
     }
 }
 
@@ -423,6 +426,9 @@ decl_error! {
 
         /// The member is not a councilor.
         NotCouncilor,
+
+        /// Insufficent funds in council for executing 'Funding Request'
+        InsufficientFundsForFundingRequest,
     }
 }
 
@@ -670,7 +676,7 @@ decl_module! {
             Ok(())
         }
 
-        #[weight = 10_000_000]
+        #[weight = 10_000_000] // TODO: Adjust weight
         pub fn set_budget_increment(origin, budget_increment: Balance::<T>) -> Result<(), Error<T>> {
             // ensure action can be started
             EnsureChecks::<T>::can_set_budget_increment(origin)?;
@@ -689,7 +695,7 @@ decl_module! {
             Ok(())
         }
 
-        #[weight = 10_000_000]
+        #[weight = 10_000_000] // TODO: Adjust weight
         pub fn set_councilor_reward(origin, councilor_reward: Balance::<T>) -> Result<(), Error<T>> {
             // ensure action can be started
             EnsureChecks::<T>::can_set_councilor_reward(origin)?;
@@ -707,6 +713,27 @@ decl_module! {
 
             Ok(())
         }
+
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn funding_request(
+            origin,
+            amount: Balance::<T>,
+            account: T::AccountId,
+        ) {
+            // Checks
+            ensure_root(origin)?;
+            let current_budget = Self::budget();
+            ensure!(amount<=current_budget, Error::<T>::InsufficientFundsForFundingRequest);
+
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            Mutations::<T>::set_budget(&(current_budget - amount));
+            let  _ = balances::Module::<T>::deposit_creating(&account, amount);
+            Self::deposit_event(RawEvent::RequestFunded(account, amount));
+        }
     }
 }
 

+ 37 - 0
runtime-modules/council/src/mock.rs

@@ -1069,6 +1069,43 @@ where
         );
     }
 
+    pub fn funding_request(
+        origin: OriginType<T::AccountId>,
+        amount: Balance<T>,
+        reciever: T::AccountId,
+        expected_result: Result<(), Error<T>>,
+    ) {
+        let initial_budget = Module::<T>::budget();
+        // check method returns expected result
+        assert_eq!(
+            Module::<T>::funding_request(
+                InstanceMockUtils::<T>::mock_origin(origin),
+                amount,
+                reciever.clone(),
+            )
+            .is_ok(),
+            expected_result.is_ok(),
+        );
+
+        if expected_result.is_err() {
+            return;
+        }
+
+        assert_eq!(
+            frame_system::Module::<Runtime>::events()
+                .last()
+                .unwrap()
+                .event,
+            TestEvent::event_mod(RawEvent::RequestFunded(
+                reciever.clone().into(),
+                amount.into()
+            )),
+        );
+
+        assert_eq!(Module::<T>::budget(), initial_budget - amount);
+        assert_eq!(balances::Module::<T>::free_balance(reciever), amount);
+    }
+
     pub fn plan_budget_refill(
         origin: OriginType<T::AccountId>,
         next_refill: T::BlockNumber,

+ 35 - 0
runtime-modules/council/src/tests.rs

@@ -1477,6 +1477,17 @@ fn council_origin_validator_fails_with_unregistered_member() {
     });
 }
 
+#[test]
+fn test_funding_request_fails_insufficient_fundings() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let origin = OriginType::Root;
+        Mocks::set_budget(origin.clone(), 0, Ok(()));
+        Mocks::funding_request(origin, 1, 0, Err(Error::InsufficientFundsForFundingRequest));
+    });
+}
+
 #[test]
 fn council_origin_validator_succeeds() {
     let config = default_genesis_config();
@@ -1505,6 +1516,16 @@ fn council_origin_validator_succeeds() {
     });
 }
 
+#[test]
+fn test_funding_request_fails_permission() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let origin = OriginType::Signed(0);
+        Mocks::funding_request(origin.into(), 1, 0, Err(Error::BadOrigin));
+    });
+}
+
 #[test]
 fn council_origin_validator_fails_with_not_councilor() {
     let config = default_genesis_config();
@@ -1587,3 +1608,17 @@ fn council_many_cycle_rewards() {
         );
     });
 }
+
+#[test]
+fn test_funding_request_succeeds() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let origin = OriginType::Root;
+        let initial_budget = 100;
+        let funding_amount = 5;
+        let recieving_account = 0;
+        Mocks::set_budget(origin.clone(), initial_budget, Ok(()));
+        Mocks::funding_request(origin, funding_amount, recieving_account, Ok(()));
+    });
+}

+ 0 - 31
runtime-modules/proposals/codex/src/benchmarking.rs

@@ -566,37 +566,6 @@ benchmarks! {
     verify {
         assert_new_budgets::<T>(101, 99, WorkingGroup::Membership);
     }
-
-    funding_request {
-        let council_budget = BalanceOf::<T>::from(100);
-        let transfer_balance = BalanceOf::<T>::from(60);
-        let recieving_account = account::<T::AccountId>("reciever", 0, SEED);
-        Council::<T>::set_budget(RawOrigin::Root.into(), council_budget).unwrap();
-        assert_eq!(
-            Council::<T>::budget(),
-            council_budget,
-            "Council budget not updated"
-        );
-
-        assert_eq!(
-            Balances::<T>::free_balance(&recieving_account),
-            Zero::zero(),
-            "Recieving account has funds",
-        );
-    }: _ (RawOrigin::Root, transfer_balance, recieving_account.clone())
-    verify {
-        assert_eq!(
-            Council::<T>::budget(),
-            council_budget - transfer_balance,
-            "Council didn't discount transference",
-        );
-
-        assert_eq!(
-            Balances::<T>::free_balance(recieving_account),
-            transfer_balance,
-            "Recieving account didn't recieve amount",
-        );
-    }
 }
 
 #[cfg(test)]

+ 5 - 21
runtime-modules/proposals/codex/src/lib.rs

@@ -61,7 +61,7 @@ mod tests;
 mod benchmarking;
 
 use frame_support::dispatch::DispatchResult;
-use frame_support::traits::{Currency, Get};
+use frame_support::traits::Get;
 use frame_support::weights::{DispatchClass, Weight};
 use frame_support::{decl_error, decl_module, decl_storage, ensure, print};
 use frame_system::ensure_root;
@@ -313,8 +313,8 @@ decl_error! {
         /// Invalid 'decrease stake proposal' parameter - cannot decrease by zero balance.
         DecreasingStakeIsZero,
 
-        /// Insufficent funds in council for executing 'Funding Request' proposal
-        InsufficientFundsForFundingRequest,
+        /// Insufficient funds for 'Update Working Group Budget' proposal execution
+        InsufficientFundsForBudgetUpdate,
     }
 }
 
@@ -551,13 +551,13 @@ decl_module! {
 
             match balance_kind {
                 BalanceKind::Positive => {
-                    ensure!(amount<=current_budget, Error::<T>::InsufficientFundsForFundingRequest);
+                    ensure!(amount<=current_budget, Error::<T>::InsufficientFundsForBudgetUpdate);
 
                     call_wg!(working_group<T>, set_budget, origin.clone(), wg_budget.saturating_add(amount))?;
                     Council::<T>::set_budget(origin, current_budget - amount)?;
                 },
                 BalanceKind::Negative => {
-                    ensure!(amount <= wg_budget, Error::<T>::InsufficientFundsForFundingRequest);
+                    ensure!(amount <= wg_budget, Error::<T>::InsufficientFundsForBudgetUpdate);
 
                     call_wg!(working_group<T>, set_budget, origin.clone(), wg_budget - amount)?;
                     Council::<T>::set_budget(origin, current_budget.saturating_add(amount))?;
@@ -565,22 +565,6 @@ decl_module! {
             }
         }
 
-        /// Funding request proposal
-        #[weight = WeightInfoCodex::<T>::funding_request()] // TODO: adjust weight
-        pub fn funding_request(
-            origin,
-            amount: BalanceOf<T>,
-            account: T::AccountId,
-        ) {
-            // Checks
-            ensure_root(origin.clone())?;
-            let current_budget = Council::<T>::budget();
-            ensure!(amount<=current_budget, Error::<T>::InsufficientFundsForFundingRequest);
-
-            // Mutation safe
-            Council::<T>::set_budget(origin, current_budget - amount)?;
-            let  _ = balances::Module::<T>::deposit_creating(&account, amount);
-        }
     }
 }
 

+ 0 - 39
runtime-modules/proposals/codex/src/tests/mod.rs

@@ -1003,45 +1003,6 @@ fn run_create_terminate_working_group_leader_role_proposal_common_checks_succeed
     });
 }
 
-#[test]
-fn test_funding_request_fails_permission() {
-    initial_test_ext().execute_with(|| {
-        assert_eq!(
-            ProposalCodex::funding_request(RawOrigin::Signed(0).into(), 1, 0),
-            Err(DispatchError::BadOrigin)
-        );
-    });
-}
-
-#[test]
-fn test_funding_request_fails_insufficient_fundings() {
-    initial_test_ext().execute_with(|| {
-        Council::<Test>::set_budget(RawOrigin::Root.into(), 0).unwrap();
-        assert_eq!(
-            ProposalCodex::funding_request(RawOrigin::Root.into(), 1, 0),
-            Err(Error::<Test>::InsufficientFundsForFundingRequest.into())
-        );
-    });
-}
-
-#[test]
-fn test_funding_request_succeeds() {
-    initial_test_ext().execute_with(|| {
-        let initial_budget = 100;
-        let funding_amount = 5;
-        let recieving_account = 0;
-        Council::<Test>::set_budget(RawOrigin::Root.into(), initial_budget).unwrap();
-        assert_eq!(Council::<Test>::budget(), initial_budget);
-        ProposalCodex::funding_request(RawOrigin::Root.into(), funding_amount, recieving_account)
-            .unwrap();
-        assert_eq!(Council::<Test>::budget(), initial_budget - funding_amount);
-        assert_eq!(
-            balances::Module::<Test>::free_balance(recieving_account),
-            funding_amount
-        );
-    });
-}
-
 #[test]
 fn create_amend_constitution_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {

+ 1 - 1
runtime/src/integration/proposals/proposal_encoder.rs

@@ -37,7 +37,7 @@ impl ProposalEncoder<Runtime> for ExtrinsicProposalEncoder {
                 Call::ProposalsCodex(proposals_codex::Call::execute_signal_proposal(signal))
             }
             ProposalDetails::FundingRequest(balance, destination) => {
-                Call::ProposalsCodex(proposals_codex::Call::funding_request(balance, destination))
+                Call::Council(council::Call::funding_request(balance, destination))
             }
             ProposalDetails::SetMaxValidatorCount(new_validator_count) => Call::Staking(
                 pallet_staking::Call::set_validator_count(new_validator_count),