Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

pallet-collective: allow customized default vote #6984

Merged
7 commits merged into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 258,
spec_version: 259,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -520,6 +520,7 @@ impl pallet_collective::Trait<CouncilCollective> for Runtime {
type MotionDuration = CouncilMotionDuration;
type MaxProposals = CouncilMaxProposals;
type MaxMembers = CouncilMaxMembers;
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = weights::pallet_collective::WeightInfo;
}

Expand Down Expand Up @@ -569,6 +570,7 @@ impl pallet_collective::Trait<TechnicalCollective> for Runtime {
type MotionDuration = TechnicalMotionDuration;
type MaxProposals = TechnicalMaxProposals;
type MaxMembers = TechnicalMaxMembers;
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = weights::pallet_collective::WeightInfo;
}

Expand Down
58 changes: 56 additions & 2 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,52 @@ pub type ProposalIndex = u32;
/// vote exactly once, therefore also the number of votes for any given motion.
pub type MemberCount = u32;

/// Default voting strategy when a member is inactive.
pub trait DefaultVote {
/// Get the default voting strategy, given:
///
/// - Whether the prime member voted Aye.
/// - Raw number of yes votes.
/// - Raw number of no votes.
/// - Total number of member count.
fn default_vote(
prime_voted_aye: bool,
yes_votes: MemberCount,
no_votes: MemberCount,
len: MemberCount,
) -> bool;
}

/// Set the prime member's vote as the default vote.
pub struct PrimeDefaultVote;

impl DefaultVote for PrimeDefaultVote {
fn default_vote(
prime_voted_aye: bool,
_yes_votes: MemberCount,
_no_votes: MemberCount,
_len: MemberCount,
) -> bool {
prime_voted_aye
}
}

/// First see if yes vote are over majority of the whole collective. If so, set the default vote
/// as yes. Otherwise, use the prime meber's vote as the default vote.
pub struct MoreThanMajorityThenPrimeDefaultVote;

impl DefaultVote for MoreThanMajorityThenPrimeDefaultVote {
fn default_vote(
prime_voted_aye: bool,
yes_votes: MemberCount,
_no_votes: MemberCount,
len: MemberCount,
) -> bool {
let more_than_majority = yes_votes * 2 > len;
more_than_majority || prime_voted_aye
}
}

pub trait WeightInfo {
fn set_members(m: u32, n: u32, p: u32, ) -> Weight;
fn execute(b: u32, m: u32, ) -> Weight;
Expand Down Expand Up @@ -110,6 +156,9 @@ pub trait Trait<I: Instance=DefaultInstance>: frame_system::Trait {
/// + This pallet assumes that dependents keep to the limit without enforcing it.
type MaxMembers: Get<MemberCount>;

/// Default vote strategy of this collective.
type DefaultVote: DefaultVote;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -587,8 +636,11 @@ decl_module! {
// Only allow actual closing of the proposal after the voting period has ended.
ensure!(system::Module::<T>::block_number() >= voting.end, Error::<T, I>::TooEarly);

// default to true only if there's a prime and they voted in favour.
let default = Self::prime().map_or(false, |who| voting.ayes.iter().any(|a| a == &who));
let prime_voted_aye = Self::prime()
.map_or(false, |who| voting.ayes.iter().any(|a| a == &who));

// default voting strategy.
let default = T::DefaultVote::default_vote(prime_voted_aye, yes_votes, no_votes, seats);

let abstentions = seats - (yes_votes + no_votes);
match default {
Expand Down Expand Up @@ -945,6 +997,7 @@ mod tests {
type MotionDuration = MotionDuration;
type MaxProposals = MaxProposals;
type MaxMembers = MaxMembers;
type DefaultVote = PrimeDefaultVote;
type WeightInfo = ();
}
impl Trait for Test {
Expand All @@ -954,6 +1007,7 @@ mod tests {
type MotionDuration = MotionDuration;
type MaxProposals = MaxProposals;
type MaxMembers = MaxMembers;
type DefaultVote = PrimeDefaultVote;
type WeightInfo = ();
}

Expand Down