Jelajahi Sumber

runtime: constitution: Add hashing for the constitution text.

- add comments
- add amendment number increment
Shamil Gadelshin 4 tahun lalu
induk
melakukan
a80d1effe9

+ 1 - 0
Cargo.lock

@@ -3314,6 +3314,7 @@ dependencies = [
  "frame-system",
  "parity-scale-codec",
  "serde",
+ "sp-runtime",
  "sp-std",
 ]
 

+ 2 - 0
runtime-modules/constitution/Cargo.toml

@@ -10,6 +10,7 @@ codec = { package = 'parity-scale-codec', version = '1.3.1', default-features =
 sp-std = { package = 'sp-std', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
 frame-support = { package = 'frame-support', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
 system = { package = 'frame-system', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
 
 [features]
 default = ['std']
@@ -19,4 +20,5 @@ std = [
     'sp-std/std',
     'frame-support/std',
     'system/std',
+    'sp-runtime/std',
 ]

+ 26 - 1
runtime-modules/constitution/src/lib.rs

@@ -1,3 +1,9 @@
+//! # Constitution pallet.
+//! `Constitution` pallet for the Joystream platform. Version 1.
+//! It contains current constitution text hash and amendment number in the storage and extrinsic for
+//! setting the new constitution.
+//!
+
 // Ensure we're `no_std` when compiling for Wasm.
 #![cfg_attr(not(feature = "std"), no_std)]
 
@@ -5,6 +11,7 @@ use codec::{Decode, Encode};
 use frame_support::{decl_event, decl_module, decl_storage};
 #[cfg(feature = "std")]
 use serde::{Deserialize, Serialize};
+use sp_runtime::traits::Hash;
 use sp_std::vec::Vec;
 use system::ensure_root;
 
@@ -12,10 +19,14 @@ pub trait Trait: system::Trait {
     type Event: From<Event> + Into<<Self as system::Trait>::Event>;
 }
 
+/// Contains constitution text hash and its amendment number.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, Default)]
 pub struct ConstitutionInfo {
+    /// Constitution text hash.
     pub text_hash: Vec<u8>,
+
+    /// Constitution amendment number.
     pub amendment_number: u32,
 }
 
@@ -28,6 +39,10 @@ decl_storage! {
 
 decl_event! {
     pub enum Event {
+        /// Emits on constitution amendment.
+        /// Parameters:
+        /// - constitution text hash
+        /// - amendment number
         ConstutionAmended(Vec<u8>, u32),
     }
 }
@@ -36,11 +51,19 @@ decl_module! {
     pub struct Module<T: Trait> for enum Call where origin: T::Origin {
         fn deposit_event() = default;
 
+        /// Sets the current constitution hash.It also sets the amendment number for the
+        /// constitution and increment it for the next amendment. Requires root origin.
         #[weight = 10_000_000] // TODO: adjust weight
         fn amend_contitution(origin, constitution_text: Vec<u8>) {
             ensure_root(origin)?;
 
-            let hash = Vec::new();
+            //
+            // == MUTATION SAFE ==
+            //
+
+            let hashed = T::Hashing::hash(&constitution_text);
+            let hash = hashed.as_ref().to_vec();
+
             let amendment_number = Self::next_amendment_number();
 
             let constitution = ConstitutionInfo{
@@ -49,6 +72,8 @@ decl_module! {
             };
 
             Constitution::put(constitution);
+            NextAmendmentNumber::put(amendment_number + 1);
+
             Self::deposit_event(Event::ConstutionAmended(hash, amendment_number));
         }
     }

+ 1 - 1
runtime-modules/proposals/engine/src/lib.rs

@@ -131,6 +131,7 @@ mod tests;
 
 use codec::Decode;
 use frame_support::dispatch::{DispatchError, DispatchResult, UnfilteredDispatchable};
+use frame_support::sp_std::marker::PhantomData;
 use frame_support::storage::IterableStorageMap;
 use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
 use frame_support::{
@@ -139,7 +140,6 @@ use frame_support::{
 use sp_arithmetic::traits::Zero;
 use sp_std::vec::Vec;
 use system::{ensure_root, RawOrigin};
-use frame_support::sp_std::marker::PhantomData;
 
 use common::origin::ActorOriginValidator;