Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/enable payment simulation #974

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
51faebf
feat: define protobuf file for params
rahulghangas Oct 20, 2022
38a6897
feat: define protobuf file for querying params
rahulghangas Oct 20, 2022
5b3d188
feat: define protobuf file for genesis value for params
rahulghangas Oct 20, 2022
fe5726c
feat: type defs for params and relevant protobuf generated files
rahulghangas Oct 20, 2022
4617ccd
feat: add cli command to query params
rahulghangas Oct 20, 2022
e4bed13
feat: getter and setter methods to get/set params from keeper
rahulghangas Oct 20, 2022
03ede00
feat: define param stores in keeper
rahulghangas Oct 20, 2022
7b77add
chore: pass releavnt stores when initializing payment keeper
rahulghangas Oct 20, 2022
f4bc0b2
feat: implement callback to query params
rahulghangas Oct 20, 2022
7b2936a
chore: typo
rahulghangas Oct 20, 2022
7f7e6fb
chore: grammar
rahulghangas Oct 20, 2022
fb951c9
chore: line ending
rahulghangas Oct 20, 2022
9c13d2c
fix: assign storeKey and memKey
rahulghangas Oct 20, 2022
cb55e9f
fix: follow standard while naming proto var
rahulghangas Oct 20, 2022
ec06af5
chore: documentation comment
rahulghangas Oct 20, 2022
a51a379
chore: run gofumpt
rahulghangas Oct 20, 2022
85d8b69
fix: generate pb files using correct version
rahulghangas Oct 20, 2022
4aeac74
chore: make params uint32 instead of int32 and set default values
rahulghangas Oct 20, 2022
1d6b38e
chore: typo
rahulghangas Oct 20, 2022
092cc39
chore: better comment
rahulghangas Oct 20, 2022
499fe6f
fix: min/max square size validation
rahulghangas Oct 20, 2022
500a6f5
chore: define genesis state params
rahulghangas Oct 20, 2022
b30879b
chore: set key table for subspace if not already set
rahulghangas Oct 20, 2022
2085c1b
test: define dummy payment keeper for testing
rahulghangas Oct 25, 2022
aedb534
test: define nullify module to set data to nil
rahulghangas Oct 25, 2022
e89cab3
test: params query test
rahulghangas Oct 25, 2022
9f7ed51
test: genesis test
rahulghangas Oct 25, 2022
beeb454
test: add params keeper test
rahulghangas Oct 25, 2022
45e6ff3
chore: cleanup
rahulghangas Oct 31, 2022
cd3e995
test: removing fill assertions and unsafe code
rahulghangas Nov 3, 2022
6e44517
chore: assert that min/max sqaure size is not 0
rahulghangas Nov 3, 2022
f66f6df
test: add equality check for genesis params
rahulghangas Nov 3, 2022
a53051e
chore update dependency
rahulghangas Nov 3, 2022
a745274
test: fix test by defining min/max square size
rahulghangas Nov 3, 2022
14034e5
feat: define skeletal simulation package
rahulghangas Nov 8, 2022
f3bfa34
feat: define methods used for module simulation
rahulghangas Nov 8, 2022
bb00bef
chore: remove unnecessary comments
rahulghangas Nov 8, 2022
ac6d74c
chore: gofumpted
rahulghangas Nov 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.PaymentKeeper = *paymentmodulekeeper.NewKeeper(
appCodec,
keys[paymentmoduletypes.StoreKey],
keys[paymentmoduletypes.MemStoreKey],
app.GetSubspace(paymentmoduletypes.ModuleName),
)
paymentmod := paymentmodule.NewAppModule(appCodec, app.PaymentKeeper)

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/payment/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
syntax = "proto3";
package payment;

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

option go_package = "github.com/celestiaorg/celestia-app/x/payment/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/payment/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package payment;

import "gogoproto/gogo.proto";

option go_package = "github.com/celestiaorg/celestia-app/x/payment/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\""];
}
13 changes: 13 additions & 0 deletions proto/payment/query.proto
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
syntax = "proto3";
package payment;

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

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

// Query defines the gRPC querier service.
service Query {
// Params queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/payment/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/payment.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/payment/keeper"
"github.com/celestiaorg/celestia-app/x/payment/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 PaymentKeeper(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,
"Payment",
)
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/payment/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/payment/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/payment/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/payment/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/payment/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package payment_test

import (
"testing"

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

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

k, ctx := keepertest.PaymentKeeper(t)
payment.InitGenesis(ctx, *k, genesisState)
got := payment.ExportGenesis(ctx, *k)
require.NotNil(t, got)
require.Equal(t, types.DefaultParams(), got.Params)
}
7 changes: 7 additions & 0 deletions x/payment/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/payment/types"
)

var _ types.QueryServer = Keeper{}
19 changes: 19 additions & 0 deletions x/payment/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/payment/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/payment/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/payment/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestParamsQuery(t *testing.T) {
keeper, ctx := testkeeper.PaymentKeeper(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/payment/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/payment/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 payment 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/payment/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/payment/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 set 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/payment/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/payment/types"
"github.com/stretchr/testify/require"
)

func TestGetParams(t *testing.T) {
k, ctx := testkeeper.PaymentKeeper(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))
}
Loading