Skip to content

Commit

Permalink
refactor(x/gov): swap vote alias (#19718)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Mar 12, 2024
1 parent fea88d1 commit d808ef8
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 87 deletions.
62 changes: 31 additions & 31 deletions api/cosmos/gov/v1/gov.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions x/gov/client/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ func TestNormalizeWeightedVoteOptions(t *testing.T) {
}{
"simple Yes": {
options: "Yes",
normalized: "VOTE_OPTION_ONE=1",
normalized: "VOTE_OPTION_YES=1",
},
"simple yes": {
options: "yes",
normalized: "VOTE_OPTION_ONE=1",
normalized: "VOTE_OPTION_YES=1",
},
"formal yes": {
options: "yes=1",
normalized: "VOTE_OPTION_ONE=1",
normalized: "VOTE_OPTION_YES=1",
},
"half yes half no": {
options: "yes=0.5,no=0.5",
normalized: "VOTE_OPTION_ONE=0.5,VOTE_OPTION_THREE=0.5",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.5",
},
"3 options": {
options: "Yes=0.5,No=0.4,NoWithVeto=0.1",
normalized: "VOTE_OPTION_ONE=0.5,VOTE_OPTION_THREE=0.4,VOTE_OPTION_FOUR=0.1",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.4,VOTE_OPTION_NO_WITH_VETO=0.1",
},
"zero weight option": {
options: "Yes=0.5,No=0.5,NoWithVeto=0",
normalized: "VOTE_OPTION_ONE=0.5,VOTE_OPTION_THREE=0.5,VOTE_OPTION_FOUR=0",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.5,VOTE_OPTION_NO_WITH_VETO=0",
},
"minus weight option": {
options: "Yes=0.5,No=0.6,NoWithVeto=-0.1",
normalized: "VOTE_OPTION_ONE=0.5,VOTE_OPTION_THREE=0.6,VOTE_OPTION_FOUR=-0.1",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.6,VOTE_OPTION_NO_WITH_VETO=-0.1",
},
"empty options": {
options: "",
Expand Down
10 changes: 5 additions & 5 deletions x/gov/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func (suite *KeeperTestSuite) TestMsgVoteWeighted() {
voter: proposer,
metadata: "",
expErr: true,
expErrMsg: `option:VOTE_OPTION_ONE weight:"0.000000000000000000" : invalid vote option`,
expErrMsg: `option:VOTE_OPTION_YES weight:"0.000000000000000000" : invalid vote option`,
},
"negative weight": {
preRun: func() uint64 {
Expand All @@ -738,7 +738,7 @@ func (suite *KeeperTestSuite) TestMsgVoteWeighted() {
voter: proposer,
metadata: "",
expErr: true,
expErrMsg: `option:VOTE_OPTION_ONE weight:"-1.000000000000000000" : invalid vote option`,
expErrMsg: `option:VOTE_OPTION_YES weight:"-1.000000000000000000" : invalid vote option`,
},
"individual weight > 1 but weights sum == 1": {
preRun: func() uint64 {
Expand All @@ -751,7 +751,7 @@ func (suite *KeeperTestSuite) TestMsgVoteWeighted() {
voter: proposer,
metadata: "",
expErr: true,
expErrMsg: `option:VOTE_OPTION_ONE weight:"2.000000000000000000" : invalid vote option`,
expErrMsg: `option:VOTE_OPTION_YES weight:"2.000000000000000000" : invalid vote option`,
},
"empty options": {
preRun: func() uint64 {
Expand Down Expand Up @@ -1368,7 +1368,7 @@ func (suite *KeeperTestSuite) TestLegacyVoteWeighted() {
voter: proposer,
metadata: "",
expErr: true,
expErrMsg: `option:VOTE_OPTION_ONE weight:"0.000000000000000000" : invalid vote option`,
expErrMsg: `option:VOTE_OPTION_YES weight:"0.000000000000000000" : invalid vote option`,
},
"negative weight": {
preRun: func() uint64 {
Expand All @@ -1383,7 +1383,7 @@ func (suite *KeeperTestSuite) TestLegacyVoteWeighted() {
voter: proposer,
metadata: "",
expErr: true,
expErrMsg: `option:VOTE_OPTION_ONE weight:"-1.000000000000000000" : invalid vote option`,
expErrMsg: `option:VOTE_OPTION_YES weight:"-1.000000000000000000" : invalid vote option`,
},
"empty options": {
preRun: func() uint64 {
Expand Down
16 changes: 8 additions & 8 deletions x/gov/proto/cosmos/gov/v1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ enum VoteOption {

// VOTE_OPTION_UNSPECIFIED defines a no-op vote option.
VOTE_OPTION_UNSPECIFIED = 0;
// VOTE_OPTION_ONE defines the first proposal vote option.
VOTE_OPTION_ONE = 1;
// VOTE_OPTION_YES defines the yes proposal vote option.
VOTE_OPTION_YES = 1;
// VOTE_OPTION_TWO defines the second proposal vote option.
VOTE_OPTION_TWO = 2;
// VOTE_OPTION_ONE defines the first proposal vote option.
VOTE_OPTION_ONE = 1;
// VOTE_OPTION_ABSTAIN defines the abstain proposal vote option.
VOTE_OPTION_ABSTAIN = 2;
// VOTE_OPTION_THREE defines the third proposal vote option.
VOTE_OPTION_THREE = 3;
// VOTE_OPTION_TWO defines the second proposal vote option.
VOTE_OPTION_TWO = 2;
// VOTE_OPTION_NO defines the no proposal vote option.
VOTE_OPTION_NO = 3;
// VOTE_OPTION_FOUR defines the fourth proposal vote option.
VOTE_OPTION_FOUR = 4;
// VOTE_OPTION_THREE defines the third proposal vote option.
VOTE_OPTION_THREE = 3;
// VOTE_OPTION_NO_WITH_VETO defines the no with veto proposal vote option.
VOTE_OPTION_NO_WITH_VETO = 4;
// VOTE_OPTION_FOUR defines the fourth proposal vote option.
VOTE_OPTION_FOUR = 4;
// VOTE_OPTION_SPAM defines the spam proposal vote option.
VOTE_OPTION_SPAM = 5;
}
Expand Down
72 changes: 36 additions & 36 deletions x/gov/types/v1/gov.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions x/gov/types/v1/vote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v1_test

import (
"testing"

"github.com/stretchr/testify/require"

v1 "cosmossdk.io/x/gov/types/v1"

codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

func TestVoteAlias(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

testCases := []struct {
name string
input string
expected v1.MsgVote
expectedErrMsg string
}{
{
name: "valid vote",
input: `{"proposal_id":"1","voter":"cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66","option":"VOTE_OPTION_YES","metadata":"test"}`,
expected: v1.MsgVote{
ProposalId: 1,
Voter: "cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66",
Option: v1.VoteOption_VOTE_OPTION_YES,
Metadata: "test",
},
},
{
name: "valid vote alias",
input: `{"proposal_id":"1","voter":"cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66","option":"VOTE_OPTION_ONE","metadata":"test"}`,
expected: v1.MsgVote{
ProposalId: 1,
Voter: "cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66",
Option: v1.VoteOption_VOTE_OPTION_ONE,
Metadata: "test",
},
},
{
name: "invalid vote",
input: `{"proposal_id":"1","voter":"cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66","option":"VOTE_OPTION_HELLO","metadata":"test"}`,
expectedErrMsg: "unknown value \"VOTE_OPTION_HELLO\" for enum cosmos.gov.v1.VoteOption",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vote := &v1.MsgVote{}
err := cdc.UnmarshalJSON([]byte(tc.input), vote)
if tc.expectedErrMsg != "" {
require.ErrorContains(t, err, tc.expectedErrMsg)
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit d808ef8

Please sign in to comment.