From 8be5d05deaeee34ad3ae3c998b3a19d4b64e2ba6 Mon Sep 17 00:00:00 2001 From: harry <53987565+h5law@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:05:44 +0100 Subject: [PATCH] [Gateway] Scaffold Gateway module and nothing else Simply ran ``` ignite scaffold module gateway --dep bank,account ``` --- app/app.go | 23 ++++ docs/static/openapi.yml | 46 ++++++++ proto/pocket/gateway/genesis.proto | 12 +++ proto/pocket/gateway/params.proto | 12 +++ proto/pocket/gateway/query.proto | 26 +++++ proto/pocket/gateway/tx.proto | 7 ++ testutil/keeper/gateway.go | 54 ++++++++++ x/gateway/client/cli/query.go | 31 ++++++ x/gateway/client/cli/query_params.go | 36 +++++++ x/gateway/client/cli/tx.go | 36 +++++++ x/gateway/genesis.go | 23 ++++ x/gateway/genesis_test.go | 29 +++++ x/gateway/keeper/keeper.go | 54 ++++++++++ x/gateway/keeper/msg_server.go | 17 +++ x/gateway/keeper/msg_server_test.go | 23 ++++ x/gateway/keeper/params.go | 16 +++ x/gateway/keeper/params_test.go | 18 ++++ x/gateway/keeper/query.go | 7 ++ x/gateway/keeper/query_params.go | 19 ++++ x/gateway/keeper/query_params_test.go | 21 ++++ x/gateway/module.go | 148 ++++++++++++++++++++++++++ x/gateway/module_simulation.go | 64 +++++++++++ x/gateway/simulation/helpers.go | 15 +++ x/gateway/types/codec.go | 23 ++++ x/gateway/types/errors.go | 12 +++ x/gateway/types/expected_keepers.go | 18 ++++ x/gateway/types/genesis.go | 24 +++++ x/gateway/types/genesis_test.go | 41 +++++++ x/gateway/types/keys.go | 19 ++++ x/gateway/types/params.go | 39 +++++++ x/gateway/types/types.go | 1 + 31 files changed, 914 insertions(+) create mode 100644 proto/pocket/gateway/genesis.proto create mode 100644 proto/pocket/gateway/params.proto create mode 100644 proto/pocket/gateway/query.proto create mode 100644 proto/pocket/gateway/tx.proto create mode 100644 testutil/keeper/gateway.go create mode 100644 x/gateway/client/cli/query.go create mode 100644 x/gateway/client/cli/query_params.go create mode 100644 x/gateway/client/cli/tx.go create mode 100644 x/gateway/genesis.go create mode 100644 x/gateway/genesis_test.go create mode 100644 x/gateway/keeper/keeper.go create mode 100644 x/gateway/keeper/msg_server.go create mode 100644 x/gateway/keeper/msg_server_test.go create mode 100644 x/gateway/keeper/params.go create mode 100644 x/gateway/keeper/params_test.go create mode 100644 x/gateway/keeper/query.go create mode 100644 x/gateway/keeper/query_params.go create mode 100644 x/gateway/keeper/query_params_test.go create mode 100644 x/gateway/module.go create mode 100644 x/gateway/module_simulation.go create mode 100644 x/gateway/simulation/helpers.go create mode 100644 x/gateway/types/codec.go create mode 100644 x/gateway/types/errors.go create mode 100644 x/gateway/types/expected_keepers.go create mode 100644 x/gateway/types/genesis.go create mode 100644 x/gateway/types/genesis_test.go create mode 100644 x/gateway/types/keys.go create mode 100644 x/gateway/types/params.go create mode 100644 x/gateway/types/types.go diff --git a/app/app.go b/app/app.go index 60efd769c..6ac9847e7 100644 --- a/app/app.go +++ b/app/app.go @@ -130,6 +130,9 @@ import ( suppliermodulekeeper "pocket/x/supplier/keeper" suppliermoduletypes "pocket/x/supplier/types" + gatewaymodule "pocket/x/gateway" + gatewaymodulekeeper "pocket/x/gateway/keeper" + gatewaymoduletypes "pocket/x/gateway/types" // this line is used by starport scaffolding # stargate/app/moduleImport appparams "pocket/app/params" @@ -196,6 +199,7 @@ var ( sessionmodule.AppModuleBasic{}, applicationmodule.AppModuleBasic{}, suppliermodule.AppModuleBasic{}, + gatewaymodule.AppModuleBasic{}, // this line is used by starport scaffolding # stargate/app/moduleBasic ) @@ -212,6 +216,7 @@ var ( suppliermoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking}, servicemoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking}, applicationmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking}, + gatewaymoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking}, // this line is used by starport scaffolding # stargate/app/maccPerms } ) @@ -280,6 +285,7 @@ type App struct { ApplicationKeeper applicationmodulekeeper.Keeper SupplierKeeper suppliermodulekeeper.Keeper + GatewayKeeper gatewaymodulekeeper.Keeper // this line is used by starport scaffolding # stargate/app/keeperDeclaration // mm is the module manager @@ -331,6 +337,7 @@ func New( sessionmoduletypes.StoreKey, applicationmoduletypes.StoreKey, suppliermoduletypes.StoreKey, + gatewaymoduletypes.StoreKey, // this line is used by starport scaffolding # stargate/app/storeKey ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) @@ -599,6 +606,17 @@ func New( ) supplierModule := suppliermodule.NewAppModule(appCodec, app.SupplierKeeper, app.AccountKeeper, app.BankKeeper) + app.GatewayKeeper = *gatewaymodulekeeper.NewKeeper( + appCodec, + keys[gatewaymoduletypes.StoreKey], + keys[gatewaymoduletypes.MemStoreKey], + app.GetSubspace(gatewaymoduletypes.ModuleName), + + app.BankKeeper, + app.AccountKeeper, + ) + gatewayModule := gatewaymodule.NewAppModule(appCodec, app.GatewayKeeper, app.AccountKeeper, app.BankKeeper) + // this line is used by starport scaffolding # stargate/app/keeperDefinition /**** IBC Routing ****/ @@ -665,6 +683,7 @@ func New( sessionModule, applicationModule, supplierModule, + gatewayModule, // this line is used by starport scaffolding # stargate/app/appModule crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)), // always be last to make sure that it checks for all invariants and not only part of them @@ -702,6 +721,7 @@ func New( sessionmoduletypes.ModuleName, applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, + gatewaymoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/beginBlockers ) @@ -732,6 +752,7 @@ func New( sessionmoduletypes.ModuleName, applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, + gatewaymoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/endBlockers ) @@ -767,6 +788,7 @@ func New( sessionmoduletypes.ModuleName, applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, + gatewaymoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/initGenesis } app.mm.SetOrderInitGenesis(genesisModuleOrder...) @@ -996,6 +1018,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(sessionmoduletypes.ModuleName) paramsKeeper.Subspace(applicationmoduletypes.ModuleName) paramsKeeper.Subspace(suppliermoduletypes.ModuleName) + paramsKeeper.Subspace(gatewaymoduletypes.ModuleName) // this line is used by starport scaffolding # stargate/app/paramSubspace return paramsKeeper diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 2ba0c4d4b..3c8a8e97e 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -46634,6 +46634,42 @@ paths: additionalProperties: {} tags: - Query + /pocket/gateway/params: + get: + summary: Parameters queries the parameters of the module. + operationId: PocketGatewayParams + responses: + '200': + description: A successful response. + schema: + type: object + properties: + params: + description: params holds all the parameters of this module. + type: object + description: >- + QueryParamsResponse is response type for the Query/Params RPC + method. + default: + description: An unexpected error response. + schema: + type: object + properties: + code: + type: integer + format: int32 + message: + type: string + details: + type: array + items: + type: object + properties: + '@type': + type: string + additionalProperties: {} + tags: + - Query /pocket/pocket/params: get: summary: Parameters queries the parameters of the module. @@ -75741,6 +75777,16 @@ definitions: description: params holds all the parameters of this module. type: object description: QueryParamsResponse is response type for the Query/Params RPC method. + pocket.gateway.Params: + type: object + description: Params defines the parameters for the module. + pocket.gateway.QueryParamsResponse: + type: object + properties: + params: + description: params holds all the parameters of this module. + type: object + description: QueryParamsResponse is response type for the Query/Params RPC method. pocket.pocket.Params: type: object description: Params defines the parameters for the module. diff --git a/proto/pocket/gateway/genesis.proto b/proto/pocket/gateway/genesis.proto new file mode 100644 index 000000000..4cbfeff66 --- /dev/null +++ b/proto/pocket/gateway/genesis.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package pocket.gateway; + +import "gogoproto/gogo.proto"; +import "pocket/gateway/params.proto"; + +option go_package = "pocket/x/gateway/types"; + +// GenesisState defines the gateway module's genesis state. +message GenesisState { + Params params = 1 [(gogoproto.nullable) = false]; +} diff --git a/proto/pocket/gateway/params.proto b/proto/pocket/gateway/params.proto new file mode 100644 index 000000000..8d5f42acd --- /dev/null +++ b/proto/pocket/gateway/params.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package pocket.gateway; + +import "gogoproto/gogo.proto"; + +option go_package = "pocket/x/gateway/types"; + +// Params defines the parameters for the module. +message Params { + option (gogoproto.goproto_stringer) = false; + +} diff --git a/proto/pocket/gateway/query.proto b/proto/pocket/gateway/query.proto new file mode 100644 index 000000000..5e50b7fde --- /dev/null +++ b/proto/pocket/gateway/query.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package pocket.gateway; + +import "gogoproto/gogo.proto"; +import "google/api/annotations.proto"; +import "cosmos/base/query/v1beta1/pagination.proto"; +import "pocket/gateway/params.proto"; + +option go_package = "pocket/x/gateway/types"; + +// Query defines the gRPC querier service. +service Query { + // Parameters queries the parameters of the module. + rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { + option (google.api.http).get = "/pocket/gateway/params"; + } +} + +// QueryParamsRequest is request type for the Query/Params RPC method. +message QueryParamsRequest {} + +// QueryParamsResponse is response type for the Query/Params RPC method. +message QueryParamsResponse { + // params holds all the parameters of this module. + Params params = 1 [(gogoproto.nullable) = false]; +} \ No newline at end of file diff --git a/proto/pocket/gateway/tx.proto b/proto/pocket/gateway/tx.proto new file mode 100644 index 000000000..6877dd05f --- /dev/null +++ b/proto/pocket/gateway/tx.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package pocket.gateway; + +option go_package = "pocket/x/gateway/types"; + +// Msg defines the Msg service. +service Msg {} \ No newline at end of file diff --git a/testutil/keeper/gateway.go b/testutil/keeper/gateway.go new file mode 100644 index 000000000..8447a7bf2 --- /dev/null +++ b/testutil/keeper/gateway.go @@ -0,0 +1,54 @@ +package keeper + +import ( + "testing" + + tmdb "github.com/cometbft/cometbft-db" + "github.com/cometbft/cometbft/libs/log" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/store" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + typesparams "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/stretchr/testify/require" + "pocket/x/gateway/keeper" + "pocket/x/gateway/types" +) + +func GatewayKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { + storeKey := sdk.NewKVStoreKey(types.StoreKey) + memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) + + db := tmdb.NewMemDB() + stateStore := store.NewCommitMultiStore(db) + stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) + stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) + require.NoError(t, stateStore.LoadLatestVersion()) + + registry := codectypes.NewInterfaceRegistry() + cdc := codec.NewProtoCodec(registry) + + paramsSubspace := typesparams.NewSubspace(cdc, + types.Amino, + storeKey, + memStoreKey, + "GatewayParams", + ) + k := keeper.NewKeeper( + cdc, + storeKey, + memStoreKey, + paramsSubspace, + nil, + nil, + ) + + ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) + + // Initialize params + k.SetParams(ctx, types.DefaultParams()) + + return k, ctx +} diff --git a/x/gateway/client/cli/query.go b/x/gateway/client/cli/query.go new file mode 100644 index 000000000..8e86c7103 --- /dev/null +++ b/x/gateway/client/cli/query.go @@ -0,0 +1,31 @@ +package cli + +import ( + "fmt" + // "strings" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + // "github.com/cosmos/cosmos-sdk/client/flags" + // sdk "github.com/cosmos/cosmos-sdk/types" + + "pocket/x/gateway/types" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string) *cobra.Command { + // Group gateway queries under a subcommand + cmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + cmd.AddCommand(CmdQueryParams()) + // this line is used by starport scaffolding # 1 + + return cmd +} diff --git a/x/gateway/client/cli/query_params.go b/x/gateway/client/cli/query_params.go new file mode 100644 index 000000000..b868cb116 --- /dev/null +++ b/x/gateway/client/cli/query_params.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/spf13/cobra" + + "pocket/x/gateway/types" +) + +func CmdQueryParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "params", + Short: "shows the parameters of the module", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/gateway/client/cli/tx.go b/x/gateway/client/cli/tx.go new file mode 100644 index 000000000..fd75579ee --- /dev/null +++ b/x/gateway/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "fmt" + "time" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + // "github.com/cosmos/cosmos-sdk/client/flags" + "pocket/x/gateway/types" +) + +var ( + DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) +) + +const ( + flagPacketTimeoutTimestamp = "packet-timeout-timestamp" + listSeparator = "," +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + // this line is used by starport scaffolding # 1 + + return cmd +} diff --git a/x/gateway/genesis.go b/x/gateway/genesis.go new file mode 100644 index 000000000..54ebc446a --- /dev/null +++ b/x/gateway/genesis.go @@ -0,0 +1,23 @@ +package gateway + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "pocket/x/gateway/keeper" + "pocket/x/gateway/types" +) + +// InitGenesis initializes the module's state from a provided genesis state. +func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { + // this line is used by starport scaffolding # genesis/module/init + k.SetParams(ctx, genState.Params) +} + +// ExportGenesis returns the module's exported genesis +func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { + genesis := types.DefaultGenesis() + genesis.Params = k.GetParams(ctx) + + // this line is used by starport scaffolding # genesis/module/export + + return genesis +} diff --git a/x/gateway/genesis_test.go b/x/gateway/genesis_test.go new file mode 100644 index 000000000..998ce48ea --- /dev/null +++ b/x/gateway/genesis_test.go @@ -0,0 +1,29 @@ +package gateway_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + keepertest "pocket/testutil/keeper" + "pocket/testutil/nullify" + "pocket/x/gateway" + "pocket/x/gateway/types" +) + +func TestGenesis(t *testing.T) { + genesisState := types.GenesisState{ + Params: types.DefaultParams(), + + // this line is used by starport scaffolding # genesis/test/state + } + + k, ctx := keepertest.GatewayKeeper(t) + gateway.InitGenesis(ctx, *k, genesisState) + got := gateway.ExportGenesis(ctx, *k) + require.NotNil(t, got) + + nullify.Fill(&genesisState) + nullify.Fill(got) + + // this line is used by starport scaffolding # genesis/test/assert +} diff --git a/x/gateway/keeper/keeper.go b/x/gateway/keeper/keeper.go new file mode 100644 index 000000000..c132d3a48 --- /dev/null +++ b/x/gateway/keeper/keeper.go @@ -0,0 +1,54 @@ +package keeper + +import ( + "fmt" + + "github.com/cometbft/cometbft/libs/log" + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + + "pocket/x/gateway/types" +) + +type ( + Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + paramstore paramtypes.Subspace + + bankKeeper types.BankKeeper + accountKeeper types.AccountKeeper + } +) + +func NewKeeper( + cdc codec.BinaryCodec, + storeKey, + memKey storetypes.StoreKey, + ps paramtypes.Subspace, + + bankKeeper types.BankKeeper, + accountKeeper types.AccountKeeper, +) *Keeper { + // set KeyTable if it has not already been set + if !ps.HasKeyTable() { + ps = ps.WithKeyTable(types.ParamKeyTable()) + } + + return &Keeper{ + cdc: cdc, + storeKey: storeKey, + memKey: memKey, + paramstore: ps, + + bankKeeper: bankKeeper, + accountKeeper: accountKeeper, + } +} + +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} diff --git a/x/gateway/keeper/msg_server.go b/x/gateway/keeper/msg_server.go new file mode 100644 index 000000000..9c9e6c757 --- /dev/null +++ b/x/gateway/keeper/msg_server.go @@ -0,0 +1,17 @@ +package keeper + +import ( + "pocket/x/gateway/types" +) + +type msgServer struct { + Keeper +} + +// NewMsgServerImpl returns an implementation of the MsgServer interface +// for the provided Keeper. +func NewMsgServerImpl(keeper Keeper) types.MsgServer { + return &msgServer{Keeper: keeper} +} + +var _ types.MsgServer = msgServer{} diff --git a/x/gateway/keeper/msg_server_test.go b/x/gateway/keeper/msg_server_test.go new file mode 100644 index 000000000..fc97c41e2 --- /dev/null +++ b/x/gateway/keeper/msg_server_test.go @@ -0,0 +1,23 @@ +package keeper_test + +import ( + "context" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + keepertest "pocket/testutil/keeper" + "pocket/x/gateway/keeper" + "pocket/x/gateway/types" +) + +func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) { + k, ctx := keepertest.GatewayKeeper(t) + return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx) +} + +func TestMsgServer(t *testing.T) { + ms, ctx := setupMsgServer(t) + require.NotNil(t, ms) + require.NotNil(t, ctx) +} diff --git a/x/gateway/keeper/params.go b/x/gateway/keeper/params.go new file mode 100644 index 000000000..d4fc473ed --- /dev/null +++ b/x/gateway/keeper/params.go @@ -0,0 +1,16 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "pocket/x/gateway/types" +) + +// GetParams get all parameters as types.Params +func (k Keeper) GetParams(ctx sdk.Context) types.Params { + return types.NewParams() +} + +// SetParams set the params +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramstore.SetParamSet(ctx, ¶ms) +} diff --git a/x/gateway/keeper/params_test.go b/x/gateway/keeper/params_test.go new file mode 100644 index 000000000..171bb5d30 --- /dev/null +++ b/x/gateway/keeper/params_test.go @@ -0,0 +1,18 @@ +package keeper_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + testkeeper "pocket/testutil/keeper" + "pocket/x/gateway/types" +) + +func TestGetParams(t *testing.T) { + k, ctx := testkeeper.GatewayKeeper(t) + params := types.DefaultParams() + + k.SetParams(ctx, params) + + require.EqualValues(t, params, k.GetParams(ctx)) +} diff --git a/x/gateway/keeper/query.go b/x/gateway/keeper/query.go new file mode 100644 index 000000000..2b7887a31 --- /dev/null +++ b/x/gateway/keeper/query.go @@ -0,0 +1,7 @@ +package keeper + +import ( + "pocket/x/gateway/types" +) + +var _ types.QueryServer = Keeper{} diff --git a/x/gateway/keeper/query_params.go b/x/gateway/keeper/query_params.go new file mode 100644 index 000000000..188c71c02 --- /dev/null +++ b/x/gateway/keeper/query_params.go @@ -0,0 +1,19 @@ +package keeper + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "pocket/x/gateway/types" +) + +func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { + if req == nil { + return nil, status.Error(codes.InvalidArgument, "invalid request") + } + ctx := sdk.UnwrapSDKContext(goCtx) + + return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil +} diff --git a/x/gateway/keeper/query_params_test.go b/x/gateway/keeper/query_params_test.go new file mode 100644 index 000000000..b2fa1f57d --- /dev/null +++ b/x/gateway/keeper/query_params_test.go @@ -0,0 +1,21 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + testkeeper "pocket/testutil/keeper" + "pocket/x/gateway/types" +) + +func TestParamsQuery(t *testing.T) { + keeper, ctx := testkeeper.GatewayKeeper(t) + wctx := sdk.WrapSDKContext(ctx) + params := types.DefaultParams() + keeper.SetParams(ctx, params) + + response, err := keeper.Params(wctx, &types.QueryParamsRequest{}) + require.NoError(t, err) + require.Equal(t, &types.QueryParamsResponse{Params: params}, response) +} diff --git a/x/gateway/module.go b/x/gateway/module.go new file mode 100644 index 000000000..e1aacb5d9 --- /dev/null +++ b/x/gateway/module.go @@ -0,0 +1,148 @@ +package gateway + +import ( + "context" + "encoding/json" + "fmt" + // this line is used by starport scaffolding # 1 + + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/spf13/cobra" + + abci "github.com/cometbft/cometbft/abci/types" + + "github.com/cosmos/cosmos-sdk/client" + "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/module" + "pocket/x/gateway/client/cli" + "pocket/x/gateway/keeper" + "pocket/x/gateway/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// ---------------------------------------------------------------------------- +// AppModuleBasic +// ---------------------------------------------------------------------------- + +// AppModuleBasic implements the AppModuleBasic interface that defines the independent methods a Cosmos SDK module needs to implement. +type AppModuleBasic struct { + cdc codec.BinaryCodec +} + +func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic { + return AppModuleBasic{cdc: cdc} +} + +// Name returns the name of the module as a string +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterLegacyAminoCodec registers the amino codec for the module, which is used to marshal and unmarshal structs to/from []byte in order to persist them in the module's KVStore +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterCodec(cdc) +} + +// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message +func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { + types.RegisterInterfaces(reg) +} + +// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage. The default GenesisState need to be defined by the module developer and is primarily used for testing +func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { + return cdc.MustMarshalJSON(types.DefaultGenesis()) +} + +// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { + var genState types.GenesisState + if err := cdc.UnmarshalJSON(bz, &genState); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) + } + return genState.Validate() +} + +// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module +func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) +} + +// GetTxCmd returns the root Tx command for the module. The subcommands of this root command are used by end-users to generate new transactions containing messages defined in the module +func (a AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd() +} + +// GetQueryCmd returns the root query command for the module. The subcommands of this root command are used by end-users to generate new queries to the subset of the state defined by the module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(types.StoreKey) +} + +// ---------------------------------------------------------------------------- +// AppModule +// ---------------------------------------------------------------------------- + +// AppModule implements the AppModule interface that defines the inter-dependent methods that modules need to implement +type AppModule struct { + AppModuleBasic + + keeper keeper.Keeper + accountKeeper types.AccountKeeper + bankKeeper types.BankKeeper +} + +func NewAppModule( + cdc codec.Codec, + keeper keeper.Keeper, + accountKeeper types.AccountKeeper, + bankKeeper types.BankKeeper, +) AppModule { + return AppModule{ + AppModuleBasic: NewAppModuleBasic(cdc), + keeper: keeper, + accountKeeper: accountKeeper, + bankKeeper: bankKeeper, + } +} + +// RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries +func (am AppModule) RegisterServices(cfg module.Configurator) { + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(cfg.QueryServer(), am.keeper) +} + +// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted) +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// InitGenesis performs the module's genesis initialization. It returns no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { + var genState types.GenesisState + // Initialize global index to index in genesis state + cdc.MustUnmarshalJSON(gs, &genState) + + InitGenesis(ctx, am.keeper, genState) + + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the module's exported genesis state as raw JSON bytes. +func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { + genState := ExportGenesis(ctx, am.keeper) + return cdc.MustMarshalJSON(genState) +} + +// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1 +func (AppModule) ConsensusVersion() uint64 { return 1 } + +// BeginBlock contains the logic that is automatically triggered at the beginning of each block +func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} + +// EndBlock contains the logic that is automatically triggered at the end of each block +func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} diff --git a/x/gateway/module_simulation.go b/x/gateway/module_simulation.go new file mode 100644 index 000000000..d1355a7a8 --- /dev/null +++ b/x/gateway/module_simulation.go @@ -0,0 +1,64 @@ +package gateway + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" + "pocket/testutil/sample" + gatewaysimulation "pocket/x/gateway/simulation" + "pocket/x/gateway/types" +) + +// avoid unused import issue +var ( + _ = sample.AccAddress + _ = gatewaysimulation.FindAccount + _ = simulation.MsgEntryKind + _ = baseapp.Paramspace + _ = rand.Rand{} +) + +const ( +// this line is used by starport scaffolding # simapp/module/const +) + +// GenerateGenesisState creates a randomized GenState of the module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + accs := make([]string, len(simState.Accounts)) + for i, acc := range simState.Accounts { + accs[i] = acc.Address.String() + } + gatewayGenesis := types.GenesisState{ + Params: types.DefaultParams(), + // this line is used by starport scaffolding # simapp/module/genesisState + } + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&gatewayGenesis) +} + +// RegisterStoreDecoder registers a decoder. +func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// WeightedOperations returns the all the gov module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + operations := make([]simtypes.WeightedOperation, 0) + + // this line is used by starport scaffolding # simapp/module/operation + + return operations +} + +// ProposalMsgs returns msgs used for governance proposals for simulations. +func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { + return []simtypes.WeightedProposalMsg{ + // this line is used by starport scaffolding # simapp/module/OpMsg + } +} diff --git a/x/gateway/simulation/helpers.go b/x/gateway/simulation/helpers.go new file mode 100644 index 000000000..92c437c0d --- /dev/null +++ b/x/gateway/simulation/helpers.go @@ -0,0 +1,15 @@ +package simulation + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" +) + +// FindAccount find a specific address from an account list +func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) { + creator, err := sdk.AccAddressFromBech32(address) + if err != nil { + panic(err) + } + return simtypes.FindAccount(accs, creator) +} diff --git a/x/gateway/types/codec.go b/x/gateway/types/codec.go new file mode 100644 index 000000000..844157a87 --- /dev/null +++ b/x/gateway/types/codec.go @@ -0,0 +1,23 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + // this line is used by starport scaffolding # 1 + "github.com/cosmos/cosmos-sdk/types/msgservice" +) + +func RegisterCodec(cdc *codec.LegacyAmino) { + // this line is used by starport scaffolding # 2 +} + +func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { + // this line is used by starport scaffolding # 3 + + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} + +var ( + Amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) +) diff --git a/x/gateway/types/errors.go b/x/gateway/types/errors.go new file mode 100644 index 000000000..32dfef208 --- /dev/null +++ b/x/gateway/types/errors.go @@ -0,0 +1,12 @@ +package types + +// DONTCOVER + +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// x/gateway module sentinel errors +var ( + ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") +) diff --git a/x/gateway/types/expected_keepers.go b/x/gateway/types/expected_keepers.go new file mode 100644 index 000000000..6aa6e9778 --- /dev/null +++ b/x/gateway/types/expected_keepers.go @@ -0,0 +1,18 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +// AccountKeeper defines the expected account keeper used for simulations (noalias) +type AccountKeeper interface { + GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI + // Methods imported from account should be defined here +} + +// BankKeeper defines the expected interface needed to retrieve account balances. +type BankKeeper interface { + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + // Methods imported from bank should be defined here +} diff --git a/x/gateway/types/genesis.go b/x/gateway/types/genesis.go new file mode 100644 index 000000000..0af9b4416 --- /dev/null +++ b/x/gateway/types/genesis.go @@ -0,0 +1,24 @@ +package types + +import ( +// this line is used by starport scaffolding # genesis/types/import +) + +// DefaultIndex is the default global index +const DefaultIndex uint64 = 1 + +// DefaultGenesis returns the default genesis state +func DefaultGenesis() *GenesisState { + return &GenesisState{ + // this line is used by starport scaffolding # genesis/types/default + Params: DefaultParams(), + } +} + +// Validate performs basic genesis state validation returning an error upon any +// failure. +func (gs GenesisState) Validate() error { + // this line is used by starport scaffolding # genesis/types/validate + + return gs.Params.Validate() +} diff --git a/x/gateway/types/genesis_test.go b/x/gateway/types/genesis_test.go new file mode 100644 index 000000000..22f932789 --- /dev/null +++ b/x/gateway/types/genesis_test.go @@ -0,0 +1,41 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "pocket/x/gateway/types" +) + +func TestGenesisState_Validate(t *testing.T) { + tests := []struct { + desc string + genState *types.GenesisState + valid bool + }{ + { + desc: "default is valid", + genState: types.DefaultGenesis(), + valid: true, + }, + { + desc: "valid genesis state", + genState: &types.GenesisState{ + + // this line is used by starport scaffolding # types/genesis/validField + }, + valid: true, + }, + // this line is used by starport scaffolding # types/genesis/testcase + } + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + err := tc.genState.Validate() + if tc.valid { + require.NoError(t, err) + } else { + require.Error(t, err) + } + }) + } +} diff --git a/x/gateway/types/keys.go b/x/gateway/types/keys.go new file mode 100644 index 000000000..9e70da166 --- /dev/null +++ b/x/gateway/types/keys.go @@ -0,0 +1,19 @@ +package types + +const ( + // ModuleName defines the module name + ModuleName = "gateway" + + // StoreKey defines the primary module store key + StoreKey = ModuleName + + // RouterKey defines the module's message routing key + RouterKey = ModuleName + + // MemStoreKey defines the in-memory store key + MemStoreKey = "mem_gateway" +) + +func KeyPrefix(p string) []byte { + return []byte(p) +} diff --git a/x/gateway/types/params.go b/x/gateway/types/params.go new file mode 100644 index 000000000..357196ad6 --- /dev/null +++ b/x/gateway/types/params.go @@ -0,0 +1,39 @@ +package types + +import ( + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "gopkg.in/yaml.v2" +) + +var _ paramtypes.ParamSet = (*Params)(nil) + +// ParamKeyTable the param key table for launch module +func ParamKeyTable() paramtypes.KeyTable { + return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) +} + +// NewParams creates a new Params instance +func NewParams() Params { + return Params{} +} + +// DefaultParams returns a default set of parameters +func DefaultParams() Params { + return NewParams() +} + +// ParamSetPairs get the params.ParamSet +func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { + return paramtypes.ParamSetPairs{} +} + +// Validate validates the set of params +func (p Params) Validate() error { + return nil +} + +// String implements the Stringer interface. +func (p Params) String() string { + out, _ := yaml.Marshal(p) + return string(out) +} diff --git a/x/gateway/types/types.go b/x/gateway/types/types.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/gateway/types/types.go @@ -0,0 +1 @@ +package types