Skip to content

Commit

Permalink
add unit test for migration v4
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 4, 2024
1 parent 5888d1a commit 8718efb
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 78 deletions.
2 changes: 1 addition & 1 deletion testutil/sample/sample.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion x/emissions/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package emissions_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

keepertest "github.com/zeta-chain/node/testutil/keeper"
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestGenesis(t *testing.T) {

t.Run("should error for invalid params", func(t *testing.T) {
params := types.DefaultParams()
params.MinBondFactor = "0.50"
params.ObserverSlashAmount = sdk.NewInt(-1)

genesisState := types.GenesisState{
Params: params,
Expand Down
3 changes: 3 additions & 0 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func NewKeeper(

func (k Keeper) GetCodec() codec.BinaryCodec {
return k.cdc
}

func (k Keeper) GetStoreKey() storetypes.StoreKey {
return k.storeKey
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
77 changes: 17 additions & 60 deletions x/emissions/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "",
},
Expand All @@ -33,76 +35,20 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(-100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "slash amount cannot be less than 0",
},
{
name: "MaxBondFactor too high",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "max bond factor cannot be higher that 1.25",
},
{
name: "MinBondFactor too low",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "min bond factor cannot be lower that 0.75",
},
{
name: "invalid block time",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "invalid block time",
},
{
name: "invalid block time less than 0",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "block time cannot be less than or equal to 0",
},
{
name: "bond ratio too high",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "target bond ratio cannot be more than 100 percent",
},
{
name: "bond ratio too low",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "00.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
},
constainsErr: "target bond ratio cannot be less than 0 percent",
},
{
name: "validator emission percentage too high",
params: emissionstypes.Params{
ValidatorEmissionPercentage: "1.50",
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "validator emission percentage cannot be more than 100 percent",
},
Expand All @@ -113,6 +59,8 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "validator emission percentage cannot be less than 0 percent",
},
Expand All @@ -123,6 +71,8 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "-00.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "observer emission percentage cannot be less than 0 percent",
},
Expand All @@ -133,6 +83,8 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "150.25",
TssSignerEmissionPercentage: "00.25",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "observer emission percentage cannot be more than 100 percent",
},
Expand All @@ -143,6 +95,8 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "102.22",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "tss emission percentage cannot be more than 100 percent",
},
Expand All @@ -153,9 +107,12 @@ func TestKeeper_GetParams(t *testing.T) {
ObserverEmissionPercentage: "00.25",
TssSignerEmissionPercentage: "-102.22",
ObserverSlashAmount: sdkmath.NewInt(100000000000000000),
BallotMaturityBlocks: int64(emissionstypes.BallotMaturityBlocks),
BlockRewardAmount: emissionstypes.BlockReward,
},
constainsErr: "tss emission percentage cannot be less than 0 percent",
},
// TODO: add more test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions x/emissions/migrations/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func MigrateStore(

currParams.ObserverSlashAmount = types.ObserverSlashAmount
currParams.BallotMaturityBlocks = int64(types.BallotMaturityBlocks)

// BlockRewardAmount is a new parameter introduced in version 4
currParams.BlockRewardAmount = types.BlockReward
err := currParams.Validate()
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions x/emissions/migrations/v3/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestMigrate(t *testing.T) {
require.True(t, found)
legacyParams.ObserverSlashAmount = sdkmath.NewInt(100000000000000000)
legacyParams.BallotMaturityBlocks = 100
legacyParams.BlockRewardAmount = types.BlockReward
require.Equal(t, legacyParams, params)
})

Expand All @@ -67,14 +68,14 @@ func TestMigrate(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionsKeeper(t)

legacyParams := types.Params{
ValidatorEmissionPercentage: "00.50",
ValidatorEmissionPercentage: "-00.50",
ObserverEmissionPercentage: "00.35",
TssSignerEmissionPercentage: "00.15",
ObserverSlashAmount: sdk.ZeroInt(),
}
legacySubspace := newMockSubspace(legacyParams)

err := v3.MigrateStore(ctx, k, legacySubspace)
require.ErrorContains(t, err, "min bond factor cannot be lower that 0.75")
require.ErrorContains(t, err, "validator emission percentage cannot be less than 0 percent")
})
}
17 changes: 7 additions & 10 deletions x/emissions/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package v4

import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,6 +11,8 @@ import (

type EmissionsKeeper interface {
SetParams(ctx sdk.Context, params types.Params) error
GetCodec() codec.BinaryCodec
GetStoreKey() storetypes.StoreKey
}

// Migrate migrates the x/emissions module state from the consensus version 2 to
Expand All @@ -20,15 +21,11 @@ type EmissionsKeeper interface {
// module state.
func MigrateStore(
ctx sdk.Context,
storeKey storetypes.StoreKey,
cdc codec.Codec,
emissionsKeeper EmissionsKeeper,
) error {
currentParams, found := GetParamsLegacy(ctx, storeKey, cdc)
currentParams, found := GetParamsLegacy(ctx, emissionsKeeper.GetStoreKey(), emissionsKeeper.GetCodec())
if !found {
err := fmt.Errorf("failed to get legacy params")
ctx.Logger().Error("error :", err.Error())
return err
return errorsmod.Wrap(types.ErrMigrationFailed, "failed to get legacy params")
}

defaultParams := types.NewParams()
Expand All @@ -46,12 +43,12 @@ func MigrateStore(

err := emissionsKeeper.SetParams(ctx, defaultParams)
if err != nil {
return err
return errorsmod.Wrap(types.ErrMigrationFailed, err.Error())
}
return nil
}

func GetParamsLegacy(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec) (params types.LegacyParams, found bool) {
func GetParamsLegacy(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) (params types.LegacyParams, found bool) {
store := ctx.KVStore(storeKey)
bz := store.Get(types.KeyPrefix(types.ParamsKey))
if bz == nil {
Expand Down
115 changes: 112 additions & 3 deletions x/emissions/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,112 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/node/testutil/keeper"
v4 "github.com/zeta-chain/node/x/emissions/migrations/v4"
"github.com/zeta-chain/node/x/emissions/types"
)

func TestMigrateStore(t *testing.T) {
t.Run("should successfully migrate to new params", func(t *testing.T) {
t.Run("should successfully migrate to new params in mainnet", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()
mainnetParams := LegacyMainnetParams()
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)

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

//Assert
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, 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 to new params in testnet", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()
testNetParams := LegacyTestNetParams()
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, testNetParams)
require.NoError(t, err)

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

//Assert
params, found := k.GetParams(ctx)
require.True(t, found)
require.Equal(t, testNetParams.ValidatorEmissionPercentage, params.ValidatorEmissionPercentage)
require.Equal(t, testNetParams.ObserverEmissionPercentage, params.ObserverEmissionPercentage)
require.Equal(t, testNetParams.TssSignerEmissionPercentage, params.TssSignerEmissionPercentage)
require.Equal(t, testNetParams.ObserverSlashAmount, params.ObserverSlashAmount)
require.Equal(t, testNetParams.BallotMaturityBlocks, params.BallotMaturityBlocks)
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 := sdk.NewKVStoreKey(types.StoreKey)
emissionsStoreKey := k.GetStoreKey()

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

err := SetLegacyParams(ctx, emissionsStoreKey, cdc, LegacyMainnetParams())
//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)
})

t.Run("fail to migrate if legacy params are not found", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
store := ctx.KVStore(k.GetStoreKey())
store.Delete(types.KeyPrefix(types.ParamsKey))
//Act
err := v4.MigrateStore(ctx, k)
//Assert
require.ErrorIs(t, err, types.ErrMigrationFailed)
require.ErrorContains(t, err, "failed to get legacy params")
})

t.Run("fail to migrate if params are not valid", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
cdc := k.GetCodec()
emissionsStoreKey := k.GetStoreKey()
mainnetParams := LegacyMainnetParams()
mainnetParams.ObserverSlashAmount = sdkmath.NewInt(-100000000000000000)
err := SetLegacyParams(ctx, emissionsStoreKey, cdc, mainnetParams)
require.NoError(t, err)
//Act
err = v4.MigrateStore(ctx, k)
//Assert
require.ErrorIs(t, err, types.ErrMigrationFailed)
require.ErrorContains(t, err, "slash amount cannot be less than 0")
})
}

Expand Down Expand Up @@ -51,3 +144,19 @@ func LegacyMainnetParams() types.LegacyParams {
BallotMaturityBlocks: 100,
}
}

// https://zetachain-testnet-api.itrocket.net/zeta-chain/emissions/params
func LegacyTestNetParams() types.LegacyParams {
return types.LegacyParams{
MaxBondFactor: "1.25",
MinBondFactor: "0.75",
AvgBlockTime: "6.00",
TargetBondRatio: "0.67",
ObserverEmissionPercentage: "0.05",
ValidatorEmissionPercentage: "0.90",
TssSignerEmissionPercentage: "0.05",
DurationFactorConstant: "0.001877876953694702",
ObserverSlashAmount: sdkmath.NewIntFromUint64(100000000000000000),
BallotMaturityBlocks: 100,
}
}
1 change: 1 addition & 0 deletions x/emissions/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ var (
)
ErrInvalidAmount = errorsmod.Register(ModuleName, 1005, "invalid amount")
ErrUnableToSetParams = errorsmod.Register(ModuleName, 1006, "unable to set params")
ErrMigrationFailed = errorsmod.Register(ModuleName, 1007, "migration failed")
)
1 change: 0 additions & 1 deletion x/emissions/types/types.go

This file was deleted.

0 comments on commit 8718efb

Please sign in to comment.