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

fix(group): add group members weight checks (backport #13869) #13880

Merged
merged 4 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (x/group) [#13869](https://github.com/cosmos/cosmos-sdk/pull/13869) Group members weight must be positive and a finite number.
* (x/bank) [#13821](https://github.com/cosmos/cosmos-sdk/pull/13821) Fix bank store migration of coin metadata.
* (x/group) [#13808](https://github.com/cosmos/cosmos-sdk/pull/13808) Fix propagation of message events to the current context in `EndBlocker`.
* (x/gov) [#13728](https://github.com/cosmos/cosmos-sdk/pull/13728) Fix propagation of message events to the current context in `EndBlocker`.
Expand Down
13 changes: 12 additions & 1 deletion x/group/internal/math/dec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package math provides helper functions for doing mathematical calculations and parsing for the ecocredit module.
// Package math provides helper functions for doing mathematical calculations and parsing for the group module.
package math

import (
Expand Down Expand Up @@ -46,11 +46,22 @@ func (x Dec) IsPositive() bool {
return !x.dec.Negative && !x.dec.IsZero()
}

func (x Dec) IsFinite() bool {
return x.dec.Form != apd.Finite
}

// NewDecFromString returns a new Dec from a string
// It only support finite numbers, not NaN, +Inf, -Inf
func NewDecFromString(s string) (Dec, error) {
d, _, err := apd.NewFromString(s)
if err != nil {
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
}

if d.Form != apd.Finite {
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a finite decimal, got %s", s)
}

return Dec{*d}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions x/group/internal/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func TestDec(t *testing.T) {
require.NoError(t, err)
minusFivePointZero, err := NewDecFromString("-5.0")
require.NoError(t, err)
_, err = NewDecFromString("inf")
require.Error(t, err)
_, err = NewDecFromString("Infinite")
require.Error(t, err)
_, err = NewDecFromString("foo")
require.Error(t, err)
_, err = NewDecFromString("NaN")
require.Error(t, err)

res, err := two.Add(zero)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions x/group/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ func (k Keeper) TallyProposalsAtVPEnd(ctx sdk.Context) error {
return err
}
} else {
err = k.doTallyAndUpdate(ctx, &proposal, electorate, policyInfo) //nolint:gosec // implicit memory aliasing in for loop
if err != nil {
if err := k.doTallyAndUpdate(ctx, &proposal, electorate, policyInfo); err != nil {
return sdkerrors.Wrap(err, "doTallyAndUpdate")
}

Expand Down
20 changes: 20 additions & 0 deletions x/group/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,26 @@ func (s *TestSuite) TestCreateGroup() {
},
expErr: true,
},
"invalid member weight - Inf": {
req: &group.MsgCreateGroup{
Admin: addr1.String(),
Members: []group.MemberRequest{{
Address: addr3.String(),
Weight: "inf",
}},
},
expErr: true,
},
"invalid member weight - NaN": {
req: &group.MsgCreateGroup{
Admin: addr1.String(),
Members: []group.MemberRequest{{
Address: addr3.String(),
Weight: "NaN",
}},
},
expErr: true,
},
}

var seq uint32 = 1
Expand Down
12 changes: 5 additions & 7 deletions x/group/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,13 @@ func (k Keeper) doTallyAndUpdate(ctx sdk.Context, p *group.Proposal, electorate

sinceSubmission := ctx.BlockTime().Sub(p.SubmitTime) // duration passed since proposal submission.
result, err := policy.Allow(tallyResult, electorate.TotalWeight, sinceSubmission)
// If the result was final (i.e. enough votes to pass) or if the voting
// period ended, then we consider the proposal as final.
isFinal := result.Final || ctx.BlockTime().After(p.VotingPeriodEnd)

switch {
case err != nil:
if err != nil {
return sdkerrors.Wrap(err, "policy allow")
}

case isFinal:
// If the result was final (i.e. enough votes to pass) or if the voting
// period ended, then we consider the proposal as final.
if isFinal := result.Final || ctx.BlockTime().After(p.VotingPeriodEnd); isFinal {
if err := k.pruneVotes(ctx, p.Id); err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions x/group/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
if err := k.TallyProposalsAtVPEnd(ctx); err != nil {
panic(err)
}
pruneProposals(ctx, k)
}

func pruneProposals(ctx sdk.Context, k keeper.Keeper) {
err := k.PruneProposals(ctx)
if err != nil {
if err := k.PruneProposals(ctx); err != nil {
Fixed Show fixed Hide fixed
panic(err)
}
}
5 changes: 1 addition & 4 deletions x/group/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,7 @@ func (m MsgCreateGroupPolicy) GetSignBytes() []byte {

// GetSigners returns the expected signers for a MsgCreateGroupPolicy.
func (m MsgCreateGroupPolicy) GetSigners() []sdk.AccAddress {
admin, err := sdk.AccAddressFromBech32(m.Admin)
if err != nil {
panic(err)
}
admin := sdk.MustAccAddressFromBech32(m.Admin)
return []sdk.AccAddress{admin}
}

Expand Down