-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bump cosmos-sdk to v0.47.9-ics-lsm * add migrations * bump up ethermint * bump up irismod * update ValidatorLiquidStakingCap * delete unused code * Upgrade cosmos-sdk, patch to fix [GHSA-86h5-xcpx-cfqc](#GHSA-86h5-xcpx-cfqc) vulnerability
- Loading branch information
Dreamer
authored
Mar 6, 2024
1 parent
8e788f3
commit ce91df7
Showing
12 changed files
with
394 additions
and
279 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,42 +1,59 @@ | ||
package v300 | ||
|
||
var allowMessages = []string{ | ||
"/cosmos.authz.v1beta1.MsgExec", | ||
"/cosmos.authz.v1beta1.MsgGrant", | ||
"/cosmos.authz.v1beta1.MsgRevoke", | ||
"/cosmos.bank.v1beta1.MsgSend", | ||
"/cosmos.bank.v1beta1.MsgMultiSend", | ||
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", | ||
"/cosmos.distribution.v1beta1.MsgFundCommunityPool", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", | ||
"/cosmos.feegrant.v1beta1.MsgGrantAllowance", | ||
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance", | ||
"/cosmos.gov.v1beta1.MsgVoteWeighted", | ||
"/cosmos.gov.v1beta1.MsgSubmitProposal", | ||
"/cosmos.gov.v1beta1.MsgDeposit", | ||
"/cosmos.gov.v1beta1.MsgVote", | ||
"/cosmos.gov.v1.MsgVoteWeighted", | ||
"/cosmos.gov.v1.MsgSubmitProposal", | ||
"/cosmos.gov.v1.MsgDeposit", | ||
"/cosmos.gov.v1.MsgVote", | ||
"/cosmos.staking.v1beta1.MsgEditValidator", | ||
"/cosmos.staking.v1beta1.MsgDelegate", | ||
"/cosmos.staking.v1beta1.MsgUndelegate", | ||
"/cosmos.staking.v1beta1.MsgBeginRedelegate", | ||
"/cosmos.staking.v1beta1.MsgCreateValidator", | ||
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount", | ||
"/ibc.applications.transfer.v1.MsgTransfer", | ||
"/irismod.nft.MsgIssueDenom", | ||
"/irismod.nft.MsgTransferDenom", | ||
"/irismod.nft.MsgMintNFT", | ||
"/irismod.nft.MsgEditNFT", | ||
"/irismod.nft.MsgTransferNFT", | ||
"/irismod.nft.MsgBurnNFT", | ||
"/irismod.mt.MsgIssueDenom", | ||
"/irismod.mt.MsgTransferDenom", | ||
"/irismod.mt.MsgMintMT", | ||
"/irismod.mt.MsgEditMT", | ||
"/irismod.mt.MsgTransferMT", | ||
"/irismod.mt.MsgBurnMT", | ||
} | ||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var ( | ||
// ValidatorBondFactor dictates the cap on the liquid shares | ||
// for a validator - determined as a multiple to their validator bond | ||
// (e.g. ValidatorBondShares = 1000, BondFactor = 250 -> LiquidSharesCap: 250,000) | ||
ValidatorBondFactor = sdk.NewDec(250) | ||
// ValidatorLiquidStakingCap represents a cap on the portion of stake that | ||
// comes from liquid staking providers for a specific validator | ||
ValidatorLiquidStakingCap = sdk.MustNewDecFromStr("1") // 100% | ||
// GlobalLiquidStakingCap represents the percentage cap on | ||
// the portion of a chain's total stake can be liquid | ||
GlobalLiquidStakingCap = sdk.MustNewDecFromStr("0.25") // 25% | ||
|
||
allowMessages = []string{ | ||
"/cosmos.authz.v1beta1.MsgExec", | ||
"/cosmos.authz.v1beta1.MsgGrant", | ||
"/cosmos.authz.v1beta1.MsgRevoke", | ||
"/cosmos.bank.v1beta1.MsgSend", | ||
"/cosmos.bank.v1beta1.MsgMultiSend", | ||
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", | ||
"/cosmos.distribution.v1beta1.MsgFundCommunityPool", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", | ||
"/cosmos.feegrant.v1beta1.MsgGrantAllowance", | ||
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance", | ||
"/cosmos.gov.v1beta1.MsgVoteWeighted", | ||
"/cosmos.gov.v1beta1.MsgSubmitProposal", | ||
"/cosmos.gov.v1beta1.MsgDeposit", | ||
"/cosmos.gov.v1beta1.MsgVote", | ||
"/cosmos.gov.v1.MsgVoteWeighted", | ||
"/cosmos.gov.v1.MsgSubmitProposal", | ||
"/cosmos.gov.v1.MsgDeposit", | ||
"/cosmos.gov.v1.MsgVote", | ||
"/cosmos.staking.v1beta1.MsgEditValidator", | ||
"/cosmos.staking.v1beta1.MsgDelegate", | ||
"/cosmos.staking.v1beta1.MsgUndelegate", | ||
"/cosmos.staking.v1beta1.MsgBeginRedelegate", | ||
"/cosmos.staking.v1beta1.MsgCreateValidator", | ||
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount", | ||
"/ibc.applications.transfer.v1.MsgTransfer", | ||
"/irismod.nft.MsgIssueDenom", | ||
"/irismod.nft.MsgTransferDenom", | ||
"/irismod.nft.MsgMintNFT", | ||
"/irismod.nft.MsgEditNFT", | ||
"/irismod.nft.MsgTransferNFT", | ||
"/irismod.nft.MsgBurnNFT", | ||
"/irismod.mt.MsgIssueDenom", | ||
"/irismod.mt.MsgTransferDenom", | ||
"/irismod.mt.MsgMintMT", | ||
"/irismod.mt.MsgEditMT", | ||
"/irismod.mt.MsgTransferMT", | ||
"/irismod.mt.MsgBurnMT", | ||
} | ||
) |
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,135 @@ | ||
package v300 | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
// keeper contains the staking keeper functions required | ||
// for the migration | ||
type keeper interface { | ||
GetAllDelegations(ctx sdk.Context) []types.Delegation | ||
GetAllValidators(ctx sdk.Context) []types.Validator | ||
SetDelegation(ctx sdk.Context, delegation types.Delegation) | ||
SetValidator(ctx sdk.Context, validator types.Validator) | ||
RefreshTotalLiquidStaked(ctx sdk.Context) error | ||
GetParams(ctx sdk.Context) (params types.Params) | ||
SetParams(ctx sdk.Context, params types.Params) error | ||
} | ||
|
||
// MigrateParamsStore migrates the params store to the latest version. | ||
// | ||
// ctx - sdk context | ||
// k - keeper | ||
func MigrateParamsStore(ctx sdk.Context, k keeper) { | ||
params := k.GetParams(ctx) | ||
params.ValidatorBondFactor = ValidatorBondFactor | ||
params.ValidatorLiquidStakingCap = ValidatorLiquidStakingCap | ||
params.GlobalLiquidStakingCap = GlobalLiquidStakingCap | ||
k.SetParams(ctx, params) | ||
} | ||
|
||
// MigrateValidators Set each validator's ValidatorBondShares and LiquidShares to 0 | ||
func MigrateValidators(ctx sdk.Context, k keeper) { | ||
for _, validator := range k.GetAllValidators(ctx) { | ||
validator.ValidatorBondShares = sdk.ZeroDec() | ||
validator.LiquidShares = sdk.ZeroDec() | ||
k.SetValidator(ctx, validator) | ||
} | ||
} | ||
|
||
// MigrateDelegations Set each delegation's ValidatorBond field to false | ||
func MigrateDelegations(ctx sdk.Context, k keeper) { | ||
for _, delegation := range k.GetAllDelegations(ctx) { | ||
delegation.ValidatorBond = false | ||
k.SetDelegation(ctx, delegation) | ||
} | ||
} | ||
|
||
// MigrateUBDEntries will remove the ubdEntries with same creation_height | ||
// and create a new ubdEntry with updated balance and initial_balance | ||
func MigrateUBDEntries(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error { | ||
iterator := sdk.KVStorePrefixIterator(store, types.UnbondingDelegationKey) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
ubd := types.MustUnmarshalUBD(cdc, iterator.Value()) | ||
|
||
entriesAtSameCreationHeight := make(map[int64][]types.UnbondingDelegationEntry) | ||
for _, ubdEntry := range ubd.Entries { | ||
entriesAtSameCreationHeight[ubdEntry.CreationHeight] = append(entriesAtSameCreationHeight[ubdEntry.CreationHeight], ubdEntry) | ||
} | ||
|
||
creationHeights := make([]int64, 0, len(entriesAtSameCreationHeight)) | ||
for k := range entriesAtSameCreationHeight { | ||
creationHeights = append(creationHeights, k) | ||
} | ||
|
||
sort.Slice(creationHeights, func(i, j int) bool { return creationHeights[i] < creationHeights[j] }) | ||
|
||
ubd.Entries = make([]types.UnbondingDelegationEntry, 0, len(creationHeights)) | ||
|
||
for _, h := range creationHeights { | ||
ubdEntry := types.UnbondingDelegationEntry{ | ||
Balance: sdk.ZeroInt(), | ||
InitialBalance: sdk.ZeroInt(), | ||
} | ||
for _, entry := range entriesAtSameCreationHeight[h] { | ||
ubdEntry.Balance = ubdEntry.Balance.Add(entry.Balance) | ||
ubdEntry.InitialBalance = ubdEntry.InitialBalance.Add(entry.InitialBalance) | ||
ubdEntry.CreationHeight = entry.CreationHeight | ||
ubdEntry.CompletionTime = entry.CompletionTime | ||
} | ||
ubd.Entries = append(ubd.Entries, ubdEntry) | ||
} | ||
|
||
// set the new ubd to the store | ||
setUBDToStore(ctx, store, cdc, ubd) | ||
} | ||
return nil | ||
} | ||
|
||
func setUBDToStore(_ sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, ubd types.UnbondingDelegation) { | ||
delegatorAddress := sdk.MustAccAddressFromBech32(ubd.DelegatorAddress) | ||
|
||
bz := types.MustMarshalUBD(cdc, ubd) | ||
|
||
addr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
key := types.GetUBDKey(delegatorAddress, addr) | ||
|
||
store.Set(key, bz) | ||
} | ||
|
||
// MigrateStore performs the in-place store migration for adding LSM support to v0.45.16-ics, including: | ||
// - Adding params ValidatorBondFactor, GlobalLiquidStakingCap, ValidatorLiquidStakingCap | ||
// - Setting each validator's ValidatorBondShares and LiquidShares to 0 | ||
// - Setting each delegation's ValidatorBond field to false | ||
// - Calculating the total liquid staked by summing the delegations from ICA accounts | ||
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, k keeper) error { | ||
store := ctx.KVStore(storeKey) | ||
|
||
ctx.Logger().Info("Staking LSM Migration: Migrating param store") | ||
MigrateParamsStore(ctx, k) | ||
|
||
ctx.Logger().Info("Staking LSM Migration: Migrating validators") | ||
MigrateValidators(ctx, k) | ||
|
||
ctx.Logger().Info("Staking LSM Migration: Migrating delegations") | ||
MigrateDelegations(ctx, k) | ||
|
||
ctx.Logger().Info("Staking LSM Migration: Migrating UBD entries") | ||
if err := MigrateUBDEntries(ctx, store, cdc); err != nil { | ||
return err | ||
} | ||
|
||
ctx.Logger().Info("Staking LSM Migration: Calculating total liquid staked") | ||
return k.RefreshTotalLiquidStaked(ctx) | ||
} |
Oops, something went wrong.