From 7daf806ab7add9392c3ceaa83b342a116657d6e2 Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Wed, 25 Mar 2020 21:26:31 +0400 Subject: [PATCH 1/4] content working group: added set_mint_capacity dispatchable and tests --- .../content-working-group/src/genesis.rs | 6 +- .../content-working-group/src/lib.rs | 45 ++++++++++++- .../content-working-group/src/mock.rs | 9 ++- .../content-working-group/src/tests.rs | 66 ++++++++++++++++++- 4 files changed, 119 insertions(+), 7 deletions(-) diff --git a/runtime-modules/content-working-group/src/genesis.rs b/runtime-modules/content-working-group/src/genesis.rs index fcc4a4396b..6cf74f39c1 100644 --- a/runtime-modules/content-working-group/src/genesis.rs +++ b/runtime-modules/content-working-group/src/genesis.rs @@ -43,11 +43,11 @@ pub struct GenesisConfigBuilder { } impl GenesisConfigBuilder { - /* - pub fn set_mint(mut self, mint: ::MintId) -> Self { - self.mint = mint; + pub fn with_mint_capacity(mut self, capacity: minting::BalanceOf) -> Self { + self.mint_capacity = capacity; self } + /* pub fn set_channel_handle_constraint(mut self, constraint: InputValidationLengthConstraint) -> Self { self.channel_description_constraint = constraint; self diff --git a/runtime-modules/content-working-group/src/lib.rs b/runtime-modules/content-working-group/src/lib.rs index b0c6aeea93..d8e2d8f81d 100755 --- a/runtime-modules/content-working-group/src/lib.rs +++ b/runtime-modules/content-working-group/src/lib.rs @@ -1080,7 +1080,9 @@ decl_event! { CuratorApplicationId = CuratorApplicationId, CuratorId = CuratorId, CuratorApplicationIdToCuratorIdMap = CuratorApplicationIdToCuratorIdMap, + MintBalanceOf = minting::BalanceOf, ::AccountId, + ::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 @@ -2033,6 +2041,41 @@ decl_module! { let mint = >::mints(mint_id); // must exist let new_capacity = mint.capacity() + additional_capacity; let _ = >::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 + ) { + ensure_root(origin)?; + + let mint_id = Self::mint(); + + // Mint must exist - it is set at genesis + let mint = >::mints(mint_id); + + let current_capacity = mint.capacity(); + + if new_capacity != current_capacity { + // Cannot fail if mint exists + let _ = >::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 + )); + } + } + } } } diff --git a/runtime-modules/content-working-group/src/mock.rs b/runtime-modules/content-working-group/src/mock.rs index 3f2f4a57f3..d99e240cf2 100644 --- a/runtime-modules/content-working-group/src/mock.rs +++ b/runtime-modules/content-working-group/src/mock.rs @@ -68,7 +68,9 @@ pub type RawLibTestEvent = RawEvent< CuratorApplicationId, CuratorId, CuratorApplicationIdToCuratorIdMap, + minting::BalanceOf, ::AccountId, + ::MintId, >; pub fn get_last_event_or_panic() -> RawLibTestEvent { @@ -220,11 +222,13 @@ impl TestExternalitiesBuilder { self.membership_config = Some(membership_config); self } - pub fn set_content_wg_config(mut self, conteng_wg_config: GenesisConfig) -> Self { + */ + + pub fn with_content_wg_config(mut self, conteng_wg_config: GenesisConfig) -> 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 TestExternalitiesBuilder { pub type System = system::Module; pub type Balances = balances::Module; pub type ContentWorkingGroup = Module; +pub type Minting = minting::Module; diff --git a/runtime-modules/content-working-group/src/tests.rs b/runtime-modules/content-working-group/src/tests.rs index 03cc88e36d..cb177f3bfd 100644 --- a/runtime-modules/content-working-group/src/tests.rs +++ b/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 { generate_text((constraint.max() + 1) as usize) } + +#[test] +fn setting_mint_capacity() { + const MINT_CAPACITY: u64 = 50000; + + TestExternalitiesBuilder::::default() + .with_content_wg_config( + genesis::GenesisConfigBuilder::::default() + .with_mint_capacity(MINT_CAPACITY) + .build(), + ) + .build() + .execute_with(|| {}); +} + +#[test] +fn setting_mint_capacity() { + const MINT_CAPACITY: u64 = 50000; + + TestExternalitiesBuilder::::default() + .with_content_wg_config( + genesis::GenesisConfigBuilder::::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); + }); +} From 06b397fa0f649d443ed96eeac71142cd5593076a Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Wed, 25 Mar 2020 21:44:36 +0400 Subject: [PATCH 2/4] content working group: test for increase_mint_capacity --- .../content-working-group/src/tests.rs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/runtime-modules/content-working-group/src/tests.rs b/runtime-modules/content-working-group/src/tests.rs index cb177f3bfd..8d8f6f8198 100644 --- a/runtime-modules/content-working-group/src/tests.rs +++ b/runtime-modules/content-working-group/src/tests.rs @@ -2186,7 +2186,7 @@ pub fn generate_too_long_length_buffer(constraint: &InputValidationLengthConstra } #[test] -fn setting_mint_capacity() { +fn increasing_mint_capacity() { const MINT_CAPACITY: u64 = 50000; TestExternalitiesBuilder::::default() @@ -2196,7 +2196,27 @@ fn setting_mint_capacity() { .build(), ) .build() - .execute_with(|| {}); + .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] From 3254ef1a1b38179739c6c65b844c3805c007a525 Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Wed, 25 Mar 2020 21:45:00 +0400 Subject: [PATCH 3/4] bumpp runtime spec 9 since we added methods to the public api --- Cargo.lock | 2 +- runtime/Cargo.toml | 2 +- runtime/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc4b914ad6..eea76befb9 100644 --- a/Cargo.lock +++ b/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", diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 25c158357d..6ff953659c 100644 --- a/runtime/Cargo.toml +++ b/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'] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d8ed411c8c..92c4bbb9f0 100644 --- a/runtime/src/lib.rs +++ b/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, }; From 1d115a84a74548faa113f678570de987e90417b4 Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Thu, 26 Mar 2020 18:21:00 +0400 Subject: [PATCH 4/4] do not ignore result value when calling set_mint_capacity --- runtime-modules/content-working-group/src/lib.rs | 4 ++-- runtime-modules/token-minting/src/lib.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/runtime-modules/content-working-group/src/lib.rs b/runtime-modules/content-working-group/src/lib.rs index d8e2d8f81d..482a4a49c3 100755 --- a/runtime-modules/content-working-group/src/lib.rs +++ b/runtime-modules/content-working-group/src/lib.rs @@ -2040,7 +2040,7 @@ decl_module! { let mint_id = Self::mint(); let mint = >::mints(mint_id); // must exist let new_capacity = mint.capacity() + additional_capacity; - let _ = >::set_mint_capacity(mint_id, new_capacity); + >::set_mint_capacity(mint_id, new_capacity)?; Self::deposit_event(RawEvent::MintCapacityIncreased( mint_id, additional_capacity, new_capacity @@ -2063,7 +2063,7 @@ decl_module! { if new_capacity != current_capacity { // Cannot fail if mint exists - let _ = >::set_mint_capacity(mint_id, new_capacity); + >::set_mint_capacity(mint_id, new_capacity)?; if new_capacity > current_capacity { Self::deposit_event(RawEvent::MintCapacityIncreased( diff --git a/runtime-modules/token-minting/src/lib.rs b/runtime-modules/token-minting/src/lib.rs index b84237708e..df9c6fef91 100755 --- a/runtime-modules/token-minting/src/lib.rs +++ b/runtime-modules/token-minting/src/lib.rs @@ -73,6 +73,15 @@ impl From for TransferError { } } +impl From 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 { // First adjustment will be after AdjustOnInterval.block_interval