From eecbbff9d21a7fc44c81bd0a5dcbd2a402d7e221 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Sun, 1 Sep 2024 21:18:23 +0900 Subject: [PATCH 01/12] feat(x/evmstaking): two pointer partial sweep with store --- client/x/evmstaking/keeper/migrations.go | 20 +++++ client/x/evmstaking/keeper/params.go | 34 ++++++++ client/x/evmstaking/keeper/withdraw.go | 99 +++++++++++++++--------- client/x/evmstaking/module/module.go | 10 +-- client/x/evmstaking/types/keys.go | 9 ++- 5 files changed, 121 insertions(+), 51 deletions(-) create mode 100644 client/x/evmstaking/keeper/migrations.go diff --git a/client/x/evmstaking/keeper/migrations.go b/client/x/evmstaking/keeper/migrations.go new file mode 100644 index 00000000..58642c14 --- /dev/null +++ b/client/x/evmstaking/keeper/migrations.go @@ -0,0 +1,20 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Migrator is a struct for handling in-place store migrations. +type Migrator struct { + keeper Keeper +} + +// NewMigrator returns a new Migrator. +func NewMigrator(keeper Keeper) Migrator { + return Migrator{keeper: keeper} +} + +// Migrate1to2 migrates x/evmstaking from version 1 to 2. +func (Migrator) Migrate1to2(sdk.Context) error { + return nil +} diff --git a/client/x/evmstaking/keeper/params.go b/client/x/evmstaking/keeper/params.go index a500fed5..81b96398 100644 --- a/client/x/evmstaking/keeper/params.go +++ b/client/x/evmstaking/keeper/params.go @@ -106,3 +106,37 @@ func (k Keeper) GetNextValidatorSweepIndex(ctx context.Context) (nextValIndex sd return nextValIndex, nil } + +func (k Keeper) SetNextValidatorDelegationSweepIndex(ctx context.Context, nextValDelIndex sdk.IntProto) error { + store := k.storeService.OpenKVStore(ctx) + bz, err := k.cdc.Marshal(&nextValDelIndex) + if err != nil { + return errors.Wrap(err, "marshal next validator delegation sweep index") + } + + err = store.Set(types.NextValidatorDelegationSweepIndexKey, bz) + if err != nil { + return errors.Wrap(err, "set next validator delegation sweep index") + } + + return nil +} + +func (k Keeper) GetNextValidatorDelegationSweepIndex(ctx context.Context) (nextValDelIndex sdk.IntProto, err error) { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.NextValidatorDelegationSweepIndexKey) + if err != nil { + return nextValDelIndex, errors.Wrap(err, "get next validator delegation sweep index") + } + + if bz == nil { + return sdk.IntProto{Int: math.NewInt(0)}, nil + } + + err = k.cdc.Unmarshal(bz, &nextValDelIndex) + if err != nil { + return nextValDelIndex, errors.Wrap(err, "unmarshal next validator delegation sweep index") + } + + return nextValDelIndex, nil +} diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index 87f28f51..d19cfb56 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -20,12 +20,19 @@ import ( ) func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withdrawal, error) { - // TODO: user more fine-grained cursor with next delegator sweep index. nextValSweepIndex, err := k.GetNextValidatorSweepIndex(ctx) if err != nil { return nil, err } + + nextValDelSweepIndex, err := k.GetNextValidatorDelegationSweepIndex(ctx) + if err != nil { + return nil, err + } + nextValIndex := nextValSweepIndex.Int.Int64() + nextValDelIndex := nextValDelSweepIndex.Int.Int64() + // Get all validators first, and then do a circular sweep validatorSet, err := (k.stakingKeeper.(*skeeper.Keeper)).GetAllValidators(ctx) if err != nil { @@ -41,6 +48,7 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd "next_validator_index", nextValIndex, ) nextValIndex = 0 + nextValDelIndex = 0 } // Iterate all validators from `nextValidatorIndex` to find out eligible partial withdrawals. @@ -48,21 +56,19 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd swept uint32 withdrawals []estypes.Withdrawal ) + // Get sweep limit per block. sweepBound, err := k.MaxSweepPerBlock(ctx) if err != nil { return nil, err } + // Get minimal partial withdrawal amount. minPartialWithdrawalAmount, err := k.MinPartialWithdrawalAmount(ctx) if err != nil { return nil, err } - log.Debug( - ctx, "partial withdrawal params", - "min_partial_withdraw_amount", minPartialWithdrawalAmount, - "max_sweep_per_block", sweepBound, - ) + // Sweep and get eligible partial withdrawals. for range validatorSet { if swept > sweepBound { @@ -82,76 +88,83 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd } valAddr := sdk.ValAddress(valBz) valAccAddr := sdk.AccAddress(valAddr) + // Get validator commissions. valCommission, err := k.distributionKeeper.GetValidatorAccumulatedCommission(ctx, valAddr) if err != nil { return nil, err } - log.Debug( - ctx, "Get validator commission", - "val_addr", valAddr.String(), - "commission_amount", valCommission.Commission.String(), - ) + // Get all delegators of the validator. delegators, err := (k.stakingKeeper.(*skeeper.Keeper)).GetValidatorDelegations(ctx, valAddr) if err != nil { return nil, errors.Wrap(err, "get validator delegations") } - swept += uint32(len(delegators)) - log.Debug( - ctx, "Get all delegators of validator", - "val_addr", valAddr.String(), - "delegator_amount", len(delegators), - ) - // Get delegator rewards. - for i := range delegators { + + nextDelegators := delegators[nextValDelIndex:] + + // Check if the sweep should stop prematurely as the current delegator loop exceeds the sweep bound while sweeping. + shouldStopPrematurely := swept+uint32(len(nextDelegators)) > sweepBound + stoppedPrematurely := false + + // Iterate on the validator's delegator rewards in the range [nextValDelIndex, len(delegators)]. + for i := range nextDelegators { // Get end current period and calculate rewards. endingPeriod, err := k.distributionKeeper.IncrementValidatorPeriod(ctx, validatorSet[nextValIndex]) if err != nil { return nil, err } - delRewards, err := k.distributionKeeper.CalculateDelegationRewards(ctx, validatorSet[nextValIndex], delegators[i], endingPeriod) + + delRewards, err := k.distributionKeeper.CalculateDelegationRewards(ctx, validatorSet[nextValIndex], nextDelegators[i], endingPeriod) if err != nil { return nil, err } - if delegators[i].DelegatorAddress == valAccAddr.String() { + + if nextDelegators[i].DelegatorAddress == valAccAddr.String() { delRewards = delRewards.Add(valCommission.Commission...) } delRewardsTruncated, _ := delRewards.TruncateDecimal() bondDenomAmount := delRewardsTruncated.AmountOf(sdk.DefaultBondDenom).Uint64() - log.Debug( - ctx, "Calculate delegator rewards", - "val_addr", valAddr.String(), - "del_addr", delegators[i].DelegatorAddress, - "rewards_amount", bondDenomAmount, - "ending_period", endingPeriod, - ) - if bondDenomAmount >= minPartialWithdrawalAmount { - delEvmAddr, err := k.DelegatorMap.Get(ctx, delegators[i].DelegatorAddress) + delEvmAddr, err := k.DelegatorMap.Get(ctx, nextDelegators[i].DelegatorAddress) if err != nil { return nil, errors.Wrap(err, "map delegator pubkey to evm address") } + withdrawals = append(withdrawals, estypes.NewWithdrawal( uint64(sdk.UnwrapSDKContext(ctx).BlockHeight()), - delegators[i].DelegatorAddress, + nextDelegators[i].DelegatorAddress, valAddr.String(), delEvmAddr, bondDenomAmount, )) + } - log.Debug( - ctx, "Found an eligible partial withdrawal", - "val_addr", valAddr.String(), - "del_addr", delegators[i].DelegatorAddress, - "del_evm_addr", delEvmAddr, - "rewards_amount", bondDenomAmount, - ) + nextValDelIndex++ + + // Current delegator loop exceeds the sweep bound while sweeping, so we break prematurely from validator delegation sweep. + // The current value of `nextValIndex` and `nextValDelIndex` will be used in the next sweep (next block). + if shouldStopPrematurely && swept+uint32(i) > sweepBound { + stoppedPrematurely = true + break } } + + // If the validator's delegation loop was stopped prematurely, we break from the validator sweep loop. + if stoppedPrematurely { + break + } + + // Here, we have looped through all delegators of the validator (since we did not prematurely stop in the loop above). + // Thus, we signal to progress to the next validator by resetting the nextValDelIndex and circularly incrementing the nextValIndex nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + nextValDelIndex = 0 + + // Increase the total swept amount. + swept += uint32(len(delegators)) } + // Update the nextValidatorSweepIndex. if err := k.SetNextValidatorSweepIndex( ctx, @@ -159,9 +172,19 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd ); err != nil { return nil, err } + + // Update the nextValidatorDelegationSweepIndex. + if err := k.SetNextValidatorDelegationSweepIndex( + ctx, + sdk.IntProto{Int: math.NewInt(nextValDelIndex)}, + ); err != nil { + return nil, err + } + log.Debug( ctx, "Finish validator sweep for partial withdrawals", "next_validator_index", nextValIndex, + "next_validator_delegation_index", nextValDelIndex, "partial_withdrawals", len(withdrawals), ) diff --git a/client/x/evmstaking/module/module.go b/client/x/evmstaking/module/module.go index 699f9fa3..2f38081e 100644 --- a/client/x/evmstaking/module/module.go +++ b/client/x/evmstaking/module/module.go @@ -30,9 +30,6 @@ var ( _ appmodule.AppModule = AppModule{} ) -// ConsensusVersion defines the current module consensus version. -const ConsensusVersion = 1 - // ---------------------------------------------------------------------------- // AppModuleBasic // ---------------------------------------------------------------------------- @@ -106,7 +103,7 @@ func (AppModule) IsOnePerModuleType() {} func (AppModule) IsAppModule() {} // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } +func (AppModule) ConsensusVersion() uint64 { return 1 } // DefaultGenesis returns default genesis state as raw bytes for the module. func (AppModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { @@ -114,11 +111,6 @@ func (AppModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { } // RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries. -// -// func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { -// types.RegisterQueryServer(registrar, am.keeper) -// return nil -// } func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterQueryServer(cfg.QueryServer(), am.keeper) types.RegisterMsgServiceServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) diff --git a/client/x/evmstaking/types/keys.go b/client/x/evmstaking/types/keys.go index f453a6d2..4a6ed016 100644 --- a/client/x/evmstaking/types/keys.go +++ b/client/x/evmstaking/types/keys.go @@ -15,8 +15,9 @@ const ( // KVStore keys. var ( - ParamsKey = collections.NewPrefix(0) - WithdrawalQueueKey = collections.NewPrefix(1) - DelegatorMapKey = collections.NewPrefix(2) - NextValidatorSweepIndexKey = collections.NewPrefix(3) + ParamsKey = collections.NewPrefix(0) + WithdrawalQueueKey = collections.NewPrefix(1) + DelegatorMapKey = collections.NewPrefix(2) + NextValidatorSweepIndexKey = collections.NewPrefix(3) + NextValidatorDelegationSweepIndexKey = collections.NewPrefix(4) ) From 8657688c3e56dd15e6e97ba639708dd6051d572a Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Sun, 1 Sep 2024 21:18:53 +0900 Subject: [PATCH 02/12] feat(app): upgrade v0.10.0 --- client/app/upgrades.go | 5 +- client/app/upgrades/v0_10_0/constants.go | 16 +++++++ client/app/upgrades/v0_10_0/upgrades.go | 59 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 client/app/upgrades/v0_10_0/constants.go create mode 100644 client/app/upgrades/v0_10_0/upgrades.go diff --git a/client/app/upgrades.go b/client/app/upgrades.go index e1bb1088..9a4c089d 100644 --- a/client/app/upgrades.go +++ b/client/app/upgrades.go @@ -8,12 +8,15 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/piplabs/story/client/app/upgrades" + "github.com/piplabs/story/client/app/upgrades/v0_10_0" ) var ( // `Upgrades` defines the upgrade handlers and store loaders for the application. // New upgrades should be added to this slice after they are implemented. - Upgrades = []upgrades.Upgrade{} + Upgrades = []upgrades.Upgrade{ + v0_10_0.Upgrade, + } // Forks are for hard forks that breaks backward compatibility. Forks = []upgrades.Fork{} ) diff --git a/client/app/upgrades/v0_10_0/constants.go b/client/app/upgrades/v0_10_0/constants.go new file mode 100644 index 00000000..3cb1acd2 --- /dev/null +++ b/client/app/upgrades/v0_10_0/constants.go @@ -0,0 +1,16 @@ +//nolint:revive,stylecheck // version underscores +package v0_10_0 + +import ( + storetypes "cosmossdk.io/store/types" + + "github.com/piplabs/story/client/app/upgrades" +) + +const UpgradeName = "v0.10.0" + +var Upgrade = upgrades.Upgrade{ + UpgradeName: UpgradeName, + CreateUpgradeHandler: CreateUpgradeHandler, + StoreUpgrades: storetypes.StoreUpgrades{}, +} diff --git a/client/app/upgrades/v0_10_0/upgrades.go b/client/app/upgrades/v0_10_0/upgrades.go new file mode 100644 index 00000000..b8e7b3ad --- /dev/null +++ b/client/app/upgrades/v0_10_0/upgrades.go @@ -0,0 +1,59 @@ +//nolint:revive,stylecheck // version underscores +package v0_10_0 + +import ( + "context" + + "cosmossdk.io/math" + upgradetypes "cosmossdk.io/x/upgrade/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/piplabs/story/client/app/keepers" + "github.com/piplabs/story/lib/errors" + clog "github.com/piplabs/story/lib/log" +) + +const ( + NewMaxSweepPerBlock = 1024 +) + +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, + keepers *keepers.Keepers, +) upgradetypes.UpgradeHandler { + return func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + clog.Info(ctx, "Starting module migrations...") + + vm, err := mm.RunMigrations(ctx, configurator, vm) + if err != nil { + return vm, errors.Wrap(err, "run migrations") + } + + clog.Info(ctx, "Setting NextValidatorDelegationSweepIndex parameter...") + if err := keepers.EvmStakingKeeper.SetNextValidatorDelegationSweepIndex( + ctx, + sdk.IntProto{Int: math.NewInt(0)}, + ); err != nil { + return vm, errors.Wrap(err, "set evmstaking NextValidatorDelegationSweepIndex") + } + + // Update MaxSweepPerBlock + clog.Info(ctx, "Updating MaxSweepPerBlock parameter...") + params, err := keepers.EvmStakingKeeper.GetParams(ctx) + if err != nil { + return vm, errors.Wrap(err, "get evmstaking params") + } + + params.MaxSweepPerBlock = NewMaxSweepPerBlock + if err = keepers.EvmStakingKeeper.SetParams(ctx, params); err != nil { + return vm, errors.Wrap(err, "set evmstaking params") + } + + clog.Info(ctx, "Upgrade v0.10.0 complete") + + return vm, nil + } +} From 9a2ff61011bdb50dfef48cf30ae254cc86d73fe3 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Sun, 1 Sep 2024 21:22:11 +0900 Subject: [PATCH 03/12] chore(buildinfo): bump to v0.10.0 --- lib/buildinfo/buildinfo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/buildinfo/buildinfo.go b/lib/buildinfo/buildinfo.go index bf5984ef..88d348c1 100644 --- a/lib/buildinfo/buildinfo.go +++ b/lib/buildinfo/buildinfo.go @@ -14,8 +14,8 @@ import ( const ( VersionMajor = 0 // Major version component of the current release - VersionMinor = 9 // Minor version component of the current release - VersionPatch = 12 // Patch version component of the current release + VersionMinor = 10 // Minor version component of the current release + VersionPatch = 0 // Patch version component of the current release VersionMeta = "unstable" // Version metadata to append to the version string ) From 00f9c0061dd85432abfd2f9faa4abe070e68f833 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Thu, 5 Sep 2024 16:48:31 +0900 Subject: [PATCH 04/12] chore(x/evmstaking): remove checked file --- client/x/evmstaking/keeper/migrations.go | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 client/x/evmstaking/keeper/migrations.go diff --git a/client/x/evmstaking/keeper/migrations.go b/client/x/evmstaking/keeper/migrations.go deleted file mode 100644 index 58642c14..00000000 --- a/client/x/evmstaking/keeper/migrations.go +++ /dev/null @@ -1,20 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper Keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(keeper Keeper) Migrator { - return Migrator{keeper: keeper} -} - -// Migrate1to2 migrates x/evmstaking from version 1 to 2. -func (Migrator) Migrate1to2(sdk.Context) error { - return nil -} From 09c0c1ebbdce306c3ff38f67b5ff69a6046a862c Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Fri, 6 Sep 2024 18:04:06 +0900 Subject: [PATCH 05/12] fix(x/evmstaking): loop logic and indice change --- client/x/evmstaking/keeper/withdraw.go | 30 ++++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index d19cfb56..f82b4ffc 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -71,13 +71,11 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd // Sweep and get eligible partial withdrawals. for range validatorSet { - if swept > sweepBound { - break - } - if validatorSet[nextValIndex].IsJailed() { // nextValIndex should be updated, even if the validator is jailed, to progress to the sweep. nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + nextValDelIndex = 0 + continue } @@ -101,11 +99,22 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd return nil, errors.Wrap(err, "get validator delegations") } + if nextValDelIndex >= int64(len(delegators)) { + nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + nextValDelIndex = 0 + + continue + } + nextDelegators := delegators[nextValDelIndex:] + var shouldStopPrematurely bool // Check if the sweep should stop prematurely as the current delegator loop exceeds the sweep bound while sweeping. - shouldStopPrematurely := swept+uint32(len(nextDelegators)) > sweepBound - stoppedPrematurely := false + remainingSweep := sweepBound - swept + if uint32(len(nextDelegators)) > remainingSweep { + nextDelegators = nextDelegators[:remainingSweep] + shouldStopPrematurely = true + } // Iterate on the validator's delegator rewards in the range [nextValDelIndex, len(delegators)]. for i := range nextDelegators { @@ -142,17 +151,10 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd } nextValDelIndex++ - - // Current delegator loop exceeds the sweep bound while sweeping, so we break prematurely from validator delegation sweep. - // The current value of `nextValIndex` and `nextValDelIndex` will be used in the next sweep (next block). - if shouldStopPrematurely && swept+uint32(i) > sweepBound { - stoppedPrematurely = true - break - } } // If the validator's delegation loop was stopped prematurely, we break from the validator sweep loop. - if stoppedPrematurely { + if shouldStopPrematurely { break } From 80673061a3c7fa99ef64e3b2378138d8b61b1d41 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:01:29 -0500 Subject: [PATCH 06/12] feat(x/evmstaking): genesis state validator sweep --- client/x/evmstaking/keeper/params.go | 60 +++--- client/x/evmstaking/types/genesis.go | 8 + client/x/evmstaking/types/genesis.pb.go | 274 +++++++++++++++++++++++- client/x/evmstaking/types/genesis.proto | 14 ++ 4 files changed, 315 insertions(+), 41 deletions(-) diff --git a/client/x/evmstaking/keeper/params.go b/client/x/evmstaking/keeper/params.go index 81b96398..58d34a27 100644 --- a/client/x/evmstaking/keeper/params.go +++ b/client/x/evmstaking/keeper/params.go @@ -11,6 +11,11 @@ import ( "github.com/piplabs/story/lib/errors" ) +type ValidatorSweepIndex struct { + nextValIndex sdk.IntProto + nextValDelIndex sdk.IntProto +} + func (k Keeper) MaxWithdrawalPerBlock(ctx context.Context) (uint32, error) { params, err := k.GetParams(ctx) if err != nil { @@ -73,70 +78,59 @@ func (k Keeper) GetParams(ctx context.Context) (params types.Params, err error) return params, nil } -func (k Keeper) SetNextValidatorSweepIndex(ctx context.Context, nextValIndex sdk.IntProto) error { +func (k Keeper) SetValidatorSweepIndex(ctx context.Context, nextValIndex sdk.IntProto, nextValDelIndex sdk.IntProto) error { store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(&nextValIndex) + bz, err := k.cdc.Marshal(&types.ValidatorSweepIndex{ + NextValIndex: nextValIndex.Int.Uint64(), + NextValDelIndex: nextValDelIndex.Int.Uint64(), + }) if err != nil { - return errors.Wrap(err, "marshal next validator sweep index") + return errors.Wrap(err, "marshal validator sweep index") } - err = store.Set(types.NextValidatorSweepIndexKey, bz) + err = store.Set(types.ValidatorSweepIndexKey, bz) if err != nil { - return errors.Wrap(err, "set next validator sweep index") + return errors.Wrap(err, "set validator sweep index") } return nil } -func (k Keeper) GetNextValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.IntProto, err error) { +func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.IntProto, nextValDelIndex sdk.IntProto, err error) { store := k.storeService.OpenKVStore(ctx) - bz, err := store.Get(types.NextValidatorSweepIndexKey) + bz, err := store.Get(types.ValidatorSweepIndexKey) if err != nil { - return nextValIndex, errors.Wrap(err, "get next validator sweep index") + return nextValIndex, nextValDelIndex, errors.Wrap(err, "get validator sweep index") } if bz == nil { - return sdk.IntProto{Int: math.NewInt(0)}, nil + return sdk.IntProto{Int: math.NewInt(0)}, sdk.IntProto{Int: math.NewInt(0)}, nil } - err = k.cdc.Unmarshal(bz, &nextValIndex) + var sweepIndex types.ValidatorSweepIndex + err = k.cdc.Unmarshal(bz, &sweepIndex) if err != nil { - return nextValIndex, errors.Wrap(err, "unmarshal next validator sweep index") + return nextValIndex, nextValDelIndex, errors.Wrap(err, "unmarshal validator sweep index") } - return nextValIndex, nil + return nextValIndex, nextValDelIndex, nil } -func (k Keeper) SetNextValidatorDelegationSweepIndex(ctx context.Context, nextValDelIndex sdk.IntProto) error { +func (k Keeper) GetOldValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.IntProto, err error) { store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(&nextValDelIndex) + bz, err := store.Get(types.ValidatorSweepIndexKey) if err != nil { - return errors.Wrap(err, "marshal next validator delegation sweep index") - } - - err = store.Set(types.NextValidatorDelegationSweepIndexKey, bz) - if err != nil { - return errors.Wrap(err, "set next validator delegation sweep index") - } - - return nil -} - -func (k Keeper) GetNextValidatorDelegationSweepIndex(ctx context.Context) (nextValDelIndex sdk.IntProto, err error) { - store := k.storeService.OpenKVStore(ctx) - bz, err := store.Get(types.NextValidatorDelegationSweepIndexKey) - if err != nil { - return nextValDelIndex, errors.Wrap(err, "get next validator delegation sweep index") + return nextValIndex, errors.Wrap(err, "get next validator sweep index") } if bz == nil { return sdk.IntProto{Int: math.NewInt(0)}, nil } - err = k.cdc.Unmarshal(bz, &nextValDelIndex) + err = k.cdc.Unmarshal(bz, &nextValIndex) if err != nil { - return nextValDelIndex, errors.Wrap(err, "unmarshal next validator delegation sweep index") + return nextValIndex, errors.Wrap(err, "unmarshal next validator sweep index") } - return nextValDelIndex, nil + return nextValIndex, nil } diff --git a/client/x/evmstaking/types/genesis.go b/client/x/evmstaking/types/genesis.go index 6f230796..96965cc6 100644 --- a/client/x/evmstaking/types/genesis.go +++ b/client/x/evmstaking/types/genesis.go @@ -3,6 +3,10 @@ package types func NewGenesisState(params Params) *GenesisState { return &GenesisState{ Params: params, + ValidatorSweepIndex: &ValidatorSweepIndex{ + NextValIndex: 0, + NextValDelIndex: 0, + }, } } @@ -10,5 +14,9 @@ func NewGenesisState(params Params) *GenesisState { func DefaultGenesisState() *GenesisState { return &GenesisState{ Params: DefaultParams(), + ValidatorSweepIndex: &ValidatorSweepIndex{ + NextValIndex: 0, + NextValDelIndex: 0, + }, } } diff --git a/client/x/evmstaking/types/genesis.pb.go b/client/x/evmstaking/types/genesis.pb.go index 6105a7a6..2700450e 100644 --- a/client/x/evmstaking/types/genesis.pb.go +++ b/client/x/evmstaking/types/genesis.pb.go @@ -25,6 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type GenesisState struct { Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` + // TODO: Add withdrawals collections field as ORM if needed + ValidatorSweepIndex *ValidatorSweepIndex `protobuf:"bytes,2,opt,name=validator_sweep_index,json=validatorSweepIndex,proto3" json:"validator_sweep_index,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -67,8 +69,68 @@ func (m *GenesisState) GetParams() Params { return Params{} } +func (m *GenesisState) GetValidatorSweepIndex() *ValidatorSweepIndex { + if m != nil { + return m.ValidatorSweepIndex + } + return nil +} + +type ValidatorSweepIndex struct { + NextValIndex uint64 `protobuf:"varint,1,opt,name=next_val_index,json=nextValIndex,proto3" json:"next_val_index,omitempty" yaml:"next_val_index"` + NextValDelIndex uint64 `protobuf:"varint,2,opt,name=next_val_del_index,json=nextValDelIndex,proto3" json:"next_val_del_index,omitempty" yaml:"next_val_del_index"` +} + +func (m *ValidatorSweepIndex) Reset() { *m = ValidatorSweepIndex{} } +func (m *ValidatorSweepIndex) String() string { return proto.CompactTextString(m) } +func (*ValidatorSweepIndex) ProtoMessage() {} +func (*ValidatorSweepIndex) Descriptor() ([]byte, []int) { + return fileDescriptor_bf57cf100cbaf4bd, []int{1} +} +func (m *ValidatorSweepIndex) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ValidatorSweepIndex) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ValidatorSweepIndex.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ValidatorSweepIndex) XXX_Merge(src proto.Message) { + xxx_messageInfo_ValidatorSweepIndex.Merge(m, src) +} +func (m *ValidatorSweepIndex) XXX_Size() int { + return m.Size() +} +func (m *ValidatorSweepIndex) XXX_DiscardUnknown() { + xxx_messageInfo_ValidatorSweepIndex.DiscardUnknown(m) +} + +var xxx_messageInfo_ValidatorSweepIndex proto.InternalMessageInfo + +func (m *ValidatorSweepIndex) GetNextValIndex() uint64 { + if m != nil { + return m.NextValIndex + } + return 0 +} + +func (m *ValidatorSweepIndex) GetNextValDelIndex() uint64 { + if m != nil { + return m.NextValDelIndex + } + return 0 +} + func init() { proto.RegisterType((*GenesisState)(nil), "client.x.evmstaking.types.GenesisState") + proto.RegisterType((*ValidatorSweepIndex)(nil), "client.x.evmstaking.types.ValidatorSweepIndex") } func init() { @@ -76,19 +138,27 @@ func init() { } var fileDescriptor_bf57cf100cbaf4bd = []byte{ - // 181 bytes of a gzipped FileDescriptorProto + // 309 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4f, 0xce, 0xc9, 0x4c, 0xcd, 0x2b, 0xd1, 0xaf, 0xd0, 0x4f, 0x2d, 0xcb, 0x2d, 0x2e, 0x49, 0xcc, 0xce, 0xcc, 0x4b, 0xd7, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x84, 0x28, 0xd4, 0xab, 0xd0, 0x43, 0x28, 0xd4, 0x03, 0x2b, 0x94, 0x12, 0x49, 0xcf, 0x4f, 0xcf, 0x07, 0xab, 0xd2, 0x07, 0xb1, 0x20, 0x1a, 0xa4, 0xd4, 0x70, 0x9b, - 0x5c, 0x90, 0x58, 0x94, 0x98, 0x0b, 0x35, 0x58, 0xc9, 0x9f, 0x8b, 0xc7, 0x1d, 0x62, 0x53, 0x70, - 0x49, 0x62, 0x49, 0xaa, 0x90, 0x3d, 0x17, 0x1b, 0x44, 0x5e, 0x82, 0x51, 0x81, 0x51, 0x83, 0xdb, - 0x48, 0x51, 0x0f, 0xa7, 0xcd, 0x7a, 0x01, 0x60, 0x85, 0x4e, 0x2c, 0x27, 0xee, 0xc9, 0x33, 0x04, - 0x41, 0xb5, 0x39, 0x19, 0x9f, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, - 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x24, - 0x4e, 0x27, 0x25, 0xb1, 0x81, 0x1d, 0x63, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x1d, 0x1e, 0x57, - 0x0f, 0x10, 0x01, 0x00, 0x00, + 0x5c, 0x90, 0x58, 0x94, 0x98, 0x0b, 0x35, 0x58, 0x69, 0x33, 0x23, 0x17, 0x8f, 0x3b, 0xc4, 0xaa, + 0xe0, 0x92, 0xc4, 0x92, 0x54, 0x21, 0x7b, 0x2e, 0x36, 0x88, 0x02, 0x09, 0x46, 0x05, 0x46, 0x0d, + 0x6e, 0x23, 0x45, 0x3d, 0x9c, 0x56, 0xeb, 0x05, 0x80, 0x15, 0x3a, 0xb1, 0x9c, 0xb8, 0x27, 0xcf, + 0x10, 0x04, 0xd5, 0x26, 0x94, 0xc4, 0x25, 0x5a, 0x96, 0x98, 0x93, 0x99, 0x92, 0x58, 0x92, 0x5f, + 0x14, 0x5f, 0x5c, 0x9e, 0x9a, 0x5a, 0x10, 0x9f, 0x99, 0x97, 0x92, 0x5a, 0x21, 0xc1, 0x04, 0x36, + 0x4f, 0x0f, 0x8f, 0x79, 0x61, 0x30, 0x7d, 0xc1, 0x20, 0x6d, 0x9e, 0x20, 0x5d, 0x41, 0xc2, 0x65, + 0x98, 0x82, 0x4a, 0x8b, 0x18, 0xb9, 0x84, 0xb1, 0x28, 0x16, 0xb2, 0xe7, 0xe2, 0xcb, 0x4b, 0xad, + 0x28, 0x89, 0x2f, 0x4b, 0xcc, 0x81, 0x5a, 0x0a, 0xf2, 0x04, 0x8b, 0x93, 0xe4, 0xa7, 0x7b, 0xf2, + 0xa2, 0x95, 0x89, 0xb9, 0x39, 0x56, 0x4a, 0xa8, 0xf2, 0x4a, 0x41, 0x3c, 0x20, 0x81, 0xb0, 0xc4, + 0x1c, 0x88, 0x01, 0x5e, 0x5c, 0x42, 0x70, 0x05, 0x29, 0xa9, 0x39, 0x48, 0x2e, 0x67, 0x71, 0x92, + 0xfd, 0x74, 0x4f, 0x5e, 0x12, 0xcd, 0x10, 0xb8, 0x1a, 0xa5, 0x20, 0x7e, 0xa8, 0x41, 0x2e, 0xa9, + 0x10, 0xb3, 0x9c, 0x8c, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, + 0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, 0x4a, 0x12, + 0x67, 0xe4, 0x24, 0xb1, 0x81, 0xa3, 0xc5, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x7b, 0x06, 0x14, + 0x74, 0x1a, 0x02, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -111,6 +181,18 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.ValidatorSweepIndex != nil { + { + size, err := m.ValidatorSweepIndex.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } { size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -124,6 +206,39 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *ValidatorSweepIndex) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ValidatorSweepIndex) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ValidatorSweepIndex) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.NextValDelIndex != 0 { + i = encodeVarintGenesis(dAtA, i, uint64(m.NextValDelIndex)) + i-- + dAtA[i] = 0x10 + } + if m.NextValIndex != 0 { + i = encodeVarintGenesis(dAtA, i, uint64(m.NextValIndex)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int { offset -= sovGenesis(v) base := offset @@ -143,6 +258,25 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.Params.Size() n += 1 + l + sovGenesis(uint64(l)) + if m.ValidatorSweepIndex != nil { + l = m.ValidatorSweepIndex.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + return n +} + +func (m *ValidatorSweepIndex) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.NextValIndex != 0 { + n += 1 + sovGenesis(uint64(m.NextValIndex)) + } + if m.NextValDelIndex != 0 { + n += 1 + sovGenesis(uint64(m.NextValDelIndex)) + } return n } @@ -214,6 +348,130 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ValidatorSweepIndex", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.ValidatorSweepIndex == nil { + m.ValidatorSweepIndex = &ValidatorSweepIndex{} + } + if err := m.ValidatorSweepIndex.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ValidatorSweepIndex) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ValidatorSweepIndex: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ValidatorSweepIndex: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field NextValIndex", wireType) + } + m.NextValIndex = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.NextValIndex |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field NextValDelIndex", wireType) + } + m.NextValDelIndex = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.NextValDelIndex |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/client/x/evmstaking/types/genesis.proto b/client/x/evmstaking/types/genesis.proto index 6a7433de..1b094232 100644 --- a/client/x/evmstaking/types/genesis.proto +++ b/client/x/evmstaking/types/genesis.proto @@ -9,4 +9,18 @@ option go_package = "client/x/evmstaking/types"; message GenesisState { Params params = 1 [(gogoproto.nullable) = false]; // TODO: Add withdrawals collections field as ORM if needed + ValidatorSweepIndex validator_sweep_index = 2; +} + +message ValidatorSweepIndex { + uint64 next_val_index = 1 [ + // TODO: use custom Int type, need to resolve issue in auto-generated pb.go + // (cosmos_proto.scalar) = "cosmos.Int", + // (gogoproto.customtype) = "cosmossdk.io/math.Int", + // (gogoproto.nullable) = false, + (gogoproto.moretags) = "yaml:\"next_val_index\"" + ]; + uint64 next_val_del_index = 2 [ + (gogoproto.moretags) = "yaml:\"next_val_del_index\"" + ]; } \ No newline at end of file From cb0ab60aa9278eee0781a3bc3e18e757f5a6553e Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:02:09 -0500 Subject: [PATCH 07/12] feat(x/evmstaking): sweep index get --- client/x/evmstaking/keeper/withdraw.go | 18 +++--------------- client/x/evmstaking/types/keys.go | 9 ++++----- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index f82b4ffc..7c828fe1 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -20,12 +20,7 @@ import ( ) func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withdrawal, error) { - nextValSweepIndex, err := k.GetNextValidatorSweepIndex(ctx) - if err != nil { - return nil, err - } - - nextValDelSweepIndex, err := k.GetNextValidatorDelegationSweepIndex(ctx) + nextValSweepIndex, nextValDelSweepIndex, err := k.GetValidatorSweepIndex(ctx) if err != nil { return nil, err } @@ -167,17 +162,10 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd swept += uint32(len(delegators)) } - // Update the nextValidatorSweepIndex. - if err := k.SetNextValidatorSweepIndex( + // Update the validator sweep index. + if err := k.SetValidatorSweepIndex( ctx, sdk.IntProto{Int: math.NewInt(nextValIndex)}, - ); err != nil { - return nil, err - } - - // Update the nextValidatorDelegationSweepIndex. - if err := k.SetNextValidatorDelegationSweepIndex( - ctx, sdk.IntProto{Int: math.NewInt(nextValDelIndex)}, ); err != nil { return nil, err diff --git a/client/x/evmstaking/types/keys.go b/client/x/evmstaking/types/keys.go index 4a6ed016..8ae47e53 100644 --- a/client/x/evmstaking/types/keys.go +++ b/client/x/evmstaking/types/keys.go @@ -15,9 +15,8 @@ const ( // KVStore keys. var ( - ParamsKey = collections.NewPrefix(0) - WithdrawalQueueKey = collections.NewPrefix(1) - DelegatorMapKey = collections.NewPrefix(2) - NextValidatorSweepIndexKey = collections.NewPrefix(3) - NextValidatorDelegationSweepIndexKey = collections.NewPrefix(4) + ParamsKey = collections.NewPrefix(0) + WithdrawalQueueKey = collections.NewPrefix(1) + DelegatorMapKey = collections.NewPrefix(2) + ValidatorSweepIndexKey = collections.NewPrefix(3) ) From dc9eef64b05617c0e81cabbee302a68da3782a36 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:05:21 -0500 Subject: [PATCH 08/12] fix(x/evmstaking): remove unused param struct --- client/x/evmstaking/keeper/params.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/client/x/evmstaking/keeper/params.go b/client/x/evmstaking/keeper/params.go index 58d34a27..6ccf63bb 100644 --- a/client/x/evmstaking/keeper/params.go +++ b/client/x/evmstaking/keeper/params.go @@ -11,11 +11,6 @@ import ( "github.com/piplabs/story/lib/errors" ) -type ValidatorSweepIndex struct { - nextValIndex sdk.IntProto - nextValDelIndex sdk.IntProto -} - func (k Keeper) MaxWithdrawalPerBlock(ctx context.Context) (uint32, error) { params, err := k.GetParams(ctx) if err != nil { From 1a77e775810a6f5b411ca9493c7bde62499595ee Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:08:14 -0500 Subject: [PATCH 09/12] feat(upgrade): retrieve old sweep value and set new --- client/app/upgrades/v0_10_0/upgrades.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/app/upgrades/v0_10_0/upgrades.go b/client/app/upgrades/v0_10_0/upgrades.go index b8e7b3ad..6a2f94f7 100644 --- a/client/app/upgrades/v0_10_0/upgrades.go +++ b/client/app/upgrades/v0_10_0/upgrades.go @@ -33,9 +33,16 @@ func CreateUpgradeHandler( } clog.Info(ctx, "Setting NextValidatorDelegationSweepIndex parameter...") - if err := keepers.EvmStakingKeeper.SetNextValidatorDelegationSweepIndex( + nextValIndex, err := keepers.EvmStakingKeeper.GetOldValidatorSweepIndex(ctx) + if err != nil { + return vm, errors.Wrap(err, "get old validator sweep index") + } + + nextValDelIndex := sdk.IntProto{Int: math.NewInt(0)} + if err := keepers.EvmStakingKeeper.SetValidatorSweepIndex( ctx, - sdk.IntProto{Int: math.NewInt(0)}, + nextValIndex, + nextValDelIndex, ); err != nil { return vm, errors.Wrap(err, "set evmstaking NextValidatorDelegationSweepIndex") } From eeee43b3c0de4870b88c20430dd2d349bed9c846 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:14:19 -0500 Subject: [PATCH 10/12] fix(x/evmstaking): swept len --- client/x/evmstaking/keeper/withdraw.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index 7c828fe1..9327613d 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -159,7 +159,7 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd nextValDelIndex = 0 // Increase the total swept amount. - swept += uint32(len(delegators)) + swept += uint32(len(nextDelegators)) } // Update the validator sweep index. From 63e78d4fae0c30e35bf00616e3c371b6df2ded78 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 11:33:08 -0500 Subject: [PATCH 11/12] test(x/evmstaking): val sweep index --- client/x/evmstaking/keeper/withdraw_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/x/evmstaking/keeper/withdraw_test.go b/client/x/evmstaking/keeper/withdraw_test.go index 0762a9eb..196ab100 100644 --- a/client/x/evmstaking/keeper/withdraw_test.go +++ b/client/x/evmstaking/keeper/withdraw_test.go @@ -67,9 +67,9 @@ func (s *TestSuite) TestExpectedPartialWithdrawals() { }, }, { - name: "pass: next val sweep index is out of bounds, so it should be reset to 0 which is the index of the first validator", + name: "pass: val sweep index is out of bounds, so it should be reset to 0 which is the index of the first validator", preRun: func(_ sdk.Context) { - require.NoError(keeper.SetNextValidatorSweepIndex(ctx, sdk.IntProto{Int: sdkmath.NewInt(100)})) + require.NoError(keeper.SetValidatorSweepIndex(ctx, sdk.IntProto{Int: sdkmath.NewInt(100)}, sdk.IntProto{Int: sdkmath.NewInt(0)})) distrKeeper.EXPECT().GetValidatorAccumulatedCommission(gomock.Any(), gomock.Any()).Return(dtypes.ValidatorAccumulatedCommission{}, nil) distrKeeper.EXPECT().IncrementValidatorPeriod(gomock.Any(), gomock.Any()).Return(uint64(0), nil) distrKeeper.EXPECT().CalculateDelegationRewards(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(delRewards, nil) From 82009409266e9f5cfedf468405b77c5e7a73e541 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 9 Sep 2024 19:18:52 -0500 Subject: [PATCH 12/12] fix(client): get return and type uint64 --- client/app/upgrades/v0_10_0/upgrades.go | 4 +--- client/x/evmstaking/keeper/params.go | 23 ++++++++++----------- client/x/evmstaking/keeper/withdraw.go | 21 +++++++------------ client/x/evmstaking/keeper/withdraw_test.go | 2 +- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/client/app/upgrades/v0_10_0/upgrades.go b/client/app/upgrades/v0_10_0/upgrades.go index 6a2f94f7..bc950dbf 100644 --- a/client/app/upgrades/v0_10_0/upgrades.go +++ b/client/app/upgrades/v0_10_0/upgrades.go @@ -4,10 +4,8 @@ package v0_10_0 import ( "context" - "cosmossdk.io/math" upgradetypes "cosmossdk.io/x/upgrade/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/piplabs/story/client/app/keepers" @@ -38,7 +36,7 @@ func CreateUpgradeHandler( return vm, errors.Wrap(err, "get old validator sweep index") } - nextValDelIndex := sdk.IntProto{Int: math.NewInt(0)} + nextValDelIndex := uint64(0) if err := keepers.EvmStakingKeeper.SetValidatorSweepIndex( ctx, nextValIndex, diff --git a/client/x/evmstaking/keeper/params.go b/client/x/evmstaking/keeper/params.go index 6ccf63bb..f978c836 100644 --- a/client/x/evmstaking/keeper/params.go +++ b/client/x/evmstaking/keeper/params.go @@ -3,8 +3,6 @@ package keeper import ( "context" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/piplabs/story/client/x/evmstaking/types" @@ -73,11 +71,11 @@ func (k Keeper) GetParams(ctx context.Context) (params types.Params, err error) return params, nil } -func (k Keeper) SetValidatorSweepIndex(ctx context.Context, nextValIndex sdk.IntProto, nextValDelIndex sdk.IntProto) error { +func (k Keeper) SetValidatorSweepIndex(ctx context.Context, nextValIndex uint64, nextValDelIndex uint64) error { store := k.storeService.OpenKVStore(ctx) bz, err := k.cdc.Marshal(&types.ValidatorSweepIndex{ - NextValIndex: nextValIndex.Int.Uint64(), - NextValDelIndex: nextValDelIndex.Int.Uint64(), + NextValIndex: nextValIndex, + NextValDelIndex: nextValDelIndex, }) if err != nil { return errors.Wrap(err, "marshal validator sweep index") @@ -91,7 +89,7 @@ func (k Keeper) SetValidatorSweepIndex(ctx context.Context, nextValIndex sdk.Int return nil } -func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.IntProto, nextValDelIndex sdk.IntProto, err error) { +func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex uint64, nextValDelIndex uint64, err error) { store := k.storeService.OpenKVStore(ctx) bz, err := store.Get(types.ValidatorSweepIndexKey) if err != nil { @@ -99,7 +97,7 @@ func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.In } if bz == nil { - return sdk.IntProto{Int: math.NewInt(0)}, sdk.IntProto{Int: math.NewInt(0)}, nil + return nextValIndex, nextValDelIndex, nil } var sweepIndex types.ValidatorSweepIndex @@ -108,10 +106,10 @@ func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.In return nextValIndex, nextValDelIndex, errors.Wrap(err, "unmarshal validator sweep index") } - return nextValIndex, nextValDelIndex, nil + return sweepIndex.NextValIndex, sweepIndex.NextValDelIndex, nil } -func (k Keeper) GetOldValidatorSweepIndex(ctx context.Context) (nextValIndex sdk.IntProto, err error) { +func (k Keeper) GetOldValidatorSweepIndex(ctx context.Context) (nextValIndex uint64, err error) { store := k.storeService.OpenKVStore(ctx) bz, err := store.Get(types.ValidatorSweepIndexKey) if err != nil { @@ -119,13 +117,14 @@ func (k Keeper) GetOldValidatorSweepIndex(ctx context.Context) (nextValIndex sdk } if bz == nil { - return sdk.IntProto{Int: math.NewInt(0)}, nil + return nextValIndex, nil } - err = k.cdc.Unmarshal(bz, &nextValIndex) + var nextValIndexProto sdk.IntProto + err = k.cdc.Unmarshal(bz, &nextValIndexProto) if err != nil { return nextValIndex, errors.Wrap(err, "unmarshal next validator sweep index") } - return nextValIndex, nil + return nextValIndexProto.Int.Uint64(), nil } diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index 9327613d..22f2cf81 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -3,8 +3,6 @@ package keeper import ( "context" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" dtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" skeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" @@ -20,21 +18,18 @@ import ( ) func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withdrawal, error) { - nextValSweepIndex, nextValDelSweepIndex, err := k.GetValidatorSweepIndex(ctx) + nextValIndex, nextValDelIndex, err := k.GetValidatorSweepIndex(ctx) if err != nil { return nil, err } - nextValIndex := nextValSweepIndex.Int.Int64() - nextValDelIndex := nextValDelSweepIndex.Int.Int64() - // Get all validators first, and then do a circular sweep validatorSet, err := (k.stakingKeeper.(*skeeper.Keeper)).GetAllValidators(ctx) if err != nil { return nil, errors.Wrap(err, "get all validators") } - if nextValIndex >= int64(len(validatorSet)) { + if nextValIndex >= uint64(len(validatorSet)) { // TODO: TBD log.Warn( ctx, "NextValidatorIndex exceeds the validator set size", @@ -68,7 +63,7 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd for range validatorSet { if validatorSet[nextValIndex].IsJailed() { // nextValIndex should be updated, even if the validator is jailed, to progress to the sweep. - nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + nextValIndex = (nextValIndex + 1) % uint64(len(validatorSet)) nextValDelIndex = 0 continue @@ -94,8 +89,8 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd return nil, errors.Wrap(err, "get validator delegations") } - if nextValDelIndex >= int64(len(delegators)) { - nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + if nextValDelIndex >= uint64(len(delegators)) { + nextValIndex = (nextValIndex + 1) % uint64(len(validatorSet)) nextValDelIndex = 0 continue @@ -155,7 +150,7 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd // Here, we have looped through all delegators of the validator (since we did not prematurely stop in the loop above). // Thus, we signal to progress to the next validator by resetting the nextValDelIndex and circularly incrementing the nextValIndex - nextValIndex = (nextValIndex + 1) % int64(len(validatorSet)) + nextValIndex = (nextValIndex + 1) % uint64(len(validatorSet)) nextValDelIndex = 0 // Increase the total swept amount. @@ -165,8 +160,8 @@ func (k Keeper) ExpectedPartialWithdrawals(ctx context.Context) ([]estypes.Withd // Update the validator sweep index. if err := k.SetValidatorSweepIndex( ctx, - sdk.IntProto{Int: math.NewInt(nextValIndex)}, - sdk.IntProto{Int: math.NewInt(nextValDelIndex)}, + nextValIndex, + nextValDelIndex, ); err != nil { return nil, err } diff --git a/client/x/evmstaking/keeper/withdraw_test.go b/client/x/evmstaking/keeper/withdraw_test.go index 196ab100..3e669f8e 100644 --- a/client/x/evmstaking/keeper/withdraw_test.go +++ b/client/x/evmstaking/keeper/withdraw_test.go @@ -69,7 +69,7 @@ func (s *TestSuite) TestExpectedPartialWithdrawals() { { name: "pass: val sweep index is out of bounds, so it should be reset to 0 which is the index of the first validator", preRun: func(_ sdk.Context) { - require.NoError(keeper.SetValidatorSweepIndex(ctx, sdk.IntProto{Int: sdkmath.NewInt(100)}, sdk.IntProto{Int: sdkmath.NewInt(0)})) + require.NoError(keeper.SetValidatorSweepIndex(ctx, uint64(100), uint64(0))) distrKeeper.EXPECT().GetValidatorAccumulatedCommission(gomock.Any(), gomock.Any()).Return(dtypes.ValidatorAccumulatedCommission{}, nil) distrKeeper.EXPECT().IncrementValidatorPeriod(gomock.Any(), gomock.Any()).Return(uint64(0), nil) distrKeeper.EXPECT().CalculateDelegationRewards(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(delRewards, nil)