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

Upgrade to current sdk master #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
47 changes: 27 additions & 20 deletions incubator/group/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ 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) {
Expand All @@ -36,12 +24,31 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*isStdDecisionPolicy_Sum)(nil), nil)
}

// generic sealed codec to be used throughout module
var ModuleCdc *Codec
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateGroup{},
&MsgUpdateGroupMembers{},
&MsgUpdateGroupComment{},
&MsgCreateGroupAccountStd{},
&MsgVote{},
&MsgExec{},
)
}

var (
amino = codec.New()

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

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
428 changes: 405 additions & 23 deletions incubator/group/go.sum

Large diffs are not rendered by default.

78 changes: 35 additions & 43 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
type AppModuleBasic struct {
}

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

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) {
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")
}

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

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

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
}
6 changes: 3 additions & 3 deletions incubator/group/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +53,7 @@ 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
}

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
}
16 changes: 7 additions & 9 deletions incubator/group/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

func createTestApp(isCheckTx bool) (*testdata.SimApp, sdk.Context) {
db := dbm.NewMemDB()
app := testdata.NewSimApp(log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0)
genesisState := testdata.ModuleBasics.DefaultGenesis()
app := testdata.NewSimApp(log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, "", 0)
genesisState := testdata.ModuleBasics.DefaultGenesis(app.AppCodec())
stateBytes, err := codec.MarshalJSONIndent(app.Codec(), genesisState)
if err != nil {
panic(err)
Expand Down Expand Up @@ -100,8 +100,7 @@ func TestCreateGroupScenario(t *testing.T) {
accSeq, err := app.AccountKeeper.GetSequence(ctx, myAddr)
require.NoError(t, err)
tx := types.NewTestTx(ctx, []sdk.Msg{spec.src}, privs, []uint64{accNums}, []uint64{accSeq}, fee)

resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.Codec().MustMarshalBinaryLengthPrefixed(tx)})
resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.MustEncodeTx(tx)})
// then
require.Equal(t, spec.expCode, resp.Code, resp.Log)
if spec.expCode != 0 {
Expand Down Expand Up @@ -201,8 +200,7 @@ func TestCreateGroupAccountScenario(t *testing.T) {
accSeq, err := app.AccountKeeper.GetSequence(ctx, myAddr)
require.NoError(t, err)
tx := types.NewTestTx(ctx, msgs, privs, []uint64{accNums}, []uint64{accSeq}, fee)

resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.Codec().MustMarshalBinaryLengthPrefixed(tx)})
resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.MustEncodeTx(tx)})
// then
require.Equal(t, spec.expCode, resp.Code, resp.Log)
if spec.expCode != 0 {
Expand Down Expand Up @@ -304,7 +302,7 @@ func TestFullProposalWorkflow(t *testing.T) {
privs, accNums, seqs := []crypto.PrivKey{myKey}, myAccount.GetAccountNumber(), myAccount.GetSequence()
tx := types.NewTestTx(ctx, msgs, privs, []uint64{accNums}, []uint64{seqs}, fee)

resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.Codec().MustMarshalBinaryLengthPrefixed(tx)})
resp := app.DeliverTx(abci.RequestDeliverTx{Tx: app.MustEncodeTx(tx)})
require.Equal(t, uint32(0), resp.Code, resp.Log)

// execute can not be in the same block so start new one
Expand All @@ -321,7 +319,7 @@ func TestFullProposalWorkflow(t *testing.T) {
privs, accNums, seqs = []crypto.PrivKey{myKey}, myAccount.GetAccountNumber(), myAccount.GetSequence()
tx = types.NewTestTx(ctx, msgs, privs, []uint64{accNums}, []uint64{seqs}, fee)

resp = app.DeliverTx(abci.RequestDeliverTx{Tx: app.Codec().MustMarshalBinaryLengthPrefixed(tx)})
resp = app.DeliverTx(abci.RequestDeliverTx{Tx: app.MustEncodeTx(tx)})
require.Equal(t, uint32(0), resp.Code, resp.Log)

// then verify proposal got accepted
Expand All @@ -343,7 +341,7 @@ func TestFullProposalWorkflow(t *testing.T) {
privs, accNums, seqs = []crypto.PrivKey{myKey}, myAccount.GetAccountNumber(), myAccount.GetSequence()
tx = types.NewTestTx(ctx, msgs, privs, []uint64{accNums}, []uint64{seqs}, fee)

resp = app.DeliverTx(abci.RequestDeliverTx{Tx: app.Codec().MustMarshalBinaryLengthPrefixed(tx)})
resp = app.DeliverTx(abci.RequestDeliverTx{Tx: app.MustEncodeTx(tx)})
require.Equal(t, uint32(0), resp.Code, resp.Log)

// verify second proposal
Expand Down
21 changes: 7 additions & 14 deletions incubator/group/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/modules/incubator/group"
"github.com/cosmos/modules/incubator/group/testdata"
"github.com/cosmos/modules/incubator/orm"
Expand All @@ -20,9 +19,8 @@ import (
)

func TestCreateGroup(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
k := group.NewGroupKeeper(groupKey, paramSpace, baseapp.NewRouter(), &group.MockProposalI{})
Expand Down Expand Up @@ -107,9 +105,8 @@ func TestCreateGroup(t *testing.T) {
}

func TestCreateGroupAccount(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
k := group.NewGroupKeeper(groupKey, paramSpace, baseapp.NewRouter(), &group.MockProposalI{})
Expand Down Expand Up @@ -199,9 +196,8 @@ func TestCreateGroupAccount(t *testing.T) {
}

func TestCreateProposal(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
k := group.NewGroupKeeper(groupKey, paramSpace, baseapp.NewRouter(), &testdata.MyAppProposal{})
Expand Down Expand Up @@ -351,9 +347,8 @@ func TestCreateProposal(t *testing.T) {
}

func TestVote(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
k := group.NewGroupKeeper(groupKey, paramSpace, baseapp.NewRouter(), &testdata.MyAppProposal{})
Expand Down Expand Up @@ -612,9 +607,8 @@ func TestVote(t *testing.T) {
}

func TestExecProposal(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

router := baseapp.NewRouter()
groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
Expand Down Expand Up @@ -901,9 +895,8 @@ func TestExecProposal(t *testing.T) {
}

func TestLoadParam(t *testing.T) {
amino := codec.New()
pKey, pTKey := sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey)
paramSpace := subspace.NewSubspace(amino, pKey, pTKey, group.DefaultParamspace)
paramSpace := paramtypes.NewSubspace(group.NewCodec(), pKey, pTKey, group.DefaultParamspace)

groupKey := sdk.NewKVStoreKey(group.StoreKeyName)
k := group.NewGroupKeeper(groupKey, paramSpace, baseapp.NewRouter(), &group.MockProposalI{})
Expand Down
Loading