Browse Source

Merge pull request #239 from mnaamani/content-wg-add-set-mint-capacity

Added set_mint_capacity dispatchable and tests
shamil-gadelshin 5 years ago
parent
commit
28dc00df98

+ 1 - 1
Cargo.lock

@@ -1599,7 +1599,7 @@ dependencies = [
 
 [[package]]
 name = "joystream-node-runtime"
-version = "6.8.1"
+version = "6.9.0"
 dependencies = [
  "parity-scale-codec",
  "safe-mix",

+ 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

+ 45 - 2
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>
@@ -2032,7 +2040,42 @@ decl_module! {
             let mint_id = Self::mint();
             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);
+            <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
+                <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>;

+ 85 - 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,87 @@ 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 increasing_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);
+
+            let increase = 25000;
+            // Increasing mint capacity
+            let expected_new_capacity = MINT_CAPACITY + increase;
+            assert_ok!(ContentWorkingGroup::increase_mint_capacity(
+                Origin::ROOT,
+                increase
+            ));
+            // Excpected event after increasing
+            assert_eq!(
+                get_last_event_or_panic(),
+                crate::RawEvent::MintCapacityIncreased(mint_id, increase, expected_new_capacity)
+            );
+            // Excpected value of capacity after increasing
+            let mint = Minting::mints(mint_id);
+            assert_eq!(mint.capacity(), expected_new_capacity);
+        });
+}
+
+#[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);
+        });
+}

+ 9 - 0
runtime-modules/token-minting/src/lib.rs

@@ -73,6 +73,15 @@ impl From<MintingError> for TransferError {
     }
 }
 
+impl From<GeneralError> for &'static str {
+    fn from(err: GeneralError) -> &'static str {
+        match err {
+            GeneralError::MintNotFound => "MintNotFound",
+            GeneralError::NextAdjustmentInPast => "NextAdjustmentInPast",
+        }
+    }
+}
+
 #[derive(Encode, Decode, Copy, Clone, Debug, Eq, PartialEq)]
 pub enum Adjustment<Balance: Zero, BlockNumber> {
     // First adjustment will be after AdjustOnInterval.block_interval

+ 1 - 1
runtime/Cargo.toml

@@ -5,7 +5,7 @@ edition = '2018'
 name = 'joystream-node-runtime'
 # Follow convention: https://github.com/Joystream/substrate-runtime-joystream/issues/1
 # {Authoring}.{Spec}.{Impl} of the RuntimeVersion
-version = '6.8.1'
+version = '6.9.0'
 
 [features]
 default = ['std']

+ 1 - 1
runtime/src/lib.rs

@@ -115,7 +115,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
     spec_name: create_runtime_str!("joystream-node"),
     impl_name: create_runtime_str!("joystream-node"),
     authoring_version: 6,
-    spec_version: 8,
+    spec_version: 9,
     impl_version: 0,
     apis: RUNTIME_API_VERSIONS,
 };