Enhance decl storage (#777)

* enhance macro decl_storage()

* update the state root hash

* fix one comment
This commit is contained in:
guanqun
2018-10-05 20:20:32 +08:00
committed by Gav Wood
parent 200a716a1a
commit 1cc0e3b6ea
28 changed files with 2031 additions and 1459 deletions
+5 -76
View File
@@ -32,7 +32,8 @@ extern crate hex_literal;
extern crate parity_codec as codec;
#[macro_use] extern crate parity_codec_derive;
extern crate substrate_primitives;
#[macro_use] extern crate sr_std as rstd;
#[cfg_attr(not(feature = "std"), macro_use)]
extern crate sr_std as rstd;
extern crate sr_io as runtime_io;
#[macro_use] extern crate srml_support;
extern crate sr_primitives as primitives;
@@ -40,86 +41,12 @@ extern crate srml_balances as balances;
extern crate srml_democracy as democracy;
extern crate srml_system as system;
#[cfg(feature = "std")]
use rstd::prelude::*;
#[cfg(feature = "std")]
use primitives::traits::As;
#[cfg(feature = "std")]
use srml_support::StorageValue;
pub mod voting;
pub mod motions;
pub mod seats;
pub use seats::{Trait, Module, RawEvent, Event, VoteIndex};
#[cfg(feature = "std")]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct GenesisConfig<T: seats::Trait> {
// for the voting onto the council
pub candidacy_bond: T::Balance,
pub voter_bond: T::Balance,
pub present_slash_per_voter: T::Balance,
pub carry_count: u32,
pub active_council: Vec<(T::AccountId, T::BlockNumber)>,
pub approval_voting_period: T::BlockNumber,
pub presentation_duration: T::BlockNumber,
pub desired_seats: u32,
pub term_duration: T::BlockNumber,
pub inactive_grace_period: T::BlockNumber,
// for the council's votes.
pub cooloff_period: T::BlockNumber,
pub voting_period: T::BlockNumber,
}
#[cfg(feature = "std")]
impl<T: seats::Trait + voting::Trait + motions::Trait> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
candidacy_bond: T::Balance::sa(9),
voter_bond: T::Balance::sa(0),
present_slash_per_voter: T::Balance::sa(1),
carry_count: 2,
inactive_grace_period: T::BlockNumber::sa(1),
active_council: vec![],
approval_voting_period: T::BlockNumber::sa(1000),
presentation_duration: T::BlockNumber::sa(1000),
desired_seats: 0,
term_duration: T::BlockNumber::sa(5),
cooloff_period: T::BlockNumber::sa(1000),
voting_period: T::BlockNumber::sa(3),
}
}
}
#[cfg(feature = "std")]
impl<T: seats::Trait + voting::Trait + motions::Trait> primitives::BuildStorage for GenesisConfig<T>
{
fn build_storage(self) -> ::std::result::Result<primitives::StorageMap, String> {
use codec::Encode;
Ok(map![
Self::hash(<seats::CandidacyBond<T>>::key()).to_vec() => self.candidacy_bond.encode(),
Self::hash(<seats::VotingBond<T>>::key()).to_vec() => self.voter_bond.encode(),
Self::hash(<seats::PresentSlashPerVoter<T>>::key()).to_vec() => self.present_slash_per_voter.encode(),
Self::hash(<seats::CarryCount<T>>::key()).to_vec() => self.carry_count.encode(),
Self::hash(<seats::PresentationDuration<T>>::key()).to_vec() => self.presentation_duration.encode(),
Self::hash(<seats::VotingPeriod<T>>::key()).to_vec() => self.approval_voting_period.encode(),
Self::hash(<seats::TermDuration<T>>::key()).to_vec() => self.term_duration.encode(),
Self::hash(<seats::DesiredSeats<T>>::key()).to_vec() => self.desired_seats.encode(),
Self::hash(<seats::InactiveGracePeriod<T>>::key()).to_vec() => self.inactive_grace_period.encode(),
Self::hash(<seats::ActiveCouncil<T>>::key()).to_vec() => self.active_council.encode(),
Self::hash(<voting::CooloffPeriod<T>>::key()).to_vec() => self.cooloff_period.encode(),
Self::hash(<voting::VotingPeriod<T>>::key()).to_vec() => self.voting_period.encode(),
Self::hash(<voting::Proposals<T>>::key()).to_vec() => vec![0u8; 0].encode()
])
}
}
#[cfg(test)]
mod tests {
// These re-exports are here for a reason, edit with care
@@ -205,7 +132,7 @@ mod tests {
voting_period: 3,
minimum_deposit: 1,
}.build_storage().unwrap());
t.extend(GenesisConfig::<Test>{
t.extend(seats::GenesisConfig::<Test> {
candidacy_bond: 9,
voter_bond: 3,
present_slash_per_voter: 1,
@@ -220,6 +147,8 @@ mod tests {
presentation_duration: 2,
desired_seats: 2,
term_duration: 5,
}.build_storage().unwrap());
t.extend(voting::GenesisConfig::<Test> {
cooloff_period: 2,
voting_period: 1,
}.build_storage().unwrap());
+8 -4
View File
@@ -75,13 +75,17 @@ decl_module! {
decl_storage! {
trait Store for Module<T: Trait> as CouncilMotions {
/// The (hashes of) the active proposals.
pub Proposals get(proposals): default Vec<T::Hash>;
pub Proposals get(proposals): Vec<T::Hash>;
/// Actual proposal for a given hash, if it's current.
pub ProposalOf get(proposal_of): map [ T::Hash => <T as Trait>::Proposal ];
pub ProposalOf get(proposal_of): map T::Hash => Option< <T as Trait>::Proposal >;
/// Votes for a given proposal: (required_yes_votes, yes_voters, no_voters).
pub Voting get(voting): map [ T::Hash => (ProposalIndex, u32, Vec<T::AccountId>, Vec<T::AccountId>) ];
pub Voting get(voting): map T::Hash => Option<(ProposalIndex, u32, Vec<T::AccountId>, Vec<T::AccountId>)>;
/// Proposals so far.
pub ProposalCount get(proposal_count): default u32;
pub ProposalCount get(proposal_count): u32;
}
add_extra_genesis {
config(_marker): ::std::marker::PhantomData<T>;
build(|_, _| {});
}
}
+20 -20
View File
@@ -104,53 +104,53 @@ decl_storage! {
// parameters
/// How much should be locked up in order to submit one's candidacy.
pub CandidacyBond get(candidacy_bond): required T::Balance;
pub CandidacyBond get(candidacy_bond) config(): T::Balance = T::Balance::sa(9);
/// How much should be locked up in order to be able to submit votes.
pub VotingBond get(voting_bond): required T::Balance;
pub VotingBond get(voting_bond) config(voter_bond): T::Balance;
/// The punishment, per voter, if you provide an invalid presentation.
pub PresentSlashPerVoter get(present_slash_per_voter): required T::Balance;
pub PresentSlashPerVoter get(present_slash_per_voter) config(): T::Balance = T::Balance::sa(1);
/// How many runners-up should have their approvals persist until the next vote.
pub CarryCount get(carry_count): required u32;
pub CarryCount get(carry_count) config(): u32 = 2;
/// How long to give each top candidate to present themselves after the vote ends.
pub PresentationDuration get(presentation_duration): required T::BlockNumber;
pub PresentationDuration get(presentation_duration) config(): T::BlockNumber = T::BlockNumber::sa(1000);
/// How many votes need to go by after a voter's last vote before they can be reaped if their
/// approvals are moot.
pub InactiveGracePeriod get(inactivity_grace_period): required VoteIndex;
pub InactiveGracePeriod get(inactivity_grace_period) config(inactive_grace_period): VoteIndex = 1;
/// How often (in blocks) to check for new votes.
pub VotingPeriod get(voting_period): required T::BlockNumber;
pub VotingPeriod get(voting_period) config(approval_voting_period): T::BlockNumber = T::BlockNumber::sa(1000);
/// How long each position is active for.
pub TermDuration get(term_duration): required T::BlockNumber;
pub TermDuration get(term_duration) config(): T::BlockNumber = T::BlockNumber::sa(5);
/// Number of accounts that should be sitting on the council.
pub DesiredSeats get(desired_seats): required u32;
pub DesiredSeats get(desired_seats) config(): u32;
// permanent state (always relevant, changes only at the finalisation of voting)
/// The current council. When there's a vote going on, this should still be used for executive
/// matters.
pub ActiveCouncil get(active_council): default Vec<(T::AccountId, T::BlockNumber)>;
pub ActiveCouncil get(active_council) config(): Vec<(T::AccountId, T::BlockNumber)>;
/// The total number of votes that have happened or are in progress.
pub VoteCount get(vote_index): default VoteIndex;
pub VoteCount get(vote_index): VoteIndex;
// persistent state (always relevant, changes constantly)
/// The last cleared vote index that this voter was last active at.
pub ApprovalsOf get(approvals_of): default map [ T::AccountId => Vec<bool> ];
pub ApprovalsOf get(approvals_of): map T::AccountId => Vec<bool>;
/// The vote index and list slot that the candidate `who` was registered or `None` if they are not
/// currently registered.
pub RegisterInfoOf get(candidate_reg_info): map [ T::AccountId => (VoteIndex, u32) ];
pub RegisterInfoOf get(candidate_reg_info): map T::AccountId => Option<(VoteIndex, u32)>;
/// The last cleared vote index that this voter was last active at.
pub LastActiveOf get(voter_last_active): map [ T::AccountId => VoteIndex ];
pub LastActiveOf get(voter_last_active): map T::AccountId => Option<VoteIndex>;
/// The present voter list.
pub Voters get(voters): default Vec<T::AccountId>;
pub Voters get(voters): Vec<T::AccountId>;
/// The present candidate list.
pub Candidates get(candidates): default Vec<T::AccountId>; // has holes
pub CandidateCount get(candidate_count): default u32;
pub Candidates get(candidates): Vec<T::AccountId>; // has holes
pub CandidateCount get(candidate_count): u32;
// temporary state (only relevant during finalisation/presentation)
/// The accounts holding the seats that will become free on the next tally.
pub NextFinalise get(next_finalise): (T::BlockNumber, u32, Vec<T::AccountId>);
pub NextFinalise get(next_finalise): Option<(T::BlockNumber, u32, Vec<T::AccountId>)>;
/// The stakes as they were at the point that the vote ended.
pub SnapshotedStakes get(snapshoted_stakes): required Vec<T::Balance>;
pub SnapshotedStakes get(snapshoted_stakes): Vec<T::Balance>;
/// Get the leaderboard if we;re in the presentation phase.
pub Leaderboard get(leaderboard): Vec<(T::Balance, T::AccountId)>; // ORDERED low -> high
pub Leaderboard get(leaderboard): Option<Vec<(T::Balance, T::AccountId)> >; // ORDERED low -> high
}
}
+9 -9
View File
@@ -18,7 +18,7 @@
use rstd::prelude::*;
use rstd::borrow::Borrow;
use primitives::traits::{OnFinalise, Hash};
use primitives::traits::{OnFinalise, Hash, As};
use runtime_io::print;
use srml_support::dispatch::Result;
use srml_support::{StorageValue, StorageMap, IsSubType};
@@ -43,20 +43,20 @@ decl_module! {
decl_storage! {
trait Store for Module<T: Trait> as CouncilVoting {
pub CooloffPeriod get(cooloff_period): required T::BlockNumber;
pub VotingPeriod get(voting_period): required T::BlockNumber;
pub Proposals get(proposals): required Vec<(T::BlockNumber, T::Hash)>; // ordered by expiry.
pub ProposalOf get(proposal_of): map [ T::Hash => T::Proposal ];
pub ProposalVoters get(proposal_voters): default map [ T::Hash => Vec<T::AccountId> ];
pub CouncilVoteOf get(vote_of): map [ (T::Hash, T::AccountId) => bool ];
pub VetoedProposal get(veto_of): map [ T::Hash => (T::BlockNumber, Vec<T::AccountId>) ];
pub CooloffPeriod get(cooloff_period) config(): T::BlockNumber = T::BlockNumber::sa(1000);
pub VotingPeriod get(voting_period) config(): T::BlockNumber = T::BlockNumber::sa(3);
pub Proposals get(proposals) build(|_| vec![0u8; 0]): Vec<(T::BlockNumber, T::Hash)>; // ordered by expiry.
pub ProposalOf get(proposal_of): map T::Hash => Option<T::Proposal>;
pub ProposalVoters get(proposal_voters): map T::Hash => Vec<T::AccountId>;
pub CouncilVoteOf get(vote_of): map (T::Hash, T::AccountId) => Option<bool>;
pub VetoedProposal get(veto_of): map T::Hash => Option<(T::BlockNumber, Vec<T::AccountId>)>;
}
}
/// An event in this module.
decl_event!(
pub enum Event<T> where <T as system::Trait>::Hash {
/// A voting tally has happened for a referendum cancelation vote.
/// A voting tally has happened for a referendum cancellation vote.
/// Last three are yes, no, abstain counts.
TallyCancelation(Hash, u32, u32, u32),
/// A voting tally has happened for a referendum vote.