Browse Source

runtime: bounty: Add min-max funding amount constraint.

Shamil Gadelshin 4 years ago
parent
commit
b104d030e8

+ 2 - 5
runtime-modules/bounty/src/benchmarking.rs

@@ -26,12 +26,9 @@ benchmarks! {
         let i in 1 .. MAX_BYTES;
         let metadata = vec![0u8].repeat(i as usize);
 
-        let params = BountyCreationParameters::<T>{
-            metadata,
-            ..Default::default()
-        };
+        let params = BountyCreationParameters::<T>::default();
 
-    }: _ (RawOrigin::Root, params.clone())
+    }: _ (RawOrigin::Root, params.clone(), metadata)
     verify {
         let bounty = Bounty::<T>{
             creation_params: params,

+ 11 - 10
runtime-modules/bounty/src/lib.rs

@@ -27,7 +27,7 @@ pub trait WeightInfo {
 type WeightInfoBounty<T> = <T as Trait>::WeightInfo;
 
 use frame_support::weights::Weight;
-use frame_support::{decl_error, decl_event, decl_module, decl_storage, Parameter};
+use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Parameter};
 use frame_system::ensure_root;
 use sp_runtime::SaturatedConversion;
 use sp_std::vec::Vec;
@@ -135,11 +135,6 @@ pub struct BountyParameters<Balance, BlockNumber, MemberId> {
 
     /// Number of block from end of work period until oracle can no longer decide winners.
     pub judging_period: BlockNumber,
-
-    /// A standardised structure document describing user facing information,
-    /// for example a title, amount requested, deliverable, discovery metadata, link to forum etc.
-    /// Is not stored in storage, chain only sees raw extrinsic payload blob, like rationales before.
-    pub metadata: Vec<u8>,
 }
 
 /// Alias type for the Bounty.
@@ -182,8 +177,8 @@ decl_event! {
 decl_error! {
     /// Bounty pallet predefined errors
     pub enum Error for Module<T: Trait> {
-        /// Incorrect origin provided.
-        BadOrigin,
+        /// Min funding amount cannot be greater than max amount.
+        MinFundingAmountCannotBeGreaterThanMaxAmount,
     }
 }
 
@@ -191,8 +186,9 @@ decl_module! {
     pub struct Module<T: Trait> for enum Call where origin: T::Origin {
         fn deposit_event() = default;
 
-        #[weight = WeightInfoBounty::<T>::create_bounty(params.metadata.len().saturated_into())]
-        fn create_bounty(origin, params: BountyCreationParameters<T>) {
+        /// Creates a bounty. Metadata stored in the transaction log but discarded after that.
+        #[weight = WeightInfoBounty::<T>::create_bounty(_metadata.len().saturated_into())]
+        fn create_bounty(origin, params: BountyCreationParameters<T>, _metadata: Vec<u8>) {
             Self::ensure_create_bounty_parameters_valid(&origin, &params)?;
 
             //
@@ -231,6 +227,11 @@ impl<T: Trait> Module<T> {
             ensure_root(origin.clone())?;
         }
 
+        ensure!(
+            params.min_amount <= params.max_amount,
+            Error::<T>::MinFundingAmountCannotBeGreaterThanMaxAmount
+        );
+
         Ok(())
     }
 }

+ 8 - 2
runtime-modules/bounty/src/tests/fixtures.rs

@@ -41,6 +41,7 @@ pub struct CreateBountyFixture {
     origin: RawOrigin<u64>,
     metadata: Vec<u8>,
     creator_member_id: Option<u64>,
+    min_amount: u64,
 }
 
 impl CreateBountyFixture {
@@ -49,6 +50,7 @@ impl CreateBountyFixture {
             origin: RawOrigin::Root,
             metadata: Vec::new(),
             creator_member_id: None,
+            min_amount: 0,
         }
     }
 
@@ -66,18 +68,22 @@ impl CreateBountyFixture {
     pub fn with_metadata(self, metadata: Vec<u8>) -> Self {
         Self { metadata, ..self }
     }
+    pub fn with_min_amount(self, min_amount: u64) -> Self {
+        Self { min_amount, ..self }
+    }
 
     pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let params = BountyCreationParameters::<Test> {
-            metadata: self.metadata.clone(),
             creator_member_id: self.creator_member_id.clone(),
+            min_amount: self.min_amount.clone(),
             ..Default::default()
         };
 
         let next_bounty_count_value = Bounty::bounty_count() + 1;
         let bounty_id: u64 = next_bounty_count_value.into();
 
-        let actual_result = Bounty::create_bounty(self.origin.clone().into(), params);
+        let actual_result =
+            Bounty::create_bounty(self.origin.clone().into(), params, self.metadata.clone());
 
         assert_eq!(actual_result, expected_result);
 

+ 12 - 2
runtime-modules/bounty/src/tests/mod.rs

@@ -6,9 +6,9 @@ pub(crate) mod mocks;
 use frame_system::RawOrigin;
 use sp_runtime::DispatchError;
 
-use crate::RawEvent;
+use crate::{Error, RawEvent};
 use fixtures::{run_to_block, CreateBountyFixture, EventFixture};
-use mocks::build_test_externalities;
+use mocks::{build_test_externalities, Test};
 
 #[test]
 fn create_bounty_succeeds() {
@@ -42,3 +42,13 @@ fn create_bounty_fails_with_invalid_origin() {
         create_bounty_fixture.call_and_assert(Err(DispatchError::BadOrigin));
     });
 }
+
+#[test]
+fn create_bounty_fails_with_invalid_min_max_amounts() {
+    build_test_externalities().execute_with(|| {
+        let create_bounty_fixture = CreateBountyFixture::default().with_min_amount(100);
+        create_bounty_fixture.call_and_assert(Err(
+            Error::<Test>::MinFundingAmountCannotBeGreaterThanMaxAmount.into(),
+        ));
+    });
+}