Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve on dao #2947

Merged
merged 3 commits into from
Oct 13, 2021
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
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified vm/stdlib/compiled/latest/stdlib/22_Dao.mv
Binary file not shown.
Binary file modified vm/stdlib/compiled/latest/stdlib/44_DaoVoteScripts.mv
Binary file not shown.
Binary file not shown.
27 changes: 25 additions & 2 deletions vm/stdlib/modules/Dao.move
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,13 @@ module Dao {
let proposal = borrow_global_mut<Proposal<TokenT, ActionT>>(proposer_address);

// get vote
let my_vote = borrow_global_mut<Vote<TokenT>>(Signer::address_of(signer));
let my_vote = move_from<Vote<TokenT>>(Signer::address_of(signer));
{
assert(my_vote.proposer == proposer_address, Errors::invalid_argument(ERR_PROPOSER_MISMATCH));
assert(my_vote.id == proposal_id, Errors::invalid_argument(ERR_VOTED_OTHERS_ALREADY));
};
// revoke vote on proposal
let reverted_stake =do_revoke_vote(proposal, my_vote, voting_power);
let reverted_stake =do_revoke_vote(proposal, &mut my_vote, voting_power);
// emit vote changed event
let gov_info = borrow_global_mut<DaoGlobalInfo<TokenT>>(Token::token_address<TokenT>());
Event::emit_event(
Expand All @@ -507,6 +507,15 @@ module Dao {
vote: Token::value(&my_vote.stake),
},
);

// if user has no stake, destroy his vote. resolve https://github.com/starcoinorg/starcoin/issues/2925.
if (Token::value(&my_vote.stake) == 0u128) {
let Vote {stake, proposer: _, id: _, agree: _} = my_vote;
Token::destroy_zero(stake);
} else {
move_to(signer, my_vote);
};

reverted_stake
}

Expand Down Expand Up @@ -823,6 +832,20 @@ module Dao {
include CheckVoteOnProposal<TokenT>{vote, proposer_address, proposal_id};
}

/// Check whether voter has voted on proposal with `proposal_id` of `proposer_address`.
public fun has_vote<TokenT: copy + drop + store>(
voter: address,
proposer_address: address,
proposal_id: u64,
): bool acquires Vote {
if (!exists<Vote<TokenT>>(voter)) {
return false
};

let vote = borrow_global<Vote<TokenT>>(voter);
vote.proposer == proposer_address && vote.id == proposal_id
}

fun generate_next_proposal_id<TokenT: store>(): u64 acquires DaoGlobalInfo {
let gov_info = borrow_global_mut<DaoGlobalInfo<TokenT>>(Token::token_address<TokenT>());
let proposal_id = gov_info.next_proposal_id;
Expand Down
20 changes: 20 additions & 0 deletions vm/stdlib/modules/DaoVoteScripts.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ module DaoVoteScripts {
agree: bool,
votes: u128,
) {
let sender = Signer::address_of(&signer);
if (Dao::has_vote<Token>(sender, proposer_address, proposal_id)) {
// if already voted, and vote is not same as the current cast, change the existing vote.
// resolve https://github.com/starcoinorg/starcoin/issues/2925.
let (agree_voted, _) = Dao::vote_of<Token>(sender, proposer_address, proposal_id);
if (agree_voted != agree) {
Dao::change_vote<Token, ActionT>(&signer, proposer_address, proposal_id, agree);
}
};

let votes = Account::withdraw<Token>(&signer, votes);
Dao::cast_vote<Token, ActionT>(&signer, proposer_address, proposal_id, votes, agree);
}
Expand All @@ -33,6 +43,16 @@ module DaoVoteScripts {
Account::deposit(sender, my_token);
}

/// Let user change their vote during the voting time.
public(script) fun flip_vote<TokenT: copy + drop + store, ActionT: copy + drop + store>(
signer: signer,
proposer_address: address,
proposal_id: u64,
) {
let (agree, _) = Dao::vote_of<TokenT>(Signer::address_of(&signer), proposer_address, proposal_id);
Dao::change_vote<TokenT, ActionT>(&signer, proposer_address, proposal_id, !agree);
}

/// revoke some votes on a proposal
public ( script ) fun revoke_vote_of_power<Token: copy + drop + store, Action: copy + drop + store>(
signer: signer,
Expand Down
48 changes: 46 additions & 2 deletions vm/stdlib/modules/doc/Dao.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [Function `do_proposal_state`](#0x1_Dao_do_proposal_state)
- [Function `proposal_info`](#0x1_Dao_proposal_info)
- [Function `vote_of`](#0x1_Dao_vote_of)
- [Function `has_vote`](#0x1_Dao_has_vote)
- [Function `generate_next_proposal_id`](#0x1_Dao_generate_next_proposal_id)
- [Function `voting_delay`](#0x1_Dao_voting_delay)
- [Function `voting_period`](#0x1_Dao_voting_period)
Expand Down Expand Up @@ -913,13 +914,13 @@ Revoke some voting powers from vote on <code>proposal_id</code> of <code>propose
<b>let</b> proposal = borrow_global_mut&lt;<a href="Dao.md#0x1_Dao_Proposal">Proposal</a>&lt;TokenT, ActionT&gt;&gt;(proposer_address);

// get vote
<b>let</b> my_vote = borrow_global_mut&lt;<a href="Dao.md#0x1_Dao_Vote">Vote</a>&lt;TokenT&gt;&gt;(<a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(signer));
<b>let</b> my_vote = move_from&lt;<a href="Dao.md#0x1_Dao_Vote">Vote</a>&lt;TokenT&gt;&gt;(<a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(signer));
{
<b>assert</b>(my_vote.proposer == proposer_address, <a href="Errors.md#0x1_Errors_invalid_argument">Errors::invalid_argument</a>(<a href="Dao.md#0x1_Dao_ERR_PROPOSER_MISMATCH">ERR_PROPOSER_MISMATCH</a>));
<b>assert</b>(my_vote.id == proposal_id, <a href="Errors.md#0x1_Errors_invalid_argument">Errors::invalid_argument</a>(<a href="Dao.md#0x1_Dao_ERR_VOTED_OTHERS_ALREADY">ERR_VOTED_OTHERS_ALREADY</a>));
};
// revoke vote on proposal
<b>let</b> reverted_stake =<a href="Dao.md#0x1_Dao_do_revoke_vote">do_revoke_vote</a>(proposal, my_vote, voting_power);
<b>let</b> reverted_stake =<a href="Dao.md#0x1_Dao_do_revoke_vote">do_revoke_vote</a>(proposal, &<b>mut</b> my_vote, voting_power);
// emit vote changed event
<b>let</b> gov_info = borrow_global_mut&lt;<a href="Dao.md#0x1_Dao_DaoGlobalInfo">DaoGlobalInfo</a>&lt;TokenT&gt;&gt;(<a href="Token.md#0x1_Token_token_address">Token::token_address</a>&lt;TokenT&gt;());
<a href="Event.md#0x1_Event_emit_event">Event::emit_event</a>(
Expand All @@ -932,6 +933,15 @@ Revoke some voting powers from vote on <code>proposal_id</code> of <code>propose
vote: <a href="Token.md#0x1_Token_value">Token::value</a>(&my_vote.stake),
},
);

// <b>if</b> user has no stake, destroy his vote. resolve https://github.com/starcoinorg/starcoin/issues/2925.
<b>if</b> (<a href="Token.md#0x1_Token_value">Token::value</a>(&my_vote.stake) == 0u128) {
<b>let</b> <a href="Dao.md#0x1_Dao_Vote">Vote</a> {stake, proposer: _, id: _, agree: _} = my_vote;
<a href="Token.md#0x1_Token_destroy_zero">Token::destroy_zero</a>(stake);
} <b>else</b> {
move_to(signer, my_vote);
};

reverted_stake
}
</code></pre>
Expand Down Expand Up @@ -1304,6 +1314,40 @@ Get voter's vote info on proposal with <code>proposal_id</code> of <code>propose



</details>

<a name="0x1_Dao_has_vote"></a>

## Function `has_vote`

Check whether voter has voted on proposal with <code>proposal_id</code> of <code>proposer_address</code>.


<pre><code><b>public</b> <b>fun</b> <a href="Dao.md#0x1_Dao_has_vote">has_vote</a>&lt;TokenT: <b>copy</b>, drop, store&gt;(voter: address, proposer_address: address, proposal_id: u64): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="Dao.md#0x1_Dao_has_vote">has_vote</a>&lt;TokenT: <b>copy</b> + drop + store&gt;(
voter: address,
proposer_address: address,
proposal_id: u64,
): bool <b>acquires</b> <a href="Dao.md#0x1_Dao_Vote">Vote</a> {
<b>if</b> (!<b>exists</b>&lt;<a href="Dao.md#0x1_Dao_Vote">Vote</a>&lt;TokenT&gt;&gt;(voter)) {
<b>return</b> <b>false</b>
};

<b>let</b> vote = borrow_global&lt;<a href="Dao.md#0x1_Dao_Vote">Vote</a>&lt;TokenT&gt;&gt;(voter);
vote.proposer == proposer_address && vote.id == proposal_id
}
</code></pre>



</details>

<a name="0x1_Dao_generate_next_proposal_id"></a>
Expand Down
41 changes: 41 additions & 0 deletions vm/stdlib/modules/doc/DaoVoteScripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- [Function `cast_vote`](#0x1_DaoVoteScripts_cast_vote)
- [Function `revoke_vote`](#0x1_DaoVoteScripts_revoke_vote)
- [Function `flip_vote`](#0x1_DaoVoteScripts_flip_vote)
- [Function `revoke_vote_of_power`](#0x1_DaoVoteScripts_revoke_vote_of_power)
- [Function `unstake_vote`](#0x1_DaoVoteScripts_unstake_vote)
- [Specification](#@Specification_0)
Expand Down Expand Up @@ -42,6 +43,16 @@
agree: bool,
votes: u128,
) {
<b>let</b> sender = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(&signer);
<b>if</b> (<a href="Dao.md#0x1_Dao_has_vote">Dao::has_vote</a>&lt;<a href="Token.md#0x1_Token">Token</a>&gt;(sender, proposer_address, proposal_id)) {
// <b>if</b> already voted, and vote is not same <b>as</b> the current cast, change the existing vote.
// resolve https://github.com/starcoinorg/starcoin/issues/2925.
<b>let</b> (agree_voted, _) = <a href="Dao.md#0x1_Dao_vote_of">Dao::vote_of</a>&lt;<a href="Token.md#0x1_Token">Token</a>&gt;(sender, proposer_address, proposal_id);
<b>if</b> (agree_voted != agree) {
<a href="Dao.md#0x1_Dao_change_vote">Dao::change_vote</a>&lt;<a href="Token.md#0x1_Token">Token</a>, ActionT&gt;(&signer, proposer_address, proposal_id, agree);
}
};

<b>let</b> votes = <a href="Account.md#0x1_Account_withdraw">Account::withdraw</a>&lt;<a href="Token.md#0x1_Token">Token</a>&gt;(&signer, votes);
<a href="Dao.md#0x1_Dao_cast_vote">Dao::cast_vote</a>&lt;<a href="Token.md#0x1_Token">Token</a>, ActionT&gt;(&signer, proposer_address, proposal_id, votes, agree);
}
Expand Down Expand Up @@ -81,6 +92,36 @@ revoke all votes on a proposal



</details>

<a name="0x1_DaoVoteScripts_flip_vote"></a>

## Function `flip_vote`

Let user change their vote during the voting time.


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="DaoVoteScripts.md#0x1_DaoVoteScripts_flip_vote">flip_vote</a>&lt;TokenT: <b>copy</b>, drop, store, ActionT: <b>copy</b>, drop, store&gt;(signer: signer, proposer_address: address, proposal_id: u64)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="DaoVoteScripts.md#0x1_DaoVoteScripts_flip_vote">flip_vote</a>&lt;TokenT: <b>copy</b> + drop + store, ActionT: <b>copy</b> + drop + store&gt;(
signer: signer,
proposer_address: address,
proposal_id: u64,
) {
<b>let</b> (agree, _) = <a href="Dao.md#0x1_Dao_vote_of">Dao::vote_of</a>&lt;TokenT&gt;(<a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(&signer), proposer_address, proposal_id);
<a href="Dao.md#0x1_Dao_change_vote">Dao::change_vote</a>&lt;TokenT, ActionT&gt;(&signer, proposer_address, proposal_id, !agree);
}
</code></pre>



</details>

<a name="0x1_DaoVoteScripts_revoke_vote_of_power"></a>
Expand Down