-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support cosmos lsm #2909
Merged
Merged
feat: support cosmos lsm #2909
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9aa1d9f
bump cosmos-sdk to v0.47.9-ics-lsm
dreamer-zq 5048372
add migrations
dreamer-zq 9dfb142
bump up ethermint
dreamer-zq a957cf6
bump up irismod
dreamer-zq 2f6b730
update ValidatorLiquidStakingCap
dreamer-zq 230da71
delete unused code
dreamer-zq b4e7606
Upgrade cosmos-sdk, patch to fix [GHSA-86h5-xcpx-cfqc](#https://githu…
dreamer-zq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -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) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider tightening the file permissions for files containing sensitive information, such as private key seed words, to restrict access to the file owner only. For example, changing permissions to
0600
would be more secure.Consider refactoring the
InitTestnet
function to improve modularity and readability. Breaking down the function into smaller, more focused functions could enhance maintainability and make the code easier to understand and modify in the future.