-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
827 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶ms) | ||
if err != nil { | ||
return types.LegacyParams{}, false | ||
} | ||
|
||
return params, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶ms) | ||
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.