Skip to content

Commit

Permalink
stake param fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcher142 committed May 14, 2020
1 parent c58cc35 commit 6a22ba6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
4 changes: 3 additions & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ func TestPoolParamsQuery(t *testing.T) {
var params stake.Params
err := cdc.UnmarshalJSON([]byte(body), &params)
require.Nil(t, err)
require.True(t, defaultParams.Equal(params))
require.EqualValues(t, defaultParams.UnbondingTime, params.UnbondingTime)
require.EqualValues(t, defaultParams.MaxValidators, params.MaxValidators)
require.EqualValues(t, defaultParams.BondDenom, params.BondDenom)

res, body = Request(t, port, "GET", "/stake/pool", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
31 changes: 29 additions & 2 deletions x/stake/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (k Keeper) BondDenom(ctx sdk.Context) (res string) {
}

func (k Keeper) MinSelfDelegation(ctx sdk.Context) (res int64) {
k.paramstore.Get(ctx, types.KeyMinSelfDelegation, &res)
k.paramstore.GetIfExists(ctx, types.KeyMinSelfDelegation, &res)
return
}

Expand All @@ -61,7 +61,34 @@ func (k Keeper) GetParams(ctx sdk.Context) (res types.Params) {
return
}

// in order to be compatible with before
type paramBeforeBscUpgrade struct {
UnbondingTime time.Duration `json:"unbonding_time"`

MaxValidators uint16 `json:"max_validators"` // maximum number of validators
BondDenom string `json:"bond_denom"` // bondable coin denomination
}

// Implements params.ParamSet
func (p *paramBeforeBscUpgrade) KeyValuePairs() params.KeyValuePairs {
return params.KeyValuePairs{
{types.KeyUnbondingTime, &p.UnbondingTime},
{types.KeyMaxValidators, &p.MaxValidators},
{types.KeyBondDenom, &p.BondDenom},
}
}

// set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
sdk.Upgrade(sdk.LaunchBscUpgrade, func() {
var pb paramBeforeBscUpgrade
pb.UnbondingTime = params.UnbondingTime
pb.MaxValidators = params.MaxValidators
pb.BondDenom = params.BondDenom

k.paramstore.SetParamSet(ctx, &pb)
}, nil, func() {
k.paramstore.SetParamSet(ctx, &params)
})

}
54 changes: 54 additions & 0 deletions x/stake/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/stake/types"

abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
)

func TestSetParams(t *testing.T) {
keyStake := sdk.NewKVStoreKey("stake")
tkeyStake := sdk.NewTransientStoreKey("transient_stake")
keyParams := sdk.NewKVStoreKey("params")
tkeyParams := sdk.NewTransientStoreKey("transient_params")

db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil)
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)

cdc := MakeTestCodec()
mode := sdk.RunTxModeDeliver
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, mode, log.NewNopLogger())
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
k := NewKeeper(cdc, keyStake, tkeyStake, nil, nil, pk.Subspace(DefaultParamspace), types.DefaultCodespace)
k.SetParams(ctx, types.DefaultParams())

require.True(t, k.paramstore.Has(ctx, types.KeyUnbondingTime))
require.True(t, k.paramstore.Has(ctx, types.KeyMaxValidators))
require.True(t, k.paramstore.Has(ctx, types.KeyBondDenom))
require.False(t, k.paramstore.Has(ctx, types.KeyMinSelfDelegation))
require.False(t, k.paramstore.Has(ctx, types.KeyMinDelegationChange))

sdk.UpgradeMgr.AddUpgradeHeight(sdk.LaunchBscUpgrade, 10)
sdk.UpgradeMgr.SetHeight(10)
k.SetParams(ctx, types.DefaultParams())
require.True(t, k.paramstore.Has(ctx, types.KeyUnbondingTime))
require.True(t, k.paramstore.Has(ctx, types.KeyMaxValidators))
require.True(t, k.paramstore.Has(ctx, types.KeyBondDenom))
require.True(t, k.paramstore.Has(ctx, types.KeyMinSelfDelegation))
require.True(t, k.paramstore.Has(ctx, types.KeyMinDelegationChange))
}
3 changes: 3 additions & 0 deletions x/stake/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context
keeper := NewKeeper(cdc, keyStake, tkeyStake, ck, nil, pk.Subspace(DefaultParamspace), types.DefaultCodespace)
keeper.SetPool(ctx, types.InitialPool())
keeper.SetParams(ctx, types.DefaultParams())
sdk.UpgradeMgr.AddUpgradeHeight(sdk.LaunchBscUpgrade, 1)
sdk.UpgradeMgr.Height = 1
keeper.SetParams(ctx, types.DefaultParams())
keeper.SetupForSideChain(&scKeeper, &ibcKeeper)

// fill all the addresses with some coins, set the loose pool tokens simultaneously
Expand Down

0 comments on commit 6a22ba6

Please sign in to comment.