diff --git a/CHANGELOG.md b/CHANGELOG.md index cf662126f3..2da5ff3f31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Changed - [#1350](https://github.com/regen-network/regen-ledger/pull/1350) Move application entry point to root directory -- [#1357](https://github.com/regen-network/regen-ledger/pull/1350) Migrate from custom module manager to sdk module manager +- [#1357](https://github.com/regen-network/regen-ledger/pull/1357) Migrate from custom module manager to sdk module manager #### Removed @@ -61,15 +61,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### API Breaking Changes +- [#1337](https://github.com/regen-network/regen-ledger/pull/1337) The `NewModule` method in `ecocredit/module` requires an `authority` address. - [#1337](https://github.com/regen-network/regen-ledger/pull/1337) The `NewKeeper` method in `ecocredit/core` requires an `authority` address. - [#1337](https://github.com/regen-network/regen-ledger/pull/1337) The `AddCreditType` proposal handler has been removed. - [#1342](https://github.com/regen-network/regen-ledger/pull/1342) The `NewKeeper` method in `ecocredit/marketplace` requires an `authority` address. - [#1342](https://github.com/regen-network/regen-ledger/pull/1342) The `AllowedDenom` proposal handler has been removed. - +- [#1357](https://github.com/regen-network/regen-ledger/pull/#1357) The `NewModule` method in `ecocredit/module` requires a store `key`. #### Added -- [#1337](https://github.com/regen-network/regen-ledger/pull/1342) Add `AddAllowedDenom` msg-based gov proposal +- [#1337](https://github.com/regen-network/regen-ledger/pull/1337) Add `AddAllowedDenom` msg-based gov proposal - [#1337](https://github.com/regen-network/regen-ledger/pull/1337) Add `AddCreditType` msg-based gov proposal - [#1346](https://github.com/regen-network/regen-ledger/pull/1346) Add `RemoveAllowedDenom` msg-based gov proposal - [#1349](https://github.com/regen-network/regen-ledger/pull/1349) Add `UpdateBasketFees` msg-based gov proposal @@ -94,7 +95,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1423](https://github.com/regen-network/regen-ledger/pull/1423) Add `CreditClassAllowlistEnabled` params query - [#1450](https://github.com/regen-network/regen-ledger/pull/1450) Add `QueryBalancesByBatch` query - #### Changed - [#1362](https://github.com/regen-network/regen-ledger/pull/1362) Update `Batch` state validation checks @@ -129,6 +129,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1429](https://github.com/regen-network/regen-ledger/pull/1429) Migrated `ecocredit/client` core commands to `ecocredit/base/client` - [#1429](https://github.com/regen-network/regen-ledger/pull/1429) Migrated `ecocredit/server/core` to `ecocredit/base/keeper` - [#1429](https://github.com/regen-network/regen-ledger/pull/1429) Migrated `ecocredit/simulation` core operations to `ecocredit/base/simulation` +- [#1452](https://github.com/regen-network/regen-ledger/pull/1452) Migrated `BeginBlocker` to `ecocredit/module` ### Fixed @@ -146,7 +147,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1258](https://github.com/regen-network/regen-ledger/pull/1258) Remove group module in favor of Cosmos SDK group module -## [v4.0.1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.1) - TBD +## [v4.0.1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.1) - 2022-08-30 ### x/ecocredit diff --git a/app/app.go b/app/app.go index ee7db86375..9da3e9cad9 100644 --- a/app/app.go +++ b/app/app.go @@ -366,10 +366,10 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest // register custom modules here ecocreditMod := ecocreditmodule.NewModule( app.keys[ecocredit.ModuleName], - app.GetSubspace(ecocredit.DefaultParamspace), + authtypes.NewModuleAddress(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, - authtypes.NewModuleAddress(govtypes.ModuleName), + app.GetSubspace(ecocredit.DefaultParamspace), ) dataMod := datamodule.NewModule(app.keys[data.ModuleName], app.AccountKeeper, app.BankKeeper) diff --git a/x/ecocredit/base/keeper/keeper.go b/x/ecocredit/base/keeper/keeper.go index df09a7ef5f..8ec68ec4ce 100644 --- a/x/ecocredit/base/keeper/keeper.go +++ b/x/ecocredit/base/keeper/keeper.go @@ -10,8 +10,10 @@ import ( types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1" ) -var _ types.MsgServer = &Keeper{} -var _ types.QueryServer = &Keeper{} +var ( + _ types.MsgServer = &Keeper{} + _ types.QueryServer = &Keeper{} +) type Keeper struct { stateStore api.StateStore diff --git a/x/ecocredit/basket/keeper/keeper.go b/x/ecocredit/basket/keeper/keeper.go index d15990e088..677a3db905 100644 --- a/x/ecocredit/basket/keeper/keeper.go +++ b/x/ecocredit/basket/keeper/keeper.go @@ -9,6 +9,11 @@ import ( types "github.com/regen-network/regen-ledger/x/ecocredit/basket/types/v1" ) +var ( + _ types.MsgServer = Keeper{} + _ types.QueryServer = Keeper{} +) + // Keeper is the basket keeper. type Keeper struct { stateStore api.StateStore @@ -19,9 +24,6 @@ type Keeper struct { authority sdk.AccAddress } -var _ types.MsgServer = Keeper{} -var _ types.QueryServer = Keeper{} - // NewKeeper returns a new keeper instance. func NewKeeper( ss api.StateStore, diff --git a/x/ecocredit/marketplace/keeper/keeper.go b/x/ecocredit/marketplace/keeper/keeper.go index 1c76efc524..a495a8d82b 100644 --- a/x/ecocredit/marketplace/keeper/keeper.go +++ b/x/ecocredit/marketplace/keeper/keeper.go @@ -9,6 +9,11 @@ import ( types "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1" ) +var ( + _ types.MsgServer = Keeper{} + _ types.QueryServer = Keeper{} +) + type Keeper struct { stateStore marketapi.StateStore coreStore ecoApi.StateStore @@ -27,6 +32,3 @@ func NewKeeper(ss marketapi.StateStore, cs ecoApi.StateStore, bk ecocredit.BankK authority: authority, } } - -var _ types.MsgServer = Keeper{} -var _ types.QueryServer = Keeper{} diff --git a/x/ecocredit/server/abci.go b/x/ecocredit/module/abci.go similarity index 60% rename from x/ecocredit/server/abci.go rename to x/ecocredit/module/abci.go index 6988215f53..82a1ff196f 100644 --- a/x/ecocredit/server/abci.go +++ b/x/ecocredit/module/abci.go @@ -1,4 +1,4 @@ -package server +package module import ( "time" @@ -7,10 +7,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/regen-network/regen-ledger/x/ecocredit" + "github.com/regen-network/regen-ledger/x/ecocredit/server" ) -// BeginBlocker checks if there are any expired sell or buy orders and removes them from state. -func BeginBlocker(ctx sdk.Context, k Keeper) error { +// BeginBlocker checks if there are any expired sell orders and removes them from state. +func BeginBlocker(ctx sdk.Context, k server.Keeper) error { defer telemetry.ModuleMeasureSince(ecocredit.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) if err := k.PruneOrders(ctx); err != nil { diff --git a/x/ecocredit/module/module.go b/x/ecocredit/module/module.go index ed8daaec99..af8652d68f 100644 --- a/x/ecocredit/module/module.go +++ b/x/ecocredit/module/module.go @@ -8,7 +8,6 @@ import ( "github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/proto" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" "google.golang.org/protobuf/reflect/protoreflect" @@ -44,54 +43,32 @@ var ( _ module.AppModuleSimulation = Module{} ) +const ( + ConsensusVersion = 3 // ConsensusVersion is the module consensus version +) + +// Module implements the AppModule interface. type Module struct { - key storetypes.StoreKey + key storetypes.StoreKey + authority sdk.AccAddress + Keeper server.Keeper + accountKeeper ecocredit.AccountKeeper + bankKeeper ecocredit.BankKeeper + // legacySubspace is used solely for migration of x/ecocredit managed parameters legacySubspace paramtypes.Subspace - accountKeeper ecocredit.AccountKeeper - bankKeeper ecocredit.BankKeeper - Keeper server.Keeper - authority sdk.AccAddress -} - -func (a Module) InitGenesis(s sdk.Context, jsonCodec codec.JSONCodec, message json.RawMessage) []abci.ValidatorUpdate { - update, err := a.Keeper.InitGenesis(s, jsonCodec, message) - if err != nil { - panic(err) - } - return update -} - -func (a Module) ExportGenesis(s sdk.Context, jsonCodec codec.JSONCodec) json.RawMessage { - m, err := a.Keeper.ExportGenesis(s, jsonCodec) - if err != nil { - panic(err) - } - return m -} - -func (a Module) RegisterInvariants(reg sdk.InvariantRegistry) { - a.Keeper.RegisterInvariants(reg) -} - -func (a Module) Route() sdk.Route { - return sdk.Route{} -} - -func (a Module) QuerierRoute() string { - return ecocredit.ModuleName } -func (a Module) LegacyQuerierHandler(amino *codec.LegacyAmino) sdk.Querier { return nil } - -// NewModule returns a new Module object. +// NewModule returns a new Module. func NewModule( storeKey storetypes.StoreKey, - legacySubspace paramtypes.Subspace, + authority sdk.AccAddress, accountKeeper ecocredit.AccountKeeper, bankKeeper ecocredit.BankKeeper, - authority sdk.AccAddress, + legacySubspace paramtypes.Subspace, ) *Module { + + // legacySubspace is used solely for migration of x/ecocredit managed parameters if !legacySubspace.HasKeyTable() { legacySubspace = legacySubspace.WithKeyTable(basetypes.ParamKeyTable()) } @@ -105,14 +82,33 @@ func NewModule( } } -var _ module.AppModuleBasic = &Module{} -var _ module.AppModuleSimulation = &Module{} +/* -------------------- AppModule -------------------- */ + +// ConsensusVersion implements AppModule/ConsensusVersion. +func (Module) ConsensusVersion() uint64 { return ConsensusVersion } + +// Name implements AppModule/Name. +func (m Module) Name() string { + return ecocredit.ModuleName +} + +// Route implements AppModule/Route. +func (m Module) Route() sdk.Route { + return sdk.Route{} +} -func (a Module) Name() string { +// QuerierRoute implements AppModule/QuerierRoute. +func (m Module) QuerierRoute() string { return ecocredit.ModuleName } -func (a Module) RegisterInterfaces(registry types.InterfaceRegistry) { +// RegisterInvariants implements AppModule/RegisterInvariants. +func (m Module) RegisterInvariants(reg sdk.InvariantRegistry) { + m.Keeper.RegisterInvariants(reg) +} + +// RegisterInterfaces implements AppModule/RegisterInterfaces. +func (m Module) RegisterInterfaces(registry types.InterfaceRegistry) { baskettypes.RegisterTypes(registry) basetypes.RegisterTypes(registry) markettypes.RegisterTypes(registry) @@ -121,8 +117,9 @@ func (a Module) RegisterInterfaces(registry types.InterfaceRegistry) { basetypesv1alpha1.RegisterTypes(registry) } -func (a *Module) RegisterServices(cfg module.Configurator) { - svr := server.NewServer(a.key, a.legacySubspace, a.accountKeeper, a.bankKeeper, a.authority) +// RegisterServices implements AppModule/RegisterServices. +func (m *Module) RegisterServices(cfg module.Configurator) { + svr := server.NewServer(m.key, m.legacySubspace, m.accountKeeper, m.bankKeeper, m.authority) basetypes.RegisterMsgServer(cfg.MsgServer(), svr.CoreKeeper) basetypes.RegisterQueryServer(cfg.QueryServer(), svr.CoreKeeper) @@ -132,22 +129,57 @@ func (a *Module) RegisterServices(cfg module.Configurator) { markettypes.RegisterMsgServer(cfg.MsgServer(), svr.MarketplaceKeeper) markettypes.RegisterQueryServer(cfg.QueryServer(), svr.MarketplaceKeeper) - m := server.NewMigrator(svr, a.legacySubspace) - if err := cfg.RegisterMigration(ecocredit.ModuleName, 2, m.Migrate2to3); err != nil { + migrator := server.NewMigrator(svr, m.legacySubspace) + if err := cfg.RegisterMigration(ecocredit.ModuleName, 2, migrator.Migrate2to3); err != nil { panic(err) } - a.Keeper = svr + m.Keeper = svr } -//nolint:errcheck -func (a Module) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *runtime.ServeMux) { +// RegisterGRPCGatewayRoutes implements AppModule/RegisterGRPCGatewayRoutes. +func (m Module) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *runtime.ServeMux) { ctx := context.Background() - basetypes.RegisterQueryHandlerClient(ctx, mux, basetypes.NewQueryClient(clientCtx)) - baskettypes.RegisterQueryHandlerClient(ctx, mux, baskettypes.NewQueryClient(clientCtx)) - markettypes.RegisterQueryHandlerClient(ctx, mux, markettypes.NewQueryClient(clientCtx)) + err := basetypes.RegisterQueryHandlerClient(ctx, mux, basetypes.NewQueryClient(clientCtx)) + if err != nil { + panic(err) + } + err = baskettypes.RegisterQueryHandlerClient(ctx, mux, baskettypes.NewQueryClient(clientCtx)) + if err != nil { + panic(err) + } + err = markettypes.RegisterQueryHandlerClient(ctx, mux, markettypes.NewQueryClient(clientCtx)) + if err != nil { + panic(err) + } +} + +// RegisterLegacyAminoCodec implements AppModule/RegisterLegacyAminoCodec. +func (m Module) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + basetypes.RegisterLegacyAminoCodec(cdc) + baskettypes.RegisterLegacyAminoCodec(cdc) + markettypes.RegisterLegacyAminoCodec(cdc) +} + +// InitGenesis implements AppModule/InitGenesis. +func (m Module) InitGenesis(s sdk.Context, jsonCodec codec.JSONCodec, message json.RawMessage) []abci.ValidatorUpdate { + update, err := m.Keeper.InitGenesis(s, jsonCodec, message) + if err != nil { + panic(err) + } + return update +} + +// ExportGenesis implements AppModule/ExportGenesis. +func (m Module) ExportGenesis(s sdk.Context, jsonCodec codec.JSONCodec) json.RawMessage { + genesis, err := m.Keeper.ExportGenesis(s, jsonCodec) + if err != nil { + panic(err) + } + return genesis } -func (a Module) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { +// DefaultGenesis implements AppModule/DefaultGenesis. +func (m Module) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { db, err := ormdb.NewModuleDB(&ecocredit.ModuleSchema, ormdb.ModuleDBOptions{}) if err != nil { panic(err) @@ -191,7 +223,8 @@ func (a Module) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { return bz } -func (a Module) ValidateGenesis(cdc codec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage) error { +// ValidateGenesis implements AppModule/ValidateGenesis. +func (m Module) ValidateGenesis(_ codec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage) error { db, err := ormdb.NewModuleDB(&ecocredit.ModuleSchema, ormdb.ModuleDBOptions{}) if err != nil { return err @@ -224,62 +257,57 @@ func (a Module) ValidateGenesis(cdc codec.JSONCodec, _ sdkclient.TxEncodingConfi return genesis.ValidateGenesis(bz, params) } -func (a Module) GetQueryCmd() *cobra.Command { - return client.QueryCmd(a.Name()) +// GetTxCmd implements AppModule/GetTxCmd. +func (m Module) GetTxCmd() *cobra.Command { + return client.TxCmd(m.Name()) } -func (a Module) GetTxCmd() *cobra.Command { - return client.TxCmd(a.Name()) +// GetQueryCmd implements AppModule/GetQueryCmd. +func (m Module) GetQueryCmd() *cobra.Command { + return client.QueryCmd(m.Name()) } -// ConsensusVersion implements AppModule/ConsensusVersion. -func (Module) ConsensusVersion() uint64 { return 3 } - -/**** DEPRECATED ****/ -func (a Module) RegisterRESTRoutes(sdkclient.Context, *mux.Router) {} -func (a Module) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - basetypes.RegisterLegacyAminoCodec(cdc) - baskettypes.RegisterLegacyAminoCodec(cdc) - markettypes.RegisterLegacyAminoCodec(cdc) +// BeginBlock checks if there are any expired sell or buy orders and removes them from state. +func (m Module) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { + err := BeginBlocker(ctx, m.Keeper) + if err != nil { + panic(err) + } } -// AppModuleSimulation functions +// LegacyQuerierHandler implements AppModule/LegacyQuerierHandler. +func (m Module) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { return nil } + +/* -------------------- AppModuleSimulation -------------------- */ // GenerateGenesisState creates a randomized GenesisState of the ecocredit module. func (Module) GenerateGenesisState(simState *module.SimulationState) { simulation.RandomizedGenState(simState) } -// ProposalContents returns all the ecocredit content functions used to -// simulate proposals. -func (Module) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { +// ProposalContents implements AppModuleSimulation/ProposalContents. +func (Module) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent { return nil } -// RandomizedParams creates randomized ecocredit param changes for the simulator. -func (Module) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { +// RandomizedParams implements AppModuleSimulation/RandomizedParams. +func (Module) RandomizedParams(_ *rand.Rand) []simtypes.ParamChange { return nil } -// RegisterStoreDecoder registers a decoder for ecocredit module's types +// RegisterStoreDecoder implements AppModuleSimulation/RegisterStoreDecoder. func (Module) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} -// WeightedOperations returns all the ecocredit module operations with their respective weights. -func (a Module) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - coreQuerier, basketQuerier, marketQuerier := a.Keeper.QueryServers() +// WeightedOperations implements AppModuleSimulation/WeightedOperations. +func (m Module) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + coreServer, basketServer, marketServer := m.Keeper.QueryServers() return basesims.WeightedOperations( - simState.AppParams, simState.Cdc, - a.accountKeeper, a.bankKeeper, - coreQuerier, - basketQuerier, - marketQuerier, + simState.AppParams, + simState.Cdc, + m.accountKeeper, + m.bankKeeper, + coreServer, + basketServer, + marketServer, ) } - -// BeginBlock checks if there are any expired sell or buy orders and removes them from state. -func (a Module) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { - err := server.BeginBlocker(ctx, a.Keeper) - if err != nil { - panic(err) - } -} diff --git a/x/ecocredit/server/server_test.go b/x/ecocredit/server/server_test.go index 6f9db02bd6..2a6f94df5f 100644 --- a/x/ecocredit/server/server_test.go +++ b/x/ecocredit/server/server_test.go @@ -80,7 +80,7 @@ func setup(t *testing.T) (fixture.Factory, paramstypes.Subspace, bankkeeper.Base ) authority := authtypes.NewModuleAddress(govtypes.ModuleName) - ecocreditModule := module.NewModule(ecoKey, ecocreditSubspace, accountKeeper, bankKeeper, authority) + ecocreditModule := module.NewModule(ecoKey, authority, accountKeeper, bankKeeper, ecocreditSubspace) ff.SetModules([]sdkmodule.AppModule{ecocreditModule}) return ff, ecocreditSubspace, bankKeeper, accountKeeper diff --git a/x/ecocredit/server/tests/utils.go b/x/ecocredit/server/tests/utils.go index 936f30b435..cf2e562396 100644 --- a/x/ecocredit/server/tests/utils.go +++ b/x/ecocredit/server/tests/utils.go @@ -61,5 +61,5 @@ func NewEcocreditModule(ff fixture.Factory) *ecocredit.Module { ) _, _, addr := testdata.KeyTestPubAddr() - return ecocredit.NewModule(ecocreditKey, ecocreditSubspace, accountKeeper, bankKeeper, addr) + return ecocredit.NewModule(ecocreditKey, addr, accountKeeper, bankKeeper, ecocreditSubspace) }