Quellcode durchsuchen

referendum - module skelet I

ondratra vor 4 Jahren
Ursprung
Commit
8d5bba6a93

Datei-Diff unterdrückt, da er zu groß ist
+ 276 - 116
Cargo.lock


+ 1 - 0
Cargo.toml

@@ -12,6 +12,7 @@ members = [
 	"runtime-modules/membership",
 	"runtime-modules/memo",
 	"runtime-modules/recurring-reward",
+	"runtime-modules/referendum",
 	"runtime-modules/service-discovery",
 	"runtime-modules/stake",
 	"runtime-modules/storage",

+ 49 - 0
runtime-modules/referendum/Cargo.toml

@@ -0,0 +1,49 @@
+[package]
+name = 'substrate-referendum-module'
+version = '2.0.0'
+authors = ['Joystream contributors']
+edition = '2018'
+
+[dependencies]
+codec = { package = 'parity-scale-codec', version = '1.0.0', default-features = false, features = ['derive'] }
+serde = { version = '1.0.101', optional = true}
+
+[dependencies.srml-support]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'srml-support'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
+[dependencies.system]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'srml-system'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
+[dependencies.sr-primitives]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'sr-primitives'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
+[dependencies.primitives]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'substrate-primitives'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
+[dev-dependencies]
+runtime-io = { package = 'sr-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'}
+
+[features]
+default = ['std']
+std = [
+    'codec/std',
+    'primitives/std',
+    'runtime-io/std',
+    'serde',
+    'sr-primitives/std',
+    'srml-support/std',
+    'system/std',
+]
+

+ 141 - 0
runtime-modules/referendum/src/lib.rs

@@ -0,0 +1,141 @@
+// TODO: module documentation
+
+// NOTE: This module is instantiable pallet as described here https://substrate.dev/recipes/3-entrees/instantiable.html
+// No default instance is provided.
+
+/////////////////// Configuration //////////////////////////////////////////////
+#![cfg_attr(not(feature = "std"), no_std)]
+
+// used dependencies
+use codec::{ Decode, Encode};
+use srml_support::{decl_event, decl_module, decl_storage, dispatch};
+use std::marker::PhantomData;
+
+//use srml_support::{Parameter};
+//use sr_primitives::traits::{MaybeSerialize, Member, One, SimpleArithmetic};
+
+// conditioned dependencies
+//[cfg(feature = "std")]
+//use serde::serde_derive::{Deserialize, Serialize};
+
+// declared modules
+mod mock;
+mod tests;
+
+/////////////////// Data Structures ////////////////////////////////////////////
+
+#[derive(Encode, Decode)]
+pub enum ReferendumStage<BlockNumber> {
+    Void(BlockNumber),
+    Voting(BlockNumber),
+    Revealing(BlockNumber),
+}
+
+/////////////////// Trait, Storage, and Events /////////////////////////////////
+
+pub trait Trait<I: Instance>: system::Trait {
+    /// The overarching event type.
+    type Event: From<Event<Self, I>> + Into<<Self as system::Trait>::Event>;
+}
+
+decl_storage! {
+    trait Store for Module<T: Trait<I>, I: Instance> as Referendum {
+
+        // Current stage if there is an election running
+        Stage get(stage) config(): Option<ReferendumStage<T::BlockNumber>>;
+
+        // TODO: remove after `unused parameter` error for `I: Instance` is resolved
+        Tmp: PhantomData<I>;
+    }
+}
+
+decl_event! {
+    pub enum Event<T, I>
+    where
+        <T as system::Trait>::BlockNumber,
+    {
+        // temporary event
+        ReferendumStarted(BlockNumber),
+        RevealingPhaseStarted(BlockNumber),
+        ReferendumFinished(BlockNumber),
+    }
+}
+
+/////////////////// Module definition and implementation ///////////////////////
+
+decl_module! {
+    pub struct Module<T: Trait<I>, I: Instance> for enum Call where origin: T::Origin {
+
+        /////////////////// Lifetime ///////////////////////////////////////////
+
+        // start voting period
+        pub fn start_referendum(origin) -> dispatch::Result {
+            // prepare referendum
+
+            //EnsureChecks<T>::can_start_referendum()?;
+            EnsureChecks::<T, I>::can_start_referendum(origin)?;
+
+            Ok(())
+        }
+
+        // finish voting period
+        pub fn finish_voting(origin) -> dispatch::Result {
+            // do necessary actions to close voting
+
+            // start revealing phase
+            Self::start_revealing_period()?;
+
+            Ok(())
+        }
+
+        pub fn finish_revealing_period(origin) -> dispatch::Result {
+            // do necessary actions to finish revealing phase
+
+            Self::evaluate_referendum_results()?;
+
+            Ok(())
+        }
+
+        /////////////////// User actions ///////////////////////////////////////
+
+        pub fn vote(origin) -> dispatch::Result {
+            // recieve user's commitment
+
+            Ok(())
+        }
+
+        pub fn reveal_vote(origin) -> dispatch::Result {
+            // reveals user's commitment
+
+            Ok(())
+        }
+    }
+}
+
+/////////////////// Inner logic ////////////////////////////////////////////
+impl<T: Trait<I>, I: Instance> Module<T, I> {
+
+    fn start_revealing_period() -> dispatch::Result {
+        // do necessary actions to start commitment revealing phase
+
+        Ok(())
+    }
+
+    fn evaluate_referendum_results() -> dispatch::Result {
+        // evaluate results
+
+        Ok(())
+    }
+}
+
+/////////////////// Ensure checks //////////////////////////////////////////
+pub struct EnsureChecks<T: Trait<I>, I: Instance> {
+    _dummy: PhantomData<(T, I)>, // 0-sized data meant only to bound generic parameters
+}
+
+impl<T: Trait<I>, I: Instance> EnsureChecks<T, I> {
+    fn can_start_referendum(origin: T::Origin) -> dispatch::Result {
+
+        Ok(())
+    }
+}

+ 109 - 0
runtime-modules/referendum/src/mock.rs

@@ -0,0 +1,109 @@
+#![cfg(test)]
+
+use crate::*;
+
+use srml_support::{impl_outer_origin, parameter_types};
+use primitives::H256;
+use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::{Header}, Perbill};
+use runtime_io;
+
+use crate::{GenesisConfig};
+
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct Runtime;
+
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct Instance0;
+
+pub type TestModule = Module<Runtime, Instance0>;
+
+impl_outer_origin! {
+    pub enum Origin for Runtime {}
+}
+
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+}
+
+impl Instance for Instance0 {
+
+}
+
+//impl system::Trait for Runtime<Instance0> {
+impl system::Trait for Runtime {
+    type Origin = Origin;
+    type Index = u64;
+    type BlockNumber = u64;
+    type Call = ();
+    type Hash = H256;
+    type Hashing = BlakeTwo256;
+    type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
+    type Header = Header;
+    // type WeightMultiplierUpdate = ();
+    type Event = ();
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
+}
+
+impl<I: Instance> Trait<I> for Runtime {
+    type Event = Event<Self, I>;
+}
+
+/*
+impl timestamp::Trait for Runtime {
+    type Moment = u64;
+    type OnTimestampSet = ();
+    type MinimumPeriod = MinimumPeriod;
+}
+
+impl Trait for Runtime {
+    type Event = ();
+    type MembershipRegistry = registry::TestMembershipRegistryModule;
+    type ThreadId = u64;
+    type PostId = u64;
+}
+*/
+
+#[derive(Clone)]
+//pub enum OriginType<I: Instance> {
+pub enum OriginType {
+    Signed(<Runtime as system::Trait>::AccountId),
+    //Inherent, <== did not find how to make such an origin yet
+    Root,
+}
+
+pub fn mock_start_referendum(
+    origin: OriginType,
+    expected_result: dispatch::Result,
+) {
+    assert_eq!(
+        //TestModule::start_referendum(
+        TestModule::finish_voting(
+            origin.clone(),
+        ),
+        expected_result,
+    )
+}
+
+pub fn default_genesis_config() -> GenesisConfig<Runtime, Instance0> {
+    GenesisConfig::<Runtime> {
+        Stage: ReferendumStage::Void,
+    }
+}
+
+pub fn build_test_externalities(config: GenesisConfig<Runtime, Instance0>) -> runtime_io::TestExternalities {
+    let t = system::GenesisConfig::default()
+        .build_storage::<Runtime>()
+        .unwrap();
+
+    t.into()
+}

+ 30 - 0
runtime-modules/referendum/src/tests.rs

@@ -0,0 +1,30 @@
+#![cfg(test)]
+
+use super::*;
+use crate::mock::*;
+
+#[test]
+fn referendum_start() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let origin = OriginType::Signed(config.forum_sudo);
+
+        mock_start_referendum(origin, Ok(()));
+    });
+}
+
+#[test] #[ignore]
+fn finish_voting() {
+
+}
+
+#[test] #[ignore]
+fn finish_revealing_period() {
+
+}
+
+#[test] #[ignore]
+fn referendum_whole_process() {
+
+}

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.