Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Update group to use Any #71

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions incubator/group/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
var (
amino = codec.New()

// ModuleCdc references the global group module codec. Note, the codec
// moduleCdc references the global group module codec. Note, the codec
// should ONLY be used in certain instances of tests and for JSON encoding as Amino
// is still used for that purpose.
//
// The actual codec used for serialization should be provided to group and
// defined at the application level.
ModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry())
moduleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry())
)

func init() {
Expand Down
8 changes: 4 additions & 4 deletions incubator/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func (a AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMess
}

func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, r *mux.Router) {
//rest.RegisterRoutes(ctx, r, ModuleCdc, RouterKey)
//rest.RegisterRoutes(ctx, r, moduleCdc, RouterKey)
// todo: what client functions do we want to support?
panic("implement me")
return
}

func (a AppModuleBasic) GetTxCmd(ctx context.CLIContext) *cobra.Command {
panic("implement me")
return &cobra.Command{}
blushi marked this conversation as resolved.
Show resolved Hide resolved
}

func (a AppModuleBasic) GetQueryCmd(*codec.Codec) *cobra.Command {
//return cli.GetQueryCmd(StoreKey, cdc)
panic("implement me")
return &cobra.Command{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}

// RegisterInterfaceTypes registers module concrete types into protobuf Any.
Expand Down
21 changes: 14 additions & 7 deletions incubator/group/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ func (k Keeper) setParams(ctx sdk.Context, params Params) {

// CreateGroupAccount creates and persists a `GroupAccountMetadata`
func (k Keeper) CreateGroupAccount(ctx sdk.Context, admin sdk.AccAddress, groupID GroupID, policy DecisionPolicy, comment string) (sdk.AccAddress, error) {
// func (k Keeper) CreateGroupAccount(ctx sdk.Context, admin sdk.AccAddress, groupID GroupID, policy ThresholdDecisionPolicy, comment string) (sdk.AccAddress, error) {
maxCommentSize := k.MaxCommentSize(ctx)
if len(comment) > maxCommentSize {
return nil, errors.Wrap(ErrMaxLimit,
Expand Down Expand Up @@ -486,6 +485,7 @@ func (k Keeper) CreateProposal(ctx sdk.Context, accountAddress sdk.AccAddress, c
}

account, err := k.GetGroupAccount(ctx, accountAddress.Bytes())

if err != nil {
return 0, errors.Wrap(err, "load group account")
}
Expand All @@ -510,8 +510,20 @@ func (k Keeper) CreateProposal(ctx sdk.Context, accountAddress sdk.AccAddress, c
if err != nil {
return 0, errors.Wrap(err, "block time conversion")
}

policy := account.GetDecisionPolicy()
timeout := policy.GetTimout()

if policy == nil {
return 0, errors.Wrap(ErrEmpty, "nil policy")
}

// prevent proposal that can not succeed
err = policy.Validate(g)
if err != nil {
return 0, err
}

timeout := policy.GetTimeout()
window, err := types.DurationFromProto(&timeout)
if err != nil {
return 0, errors.Wrap(err, "maxVotingWindow time conversion")
Expand All @@ -521,11 +533,6 @@ func (k Keeper) CreateProposal(ctx sdk.Context, accountAddress sdk.AccAddress, c
return 0, errors.Wrap(err, "end time conversion")
}

// prevent proposal that can not succeed
if policy != nil && policy.GetThreshold().GT(g.TotalWeight) {
return 0, errors.Wrap(ErrInvalid, "policy threshold should not be greater than the total group weight")
}

m := reflect.New(k.proposalModelType).Interface().(ProposalI)
m.SetBase(ProposalBase{
GroupAccount: accountAddress,
Expand Down
4 changes: 2 additions & 2 deletions incubator/group/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ func TestCreateProposal(t *testing.T) {
VetoCount: sdk.ZeroDec(),
}, base.VoteState)

timout, err := types.TimestampFromProto(&base.Timeout)
timeout, err := types.TimestampFromProto(&base.Timeout)
require.NoError(t, err)
assert.Equal(t, blockTime.Add(time.Second).UTC(), timout)
assert.Equal(t, blockTime.Add(time.Second).UTC(), timeout)

if spec.srcMsgs == nil { // then empty list is ok
assert.Len(t, proposal.GetMsgs(), 0)
Expand Down
8 changes: 2 additions & 6 deletions incubator/group/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,8 @@ func (m MsgCreateGroupAccount) GetSigners() []sdk.AccAddress {

// GetSignBytes returns the bytes for the message signer to sign on
func (m MsgCreateGroupAccount) GetSignBytes() []byte {
var buf bytes.Buffer
enc := jsonpb.Marshaler{}
if err := enc.Marshal(&buf, &m); err != nil {
panic(errors.Wrap(err, "get sign bytes"))
}
return sdk.MustSortJSON(buf.Bytes())
bz := moduleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}

// ValidateBasic does a sanity check on the provided data
Expand Down
6 changes: 3 additions & 3 deletions incubator/group/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ func TestMsgCreateGroupAccount(t *testing.T) {
group: 1,
expErr: true,
},
"decision policy without timout": {
"decision policy without timeout": {
admin: myAddr,
group: 1,
threshold: sdk.ZeroDec(),
expErr: true,
},
"decision policy with invalid timout": {
"decision policy with invalid timeout": {
admin: myAddr,
group: 1,
threshold: sdk.ZeroDec(),
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestMsgCreateGroupAccount(t *testing.T) {
"any comment",
&ThresholdDecisionPolicy{
Threshold: spec.threshold,
Timout: spec.timeout,
Timeout: spec.timeout,
},
)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions incubator/group/testdata/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func RegisterCodec(cdc *codec.Codec) {
var (
amino = codec.New()

// ModuleCdc references the global x/transfer module codec. Note, the codec
// moduleCdc references the global x/transfer module codec. Note, the codec
// should ONLY be used in certain instances of tests and for JSON encoding as Amino
// is still used for that purpose.
//
// The actual codec used for serialization should be provided to x/transfer and
// defined at the application level.
ModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry())
moduleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry())
)

func init() {
Expand Down
20 changes: 11 additions & 9 deletions incubator/group/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ type DecisionPolicyResult struct {
type DecisionPolicy interface {
orm.Persistent
orm.Validateable
GetThreshold() sdk.Dec
GetTimout() types.Duration
GetTimeout() types.Duration
Allow(tally Tally, totalPower sdk.Dec, votingDuration time.Duration) (DecisionPolicyResult, error)
Validate(g GroupMetadata) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a godoc here explaining why GroupMetadata is passed in?

}

// Implements DecisionPolicy Interface
Expand All @@ -68,7 +68,7 @@ func NewThresholdDecisionPolicy(threshold sdk.Dec, timeout types.Duration) Decis

// Allow allows a proposal to pass when the tally of yes votes equals or exceeds the threshold before the timeout.
func (p ThresholdDecisionPolicy) Allow(tally Tally, totalPower sdk.Dec, votingDuration time.Duration) (DecisionPolicyResult, error) {
timeout, err := types.DurationFromProto(&p.Timout)
timeout, err := types.DurationFromProto(&p.Timeout)
if err != nil {
return DecisionPolicyResult{}, err
}
Expand All @@ -90,10 +90,13 @@ func (p *ThresholdDecisionPolicy) GetThreshold() sdk.Dec {
return p.Threshold
}

// GetTimout returns the policy threshold
// func (p ThresholdDecisionPolicy) GetTimout() types.Duration {
// return p.Timout
// }
// Validate returns an error if policy threshold is greater than the total group weight
func (p *ThresholdDecisionPolicy) Validate(g GroupMetadata) error {
if p.GetThreshold().GT(g.TotalWeight) {
return errors.Wrap(ErrInvalid, "policy threshold should not be greater than the total group weight")
}
return nil
}

func (p ThresholdDecisionPolicy) ValidateBasic() error {
if p.Threshold.IsNil() {
Expand All @@ -102,7 +105,7 @@ func (p ThresholdDecisionPolicy) ValidateBasic() error {
if p.Threshold.LT(sdk.OneDec()) {
return errors.Wrap(ErrInvalid, "threshold")
}
timeout, err := types.DurationFromProto(&p.Timout)
timeout, err := types.DurationFromProto(&p.Timeout)
if err != nil {
return errors.Wrap(err, "timeout")
}
Expand Down Expand Up @@ -147,7 +150,6 @@ func NewGroupAccountMetadata(groupAccount sdk.AccAddress, group GroupID, admin s
}

p.DecisionPolicy = any

return p, nil
}

Expand Down
Loading