From 0cc971c653abeca7047e47dc1f94bba31f88d724 Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 8 Dec 2023 13:20:06 -0800 Subject: [PATCH] Scaffolded Tokenomics module (#251) * Scaffolded tokenomics module * Fix make go_lint --- app/app.go | 23 +++- docs/static/openapi.yml | 86 ++++++++++--- proto/poktroll/tokenomics/genesis.proto | 12 ++ proto/poktroll/tokenomics/params.proto | 13 ++ proto/poktroll/tokenomics/query.proto | 26 ++++ proto/poktroll/tokenomics/tx.proto | 7 ++ testutil/keeper/tokenomics.go | 52 ++++++++ x/tokenomics/client/cli/query.go | 31 +++++ x/tokenomics/client/cli/query_params.go | 36 ++++++ x/tokenomics/client/cli/tx.go | 36 ++++++ x/tokenomics/genesis.go | 23 ++++ x/tokenomics/genesis_test.go | 29 +++++ x/tokenomics/keeper/keeper.go | 46 +++++++ x/tokenomics/keeper/msg_server.go | 17 +++ x/tokenomics/keeper/msg_server_test.go | 23 ++++ x/tokenomics/keeper/params.go | 24 ++++ x/tokenomics/keeper/params_test.go | 19 +++ x/tokenomics/keeper/query.go | 7 ++ x/tokenomics/keeper/query_params.go | 19 +++ x/tokenomics/keeper/query_params_test.go | 21 ++++ x/tokenomics/module.go | 147 +++++++++++++++++++++++ x/tokenomics/module_simulation.go | 64 ++++++++++ x/tokenomics/simulation/helpers.go | 15 +++ x/tokenomics/types/codec.go | 24 ++++ x/tokenomics/types/errors.go | 12 ++ x/tokenomics/types/expected_keepers.go | 18 +++ x/tokenomics/types/genesis.go | 24 ++++ x/tokenomics/types/genesis_test.go | 41 +++++++ x/tokenomics/types/keys.go | 19 +++ x/tokenomics/types/params.go | 72 +++++++++++ x/tokenomics/types/types.go | 1 + 31 files changed, 965 insertions(+), 22 deletions(-) create mode 100644 proto/poktroll/tokenomics/genesis.proto create mode 100644 proto/poktroll/tokenomics/params.proto create mode 100644 proto/poktroll/tokenomics/query.proto create mode 100644 proto/poktroll/tokenomics/tx.proto create mode 100644 testutil/keeper/tokenomics.go create mode 100644 x/tokenomics/client/cli/query.go create mode 100644 x/tokenomics/client/cli/query_params.go create mode 100644 x/tokenomics/client/cli/tx.go create mode 100644 x/tokenomics/genesis.go create mode 100644 x/tokenomics/genesis_test.go create mode 100644 x/tokenomics/keeper/keeper.go create mode 100644 x/tokenomics/keeper/msg_server.go create mode 100644 x/tokenomics/keeper/msg_server_test.go create mode 100644 x/tokenomics/keeper/params.go create mode 100644 x/tokenomics/keeper/params_test.go create mode 100644 x/tokenomics/keeper/query.go create mode 100644 x/tokenomics/keeper/query_params.go create mode 100644 x/tokenomics/keeper/query_params_test.go create mode 100644 x/tokenomics/module.go create mode 100644 x/tokenomics/module_simulation.go create mode 100644 x/tokenomics/simulation/helpers.go create mode 100644 x/tokenomics/types/codec.go create mode 100644 x/tokenomics/types/errors.go create mode 100644 x/tokenomics/types/expected_keepers.go create mode 100644 x/tokenomics/types/genesis.go create mode 100644 x/tokenomics/types/genesis_test.go create mode 100644 x/tokenomics/types/keys.go create mode 100644 x/tokenomics/types/params.go create mode 100644 x/tokenomics/types/types.go diff --git a/app/app.go b/app/app.go index 8dc0ace54..9f2b7de84 100644 --- a/app/app.go +++ b/app/app.go @@ -110,8 +110,6 @@ import ( ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine" ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/spf13/cast" - appparams "github.com/pokt-network/poktroll/app/params" "github.com/pokt-network/poktroll/docs" applicationmodule "github.com/pokt-network/poktroll/x/application" @@ -132,6 +130,10 @@ import ( suppliermodule "github.com/pokt-network/poktroll/x/supplier" suppliermodulekeeper "github.com/pokt-network/poktroll/x/supplier/keeper" suppliermoduletypes "github.com/pokt-network/poktroll/x/supplier/types" + tokenomicsmodule "github.com/pokt-network/poktroll/x/tokenomics" + tokenomicsmodulekeeper "github.com/pokt-network/poktroll/x/tokenomics/keeper" + tokenomicsmoduletypes "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/spf13/cast" ) const ( @@ -197,6 +199,7 @@ var ( applicationmodule.AppModuleBasic{}, suppliermodule.AppModuleBasic{}, gatewaymodule.AppModuleBasic{}, + tokenomicsmodule.AppModuleBasic{}, // this line is used by starport scaffolding # stargate/app/moduleBasic ) @@ -283,6 +286,8 @@ type App struct { SupplierKeeper suppliermodulekeeper.Keeper GatewayKeeper gatewaymodulekeeper.Keeper + + TokenomicsKeeper tokenomicsmodulekeeper.Keeper // this line is used by starport scaffolding # stargate/app/keeperDeclaration // mm is the module manager @@ -335,6 +340,7 @@ func New( applicationmoduletypes.StoreKey, suppliermoduletypes.StoreKey, gatewaymoduletypes.StoreKey, + tokenomicsmoduletypes.StoreKey, // this line is used by starport scaffolding # stargate/app/storeKey ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) @@ -634,6 +640,14 @@ func New( supplierModule := suppliermodule.NewAppModule(appCodec, app.SupplierKeeper, app.AccountKeeper, app.BankKeeper) sessionModule := sessionmodule.NewAppModule(appCodec, app.SessionKeeper, app.AccountKeeper, app.BankKeeper) + app.TokenomicsKeeper = *tokenomicsmodulekeeper.NewKeeper( + appCodec, + keys[tokenomicsmoduletypes.StoreKey], + keys[tokenomicsmoduletypes.MemStoreKey], + app.GetSubspace(tokenomicsmoduletypes.ModuleName), + ) + tokenomicsModule := tokenomicsmodule.NewAppModule(appCodec, app.TokenomicsKeeper, app.AccountKeeper, app.BankKeeper) + // this line is used by starport scaffolding # stargate/app/keeperDefinition /**** IBC Routing ****/ @@ -701,6 +715,7 @@ func New( applicationModule, supplierModule, gatewayModule, + tokenomicsModule, // 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 @@ -739,6 +754,7 @@ func New( applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, gatewaymoduletypes.ModuleName, + tokenomicsmoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/beginBlockers ) @@ -770,6 +786,7 @@ func New( applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, gatewaymoduletypes.ModuleName, + tokenomicsmoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/endBlockers ) @@ -806,6 +823,7 @@ func New( applicationmoduletypes.ModuleName, suppliermoduletypes.ModuleName, gatewaymoduletypes.ModuleName, + tokenomicsmoduletypes.ModuleName, // this line is used by starport scaffolding # stargate/app/initGenesis } app.mm.SetOrderInitGenesis(genesisModuleOrder...) @@ -1036,6 +1054,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(applicationmoduletypes.ModuleName) paramsKeeper.Subspace(suppliermoduletypes.ModuleName) paramsKeeper.Subspace(gatewaymoduletypes.ModuleName) + paramsKeeper.Subspace(tokenomicsmoduletypes.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 3a8c8fbd6..d1e14ae06 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -47176,7 +47176,7 @@ paths: service: title: >- The Service for which the application is - configured + configured for type: object properties: id: @@ -47243,7 +47243,7 @@ paths: service: title: >- The Service for which the supplier is - configured + configured for type: object properties: id: @@ -47931,14 +47931,12 @@ paths: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -48082,14 +48080,12 @@ paths: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -48237,6 +48233,46 @@ paths: type: boolean tags: - Query + /pokt-network/poktroll/tokenomics/params: + get: + summary: Parameters queries the parameters of the module. + operationId: PoktrollTokenomicsParams + responses: + '200': + description: A successful response. + schema: + type: object + properties: + params: + description: params holds all the parameters of this module. + type: object + properties: + computeToTokensMultiplier: + type: string + format: uint64 + 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 definitions: cosmos.auth.v1beta1.AddressBytesToStringResponse: type: object @@ -77500,7 +77536,7 @@ definitions: type: object properties: service: - title: The Service for which the application is configured + title: The Service for which the application is configured for type: object properties: id: @@ -77564,7 +77600,7 @@ definitions: type: object properties: service: - title: The Service for which the supplier is configured + title: The Service for which the supplier is configured for type: object properties: id: @@ -77754,7 +77790,7 @@ definitions: type: object properties: service: - title: The Service for which the application is configured + title: The Service for which the application is configured for type: object properties: id: @@ -77816,7 +77852,7 @@ definitions: type: object properties: service: - title: The Service for which the supplier is configured + title: The Service for which the supplier is configured for type: object properties: id: @@ -77996,7 +78032,6 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type @@ -78031,7 +78066,7 @@ definitions: type: object properties: service: - title: The Service for which the supplier is configured + title: The Service for which the supplier is configured for type: object properties: id: @@ -78064,7 +78099,6 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type @@ -78122,7 +78156,6 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type @@ -78162,7 +78195,7 @@ definitions: type: object properties: service: - title: The Service for which the supplier is configured + title: The Service for which the supplier is configured for type: object properties: id: @@ -78194,7 +78227,6 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type @@ -78449,14 +78481,12 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78631,14 +78661,12 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78685,3 +78713,21 @@ definitions: description: params holds all the parameters of this module. type: object description: QueryParamsResponse is response type for the Query/Params RPC method. + poktroll.tokenomics.Params: + type: object + properties: + computeToTokensMultiplier: + type: string + format: uint64 + description: Params defines the parameters for the module. + poktroll.tokenomics.QueryParamsResponse: + type: object + properties: + params: + description: params holds all the parameters of this module. + type: object + properties: + computeToTokensMultiplier: + type: string + format: uint64 + description: QueryParamsResponse is response type for the Query/Params RPC method. diff --git a/proto/poktroll/tokenomics/genesis.proto b/proto/poktroll/tokenomics/genesis.proto new file mode 100644 index 000000000..0e544a695 --- /dev/null +++ b/proto/poktroll/tokenomics/genesis.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package poktroll.tokenomics; + +import "gogoproto/gogo.proto"; +import "poktroll/tokenomics/params.proto"; + +option go_package = "github.com/pokt-network/poktroll/x/tokenomics/types"; + +// GenesisState defines the tokenomics module's genesis state. +message GenesisState { + Params params = 1 [(gogoproto.nullable) = false]; +} diff --git a/proto/poktroll/tokenomics/params.proto b/proto/poktroll/tokenomics/params.proto new file mode 100644 index 000000000..22c58a372 --- /dev/null +++ b/proto/poktroll/tokenomics/params.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; +package poktroll.tokenomics; + +import "gogoproto/gogo.proto"; + +option go_package = "github.com/pokt-network/poktroll/x/tokenomics/types"; + +// Params defines the parameters for the module. +message Params { + option (gogoproto.goproto_stringer) = false; + + uint64 computeToTokensMultiplier = 1 [(gogoproto.moretags) = "yaml:\"compute_to_tokens_multiplier\""]; +} diff --git a/proto/poktroll/tokenomics/query.proto b/proto/poktroll/tokenomics/query.proto new file mode 100644 index 000000000..b4b87a422 --- /dev/null +++ b/proto/poktroll/tokenomics/query.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package poktroll.tokenomics; + +import "gogoproto/gogo.proto"; +import "google/api/annotations.proto"; +import "cosmos/base/query/v1beta1/pagination.proto"; +import "poktroll/tokenomics/params.proto"; + +option go_package = "github.com/pokt-network/poktroll/x/tokenomics/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 = "/pokt-network/poktroll/tokenomics/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/poktroll/tokenomics/tx.proto b/proto/poktroll/tokenomics/tx.proto new file mode 100644 index 000000000..b476aa6a6 --- /dev/null +++ b/proto/poktroll/tokenomics/tx.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package poktroll.tokenomics; + +option go_package = "github.com/pokt-network/poktroll/x/tokenomics/types"; + +// Msg defines the Msg service. +service Msg {} \ No newline at end of file diff --git a/testutil/keeper/tokenomics.go b/testutil/keeper/tokenomics.go new file mode 100644 index 000000000..ed811ff3b --- /dev/null +++ b/testutil/keeper/tokenomics.go @@ -0,0 +1,52 @@ +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/pokt-network/poktroll/x/tokenomics/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +func TokenomicsKeeper(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, + "TokenomicsParams", + ) + k := keeper.NewKeeper( + cdc, + storeKey, + memStoreKey, + paramsSubspace, + ) + + ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) + + // Initialize params + k.SetParams(ctx, types.DefaultParams()) + + return k, ctx +} diff --git a/x/tokenomics/client/cli/query.go b/x/tokenomics/client/cli/query.go new file mode 100644 index 000000000..c11674331 --- /dev/null +++ b/x/tokenomics/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" + + "github.com/pokt-network/poktroll/x/tokenomics/types" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string) *cobra.Command { + // Group tokenomics 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/tokenomics/client/cli/query_params.go b/x/tokenomics/client/cli/query_params.go new file mode 100644 index 000000000..16c3b3a8b --- /dev/null +++ b/x/tokenomics/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" + + "github.com/pokt-network/poktroll/x/tokenomics/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/tokenomics/client/cli/tx.go b/x/tokenomics/client/cli/tx.go new file mode 100644 index 000000000..ca73e6c23 --- /dev/null +++ b/x/tokenomics/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" + "github.com/pokt-network/poktroll/x/tokenomics/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/tokenomics/genesis.go b/x/tokenomics/genesis.go new file mode 100644 index 000000000..33aba20b3 --- /dev/null +++ b/x/tokenomics/genesis.go @@ -0,0 +1,23 @@ +package tokenomics + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/pokt-network/poktroll/x/tokenomics/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/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/tokenomics/genesis_test.go b/x/tokenomics/genesis_test.go new file mode 100644 index 000000000..cc07aef93 --- /dev/null +++ b/x/tokenomics/genesis_test.go @@ -0,0 +1,29 @@ +package tokenomics_test + +import ( + "testing" + + keepertest "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/testutil/nullify" + "github.com/pokt-network/poktroll/x/tokenomics" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +func TestGenesis(t *testing.T) { + genesisState := types.GenesisState{ + Params: types.DefaultParams(), + + // this line is used by starport scaffolding # genesis/test/state + } + + k, ctx := keepertest.TokenomicsKeeper(t) + tokenomics.InitGenesis(ctx, *k, genesisState) + got := tokenomics.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/tokenomics/keeper/keeper.go b/x/tokenomics/keeper/keeper.go new file mode 100644 index 000000000..9c5a8abd3 --- /dev/null +++ b/x/tokenomics/keeper/keeper.go @@ -0,0 +1,46 @@ +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" + + "github.com/pokt-network/poktroll/x/tokenomics/types" +) + +type ( + Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + paramstore paramtypes.Subspace + } +) + +func NewKeeper( + cdc codec.BinaryCodec, + storeKey, + memKey storetypes.StoreKey, + ps paramtypes.Subspace, + +) *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, + } +} + +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} diff --git a/x/tokenomics/keeper/msg_server.go b/x/tokenomics/keeper/msg_server.go new file mode 100644 index 000000000..ab7353c82 --- /dev/null +++ b/x/tokenomics/keeper/msg_server.go @@ -0,0 +1,17 @@ +package keeper + +import ( + "github.com/pokt-network/poktroll/x/tokenomics/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/tokenomics/keeper/msg_server_test.go b/x/tokenomics/keeper/msg_server_test.go new file mode 100644 index 000000000..2af3f9161 --- /dev/null +++ b/x/tokenomics/keeper/msg_server_test.go @@ -0,0 +1,23 @@ +package keeper_test + +import ( + "context" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + keepertest "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) { + k, ctx := keepertest.TokenomicsKeeper(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/tokenomics/keeper/params.go b/x/tokenomics/keeper/params.go new file mode 100644 index 000000000..e6c5423f7 --- /dev/null +++ b/x/tokenomics/keeper/params.go @@ -0,0 +1,24 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/pokt-network/poktroll/x/tokenomics/types" +) + +// GetParams get all parameters as types.Params +func (k Keeper) GetParams(ctx sdk.Context) types.Params { + return types.NewParams( + k.ComputeToTokensMultiplier(ctx), + ) +} + +// SetParams set the params +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramstore.SetParamSet(ctx, ¶ms) +} + +// ComputeToTokensMultiplier returns the ComputeToTokensMultiplier param +func (k Keeper) ComputeToTokensMultiplier(ctx sdk.Context) (res uint64) { + k.paramstore.Get(ctx, types.KeyComputeToTokensMultiplier, &res) + return +} diff --git a/x/tokenomics/keeper/params_test.go b/x/tokenomics/keeper/params_test.go new file mode 100644 index 000000000..3fb77ea65 --- /dev/null +++ b/x/tokenomics/keeper/params_test.go @@ -0,0 +1,19 @@ +package keeper_test + +import ( + "testing" + + testkeeper "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +func TestGetParams(t *testing.T) { + k, ctx := testkeeper.TokenomicsKeeper(t) + params := types.DefaultParams() + + k.SetParams(ctx, params) + + require.EqualValues(t, params, k.GetParams(ctx)) + require.EqualValues(t, params.ComputeToTokensMultiplier, k.ComputeToTokensMultiplier(ctx)) +} diff --git a/x/tokenomics/keeper/query.go b/x/tokenomics/keeper/query.go new file mode 100644 index 000000000..bb812a52f --- /dev/null +++ b/x/tokenomics/keeper/query.go @@ -0,0 +1,7 @@ +package keeper + +import ( + "github.com/pokt-network/poktroll/x/tokenomics/types" +) + +var _ types.QueryServer = Keeper{} diff --git a/x/tokenomics/keeper/query_params.go b/x/tokenomics/keeper/query_params.go new file mode 100644 index 000000000..2d380efca --- /dev/null +++ b/x/tokenomics/keeper/query_params.go @@ -0,0 +1,19 @@ +package keeper + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +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/tokenomics/keeper/query_params_test.go b/x/tokenomics/keeper/query_params_test.go new file mode 100644 index 000000000..86fdf619c --- /dev/null +++ b/x/tokenomics/keeper/query_params_test.go @@ -0,0 +1,21 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + testkeeper "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +func TestParamsQuery(t *testing.T) { + keeper, ctx := testkeeper.TokenomicsKeeper(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/tokenomics/module.go b/x/tokenomics/module.go new file mode 100644 index 000000000..159d42526 --- /dev/null +++ b/x/tokenomics/module.go @@ -0,0 +1,147 @@ +package tokenomics + +import ( + "context" + "encoding/json" + "fmt" + + // this line is used by starport scaffolding # 1 + + 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" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/pokt-network/poktroll/x/tokenomics/client/cli" + "github.com/pokt-network/poktroll/x/tokenomics/keeper" + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/spf13/cobra" +) + +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/tokenomics/module_simulation.go b/x/tokenomics/module_simulation.go new file mode 100644 index 000000000..9076699f1 --- /dev/null +++ b/x/tokenomics/module_simulation.go @@ -0,0 +1,64 @@ +package tokenomics + +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" + "github.com/pokt-network/poktroll/testutil/sample" + tokenomicssimulation "github.com/pokt-network/poktroll/x/tokenomics/simulation" + "github.com/pokt-network/poktroll/x/tokenomics/types" +) + +// avoid unused import issue +var ( + _ = sample.AccAddress + _ = tokenomicssimulation.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() + } + tokenomicsGenesis := types.GenesisState{ + Params: types.DefaultParams(), + // this line is used by starport scaffolding # simapp/module/genesisState + } + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&tokenomicsGenesis) +} + +// 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/tokenomics/simulation/helpers.go b/x/tokenomics/simulation/helpers.go new file mode 100644 index 000000000..92c437c0d --- /dev/null +++ b/x/tokenomics/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/tokenomics/types/codec.go b/x/tokenomics/types/codec.go new file mode 100644 index 000000000..39e7482ab --- /dev/null +++ b/x/tokenomics/types/codec.go @@ -0,0 +1,24 @@ +package types + +import ( + // this line is used by starport scaffolding # 1 + + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + "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/tokenomics/types/errors.go b/x/tokenomics/types/errors.go new file mode 100644 index 000000000..362a0bca2 --- /dev/null +++ b/x/tokenomics/types/errors.go @@ -0,0 +1,12 @@ +package types + +// DONTCOVER + +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// x/tokenomics module sentinel errors +var ( + ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") +) diff --git a/x/tokenomics/types/expected_keepers.go b/x/tokenomics/types/expected_keepers.go new file mode 100644 index 000000000..6aa6e9778 --- /dev/null +++ b/x/tokenomics/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/tokenomics/types/genesis.go b/x/tokenomics/types/genesis.go new file mode 100644 index 000000000..0af9b4416 --- /dev/null +++ b/x/tokenomics/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/tokenomics/types/genesis_test.go b/x/tokenomics/types/genesis_test.go new file mode 100644 index 000000000..0f9412dab --- /dev/null +++ b/x/tokenomics/types/genesis_test.go @@ -0,0 +1,41 @@ +package types_test + +import ( + "testing" + + "github.com/pokt-network/poktroll/x/tokenomics/types" + "github.com/stretchr/testify/require" +) + +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/tokenomics/types/keys.go b/x/tokenomics/types/keys.go new file mode 100644 index 000000000..c3664bad9 --- /dev/null +++ b/x/tokenomics/types/keys.go @@ -0,0 +1,19 @@ +package types + +const ( + // ModuleName defines the module name + ModuleName = "tokenomics" + + // 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_tokenomics" +) + +func KeyPrefix(p string) []byte { + return []byte(p) +} diff --git a/x/tokenomics/types/params.go b/x/tokenomics/types/params.go new file mode 100644 index 000000000..62a18de9c --- /dev/null +++ b/x/tokenomics/types/params.go @@ -0,0 +1,72 @@ +package types + +import ( + "fmt" + + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "gopkg.in/yaml.v2" +) + +var _ paramtypes.ParamSet = (*Params)(nil) + +var ( + KeyComputeToTokensMultiplier = []byte("ComputeToTokensMultiplier") + // TODO: Determine the default value + DefaultComputeToTokensMultiplier uint64 = 0 +) + +// 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( + computeToTokensMultiplier uint64, +) Params { + return Params{ + ComputeToTokensMultiplier: computeToTokensMultiplier, + } +} + +// DefaultParams returns a default set of parameters +func DefaultParams() Params { + return NewParams( + DefaultComputeToTokensMultiplier, + ) +} + +// ParamSetPairs get the params.ParamSet +func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { + return paramtypes.ParamSetPairs{ + paramtypes.NewParamSetPair(KeyComputeToTokensMultiplier, &p.ComputeToTokensMultiplier, validateComputeToTokensMultiplier), + } +} + +// Validate validates the set of params +func (p Params) Validate() error { + if err := validateComputeToTokensMultiplier(p.ComputeToTokensMultiplier); err != nil { + return err + } + + return nil +} + +// String implements the Stringer interface. +func (p Params) String() string { + out, _ := yaml.Marshal(p) + return string(out) +} + +// validateComputeToTokensMultiplier validates the ComputeToTokensMultiplier param +func validateComputeToTokensMultiplier(v interface{}) error { + computeToTokensMultiplier, ok := v.(uint64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", v) + } + + // TODO implement validation + _ = computeToTokensMultiplier + + return nil +} diff --git a/x/tokenomics/types/types.go b/x/tokenomics/types/types.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/tokenomics/types/types.go @@ -0,0 +1 @@ +package types