Переглянути джерело

runtime: council refactor mint to not be an Option create it at genesis

Mokhtar Naamani 4 роки тому
батько
коміт
aff17e3250

+ 1 - 1
Cargo.lock

@@ -2052,7 +2052,7 @@ dependencies = [
 
 [[package]]
 name = "joystream-node-runtime"
-version = "7.0.0"
+version = "7.1.0"
 dependencies = [
  "frame-benchmarking",
  "frame-executive",

+ 9 - 37
runtime-modules/governance/src/council.rs

@@ -37,7 +37,7 @@ decl_storage! {
         /// The mint that funds council member rewards and spending proposals budget. It is an Option
         /// because it was introduced in a runtime upgrade. It will be automatically created when
         /// a successful call to set_council_mint_capacity() is made.
-        pub CouncilMint get(fn council_mint) : Option<<T as minting::Trait>::MintId>;
+        pub CouncilMint get(fn council_mint) : <T as minting::Trait>::MintId;
 
         /// The reward relationships currently in place. There may not necessarily be a 1-1 correspondance with
         /// the active council, since there are multiple ways of setting/adding/removing council members, some of which
@@ -87,15 +87,8 @@ impl<T: Trait> CouncilElected<Seats<T::AccountId, BalanceOf<T>>, T::BlockNumber>
 
         <TermEndsAt<T>>::put(next_term_ends_at);
 
-        if let Some(reward_source) = Self::council_mint() {
-            for seat in seats.iter() {
-                Self::add_reward_relationship(&seat.member, reward_source);
-            }
-        } else {
-            // Skip trying to create rewards since no mint has been created yet
-            debug::warn!(
-                "Not creating reward relationship for council seats because no mint exists"
-            );
+        for seat in seats.iter() {
+            Self::add_reward_relationship(&seat.member, Self::council_mint());
         }
 
         Self::deposit_event(RawEvent::NewCouncilTermStarted(next_term_ends_at));
@@ -111,15 +104,6 @@ impl<T: Trait> Module<T> {
         Self::active_council().iter().any(|c| c.member == *sender)
     }
 
-    /// Initializes a new mint, discarding previous mint if it existed.
-    pub fn create_new_council_mint(
-        capacity: minting::BalanceOf<T>,
-    ) -> Result<T::MintId, &'static str> {
-        let mint_id = <minting::Module<T>>::add_mint(capacity, None)?;
-        CouncilMint::<T>::put(mint_id);
-        Ok(mint_id)
-    }
-
     fn add_reward_relationship(destination: &T::AccountId, reward_source: T::MintId) {
         let recipient = <recurringrewards::Module<T>>::add_recipient();
 
@@ -189,10 +173,8 @@ decl_module! {
             // Council is being replaced so remove existing reward relationships if they exist
             Self::remove_reward_relationships();
 
-            if let Some(reward_source) = Self::council_mint() {
-                for account in accounts.clone() {
-                    Self::add_reward_relationship(&account, reward_source);
-                }
+            for account in accounts.clone() {
+                Self::add_reward_relationship(&account, Self::council_mint());
             }
 
             let new_council: Seats<T::AccountId, BalanceOf<T>> = accounts.into_iter().map(|account| {
@@ -213,9 +195,7 @@ decl_module! {
 
             ensure!(!Self::is_councilor(&account), "cannot add same account multiple times");
 
-            if let Some(reward_source) = Self::council_mint() {
-                Self::add_reward_relationship(&account, reward_source);
-            }
+            Self::add_reward_relationship(&account, Self::council_mint());
 
             let seat = Seat {
                 member: account,
@@ -261,11 +241,7 @@ decl_module! {
         pub fn set_council_mint_capacity(origin, capacity: minting::BalanceOf<T>) {
             ensure_root(origin)?;
 
-            if let Some(mint_id) = Self::council_mint() {
-                minting::Module::<T>::set_mint_capacity(mint_id, capacity).map_err(<&str>::from)?;
-            } else {
-                Self::create_new_council_mint(capacity)?;
-            }
+            minting::Module::<T>::set_mint_capacity(Self::council_mint(), capacity).map_err(<&str>::from)?;
         }
 
         /// Attempts to mint and transfer amount to destination account
@@ -273,12 +249,8 @@ decl_module! {
         fn spend_from_council_mint(origin, amount: minting::BalanceOf<T>, destination: T::AccountId) {
             ensure_root(origin)?;
 
-            if let Some(mint_id) = Self::council_mint() {
-                minting::Module::<T>::transfer_tokens(mint_id, amount, &destination)
-                    .map_err(<&str>::from)?;
-            } else {
-                return Err("CouncilHasNoMint".into());
-            }
+            minting::Module::<T>::transfer_tokens(Self::council_mint(), amount, &destination)
+                .map_err(<&str>::from)?;
         }
 
         /// Sets the council rewards which is only applied on new council being elected.

+ 8 - 1
runtime-modules/governance/src/mock.rs

@@ -8,7 +8,7 @@ use sp_core::H256;
 use sp_runtime::{
     testing::Header,
     traits::{BlakeTwo256, IdentityLookup},
-    Perbill,
+    BuildStorage, Perbill,
 };
 pub use system;
 
@@ -141,6 +141,13 @@ pub fn initial_test_ext() -> sp_io::TestExternalities {
         .assimilate_storage(&mut t)
         .unwrap();
 
+    // build the council config to initialize the mint
+    let council_config = council::GenesisConfig::<Test>::default()
+        .build_storage()
+        .unwrap();
+
+    council_config.assimilate_storage(&mut t).unwrap();
+
     t.into()
 }
 

+ 1 - 1
runtime/Cargo.toml

@@ -4,7 +4,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 = '7.0.0'
+version = '7.1.0'
 
 [dependencies]
 # Third-party dependencies

+ 1 - 1
runtime/src/lib.rs

@@ -70,7 +70,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
     spec_name: create_runtime_str!("joystream-node"),
     impl_name: create_runtime_str!("joystream-node"),
     authoring_version: 7,
-    spec_version: 0,
+    spec_version: 1,
     impl_version: 0,
     apis: crate::runtime_api::EXPORTED_RUNTIME_API_VERSIONS,
     transaction_version: 1,

+ 9 - 1
runtime/src/tests/mod.rs

@@ -4,11 +4,19 @@
 
 mod proposals_integration;
 mod storage_integration;
+use sp_runtime::BuildStorage;
 
 pub(crate) fn initial_test_ext() -> sp_io::TestExternalities {
-    let t = system::GenesisConfig::default()
+    let mut t = system::GenesisConfig::default()
         .build_storage::<crate::Runtime>()
         .unwrap();
 
+    // build the council config to initialize the mint
+    let council_config = governance::council::GenesisConfig::<crate::Runtime>::default()
+        .build_storage()
+        .unwrap();
+
+    council_config.assimilate_storage(&mut t).unwrap();
+
     t.into()
 }