Skip to content

Commit

Permalink
add unit tests under params test
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 5, 2024
1 parent f3297e5 commit 8dcc99e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 47 deletions.
2 changes: 1 addition & 1 deletion proto/zetachain/zetacore/emissions/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ message Params {
reserved 8;
}

// Deprecated: Do not use. Use Params.
// Deprecated: Do not use. Use Params Instead
message LegacyParams {
option (gogoproto.goproto_stringer) = false;
string max_bond_factor = 1;
Expand Down
2 changes: 1 addition & 1 deletion typescript/zetachain/zetacore/emissions/params_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export declare class Params extends Message<Params> {
}

/**
* Deprecated: Do not use. Use Params.
* Deprecated: Do not use. Use Params Instead
*
* @generated from message zetachain.zetacore.emissions.LegacyParams
*/
Expand Down
25 changes: 13 additions & 12 deletions x/emissions/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ type EmissionsKeeper interface {
}

// MigrateStore migrates the store from v3 to v4
// The v3 params are copied to the v4 params , and the v4 params are set in the store
// The v3 params are copied to the v4 params, and the v4 params are set in the store
// v4 params removes unused parameters from v3; these values are discarded.
// v4 introduces a new parameter, BlockRewardAmount, which is set to the default value
func MigrateStore(
ctx sdk.Context,
emissionsKeeper EmissionsKeeper,
) error {
currentParams, found := GetParamsLegacy(ctx, emissionsKeeper.GetStoreKey(), emissionsKeeper.GetCodec())
v3Params, found := GetParamsLegacy(ctx, emissionsKeeper.GetStoreKey(), emissionsKeeper.GetCodec())
if !found {
return errorsmod.Wrap(types.ErrMigrationFailed, "failed to get legacy params")
}

defaultParams := types.NewParams()
if currentParams.ValidatorEmissionPercentage != "" {
defaultParams.ValidatorEmissionPercentage = currentParams.ValidatorEmissionPercentage
v4Params := types.NewParams()
if v3Params.ValidatorEmissionPercentage != "" {
v4Params.ValidatorEmissionPercentage = v3Params.ValidatorEmissionPercentage
}
if currentParams.ObserverEmissionPercentage != "" {
defaultParams.ObserverEmissionPercentage = currentParams.ObserverEmissionPercentage
if v3Params.ObserverEmissionPercentage != "" {
v4Params.ObserverEmissionPercentage = v3Params.ObserverEmissionPercentage
}
if currentParams.TssSignerEmissionPercentage != "" {
defaultParams.TssSignerEmissionPercentage = currentParams.TssSignerEmissionPercentage
if v3Params.TssSignerEmissionPercentage != "" {
v4Params.TssSignerEmissionPercentage = v3Params.TssSignerEmissionPercentage
}
defaultParams.ObserverSlashAmount = currentParams.ObserverSlashAmount
defaultParams.BallotMaturityBlocks = currentParams.BallotMaturityBlocks
v4Params.ObserverSlashAmount = v3Params.ObserverSlashAmount
v4Params.BallotMaturityBlocks = v3Params.BallotMaturityBlocks

err := emissionsKeeper.SetParams(ctx, defaultParams)
err := emissionsKeeper.SetParams(ctx, v4Params)
if err != nil {
return errorsmod.Wrap(types.ErrMigrationFailed, err.Error())
}
Expand Down
109 changes: 86 additions & 23 deletions x/emissions/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,95 @@ func TestMigrateStore(t *testing.T) {
require.Equal(t, types.BlockReward, params.BlockRewardAmount)
})

t.Run("should successfully migrate using default values if legacy param is not available", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()
t.Run(
"should successfully migrate using default values if legacy param for ValidatorEmissionPercentage is not available",
func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()

mainnetParams := LegacyMainnetParams()
mainnetParams.ValidatorEmissionPercentage = ""
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)
mainnetParams := LegacyMainnetParams()
mainnetParams.ValidatorEmissionPercentage = ""
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)

//Act
err = v4.MigrateStore(ctx, k)
require.NoError(t, err)
//Act
err = v4.MigrateStore(ctx, k)
require.NoError(t, err)

//Assert
defaultParams := types.DefaultParams()
params, found := k.GetParams(ctx)
require.True(t, found)
require.Equal(t, defaultParams.ValidatorEmissionPercentage, params.ValidatorEmissionPercentage)
require.Equal(t, mainnetParams.ObserverEmissionPercentage, params.ObserverEmissionPercentage)
require.Equal(t, mainnetParams.TssSignerEmissionPercentage, params.TssSignerEmissionPercentage)
require.Equal(t, mainnetParams.ObserverSlashAmount, params.ObserverSlashAmount)
require.Equal(t, mainnetParams.BallotMaturityBlocks, params.BallotMaturityBlocks)
require.Equal(t, types.BlockReward, params.BlockRewardAmount)
})
//Assert
defaultParams := types.DefaultParams()
params, found := k.GetParams(ctx)
require.True(t, found)
require.Equal(t, defaultParams.ValidatorEmissionPercentage, params.ValidatorEmissionPercentage)
require.Equal(t, mainnetParams.ObserverEmissionPercentage, params.ObserverEmissionPercentage)
require.Equal(t, mainnetParams.TssSignerEmissionPercentage, params.TssSignerEmissionPercentage)
require.Equal(t, mainnetParams.ObserverSlashAmount, params.ObserverSlashAmount)
require.Equal(t, mainnetParams.BallotMaturityBlocks, params.BallotMaturityBlocks)
require.Equal(t, types.BlockReward, params.BlockRewardAmount)
},
)

t.Run(
"should successfully migrate using default values if legacy param for ObserverEmissionPercentage is not available",
func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()

mainnetParams := LegacyMainnetParams()
mainnetParams.ObserverEmissionPercentage = ""
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)

//Act
err = v4.MigrateStore(ctx, k)
require.NoError(t, err)

//Assert
defaultParams := types.DefaultParams()
params, found := k.GetParams(ctx)
require.True(t, found)
require.Equal(t, mainnetParams.ValidatorEmissionPercentage, params.ValidatorEmissionPercentage)
require.Equal(t, defaultParams.ObserverEmissionPercentage, params.ObserverEmissionPercentage)
require.Equal(t, mainnetParams.TssSignerEmissionPercentage, params.TssSignerEmissionPercentage)
require.Equal(t, mainnetParams.ObserverSlashAmount, params.ObserverSlashAmount)
require.Equal(t, mainnetParams.BallotMaturityBlocks, params.BallotMaturityBlocks)
require.Equal(t, types.BlockReward, params.BlockRewardAmount)
},
)

t.Run(
"should successfully migrate using default values if legacy param for TssSignerEmissionPercentage is not available",
func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()

mainnetParams := LegacyMainnetParams()
mainnetParams.TssSignerEmissionPercentage = ""
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)

//Act
err = v4.MigrateStore(ctx, k)
require.NoError(t, err)

//Assert
defaultParams := types.DefaultParams()
params, found := k.GetParams(ctx)
require.True(t, found)
require.Equal(t, mainnetParams.ValidatorEmissionPercentage, params.ValidatorEmissionPercentage)
require.Equal(t, mainnetParams.ObserverEmissionPercentage, params.ObserverEmissionPercentage)
require.Equal(t, defaultParams.TssSignerEmissionPercentage, params.TssSignerEmissionPercentage)
require.Equal(t, mainnetParams.ObserverSlashAmount, params.ObserverSlashAmount)
require.Equal(t, mainnetParams.BallotMaturityBlocks, params.BallotMaturityBlocks)
require.Equal(t, types.BlockReward, params.BlockRewardAmount)
},
)

t.Run("fail to migrate if legacy params are not found", func(t *testing.T) {
//Arrange
Expand Down
9 changes: 0 additions & 9 deletions x/emissions/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ const (
MemStoreKey = "mem_emissions"
WithdrawableEmissionsKey = "WithdrawableEmissions-value-"

SecsInMonth = 30 * 24 * 60 * 60
BlockRewardsInZeta = "210000000"

EmissionScheduledYears = 4
AvgBlockTime = "5.7"

ParamsKey = "Params-value-"
)

Expand All @@ -40,14 +36,9 @@ func KeyPrefix(p string) []byte {

const (
EmissionsTrackerKey = "EmissionsTracker-value-"
ParamMaxBondFactor = "MaxBondFactor"
ParamMinBondFactor = "MinBondFactor"
ParamAvgBlockTime = "AvgBlockTime"
ParamTargetBondRatio = "TargetBondRation"
ParamValidatorEmissionPercentage = "ValidatorEmissionPercentage"
ParamObserverEmissionPercentage = "ObserverEmissionPercentage"
ParamTssSignerEmissionPercentage = "SignerEmissionPercentage"
ParamDurationFactorConstant = "DurationFactorConstant"
ParamObserverSlashAmount = "ObserverSlashAmount"
)

Expand Down
2 changes: 1 addition & 1 deletion x/emissions/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions x/emissions/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestNewParams(t *testing.T) {
params.ObserverSlashAmount,
"ObserverSlashAmount should be set to 100000000000000000",
)
require.Equal(t, int64(100), params.BallotMaturityBlocks, "BallotMaturityBlocks should be set to 100")
require.Equal(t, BlockReward, params.BlockRewardAmount, "BlockRewardAmount should be set to 0")
}

func TestDefaultParams(t *testing.T) {
Expand Down Expand Up @@ -63,6 +65,15 @@ func TestValidateBallotMaturityBlocks(t *testing.T) {
require.NoError(t, validateBallotMaturityBlocks(int64(100)))
}

func TestValidateBlockRewardAmount(t *testing.T) {
require.Error(t, validateBlockRewardsAmount("0.50"))
require.Error(t, validateBlockRewardsAmount("-0.50"))
require.Error(t, validateBlockRewardsAmount(sdkmath.LegacyMustNewDecFromStr("-0.50")))
require.NoError(t, validateBlockRewardsAmount(sdkmath.LegacyMustNewDecFromStr("0.50")))
require.NoError(t, validateBlockRewardsAmount(sdkmath.LegacyZeroDec()))
require.NoError(t, validateBlockRewardsAmount(BlockReward))
}

func TestValidate(t *testing.T) {
t.Run("should validate", func(t *testing.T) {
params := NewParams()
Expand Down

0 comments on commit 8dcc99e

Please sign in to comment.