Skip to content

Commit

Permalink
add migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 4, 2024
1 parent 9bbde1c commit 5888d1a
Show file tree
Hide file tree
Showing 8 changed files with 827 additions and 44 deletions.
35 changes: 18 additions & 17 deletions proto/zetachain/zetacore/emissions/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ message Params {
reserved 8;
}

//// Deprecated: Do not use. Use Params.
// message ParamsLegacy {
// option (gogoproto.goproto_stringer) = false;
// string max_bond_factor = 1;
// string min_bond_factor = 2;
// string avg_block_time = 3;
// string target_bond_ratio = 4;
// string validator_emission_percentage = 5;
// string observer_emission_percentage = 6;
// string tss_signer_emission_percentage = 7;
// string duration_factor_constant = 8;
// string observer_slash_amount = 9 [
// (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
// (gogoproto.nullable) = false
// ];
// int64 ballot_maturity_blocks = 10;
// }
// Deprecated: Do not use. Use Params.
// TODO: Add release version when this change is going to be applied. (v20?)
message LegacyParams {
option (gogoproto.goproto_stringer) = false;
string max_bond_factor = 1;
string min_bond_factor = 2;
string avg_block_time = 3;
string target_bond_ratio = 4;
string validator_emission_percentage = 5;
string observer_emission_percentage = 6;
string tss_signer_emission_percentage = 7;
string duration_factor_constant = 8;
string observer_slash_amount = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
int64 ballot_maturity_blocks = 10;
}
5 changes: 5 additions & 0 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func NewKeeper(
}
}

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

}

Check failure on line 61 in x/emissions/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
Expand Down
3 changes: 2 additions & 1 deletion x/emissions/migrations/v3/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

keepertest "github.com/zeta-chain/node/testutil/keeper"
"github.com/zeta-chain/node/x/emissions/exported"
v3 "github.com/zeta-chain/node/x/emissions/migrations/v3"
"github.com/zeta-chain/node/x/emissions/types"

v3 "github.com/zeta-chain/node/x/emissions/migrations/v3"
)

type mockSubspace struct {
Expand Down
66 changes: 66 additions & 0 deletions x/emissions/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v4

import (
"fmt"

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

"github.com/zeta-chain/node/x/emissions/types"
)

type EmissionsKeeper interface {
SetParams(ctx sdk.Context, params types.Params) error
}

// Migrate migrates the x/emissions module state from the consensus version 2 to
// version 3. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/emissions
// module state.
func MigrateStore(
ctx sdk.Context,
storeKey storetypes.StoreKey,
cdc codec.Codec,
emissionsKeeper EmissionsKeeper,
) error {
currentParams, found := GetParamsLegacy(ctx, storeKey, cdc)
if !found {
err := fmt.Errorf("failed to get legacy params")
ctx.Logger().Error("error :", err.Error())
return err
}

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

err := emissionsKeeper.SetParams(ctx, defaultParams)
if err != nil {
return err
}
return nil
}

func GetParamsLegacy(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec) (params types.LegacyParams, found bool) {
store := ctx.KVStore(storeKey)
bz := store.Get(types.KeyPrefix(types.ParamsKey))
if bz == nil {
return types.LegacyParams{}, false
}
err := cdc.Unmarshal(bz, &params)
if err != nil {
return types.LegacyParams{}, false
}

return params, true
}
53 changes: 53 additions & 0 deletions x/emissions/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package v4_test

import (
"testing"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/node/testutil/keeper"
"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) {
k, ctx, _, _ := keepertest.EmissionsKeeper(t)

cdc := k.GetCodec()
emissionsStoreKey := sdk.NewKVStoreKey(types.StoreKey)

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

})
}

func SetLegacyParams(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, params types.LegacyParams) error {
store := ctx.KVStore(storeKey)
bz, err := cdc.Marshal(&params)
if err != nil {
return err
}

store.Set(types.KeyPrefix(types.ParamsKey), bz)
return nil
}

// https://zetachain-api.lavenderfive.com/zeta-chain/emissions/params
func LegacyMainnetParams() types.LegacyParams {
return types.LegacyParams{
MaxBondFactor: "1.25",
MinBondFactor: "0.75",
AvgBlockTime: "6.00",
TargetBondRatio: "0.67",
ObserverEmissionPercentage: "0.125",
ValidatorEmissionPercentage: "0.75",
TssSignerEmissionPercentage: "0.125",
DurationFactorConstant: "0.001877876953694702",
ObserverSlashAmount: sdkmath.NewIntFromUint64(100000000000000000),
BallotMaturityBlocks: 100,
}
}
11 changes: 11 additions & 0 deletions x/emissions/types/legacy_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

import "gopkg.in/yaml.v2"

func (m *LegacyParams) String() string {
out, err := yaml.Marshal(m)
if err != nil {
return ""
}
return string(out)
}
28 changes: 28 additions & 0 deletions x/emissions/types/legacy_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package types_test

import (
"testing"

sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/node/x/emissions/types"
"gopkg.in/yaml.v2"
)

func TestLegacyString(t *testing.T) {
params := types.LegacyParams{
MaxBondFactor: "1.25",
MinBondFactor: "0.75",
AvgBlockTime: "6.00",
TargetBondRatio: "0.67",
ObserverEmissionPercentage: "0.125",
ValidatorEmissionPercentage: "0.75",
TssSignerEmissionPercentage: "0.125",
DurationFactorConstant: "0.001877876953694702",
ObserverSlashAmount: sdkmath.NewIntFromUint64(100000000000000000),
BallotMaturityBlocks: 100,
}
out, err := yaml.Marshal(params)
require.NoError(t, err)
require.Equal(t, string(out), params.String())
}
Loading

0 comments on commit 5888d1a

Please sign in to comment.