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
63 changes: 38 additions & 25 deletions incubator/group/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,59 @@ package group

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type Codec struct {
codec.Marshaler

// Keep reference to the amino codec to allow backwards compatibility along
// with type, and interface registration.
amino *codec.Codec
}

func NewCodec(amino *codec.Codec) *Codec {
return &Codec{Marshaler: codec.NewHybridCodec(amino), amino: amino}
}

// ----------------------------------------------------------------------------

// RegisterCodec registers all the necessary crisis module concrete types and
// interfaces with the provided codec reference.
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*DecisionPolicy)(nil), nil)
cdc.RegisterConcrete(MsgCreateGroup{}, "cosmos-sdk/MsgCreateGroup", nil)
cdc.RegisterConcrete(MsgUpdateGroupMembers{}, "cosmos-sdk/MsgUpdateGroupMembers", nil)
cdc.RegisterConcrete(MsgUpdateGroupAdmin{}, "cosmos-sdk/MsgUpdateGroupAdmin", nil)
cdc.RegisterConcrete(MsgUpdateGroupComment{}, "cosmos-sdk/MsgUpdateGroupComment", nil)
cdc.RegisterConcrete(MsgCreateGroupAccountStd{}, "cosmos-sdk/MsgCreateGroupAccountStd", nil)
cdc.RegisterConcrete(MsgCreateGroupAccount{}, "cosmos-sdk/MsgCreateGroupAccount", nil)
cdc.RegisterConcrete(MsgVote{}, "cosmos-sdk/group/MsgVote", nil)
cdc.RegisterConcrete(MsgExec{}, "cosmos-sdk/group/MsgExec", nil)

// oh man... amino
cdc.RegisterConcrete(StdDecisionPolicy{}, "cosmos-sdk/StdDecisionPolicy", nil)
cdc.RegisterConcrete(&StdDecisionPolicy_Threshold{}, "cosmos-sdk/StdDecisionPolicy_Threshold", nil)
cdc.RegisterConcrete(ThresholdDecisionPolicy{}, "cosmos-sdk/ThresholdDecisionPolicy", nil)
cdc.RegisterInterface((*isStdDecisionPolicy_Sum)(nil), nil)
// cdc.RegisterConcrete(StdDecisionPolicy{}, "cosmos-sdk/StdDecisionPolicy", nil)
// cdc.RegisterConcrete(&StdDecisionPolicy_Threshold{}, "cosmos-sdk/StdDecisionPolicy_Threshold", nil)
cdc.RegisterConcrete(&ThresholdDecisionPolicy{}, "cosmos-sdk/ThresholdDecisionPolicy", nil)
// cdc.RegisterInterface((*isStdDecisionPolicy_Sum)(nil), nil)
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateGroup{},
&MsgUpdateGroupMembers{},
&MsgUpdateGroupComment{},
&MsgCreateGroupAccount{},
&MsgVote{},
&MsgExec{},
)
registry.RegisterInterface(
"cosmos_modules.incubator.group.v1_alpha.DecisionPolicy",
(*DecisionPolicy)(nil),
&ThresholdDecisionPolicy{},
)
}

// generic sealed codec to be used throughout module
var ModuleCdc *Codec
var (
amino = codec.New()

// 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())
)

func init() {
ModuleCdc = NewCodec(codec.New())
RegisterCodec(ModuleCdc.amino)
codec.RegisterCrypto(ModuleCdc.amino)
ModuleCdc.amino.Seal()
RegisterCodec(amino)
codec.RegisterCrypto(amino)
amino.Seal()
}
16 changes: 8 additions & 8 deletions incubator/group/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/cosmos/modules/incubator/group
go 1.13

require (
github.com/cosmos/cosmos-sdk v0.34.4-0.20200211145837-56c586897525
github.com/cosmos/cosmos-sdk v0.34.4-0.20200528030001-71770b5a7804
github.com/cosmos/modules/incubator/orm v0.0.0-20200117100147-88228b5fa693
github.com/gogo/protobuf v1.3.1
github.com/gorilla/mux v1.7.3
github.com/gorilla/mux v1.7.4
github.com/pkg/errors v0.9.1
github.com/regen-network/cosmos-proto v0.1.1-0.20200213154359-02baa11ea7c2
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
github.com/tendermint/tendermint v0.33.0
github.com/tendermint/tm-db v0.4.0
gopkg.in/yaml.v2 v2.2.8
github.com/regen-network/cosmos-proto v0.3.0
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.5.1
github.com/tendermint/tendermint v0.33.4
github.com/tendermint/tm-db v0.5.1
gopkg.in/yaml.v2 v2.3.0
)

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.1
Expand Down
430 changes: 407 additions & 23 deletions incubator/group/go.sum

Large diffs are not rendered by default.

86 changes: 39 additions & 47 deletions incubator/group/group.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package group

import (
"bytes"
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/modules/incubator/orm"
"github.com/gogo/protobuf/jsonpb"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -37,79 +35,73 @@ func AccountCondition(id uint64) Condition {
return NewCondition("group", "account", orm.EncodeSequence(id))
}

type AppModule struct {
keeper Keeper
}

func NewAppModule(keeper Keeper) AppModule {
return AppModule{
keeper: keeper,
}
type AppModuleBasic struct {
}

func (a AppModule) Name() string {
func (a AppModuleBasic) Name() string {
return ModuleName
}

func (a AppModule) RegisterCodec(cdc *codec.Codec) {
func (a AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
RegisterCodec(cdc) // can not be removed until sdk.StdTx support protobuf
}

func (a AppModule) DefaultGenesis() json.RawMessage {
var buf bytes.Buffer
marshaler := jsonpb.Marshaler{}
err := marshaler.Marshal(&buf, NewGenesisState())
if err != nil {
panic(errors.Wrap(err, "failed to marshal default genesis"))
}
return buf.Bytes()
func (a AppModuleBasic) DefaultGenesis(marshaler codec.JSONMarshaler) json.RawMessage {
return marshaler.MustMarshalJSON(NewGenesisState())
}

func (a AppModule) ValidateGenesis(bz json.RawMessage) error {
func (a AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error {
var data GenesisState
if err := jsonpb.Unmarshal(bytes.NewReader(bz), &data); err != nil {
return errors.Wrapf(err, "validate genesis")
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err)
}
return data.Validate()
}

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

func (a AppModule) GetTxCmd(*codec.Codec) *cobra.Command {
//return cli.GetTxCmd(StoreKey, cdc)
panic("implement me")
func (a AppModuleBasic) GetTxCmd(ctx context.CLIContext) *cobra.Command {
return nil
}

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

func (a AppModule) InitGenesis(ctx sdk.Context, bz json.RawMessage) []abci.ValidatorUpdate {
var data GenesisState
if err := jsonpb.Unmarshal(bytes.NewReader(bz), &data); err != nil {
panic(errors.Wrapf(err, "init genesis"))
// RegisterInterfaceTypes registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaceTypes(registry cdctypes.InterfaceRegistry) {
RegisterInterfaces(registry)
}

type AppModule struct {
AppModuleBasic
keeper Keeper
}

func NewAppModule(keeper Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
}
}

if err := data.Validate(); err != nil {
func (a AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
if err := genesisState.Validate(); err != nil {
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
}
a.keeper.setParams(ctx, data.Params)
a.keeper.setParams(ctx, genesisState.Params) // TODO: revisit if this makes sense
return []abci.ValidatorUpdate{}

}

func (a AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
var buf bytes.Buffer
marshaller := jsonpb.Marshaler{}
if err := marshaller.Marshal(&buf, ExportGenesis(ctx, a.keeper)); err != nil {
panic(errors.Wrap(err, "export genesis"))
}
return buf.Bytes()
func (a AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
return cdc.MustMarshalJSON(ExportGenesis(ctx, a.keeper))
}

func (a AppModule) RegisterInvariants(sdk.InvariantRegistry) {
Expand All @@ -136,5 +128,5 @@ func (a AppModule) NewQuerierHandler() sdk.Querier {
func (a AppModule) BeginBlock(sdk.Context, abci.RequestBeginBlock) {}

func (a AppModule) EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate {
return nil
return []abci.ValidatorUpdate{}
}
2 changes: 1 addition & 1 deletion incubator/group/group_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ func buildGroupResult(ctx sdk.Context, admin sdk.AccAddress, group GroupID, note
return &sdk.Result{
Data: group.Bytes(),
Log: fmt.Sprintf("Group %d %s", group, note),
Events: ctx.EventManager().Events(),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}
16 changes: 8 additions & 8 deletions incubator/group/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewHandler(k Keeper) sdk.Handler {
return handleMsgUpdateGroupComment(ctx, k, msg)
case MsgUpdateGroupMembers:
return handleMsgUpdateGroupMembers(ctx, k, msg)
case MsgCreateGroupAccountI:
return handleMsgCreateGroupAccountI(ctx, k, msg)
case MsgCreateGroupAccount:
return handleMsgCreateGroupAccount(ctx, k, msg)
case MsgVote:
return handleMsgVote(ctx, k, msg)
case MsgExec:
Expand All @@ -42,7 +42,7 @@ func handleMsgVote(ctx sdk.Context, k Keeper, msg MsgVote) (*sdk.Result, error)
// todo: event?
return &sdk.Result{
Log: fmt.Sprintf("Voted for proposal: %d", msg.Proposal),
Events: ctx.EventManager().Events(),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}

Expand All @@ -53,17 +53,17 @@ func handleMsgExec(ctx sdk.Context, k Keeper, msg MsgExec) (*sdk.Result, error)
// todo: event?
return &sdk.Result{
Log: fmt.Sprintf("Executed proposal: %d", msg.Proposal),
Events: ctx.EventManager().Events(),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}

func handleMsgCreateGroupAccountI(ctx sdk.Context, k Keeper, msg MsgCreateGroupAccountI) (*sdk.Result, error) {
func handleMsgCreateGroupAccount(ctx sdk.Context, k Keeper, msg MsgCreateGroupAccount) (*sdk.Result, error) {
decisionPolicy := msg.GetDecisionPolicy()
acc, err := k.CreateGroupAccount(ctx, msg.GetBase().Admin, msg.GetBase().Group, *decisionPolicy.GetThreshold(), msg.GetBase().Comment)
acc, err := k.CreateGroupAccount(ctx, msg.GetAdmin(), msg.GetGroup(), decisionPolicy, msg.GetComment())
if err != nil {
return nil, errors.Wrap(err, "create group account")
}
return buildGroupAccountResult(ctx, msg.GetBase().Admin, acc, "created")
return buildGroupAccountResult(ctx, msg.GetAdmin(), acc, "created")
}

func buildGroupAccountResult(ctx sdk.Context, admin sdk.AccAddress, acc sdk.AccAddress, note string) (*sdk.Result, error) {
Expand All @@ -77,6 +77,6 @@ func buildGroupAccountResult(ctx sdk.Context, admin sdk.AccAddress, acc sdk.AccA
return &sdk.Result{
Data: acc.Bytes(),
Log: fmt.Sprintf("Group account %s %s", acc.String(), note),
Events: ctx.EventManager().Events(),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}
Loading