Browse Source

content working group: added set_mint_capacity dispatchable and tests

Mokhtar Naamani 5 years ago
parent
commit
7daf806ab7

+ 3 - 3
runtime-modules/content-working-group/src/genesis.rs

@@ -43,11 +43,11 @@ pub struct GenesisConfigBuilder<T: Trait> {
 }
 
 impl<T: Trait> GenesisConfigBuilder<T> {
-    /*
-    pub fn set_mint(mut self, mint: <T as minting::Trait>::MintId) -> Self {
-        self.mint = mint;
+    pub fn with_mint_capacity(mut self, capacity: minting::BalanceOf<T>) -> Self {
+        self.mint_capacity = capacity;
         self
     }
+    /*
     pub fn set_channel_handle_constraint(mut self, constraint: InputValidationLengthConstraint) -> Self {
         self.channel_description_constraint = constraint;
         self

+ 44 - 1
runtime-modules/content-working-group/src/lib.rs

@@ -1080,7 +1080,9 @@ decl_event! {
         CuratorApplicationId = CuratorApplicationId<T>,
         CuratorId = CuratorId<T>,
         CuratorApplicationIdToCuratorIdMap = CuratorApplicationIdToCuratorIdMap<T>,
+        MintBalanceOf = minting::BalanceOf<T>,
         <T as system::Trait>::AccountId,
+        <T as minting::Trait>::MintId,
     {
         ChannelCreated(ChannelId),
         ChannelOwnershipTransferred(ChannelId),
@@ -1100,6 +1102,8 @@ decl_event! {
         CuratorRewardAccountUpdated(CuratorId, AccountId),
         ChannelUpdatedByCurationActor(ChannelId),
         ChannelCreationEnabledUpdated(bool),
+        MintCapacityIncreased(MintId, MintBalanceOf, MintBalanceOf),
+        MintCapacityDecreased(MintId, MintBalanceOf, MintBalanceOf),
     }
 }
 
@@ -2022,7 +2026,11 @@ decl_module! {
             Self::deposit_event(RawEvent::ChannelCreationEnabledUpdated(enabled));
         }
 
-        /// Add to capacity of current acive mint
+        /// Add to capacity of current acive mint.
+        /// This may be deprecated in the future, since set_mint_capacity is sufficient to
+        /// both increase and decrease capacity. Although when considering that it may be executed
+        /// by a proposal, given the temporal delay in approving a proposal, it might be more suitable
+        /// than set_mint_capacity?
         pub fn increase_mint_capacity(
             origin,
             additional_capacity: minting::BalanceOf<T>
@@ -2033,6 +2041,41 @@ decl_module! {
             let mint = <minting::Module<T>>::mints(mint_id); // must exist
             let new_capacity = mint.capacity() + additional_capacity;
             let _ = <minting::Module<T>>::set_mint_capacity(mint_id, new_capacity);
+
+            Self::deposit_event(RawEvent::MintCapacityIncreased(
+                mint_id, additional_capacity, new_capacity
+            ));
+        }
+
+        /// Sets the capacity of the current active mint
+        pub fn set_mint_capacity(
+            origin,
+            new_capacity: minting::BalanceOf<T>
+        ) {
+            ensure_root(origin)?;
+
+            let mint_id = Self::mint();
+
+            // Mint must exist - it is set at genesis
+            let mint = <minting::Module<T>>::mints(mint_id);
+
+            let current_capacity = mint.capacity();
+
+            if new_capacity != current_capacity {
+                // Cannot fail if mint exists
+                let _ = <minting::Module<T>>::set_mint_capacity(mint_id, new_capacity);
+
+                if new_capacity > current_capacity {
+                    Self::deposit_event(RawEvent::MintCapacityIncreased(
+                        mint_id, new_capacity - current_capacity, new_capacity
+                    ));
+                } else {
+                    Self::deposit_event(RawEvent::MintCapacityDecreased(
+                        mint_id, current_capacity - new_capacity, new_capacity
+                    ));
+                }
+            }
+
         }
     }
 }

+ 7 - 2
runtime-modules/content-working-group/src/mock.rs

@@ -68,7 +68,9 @@ pub type RawLibTestEvent = RawEvent<
     CuratorApplicationId<Test>,
     CuratorId<Test>,
     CuratorApplicationIdToCuratorIdMap<Test>,
+    minting::BalanceOf<Test>,
     <Test as system::Trait>::AccountId,
+    <Test as minting::Trait>::MintId,
 >;
 
 pub fn get_last_event_or_panic() -> RawLibTestEvent {
@@ -220,11 +222,13 @@ impl<T: Trait> TestExternalitiesBuilder<T> {
         self.membership_config = Some(membership_config);
         self
     }
-    pub fn set_content_wg_config(mut self, conteng_wg_config: GenesisConfig<T>) -> Self {
+    */
+
+    pub fn with_content_wg_config(mut self, conteng_wg_config: GenesisConfig<T>) -> Self {
         self.content_wg_config = Some(conteng_wg_config);
         self
     }
-    */
+
     pub fn build(self) -> runtime_io::TestExternalities {
         // Add system
         let mut t = self
@@ -260,3 +264,4 @@ impl<T: Trait> TestExternalitiesBuilder<T> {
 pub type System = system::Module<Test>;
 pub type Balances = balances::Module<Test>;
 pub type ContentWorkingGroup = Module<Test>;
+pub type Minting = minting::Module<Test>;

+ 65 - 1
runtime-modules/content-working-group/src/tests.rs

@@ -1,6 +1,6 @@
 #![cfg(test)]
 
-//use super::genesis;
+use super::genesis;
 use super::mock::{self, *};
 //use crate::membership;
 use hiring;
@@ -2184,3 +2184,67 @@ pub fn generate_too_short_length_buffer(constraint: &InputValidationLengthConstr
 pub fn generate_too_long_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
     generate_text((constraint.max() + 1) as usize)
 }
+
+#[test]
+fn setting_mint_capacity() {
+    const MINT_CAPACITY: u64 = 50000;
+
+    TestExternalitiesBuilder::<Test>::default()
+        .with_content_wg_config(
+            genesis::GenesisConfigBuilder::<Test>::default()
+                .with_mint_capacity(MINT_CAPACITY)
+                .build(),
+        )
+        .build()
+        .execute_with(|| {});
+}
+
+#[test]
+fn setting_mint_capacity() {
+    const MINT_CAPACITY: u64 = 50000;
+
+    TestExternalitiesBuilder::<Test>::default()
+        .with_content_wg_config(
+            genesis::GenesisConfigBuilder::<Test>::default()
+                .with_mint_capacity(MINT_CAPACITY)
+                .build(),
+        )
+        .build()
+        .execute_with(|| {
+            let mint_id = ContentWorkingGroup::mint();
+            let mint = Minting::mints(mint_id);
+            assert_eq!(mint.capacity(), MINT_CAPACITY);
+
+            // Decreasing mint capacity
+            let new_lower_capacity = 10000;
+            let decrease = MINT_CAPACITY - new_lower_capacity;
+            assert_ok!(ContentWorkingGroup::set_mint_capacity(
+                Origin::ROOT,
+                new_lower_capacity
+            ));
+            // Correct event after decreasing
+            assert_eq!(
+                get_last_event_or_panic(),
+                crate::RawEvent::MintCapacityDecreased(mint_id, decrease, new_lower_capacity)
+            );
+            // Correct value of capacity after decreasing
+            let mint = Minting::mints(mint_id);
+            assert_eq!(mint.capacity(), new_lower_capacity);
+
+            // Increasing mint capacity
+            let new_higher_capacity = 25000;
+            let increase = new_higher_capacity - mint.capacity();
+            assert_ok!(ContentWorkingGroup::set_mint_capacity(
+                Origin::ROOT,
+                new_higher_capacity
+            ));
+            // Excpected event after increasing
+            assert_eq!(
+                get_last_event_or_panic(),
+                crate::RawEvent::MintCapacityIncreased(mint_id, increase, new_higher_capacity)
+            );
+            // Excpected value of capacity after increasing
+            let mint = Minting::mints(mint_id);
+            assert_eq!(mint.capacity(), new_higher_capacity);
+        });
+}