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

feat: Add percentage decision policy to x/group #11072

Merged
merged 39 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6c1bc70
wip: add percentage decision policy
likhita-809 Jan 31, 2022
8100480
wip
likhita-809 Jan 31, 2022
156017e
wip
likhita-809 Jan 31, 2022
65fcd91
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Jan 31, 2022
76239c6
add cli tests
likhita-809 Feb 2, 2022
0d525c0
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 2, 2022
2ac7f9c
fix failing tests
likhita-809 Feb 2, 2022
5bc9332
add keeper tests
likhita-809 Feb 2, 2022
75580c0
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 2, 2022
1785490
address review comments
likhita-809 Feb 3, 2022
e224780
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 3, 2022
ae62daf
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 3, 2022
799c74f
Merge branch 'master' into likhita/add-percentage-decision-policy
likhita-809 Feb 3, 2022
b8137d7
Merge branch 'master' into likhita/add-percentage-decision-policy
likhita-809 Feb 3, 2022
cd320dc
fix something
likhita-809 Feb 3, 2022
6726b86
Merge branch 'likhita/add-percentage-decision-policy' of https://gith…
likhita-809 Feb 3, 2022
0fbfc04
Merge branch 'master' into likhita/add-percentage-decision-policy
likhita-809 Feb 4, 2022
213e20b
Merge branch 'master' into likhita/add-percentage-decision-policy
likhita-809 Feb 4, 2022
d4e0eae
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 4, 2022
0e864bb
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 4, 2022
d72f399
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 4, 2022
b023357
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 7, 2022
dbd44df
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 8, 2022
60af0f4
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 8, 2022
94d902f
address review comments
likhita-809 Feb 9, 2022
bd70b3d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 9, 2022
cc9ce90
nit
likhita-809 Feb 9, 2022
74a7ea3
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 9, 2022
58d9413
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 9, 2022
d9588c7
refactor tests
likhita-809 Feb 9, 2022
d19bbab
address review comments
likhita-809 Feb 9, 2022
ba7ff96
address review comments
likhita-809 Feb 10, 2022
0195d11
remove logs
likhita-809 Feb 10, 2022
445fc4d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Feb 10, 2022
ebb9c48
Merge branch 'master' into likhita/add-percentage-decision-policy
atheeshp Feb 10, 2022
aa1e46e
Merge branch 'master' into likhita/add-percentage-decision-policy
likhita-809 Feb 11, 2022
95dc544
Merge branch 'likhita/add-percentage-decision-policy' of https://gith…
likhita-809 Feb 11, 2022
2742a8e
merge branch master
likhita-809 Feb 11, 2022
27704c2
Revert "merge branch master"
likhita-809 Feb 11, 2022
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
697 changes: 633 additions & 64 deletions api/cosmos/group/v1beta1/types.pulsar.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions proto/cosmos/group/v1beta1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ message ThresholdDecisionPolicy {
google.protobuf.Duration timeout = 2 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
}

// PercentageDecisionPolicy implements the DecisionPolicy interface
message PercentageDecisionPolicy {
option (cosmos_proto.implements_interface) = "DecisionPolicy";

// percentage is the minimum percentage the weighted sum of yes votes must met for a proposal to succeed.
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
string percentage = 1;

// timeout is the duration from submission of a proposal to the end of voting period
// Within this times votes and exec messages can be submitted.
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
google.protobuf.Duration timeout = 2 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
}

// Choice defines available types of choices for voting.
enum Choice {

Expand Down
3 changes: 3 additions & 0 deletions x/group/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ Note, the '--from' flag is ignored as it is implied from [admin].
Example:
$ %s tx group create-group-policy [admin] [group-id] [metadata] \
'{"@type":"/cosmos.group.v1beta1.ThresholdDecisionPolicy", "threshold":"1", "timeout":"1s"}'

Here, we can use percentage decision policy when needed, where 0 < percentage <= 1.
Ex: '{"@type":"/cosmos.group.v1beta1.PercentageDecisionPolicy", "percentage":"0.5", "timeout":"1s"}'
`,
version.AppName,
),
Expand Down
2 changes: 2 additions & 0 deletions x/group/client/testutil/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func (s *IntegrationTestSuite) TestQueryGroupPoliciesByGroup() {
s.groupPolicies[2],
s.groupPolicies[3],
s.groupPolicies[4],
s.groupPolicies[5],
},
},
}
Expand Down Expand Up @@ -443,6 +444,7 @@ func (s *IntegrationTestSuite) TestQueryGroupPoliciesByAdmin() {
s.groupPolicies[2],
s.groupPolicies[3],
s.groupPolicies[4],
s.groupPolicies[5],
},
},
}
Expand Down
114 changes: 113 additions & 1 deletion x/group/client/testutil/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,29 @@ func (s *IntegrationTestSuite) SetupSuite() {
out, err = cli.ExecTestCLICmd(val.ClientCtx, client.QueryGroupPoliciesByGroupCmd(), []string{"1", fmt.Sprintf("--%s=json", tmcli.OutputFlag)})
s.Require().NoError(err, out.String())
}
percentage := 0.5
// create group policy with percentage decision policy
out, err = cli.ExecTestCLICmd(val.ClientCtx, client.MsgCreateGroupPolicyCmd(),
append(
[]string{
val.Address.String(),
"1",
validMetadata,
fmt.Sprintf("{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"%f\", \"timeout\":\"30000s\"}", percentage),
},
commonFlags...,
),
)
s.Require().NoError(err, out.String())
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txResp), out.String())
s.Require().Equal(uint32(0), txResp.Code, out.String())

out, err = cli.ExecTestCLICmd(val.ClientCtx, client.QueryGroupPoliciesByGroupCmd(), []string{"1", fmt.Sprintf("--%s=json", tmcli.OutputFlag)})
s.Require().NoError(err, out.String())

var res group.QueryGroupPoliciesByGroupResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
s.Require().Equal(len(res.GroupPolicies), 5)
s.Require().Equal(len(res.GroupPolicies), 6)
s.groupPolicies = res.GroupPolicies

// create a proposal
Expand Down Expand Up @@ -706,6 +725,22 @@ func (s *IntegrationTestSuite) TestTxCreateGroupPolicy() {
&sdk.TxResponse{},
0,
},
{
"correct data with percentage decision policy",
append(
[]string{
val.Address.String(),
fmt.Sprintf("%v", groupID),
validMetadata,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"0.5\", \"timeout\":\"1s\"}",
},
commonFlags...,
),
false,
"",
&sdk.TxResponse{},
0,
},
{
"with amino-json",
append(
Expand Down Expand Up @@ -771,6 +806,38 @@ func (s *IntegrationTestSuite) TestTxCreateGroupPolicy() {
&sdk.TxResponse{},
0,
},
{
"invalid percentage decision policy with negative value",
append(
[]string{
val.Address.String(),
fmt.Sprintf("%v", groupID),
validMetadata,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"-0.5\", \"timeout\":\"1s\"}",
},
commonFlags...,
),
true,
"expected a positive decimal",
&sdk.TxResponse{},
0,
},
{
"invalid percentage decision policy with value greater than 1",
append(
[]string{
val.Address.String(),
fmt.Sprintf("%v", groupID),
validMetadata,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"2\", \"timeout\":\"1s\"}",
},
commonFlags...,
),
true,
"percentage must be > 0 and <= 1",
&sdk.TxResponse{},
0,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -931,6 +998,21 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupPolicyDecisionPolicy() {
&sdk.TxResponse{},
0,
},
{
"correct data with percentage decision policy",
append(
[]string{
groupPolicy.Admin,
groupPolicy.Address,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"0.5\", \"timeout\":\"40000s\"}",
},
commonFlags...,
),
false,
"",
&sdk.TxResponse{},
0,
},
{
"with amino-json",
append(
Expand Down Expand Up @@ -977,6 +1059,36 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupPolicyDecisionPolicy() {
&sdk.TxResponse{},
0,
},
{
"invalid percentage decision policy with negative value",
append(
[]string{
groupPolicy.Admin,
groupPolicy.Address,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"-0.5\", \"timeout\":\"1s\"}",
},
commonFlags...,
),
true,
"expected a positive decimal",
&sdk.TxResponse{},
0,
},
{
"invalid percentage decision policy with value greater than 1",
append(
[]string{
groupPolicy.Admin,
groupPolicy.Address,
"{\"@type\":\"/cosmos.group.v1beta1.PercentageDecisionPolicy\", \"percentage\":\"2\", \"timeout\":\"40000s\"}",
},
commonFlags...,
),
true,
"percentage must be > 0 and <= 1",
&sdk.TxResponse{},
0,
},
}

for _, tc := range testCases {
Expand Down
2 changes: 2 additions & 0 deletions x/group/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterInterface((*DecisionPolicy)(nil), nil)
cdc.RegisterConcrete(&ThresholdDecisionPolicy{}, "cosmos-sdk/ThresholdDecisionPolicy", nil)
cdc.RegisterConcrete(&PercentageDecisionPolicy{}, "cosmos-sdk/PercentageDecisionPolicy", nil)
cdc.RegisterConcrete(&MsgCreateGroup{}, "cosmos-sdk/MsgCreateGroup", nil)
cdc.RegisterConcrete(&MsgUpdateGroupMembers{}, "cosmos-sdk/MsgUpdateGroupMembers", nil)
cdc.RegisterConcrete(&MsgUpdateGroupAdmin{}, "cosmos-sdk/MsgUpdateGroupAdmin", nil)
Expand Down Expand Up @@ -47,6 +48,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
"cosmos.group.v1beta1.DecisionPolicy",
(*DecisionPolicy)(nil),
&ThresholdDecisionPolicy{},
&PercentageDecisionPolicy{},
)
}

Expand Down
19 changes: 19 additions & 0 deletions x/group/internal/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (x Dec) Int64() (int64, error) {
return x.dec.Int64()
}

func (x Dec) Float64() (float64, error) {
return x.dec.Float64()
}
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved

func (x Dec) Cmp(y Dec) int {
return x.dec.Cmp(&y.dec)
}
Expand All @@ -101,6 +105,21 @@ func Add(x Dec, y Dec) (Dec, error) {
return x.Add(y)
}

var dec128Context = apd.Context{
Precision: 34,
MaxExponent: apd.MaxExponent,
MinExponent: apd.MinExponent,
Traps: apd.DefaultTraps,
}

// Quo returns a new Dec with value `x/y` (formatted as decimal128, 34 digit precision) without mutating any
// argument and error if there is an overflow.
func (x Dec) Quo(y Dec) (Dec, error) {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
var z Dec
_, err := dec128Context.Quo(&z.dec, &x.dec, &y.dec)
return z, sdkerrors.Wrap(err, "decimal quotient error")
}

func (x Dec) IsZero() bool {
return x.dec.IsZero()
}
Expand Down
4 changes: 4 additions & 0 deletions x/group/internal/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestDec(t *testing.T) {
require.NoError(t, err)
require.True(t, res.IsEqual(minusFivePointZero))

res, err = four.Quo(two)
require.NoError(t, err)
require.True(t, res.IsEqual(two))

require.False(t, zero.IsNegative())
require.False(t, one.IsNegative())
require.True(t, minusOne.IsNegative())
Expand Down
Loading