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

x/gov v0.43 Audit updates #9258

Merged
merged 10 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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: 2 additions & 2 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5480,7 +5480,7 @@ MsgVoteResponse defines the Msg/Vote response type.
<a name="cosmos.gov.v1beta1.MsgVoteWeighted"></a>

### MsgVoteWeighted
MsgVote defines a message to cast a vote.
MsgVoteWeighted defines a message to cast a vote.


| Field | Type | Label | Description |
Expand Down Expand Up @@ -5519,7 +5519,7 @@ Msg defines the bank Msg service.
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `SubmitProposal` | [MsgSubmitProposal](#cosmos.gov.v1beta1.MsgSubmitProposal) | [MsgSubmitProposalResponse](#cosmos.gov.v1beta1.MsgSubmitProposalResponse) | SubmitProposal defines a method to create new proposal given a content. | |
| `Vote` | [MsgVote](#cosmos.gov.v1beta1.MsgVote) | [MsgVoteResponse](#cosmos.gov.v1beta1.MsgVoteResponse) | Vote defines a method to add a vote on a specific proposal. | |
| `VoteWeighted` | [MsgVoteWeighted](#cosmos.gov.v1beta1.MsgVoteWeighted) | [MsgVoteWeightedResponse](#cosmos.gov.v1beta1.MsgVoteWeightedResponse) | WeightedVote defines a method to add a weighted vote on a specific proposal. | |
| `VoteWeighted` | [MsgVoteWeighted](#cosmos.gov.v1beta1.MsgVoteWeighted) | [MsgVoteWeightedResponse](#cosmos.gov.v1beta1.MsgVoteWeightedResponse) | VoteWeighted defines a method to add a weighted vote on a specific proposal. | |
| `Deposit` | [MsgDeposit](#cosmos.gov.v1beta1.MsgDeposit) | [MsgDepositResponse](#cosmos.gov.v1beta1.MsgDepositResponse) | Deposit defines a method to add deposit on a specific proposal. | |

<!-- end services -->
Expand Down
12 changes: 6 additions & 6 deletions proto/cosmos/gov/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ service Msg {
// Vote defines a method to add a vote on a specific proposal.
rpc Vote(MsgVote) returns (MsgVoteResponse);

// WeightedVote defines a method to add a weighted vote on a specific proposal.
// VoteWeighted defines a method to add a weighted vote on a specific proposal.
rpc VoteWeighted(MsgVoteWeighted) returns (MsgVoteWeightedResponse);

// Deposit defines a method to add deposit on a specific proposal.
Expand Down Expand Up @@ -58,21 +58,21 @@ message MsgVote {
VoteOption option = 3;
}

// MsgVote defines a message to cast a vote.
// MsgVoteResponse defines the Msg/Vote response type.
message MsgVoteResponse {}

// MsgVoteWeighted defines a message to cast a vote.
message MsgVoteWeighted {
option (gogoproto.equal) = false;
option (gogoproto.goproto_stringer) = false;
option (gogoproto.stringer) = false;
option (gogoproto.goproto_getters) = false;

uint64 proposal_id = 1 [(gogoproto.jsontag) = "proposal_id", (gogoproto.moretags) = "yaml:\"proposal_id\""];
uint64 proposal_id = 1 [(gogoproto.moretags) = "yaml:\"proposal_id\""];
string voter = 2;
repeated WeightedVoteOption options = 3 [(gogoproto.nullable) = false];
}

// MsgVoteResponse defines the Msg/Vote response type.
message MsgVoteResponse {}

// MsgVoteWeightedResponse defines the Msg/VoteWeighted response type.
message MsgVoteWeightedResponse {}

Expand Down
2 changes: 1 addition & 1 deletion x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func NewCmdWeightedVote() *cobra.Command {
find the proposal-id by running "%s query gov proposals".

Example:
$ %s tx gov vote 1 yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05 --from mykey
$ %s tx gov weighted-vote 1 yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05 --from mykey
`,
version.AppName, version.AppName,
),
Expand Down
10 changes: 5 additions & 5 deletions x/gov/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ func TestImportExportQueues_ErrorUnconsistentState(t *testing.T) {
gov.InitGenesis(ctx, app.AccountKeeper, app.BankKeeper, app.GovKeeper, &types.GenesisState{
Deposits: types.Deposits{
{
1234,
"me",
sdk.Coins{
{
ProposalId: 1234,
Depositor: "me",
Amount: sdk.Coins{
sdk.NewCoin(
"stake",
sdk.NewInt(1234),
},
),
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ func TestDeposits(t *testing.T) {
require.False(t, found)
require.Equal(t, addr0Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
require.Equal(t, addr1Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[1]))

// Test delete deposits
_, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake)
require.NoError(t, err)
app.GovKeeper.DeleteDeposits(ctx, proposalID)
deposits = app.GovKeeper.GetDeposits(ctx, proposalID)
require.Len(t, deposits, 0)
}
6 changes: 2 additions & 4 deletions x/gov/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func TestHooks(t *testing.T) {

govHooksReceiver := MockGovHooksReceiver{}

app.GovKeeper = *keeper.UpdateHooks(&app.GovKeeper,
types.NewMultiGovHooks(
&govHooksReceiver,
),
keeper.UnsafeSetHooks(
&app.GovKeeper, types.NewMultiGovHooks(&govHooksReceiver),
)

require.False(t, govHooksReceiver.AfterProposalSubmissionValid)
Expand Down
6 changes: 4 additions & 2 deletions x/gov/keeper/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package keeper

import "github.com/cosmos/cosmos-sdk/x/gov/types"

func UpdateHooks(k *Keeper, h types.GovHooks) *Keeper {
// UnsafeSetHooks updates the gov keepers hooks, overriding any potential
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// pre-existing hooks.
// WARNING: this function should only be used in tests.
func UnsafeSetHooks(k *Keeper, h types.GovHooks) {
k.hooks = h
return k
}
9 changes: 5 additions & 4 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"fmt"
"math/rand"

"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -159,7 +157,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := keeper.NewMigrator(am.keeper)
cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the gov module. It returns
Expand Down
18 changes: 16 additions & 2 deletions x/gov/spec/01_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ proposals:
`SoftwareUpgradeProposals`.
- `CommunityPoolSpendProposal` details a proposal for use of community funds,
together with how many coins are proposed to be spent, and to which recipient account.
- `ParameterChangeProposal` defines a proposal to change one or
more parameters. If accepted, the requested parameter change is updated
- `ParameterChangeProposal` defines a proposal to change one or
more parameters. If accepted, the requested parameter change is updated
automatically by the proposal handler upon conclusion of the voting period.
- `CancelSoftwareUpgradeProposal` is a gov Content type for cancelling a software upgrade.

Expand Down Expand Up @@ -118,6 +118,20 @@ proposal but accept the result of the vote.
_Note: from the UI, for urgent proposals we should maybe add a ‘Not Urgent’
option that casts a `NoWithVeto` vote._

### Weighted Votes

[ADR-037](../../../docs/architecture/adr-037-gov-split-vote.md) introduces the weighted vote feature which allows a staker to split their votes into several voting options. For example, it could use 70% of its voting power to vote Yes and 30% of its voting power to vote No.

Often times the entity owning that address might not be a single individual. For example, a company might have different stakeholders who want to vote differently, and so it makes sense to allow them to split their voting power. Currently, it is not possible for them to do "passthrough voting" and giving their users voting rights over their tokens. However, with this system, exchanges can poll their users for voting preferences, and then vote on-chain proportionally to the results of the poll.

To represent weighted vote on chain, we use the following Protobuf message.

+++ https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-alpha1/proto/cosmos/gov/v1beta1/gov.proto#L32-L40

+++ https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-alpha1/proto/cosmos/gov/v1beta1/gov.proto#L126-L137

For a weighted vote to be valid, the `options` field must not contain duplicate vote options, and the weights of all options must be equal to 1.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

### Quorum

Quorum is defined as the minimum percentage of voting power that needs to be
Expand Down
3 changes: 3 additions & 0 deletions x/gov/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func TestMsgVoteWeighted(t *testing.T) {
}, false},
{0, addrs[0], WeightedVoteOptions{}, false},
{0, addrs[0], NewNonSplitVoteOption(VoteOption(0x13)), false},
{0, addrs[0], WeightedVoteOptions{ // weight sum <1
WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDecWithPrec(5, 1)},
}, false},
}

for i, tc := range tests {
Expand Down
3 changes: 1 addition & 2 deletions x/gov/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewProposal(content Content, id uint64, submitTime, depositEndTime time.Tim
}

p := Proposal{
Content: any,
ProposalId: id,
Status: StatusDepositPeriod,
FinalTallyResult: EmptyTallyResult(),
Expand All @@ -37,8 +38,6 @@ func NewProposal(content Content, id uint64, submitTime, depositEndTime time.Tim
DepositEndTime: depositEndTime,
}

p.Content = any

return p, nil
}

Expand Down
Loading