Vote合约

circle-info

当前主网的Vote合约为1.0.0版本,非dev版本,分支见release/1.0.0

投票合约用于构建一个关于投票的项目。主要关注VotingItem这个message:

message VotingItem {
    // The voting activity id.
    aelf.Hash voting_item_id = 1;
    // The token symbol which will be accepted.
    string accepted_currency = 2;
    // Whether the vote will lock token.
    bool is_lock_token = 3;
    // The current snapshot number.
    int64 current_snapshot_number = 4;
    // The total snapshot number.
    int64 total_snapshot_number = 5;
    // The list of options.
    repeated string options = 6;
    // The register time of the voting activity.
    google.protobuf.Timestamp register_timestamp = 7;
    // The start time of the voting.
    google.protobuf.Timestamp start_timestamp = 8;
    // The end time of the voting.
    google.protobuf.Timestamp end_timestamp = 9;
    // The start time of current round of the voting.
    google.protobuf.Timestamp current_snapshot_start_timestamp = 10;
    // The sponsor address of the voting activity.
    aelf.Address sponsor = 11;
}
  • voting_item_id:投票项目的唯一标识。

  • accepted_currency:这个投票项目接受什么类型的Token(必须是系统MultiToken合约创建的Token)。

  • is_lock_token:投票者进行投票的同时是否锁定这一笔Token。

circle-info

但凡投票,都是需要锁仓的,只是如果is_lock_token为true,是让Vote合约进行锁仓;反之,需要其他系统合约代为操作锁仓。

由于Vote合约操作的锁仓并不会判断时间,因此涉及选举锁仓,会交给Election合约进行,也就是说选举投票项目的is_lock_token是false的。

  • current_snapshot_number:初始为1,每当当前投票项目做一次快照,自增1。

  • total_snapshot_number:注册投票项目的时候可以设置最多能做几次快照。

  • options:投票项目的选项。

  • register_timestamp:投票项目的注册时间。

  • start_timestamp:可以开始投票的时间。

  • current_snapshot_start_timestamp:上一次做快照的时间。

  • sponsor:投票的发起人。

其次关注两个与VotingItem相关的数据结构:

  • VotingResult:每次对投票结果做快照,实际上会生成这样一个实例,存入State.VotingResults

  • VotingRecord:用户每次投票,都会生成这样一个记录,存入State.VotingRecords

还有一个与投票用户相关的State:State.VotedItemsMap,key为投票者地址,value为一个VotedItems类型,其中保存了该用户参与的投票项目的Id,和针对每一个投票项目投出的票的VoteId。

注册投票项目 - Register

这个方法实际上只做了两回事:

  • 初始化一个VotingItem实例,插入State.VotingItems

  • 初始化一个VotingResult实例,插入State.VotingResults

State.TokenContract.IsInWhiteList的调用实际上是希望input.AcceptedCurrency在创建的时候,将Vote合约加入了白名单,这样Vote合约就能够无需用户同意,通过MultiToken合约的Lock方法锁定用户指定数额的代币。(见MultiToken合约名词解释中的相关词条)

投票 - Vote

投票行为除了会往StateDb增加一个VotingRecord实例外,还会

  • 修改相应投票项目的当前的VotingResult,以及

  • State.VotedItemsMap中为该用户增加一个VoteId记录。

赎回 - Withdraw

赎回是Vote的镜像操作,不过关于操作权限这块,会参考投票项目的IsLockToken字段:

  • 如果为true,意味着这笔投票的锁仓是交给Vote合约完成的,此时检查Sender是否为Voter自身即可;

  • 如果为false,意味着这笔投票的锁仓是交给投票项目的Sponsor完成的,此时检查Sender是否为Sponsor。

快照 - TakeSnapshot

做了三件事:

  1. 查一下当前投票结果,存为上一期;

  2. State.VotingItems中对应VotingItem的SnapshotNumber递增1;

  3. 初始化下一期的投票结果。

操作投票选项 - AddOption / AddOptions / RemoveOption / RemoveOptions

判断调用者是否为Sponsor,随后操作State.VotingItems中对应VotingItem的Option字段。

Last updated