Skip to content

Commit

Permalink
Add RewardsPerSecond param to x/community module (#1707)
Browse files Browse the repository at this point in the history
* Add RewardsPerSecond param to community

* Update rewards per second param to int

* Add rewards_per_second to protonet genesis

* Use default rewards per second of 744191

* Include value if negative in Validate error

* Rename RewardsPerSecond param to StakingRewardsPerSecond

* Add changelog entry

* Add param migration, update consensus version to 2

* Update proto docs
  • Loading branch information
drklee3 committed Sep 18, 2023
1 parent 535ff95 commit 7a8b10f
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## State Machine Breaking
- (community) [#1704] Add param to control when inflation will be disabled
- (community) [#1707] Default staking rewards per second set to `744191`

## [v0.24.0]

Expand Down Expand Up @@ -284,6 +285,7 @@ the [changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.38.4/CHANGELOG.md).
- [#257](https://github.com/Kava-Labs/kava/pulls/257) Include scripts to run
large-scale simulations remotely using aws-batch

[#1707]: https://github.com/Kava-Labs/kava/pull/1707
[#1668]: https://github.com/Kava-Labs/kava/pull/1668
[#1669]: https://github.com/Kava-Labs/kava/pull/1669
[#1655]: https://github.com/Kava-Labs/kava/pull/1655
Expand Down
3 changes: 2 additions & 1 deletion ci/env/kava-protonet/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,8 @@
},
"community": {
"params": {
"upgrade_time_disable_inflation": "2023-11-01T00:00:00Z"
"upgrade_time_disable_inflation": "2023-11-01T00:00:00Z",
"rewards_per_second": "744191"
}
},
"crisis": {
Expand Down
1 change: 1 addition & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,7 @@ Params defines the parameters of the community module.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `upgrade_time_disable_inflation` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | |
| `staking_rewards_per_second` | [string](#string) | | |



Expand Down
7 changes: 7 additions & 0 deletions proto/kava/community/v1beta1/params.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
syntax = "proto3";
package kava.community.v1beta1;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

Expand All @@ -12,4 +13,10 @@ message Params {
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
];

string staking_rewards_per_second = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}
17 changes: 13 additions & 4 deletions x/community/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/kava-labs/kava/x/community"
Expand All @@ -30,7 +31,10 @@ func (suite *genesisTestSuite) TestInitGenesis() {
accountKeeper := suite.App.GetAccountKeeper()

genesisState := types.NewGenesisState(
types.NewParams(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)),
types.NewParams(
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
sdkmath.NewInt(1000),
),
)

suite.NotPanics(func() {
Expand All @@ -49,8 +53,10 @@ func (suite *genesisTestSuite) TestInitGenesis() {
}

func (suite *genesisTestSuite) TestExportGenesis() {

params := types.NewParams(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC))
params := types.NewParams(
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
sdkmath.NewInt(1000),
)
suite.Keeper.SetParams(suite.Ctx, params)

genesisState := community.ExportGenesis(suite.Ctx, suite.Keeper)
Expand All @@ -60,7 +66,10 @@ func (suite *genesisTestSuite) TestExportGenesis() {

func (suite *genesisTestSuite) TestInitExportIsLossless() {
genesisState := types.NewGenesisState(
types.NewParams(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)),
types.NewParams(
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
sdkmath.NewInt(1000),
),
)

community.InitGenesis(suite.Ctx, suite.Keeper, suite.App.GetAccountKeeper(), genesisState)
Expand Down
5 changes: 4 additions & 1 deletion x/community/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func TestGrpcQueryTestSuite(t *testing.T) {
}

func (suite *grpcQueryTestSuite) TestGrpcQueryParams() {
p := types.NewParams(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC))
p := types.NewParams(
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
sdkmath.NewInt(1000),
)
suite.Keeper.SetParams(suite.Ctx, p)

res, err := suite.queryClient.Params(context.Background(), &types.QueryParamsRequest{})
Expand Down
27 changes: 27 additions & 0 deletions x/community/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/kava-labs/kava/x/community/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{
keeper: keeper,
}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.Migrate(
ctx,
ctx.KVStore(m.keeper.key),
m.keeper.cdc,
)
}
6 changes: 5 additions & 1 deletion x/community/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -59,7 +60,10 @@ func (suite *StoreTestSuite) TestGetSetParams() {
suite.Run("set overwrite previous value", func() {
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())

params := types.NewParams(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC))
params := types.NewParams(
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
sdkmath.NewInt(1000),
)
suite.Keeper.SetParams(suite.Ctx, params)

storedParams, found := suite.Keeper.GetParams(suite.Ctx)
Expand Down
38 changes: 38 additions & 0 deletions x/community/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v2

import (
"time"

storetypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/community/types"
)

const (
ModuleName = "mint"
)

// Migrate migrates the x/community module state from the consensus version 1 to
// version 2. Specifically, sets new parameters in the module state.
func Migrate(
ctx sdk.Context,
store storetypes.KVStore,
cdc codec.BinaryCodec,
) error {
params := types.NewParams(
// 2023-11-01T00:00:00Z
time.Date(2023, 11, 1, 0, 0, 0, 0, time.UTC),
sdk.NewInt(744191),
)

if err := params.Validate(); err != nil {
return err
}

bz := cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)

return nil
}
48 changes: 48 additions & 0 deletions x/community/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package v2_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/app"
v2 "github.com/kava-labs/kava/x/community/migrations/v2"
"github.com/kava-labs/kava/x/community/types"
)

func TestMigrateStore(t *testing.T) {
tApp := app.NewTestApp()
cdc := tApp.AppCodec()
storeKey := sdk.NewKVStoreKey("community")
ctx := testutil.DefaultContext(storeKey, sdk.NewTransientStoreKey("transient_test"))
store := ctx.KVStore(storeKey)

require.Nil(
t,
store.Get(types.ParamsKey),
"params shouldn't exist in store before migration",
)

require.NoError(t, v2.Migrate(ctx, store, cdc))

paramsBytes := store.Get(types.ParamsKey)
require.NotNil(t, paramsBytes, "params should be in store after migration")

var params types.Params
cdc.MustUnmarshal(paramsBytes, &params)

t.Logf("params: %+v", params)

require.Equal(
t,
types.NewParams(
time.Date(2023, 11, 1, 0, 0, 0, 0, time.UTC),
sdk.NewInt(744191),
),
params,
"params should be correct after migration",
)
}
11 changes: 10 additions & 1 deletion x/community/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package community
import (
"context"
"encoding/json"
"fmt"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand All @@ -20,6 +21,9 @@ import (
"github.com/kava-labs/kava/x/community/types"
)

// ConsensusVersion defines the current module consensus version.
const ConsensusVersion = 2

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -119,12 +123,17 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))

m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/community from version 1 to 2: %v", err))
}
}

// InitGenesis module init-genesis
Expand Down
25 changes: 23 additions & 2 deletions x/community/types/params.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
package types

import "time"
import (
"errors"
fmt "fmt"
"time"

sdkmath "cosmossdk.io/math"
)

var (
DefaultUpgradeTimeDisableInflation = time.Time{}
// DefaultStakingRewardsPerSecond is ~4.6 KAVA per block, 6.3s block time
DefaultStakingRewardsPerSecond = sdkmath.NewInt(744191)
)

// NewParams returns a new params object
func NewParams(upgradeTime time.Time) Params {
func NewParams(
upgradeTime time.Time,
stakingRewardsPerSecond sdkmath.Int,
) Params {
return Params{
UpgradeTimeDisableInflation: upgradeTime,
StakingRewardsPerSecond: stakingRewardsPerSecond,
}
}

// DefaultParams returns default params
func DefaultParams() Params {
return NewParams(
DefaultUpgradeTimeDisableInflation,
DefaultStakingRewardsPerSecond,
)
}

// Validate checks the params are valid
func (p Params) Validate() error {
if p.StakingRewardsPerSecond.IsNil() {
return errors.New("StakingRewardsPerSecond should not be nil")
}

if p.StakingRewardsPerSecond.IsNegative() {
return fmt.Errorf("StakingRewardsPerSecond should not be negative: %s", p.StakingRewardsPerSecond)
}

return nil
}
Loading

0 comments on commit 7a8b10f

Please sign in to comment.