Skip to content

Commit

Permalink
feat: add payment module params (celestiaorg#893)
Browse files Browse the repository at this point in the history
Adds `minSquareSize` and `maxSquareSize` as params to the payment
module.
Defines relevant stores and queries

Relevant changes in 
`proto/payment/*`
`x/payment/keeper*`
`x/payment/types/*`

- [x] closes celestiaorg#183 

Note: The constants that currently define min/max square size have not
been deprecated, will do so in another PR
  • Loading branch information
rahulghangas committed Nov 10, 2022
1 parent 3473000 commit a5272de
Show file tree
Hide file tree
Showing 23 changed files with 1,416 additions and 37 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ func New(

app.BlobKeeper = *blobmodulekeeper.NewKeeper(
appCodec,
keys[blobmoduletypes.StoreKey],
keys[blobmoduletypes.MemStoreKey],
app.GetSubspace(blobmoduletypes.ModuleName),
)
blobmod := blobmodule.NewAppModule(appCodec, app.BlobKeeper)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/regen-network/cosmos-proto v0.3.1
github.com/tendermint/tendermint v0.34.20
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down Expand Up @@ -173,7 +174,6 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
7 changes: 6 additions & 1 deletion proto/blob/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
syntax = "proto3";
package blob;

import "gogoproto/gogo.proto";
import "blob/params.proto";

option go_package = "github.com/celestiaorg/celestia-app/x/blob/types";

// GenesisState defines the capability module's genesis state.
message GenesisState {}
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
}
14 changes: 14 additions & 0 deletions proto/blob/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package blob;

import "gogoproto/gogo.proto";

option go_package = "github.com/celestiaorg/celestia-app/x/blob/types";

// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;

uint32 min_square_size = 1 [(gogoproto.moretags) = "yaml:\"min_square_size\""];
uint32 max_square_size = 2 [(gogoproto.moretags) = "yaml:\"max_square_size\""];
}
19 changes: 17 additions & 2 deletions proto/blob/query.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
syntax = "proto3";
package blob;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "blob/params.proto";

option go_package = "github.com/celestiaorg/celestia-app/x/blob/types";

// Query defines the gRPC querier service.
service Query {}
// Query defines the gRPC query service.
service Query {
// Params queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/blob/params";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}
53 changes: 53 additions & 0 deletions testutil/keeper/blob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package keeper

import (
"testing"

"github.com/celestiaorg/celestia-app/testutil"
"github.com/celestiaorg/celestia-app/x/blob/keeper"
"github.com/celestiaorg/celestia-app/x/blob/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"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

func BlobKeeper(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,
testutil.MakeTestCodec(),
storeKey,
memStoreKey,
"Blob",
)
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
}
2 changes: 2 additions & 0 deletions x/blob/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())

return cmd
}
34 changes: 34 additions & 0 deletions x/blob/client/cli/query_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli

import (
"context"

"github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)

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 := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
3 changes: 2 additions & 1 deletion x/blob/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()

genesis.Params = k.GetParams(ctx)
return genesis
}
22 changes: 22 additions & 0 deletions x/blob/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package blob_test

import (
"testing"

keepertest "github.com/celestiaorg/celestia-app/testutil/keeper"
"github.com/celestiaorg/celestia-app/x/blob"
"github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/stretchr/testify/require"
)

func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
}

k, ctx := keepertest.BlobKeeper(t)
blob.InitGenesis(ctx, *k, genesisState)
got := blob.ExportGenesis(ctx, *k)
require.NotNil(t, got)
require.Equal(t, types.DefaultParams(), got.Params)
}
7 changes: 7 additions & 0 deletions x/blob/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package keeper

import (
"github.com/celestiaorg/celestia-app/x/blob/types"
)

var _ types.QueryServer = Keeper{}
19 changes: 19 additions & 0 deletions x/blob/keeper/grpc_query_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper

import (
"context"

"github.com/celestiaorg/celestia-app/x/blob/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}
21 changes: 21 additions & 0 deletions x/blob/keeper/grpc_query_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper_test

import (
"testing"

testkeeper "github.com/celestiaorg/celestia-app/testutil/keeper"
"github.com/celestiaorg/celestia-app/x/blob/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestParamsQuery(t *testing.T) {
keeper, ctx := testkeeper.BlobKeeper(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)
}
23 changes: 20 additions & 3 deletions x/blob/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-app/x/blob/types"
"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"
)

const (
Expand All @@ -24,12 +26,27 @@ const (

// Keeper handles all the state changes for the blob module.
type Keeper struct {
cdc codec.BinaryCodec
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramStore paramtypes.Subspace
}

func NewKeeper(cdc codec.BinaryCodec) *Keeper {
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
) *Keeper {
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramStore: ps,
}
}

Expand Down
31 changes: 31 additions & 0 deletions x/blob/keeper/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
"github.com/celestiaorg/celestia-app/x/blob/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetParams gets all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
k.MinSquareSize(ctx),
k.MaxSquareSize(ctx),
)
}

// SetParams sets the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramStore.SetParamSet(ctx, &params)
}

// MinSquareSize returns the MinSquareSize param
func (k Keeper) MinSquareSize(ctx sdk.Context) (res uint32) {
k.paramStore.Get(ctx, types.KeyMinSquareSize, &res)
return
}

// MaxSquareSize returns the MaxSquareSize param
func (k Keeper) MaxSquareSize(ctx sdk.Context) (res uint32) {
k.paramStore.Get(ctx, types.KeyMaxSquareSize, &res)
return
}
20 changes: 20 additions & 0 deletions x/blob/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package keeper_test

import (
"testing"

testkeeper "github.com/celestiaorg/celestia-app/testutil/keeper"
"github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/stretchr/testify/require"
)

func TestGetParams(t *testing.T) {
k, ctx := testkeeper.BlobKeeper(t)
params := types.DefaultParams()

k.SetParams(ctx, params)

require.EqualValues(t, params, k.GetParams(ctx))
require.EqualValues(t, params.MinSquareSize, k.MinSquareSize(ctx))
require.EqualValues(t, params.MaxSquareSize, k.MaxSquareSize(ctx))
}
6 changes: 4 additions & 2 deletions x/blob/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const DefaultIndex uint64 = 1

// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{}
return &GenesisState{
Params: DefaultParams(),
}
}

// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
return nil
return gs.Params.Validate()
}
Loading

0 comments on commit a5272de

Please sign in to comment.