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

Deepanshutr/master #445

Merged
merged 14 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
19 changes: 12 additions & 7 deletions helpers/base/codec.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package base

import (
"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/utilities/rest/id_getters/docs"
schemaCodec "github.com/AssetMantle/schema/codec"
sdkClient "github.com/cosmos/cosmos-sdk/client"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
sdkCodecTypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
sdkModuleTypes "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/tx"

"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/utilities/rest/id_getters/docs"
)

type codec struct {
Expand All @@ -31,14 +30,14 @@ func (codec codec) GetLegacyAmino() *sdkCodec.LegacyAmino {
func (codec codec) InterfaceRegistry() sdkCodecTypes.InterfaceRegistry {
return codec.interfaceRegistry
}
func (codec codec) Initialize(moduleBasicManager sdkModuleTypes.BasicManager) helpers.Codec {
func (codec codec) Initialize(moduleManager helpers.ModuleManager) helpers.Codec {
std.RegisterLegacyAminoCodec(codec.legacyAmino)
std.RegisterInterfaces(codec.interfaceRegistry)
helpers.RegisterLegacyAminoCodec(codec.legacyAmino)
schemaCodec.RegisterLegacyAminoCodec(codec.legacyAmino)
docs.RegisterLegacyAminoCodec(codec.legacyAmino)
moduleBasicManager.RegisterLegacyAminoCodec(codec.legacyAmino)
moduleBasicManager.RegisterInterfaces(codec.interfaceRegistry)
moduleManager.RegisterLegacyAminoCodec(codec.legacyAmino)
moduleManager.RegisterInterfaces(codec.interfaceRegistry)
return codec
}

Expand All @@ -50,3 +49,9 @@ func CodecPrototype() helpers.Codec {
codec.legacyAmino = sdkCodec.NewLegacyAmino()
return codec
}

// TestCodec
// Deprecated: Only for testing. Use CodecPrototype instead.
func TestCodec() helpers.Codec {
return CodecPrototype().Initialize(NewModuleManager(auth.AppModule{}))
}
9 changes: 4 additions & 5 deletions helpers/base/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdkModuleTypes "github.com/cosmos/cosmos-sdk/types/module"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -22,7 +21,7 @@ func Test_codec_Initialize(t *testing.T) {
ProtoCodec *sdkCodec.ProtoCodec
}
type args struct {
moduleBasicManager sdkModuleTypes.BasicManager
moduleManager helpers.ModuleManager
}
tests := []struct {
name string
Expand All @@ -39,7 +38,7 @@ func Test_codec_Initialize(t *testing.T) {
ProtoCodec: sdkCodec.NewProtoCodec(types.NewInterfaceRegistry()),
},
args: args{
moduleBasicManager: sdkModuleTypes.BasicManager{},
moduleManager: NewModuleManager(),
},
want: codec{
interfaceRegistry: types.NewInterfaceRegistry(),
Expand All @@ -58,8 +57,8 @@ func Test_codec_Initialize(t *testing.T) {
legacyAmino: tt.fields.legacyAmino,
ProtoCodec: tt.fields.ProtoCodec,
}
Codec := codec.Initialize(tt.args.moduleBasicManager)
assert.Equalf(t, tt.want.GetProtoCodec(), Codec.GetProtoCodec(), "Initialize(%v)", tt.args.moduleBasicManager)
Codec := codec.Initialize(tt.args.moduleManager)
assert.Equalf(t, tt.want.GetProtoCodec(), Codec.GetProtoCodec(), "Initialize(%v)", tt.args.moduleManager)
})
}
}
Expand Down
82 changes: 50 additions & 32 deletions helpers/base/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package base
import (
"encoding/json"
"fmt"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
"math/rand"

abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -18,7 +20,6 @@ import (
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abciTypes "github.com/tendermint/tendermint/abci/types"

"github.com/AssetMantle/modules/helpers"
)
Expand Down Expand Up @@ -48,6 +49,15 @@ type module struct {

var _ helpers.Module = (*module)(nil)

func (module module) IsOnePerModuleType() {
//TODO implement me
panic("implement me")
}

func (module module) IsAppModule() {
//TODO implement me
panic("implement me")
}
func (module module) Name() string {
return module.name
}
Expand Down Expand Up @@ -134,10 +144,14 @@ func (module module) GetQueryCmd() *cobra.Command {
func (module module) GenerateGenesisState(simulationState *sdkModuleTypes.SimulationState) {
module.simulatorPrototype().RandomizedGenesisState(simulationState)
}
func (module module) ProposalMsgs(simulationState sdkModuleTypes.SimulationState) []simulationTypes.WeightedProposalMsg {
//TODO implement me
panic("implement me")
}
func (module module) ProposalContents(simulationState sdkModuleTypes.SimulationState) []simulationTypes.WeightedProposalContent {
return module.simulatorPrototype().WeightedProposalContentList(simulationState)
}
func (module module) RandomizedParams(r *rand.Rand) []simulationTypes.ParamChange {
func (module module) RandomizedParams(r *rand.Rand) []simulationTypes.LegacyParamChange {
return module.simulatorPrototype().ParamChangeList(r)
}
func (module module) RegisterStoreDecoder(storeDecoderRegistry sdkTypes.StoreDecoderRegistry) {
Expand All @@ -149,40 +163,44 @@ func (module module) WeightedOperations(simulationState sdkModuleTypes.Simulatio
func (module module) RegisterInvariants(invariantRegistry sdkTypes.InvariantRegistry) {
module.invariantsPrototype().Register(invariantRegistry)
}
func (module module) Route() sdkTypes.Route {
return sdkTypes.NewRoute(module.Name(), func(context sdkTypes.Context, msg sdkTypes.Msg) (*sdkTypes.Result, error) {
if module.transactions == nil {
panic(fmt.Errorf("transactions for module %s not initialized", module.Name()))
}

if message, ok := msg.(helpers.Message); ok {
if transaction := module.transactions.GetTransaction(message.Type()); transaction != nil {
return transaction.HandleMessage(sdkTypes.WrapSDKContext(context.WithEventManager(sdkTypes.NewEventManager())), message)
}
}
return nil, fmt.Errorf("message type %T is not supported by module %s", msg, module.Name())
})
}
// TODO remove if unnecessary
//
// func (module module) Route() sdkTypes.Route {
// return sdkTypes.NewRoute(module.Name(), func(context sdkTypes.Context, msg sdkTypes.Msg) (*sdkTypes.Result, error) {
// if module.transactions == nil {
// panic(fmt.Errorf("transactions for module %s not initialized", module.Name()))
// }
//
// if message, ok := msg.(helpers.Message); ok {
// if transaction := module.transactions.GetTransaction(message.Type()); transaction != nil {
// return transaction.HandleMessage(sdkTypes.WrapSDKContext(context.WithEventManager(sdkTypes.NewEventManager())), message)
// }
// }
// return nil, fmt.Errorf("message type %T is not supported by module %s", msg, module.Name())
// })
// }
func (module module) QuerierRoute() string {
return module.name
}
func (module module) LegacyQuerierHandler(_ *sdkCodec.LegacyAmino) sdkTypes.Querier {
return func(context sdkTypes.Context, path []string, requestQuery abciTypes.RequestQuery) ([]byte, error) {
if module.queries == nil {
panic(fmt.Errorf("queries for module %s not initialized", module.Name()))
}

if query := module.queries.GetQuery(path[0]); query != nil {
return query.HandleQuery(sdkTypes.WrapSDKContext(context), requestQuery)
}

if path[0] == "parameters" {
return CodecPrototype().MarshalJSON(module.parameterManager.Fetch(sdkTypes.WrapSDKContext(context)).Get())
}

return nil, fmt.Errorf("unknown query path, %v for module %v", path[0], module.Name())
}
}
// func (module module) LegacyQuerierHandler(_ *sdkCodec.LegacyAmino) sdkTypes.Querier {
// return func(context sdkTypes.Context, path []string, requestQuery abciTypes.RequestQuery) ([]byte, error) {
// if module.queries == nil {
// panic(fmt.Errorf("queries for module %s not initialized", module.Name()))
// }
//
// if query := module.queries.GetQuery(path[0]); query != nil {
// return query.HandleQuery(sdkTypes.WrapSDKContext(context), requestQuery)
// }
//
// if path[0] == "parameters" {
// return CodecPrototype().MarshalJSON(module.parameterManager.Fetch(sdkTypes.WrapSDKContext(context)).Get())
// }
//
// return nil, fmt.Errorf("unknown query path, %v for module %v", path[0], module.Name())
// }
// }
func (module module) RegisterServices(configurator sdkModuleTypes.Configurator) {
for _, query := range module.queries.Get() {
query.RegisterService(configurator)
Expand Down Expand Up @@ -236,7 +254,7 @@ func (module module) DecodeModuleTransactionRequest(transactionName string, rawM

return nil, fmt.Errorf("transaction %s is not supported by module %s", transactionName, module.Name())
}
func (module module) Initialize(kvStoreKey *sdkTypes.KVStoreKey, paramsSubspace paramsTypes.Subspace, auxiliaryKeepers ...interface{}) helpers.Module {
func (module module) Initialize(kvStoreKey *storeTypes.KVStoreKey, paramsSubspace paramsTypes.Subspace, auxiliaryKeepers ...interface{}) helpers.Module {
module.mapper = module.mapperPrototype().Initialize(kvStoreKey)

module.genesis = module.genesisPrototype()
Expand Down
117 changes: 117 additions & 0 deletions helpers/base/module_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package base

import (
"encoding/json"
"github.com/AssetMantle/modules/helpers"
abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
sdkModuleTypes "github.com/cosmos/cosmos-sdk/types/module"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
)

type moduleManager struct {
basicModules []helpers.BasicModule
orderInitGenesis []string
orderExportGenesis []string
orderBeginBlockers []string
orderEndBlockers []string
orderMigrations []string
}

var _ helpers.ModuleManager = (*moduleManager)(nil)

func (moduleManager moduleManager) AddTxCommands(command *cobra.Command) {
for _, basicModule := range moduleManager.basicModules {
if cmd := basicModule.GetTxCmd(); cmd != nil {
command.AddCommand(cmd)
}
}
}
func (moduleManager moduleManager) AddQueryCommands(rootQueryCmd *cobra.Command) {
for _, basicModule := range moduleManager.basicModules {
if cmd := basicModule.GetQueryCmd(); cmd != nil {
rootQueryCmd.AddCommand(cmd)
}
}
}
func (moduleManager moduleManager) InitGenesis(context sdkTypes.Context, jsonCodec sdkCodec.JSONCodec, genesisData map[string]json.RawMessage) abciTypes.ResponseInitChain {
return moduleManager.getModuleManager().InitGenesis(context, jsonCodec, genesisData)
}
func (moduleManager moduleManager) GetVersionMap() sdkModuleTypes.VersionMap {
return moduleManager.getModuleManager().GetVersionMap()
}
func (moduleManager moduleManager) RegisterServices(configurator sdkModuleTypes.Configurator) {
moduleManager.getModuleManager().RegisterServices(configurator)
}
func (moduleManager moduleManager) SetOrderBeginBlockers(moduleName ...string) {
sdkModuleManager := moduleManager.getModuleManager()
sdkModuleManager.SetOrderBeginBlockers(moduleName...)
moduleManager.orderBeginBlockers = sdkModuleManager.OrderBeginBlockers
}
func (moduleManager moduleManager) SetOrderEndBlockers(moduleName ...string) {
sdkModuleManager := moduleManager.getModuleManager()
sdkModuleManager.SetOrderEndBlockers(moduleName...)
moduleManager.orderEndBlockers = sdkModuleManager.OrderEndBlockers
}
func (moduleManager moduleManager) SetOrderInitGenesis(moduleName ...string) {
sdkModuleManager := moduleManager.getModuleManager()
sdkModuleManager.SetOrderInitGenesis(moduleName...)
moduleManager.orderInitGenesis = sdkModuleManager.OrderInitGenesis
}
func (moduleManager moduleManager) BeginBlock(context sdkTypes.Context, requestBeginBlock abciTypes.RequestBeginBlock) abciTypes.ResponseBeginBlock {
return moduleManager.getModuleManager().BeginBlock(context, requestBeginBlock)
}
func (moduleManager moduleManager) EndBlock(context sdkTypes.Context, requestEndBlock abciTypes.RequestEndBlock) abciTypes.ResponseEndBlock {
return moduleManager.getModuleManager().EndBlock(context, requestEndBlock)
}
func (moduleManager moduleManager) RunMigrations(context sdkTypes.Context, configurator sdkModuleTypes.Configurator, versionMap sdkModuleTypes.VersionMap) (sdkModuleTypes.VersionMap, error) {
return moduleManager.getModuleManager().RunMigrations(context, configurator, versionMap)
}
func (moduleManager moduleManager) RegisterInvariants(invariantRegistry sdkTypes.InvariantRegistry) {
moduleManager.getModuleManager().RegisterInvariants(invariantRegistry)
}
func (moduleManager moduleManager) GetBasicManager() sdkModuleTypes.BasicManager {
return sdkModuleTypes.NewBasicManager(moduleManager.getAppModulesBasic()...)
}
func (moduleManager moduleManager) ExportGenesisForModules(context sdkTypes.Context, jsonCodec sdkCodec.JSONCodec, moduleNames []string) map[string]json.RawMessage {
return moduleManager.getModuleManager().ExportGenesisForModules(context, jsonCodec, moduleNames)
}
func (moduleManager moduleManager) RegisterGRPCGatewayRoutes(context client.Context, serverMux *runtime.ServeMux) {
for _, basicModule := range moduleManager.basicModules {
basicModule.RegisterGRPCGatewayRoutes(context, serverMux)
}
}
func (moduleManager moduleManager) RegisterLegacyAminoCodec(legacyAmino *sdkCodec.LegacyAmino) {
for _, basicModule := range moduleManager.basicModules {
basicModule.RegisterLegacyAminoCodec(legacyAmino)
}
}
func (moduleManager moduleManager) RegisterInterfaces(interfaceRegistry types.InterfaceRegistry) {
for _, basicModule := range moduleManager.basicModules {
basicModule.RegisterInterfaces(interfaceRegistry)
}
}
func (moduleManager moduleManager) getModuleManager() *sdkModuleTypes.Manager {
appModules := make([]sdkModuleTypes.AppModule, len(moduleManager.basicModules))
for i, basicModule := range moduleManager.basicModules {
appModules[i] = basicModule
}
return sdkModuleTypes.NewManager(appModules...)
}
func (moduleManager moduleManager) getAppModulesBasic() []sdkModuleTypes.AppModuleBasic {
appModules := make([]sdkModuleTypes.AppModuleBasic, len(moduleManager.basicModules))
for i, basicModule := range moduleManager.basicModules {
appModules[i] = basicModule
}
return appModules
}

func NewModuleManager(basicModules ...helpers.BasicModule) helpers.ModuleManager {
return moduleManager{
basicModules: basicModules,
}
}
9 changes: 9 additions & 0 deletions helpers/basic_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package helpers

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

type BasicModule interface {
module.AppModule
}
3 changes: 1 addition & 2 deletions helpers/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

type Codec interface {
Expand All @@ -14,5 +13,5 @@ type Codec interface {
GetProtoCodec() *codec.ProtoCodec
GetLegacyAmino() *codec.LegacyAmino
InterfaceRegistry() types.InterfaceRegistry
Initialize(module.BasicManager) Codec
Initialize(ModuleManager) Codec
}
9 changes: 9 additions & 0 deletions helpers/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ import (
)

type Module interface {
BasicModule
sdkModuleTypes.AppModuleGenesis
sdkModuleTypes.EndBlockAppModule
sdkModuleTypes.BeginBlockAppModule

sdkModuleTypes.HasConsensusVersion
sdkModuleTypes.HasInvariants
sdkModuleTypes.HasServices

sdkModuleTypes.AppModuleSimulation
sdkModuleTypes.HasProposalContents
sdkModuleTypes.HasProposalMsgs

GetAuxiliary(string) Auxiliary

Expand Down
Loading
Loading