substrate3.0中的democracy模块

democracy模块(全民公投)

说明

  • 这个模块用于公众投票, 凡是substrate中有ensure_root权限要求的, 都可以通过这个模块来执行。
  • 发起提案 -> 附议 -> 每个周期去查看, 哪个附仪最多 -> 进入公投阶段
  • 只有进入Preimages的提案才能执行
  • 不能同时是验证人和提名人

存储

  • PublicProps 目前的公投
  • ReferendumInfoOf 公投的信息

常量

  • MinimumDeposit 提案需要的最小抵押金额

数据结构

/// 投票的参数
pub enum AccountVote<Balance> {
    /// A standard vote, one-way (approve or reject) with a given amount of conviction.
    /// 只选一方来投
    Standard { vote: Vote, balance: Balance },
    /// A split vote with balances given for both ways, and with no conviction, useful for
    /// parachains when voting.
    /// 两边都投
    Split { aye: Balance, nay: Balance },
}
pub struct Vote {
    pub aye: bool,
    pub conviction: Conviction,
}
/// 公投状态
pub struct ReferendumStatus<BlockNumber, Hash, Balance> {
    /// When voting on this referendum will end.
    pub(crate) end: BlockNumber,
    /// The hash of the proposal being voted on.
    pub(crate) proposal_hash: Hash,
    /// The thresholding mechanism to determine whether it passed.
    pub(crate) threshold: VoteThreshold,
    /// The delay (in blocks) to wait after a successful referendum before deploying.
    pub(crate) delay: BlockNumber,
    /// The current tally of votes in this referendum.
    /// 这次全民公决目前的计票结果
    pub(crate) tally: Tally<Balance>,
}
/// A means of determining if a vote is past pass threshold.
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, sp_runtime::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum VoteThreshold {
    /// A supermajority of approvals is needed to pass this vote.
    SuperMajorityApprove,
    /// A supermajority of rejects is needed to fail this vote.
    SuperMajorityAgainst,
    /// A simple majority of approvals is needed to pass this vote.
    SimpleMajority,
}

重要方法(对外接口)

  1. 提公投提案

    • 代码 `pub fn propose(
      origin: OriginFor<T>,
      proposal_hash: T::Hash,

      [pallet::compact] value: BalanceOf<T>,

      )`
    • 参数
      • proposal_hash 提按hash
      • value 愿意为这个提案抵押的金额
    • 逻辑
      • value要大于最小要求金额
      • 目前的公投列表长度有限制
      • 这个提案的hash如果在黑名单中, 那么必须要到期才能重新提

      问题 为什么没有重复提案的查询???
      ***

  2. 附议

    • 代码 `pub fn second(
      origin: OriginFor<T>,

      [pallet::compact] proposal: PropIndex,

      [pallet::compact] seconds_upper_bound: u32,

      )`
    • 参数
      • proposal 提案索引
      • seconds_upper_bound (没啥卵用的东西 尽量设置大一点)
    • 逻辑
      • 抵押跟提按发起人相同的金额(更新相关存储DepositOf)

  3. 对提案进行公投

    • 代码 `pub fn vote(
      origin: OriginFor<T>,

      [pallet::compact] ref_index: ReferendumIndex,

      vote: AccountVote<BalanceOf<T>>,
      )`
    • 参数
      • ref_index 公投索引
      • vote 投票方式(赞成与反对, 投票金额)
    • 逻辑
      • 投票金额一定要小于等于自己的自由余额
      • 不能是已经委托的
      • 锁仓投票金额

  4. 紧急取消公投

    • 代码 `pub fn cancel_referendum(
      origin: OriginFor<T>,

      [pallet::compact] ref_index: ReferendumIndex,

      )`
    • 参数
      • ref_index 公投索引
    • 逻辑
      • 至少2/3议会权限
      • 不能重复操作
      • 删除公投

  5. todo

    • 代码 fn external_propose(origin: OriginFor<T>, proposal_hash: T::Hash)
    • 参数
      • proposal_hash 提案hash
    • 逻辑
      • NextExternal不存在
      • 如果在黑名单中, 那么过期时间到才能操作
      • <NextExternal<T>>::put((proposal_hash, VoteThreshold::SuperMajorityApprove));

    这个可能是用来快速把提案送到公投队列


  6. todo

    • 代码 pub fn external_propose_majority( origin: OriginFor<T>, proposal_hash: T::Hash, )
    • 参数
      • proposal_hash 提案hash
    • 逻辑
      • 3/4议会权限
      • 强制更改 NextExternal
      • <NextExternal<T>>::put((proposal_hash, VoteThreshold::SimpleMajority));

  7. todo

    • 代码 pub fn external_propose_default( origin: OriginFor<T>, proposal_hash: T::Hash, )
    • 参数
      • proposal_hash 提案hash
    • 逻辑
      • 1/1议会权限
      • 强制更改 NextExternal
      • <NextExternal<T>>::put((proposal_hash, VoteThreshold::SuperMajorityAgainst))

  8. 快速通道(把提案迅速提到公投)

    • 代码 pub fn fast_track( origin: OriginFor<T>, proposal_hash: T::Hash, voting_period: T::BlockNumber, delay: T::BlockNumber, )
    • 参数
      • proposal_hash 提案hash
      • voting_period 多久开始
      • delay 开始后多久结束
    • 逻辑
      • 技术委员会权限(至少2/3)
      • NextExternal 存在
      • VoteThreshold::SuperMajorityApprove 这个不能走快速通道
      • <NextExternal<T>>::kill();

  9. 终止外部提案

    • 代码 pub fn veto_external(origin: OriginFor<T>, proposal_hash: T::Hash)
    • 参数
      • proposal_hash 提案hash
    • 逻辑
      • 任一技术委员会成员
      • NextExternal 存在
      • 把提案列入黑名单

  10. 取消一个公投

    • 代码 `pub fn cancel_referendum(
      origin: OriginFor<T>,

      [pallet::compact] ref_index: ReferendumIndex,

      )`
    • 参数
      • ref_index 公投索引
    • 逻辑
      • root权限
      • 删除公投信息

  11. 取消一个正在等待通过的公投

    • pub fn cancel_queued(origin: OriginFor<T>, which: ReferendumIndex)

辅助方法

  • 执行方法(公投通过)
    • 代码 fn do_enact_proposal(proposal_hash: T::Hash, index: ReferendumIndex) -> DispatchResult
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容