Skip to content

Commit

Permalink
feat(upgrade): add sweep index to init and export genesis (#227)
Browse files Browse the repository at this point in the history
Added `ValidatorSweepIndex` to `InitGenesis` and `ExportGenesis` with
small refactoring.

issue: none
  • Loading branch information
0xHansLee authored Oct 18, 2024
1 parent 69a77ac commit f1b194c
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 158 deletions.
5 changes: 1 addition & 4 deletions client/app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ 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{
v0_10_0.Upgrade,
}
Upgrades = []upgrades.Upgrade{}
// Forks are for hard forks that breaks backward compatibility.
Forks = []upgrades.Fork{}
)
Expand Down
16 changes: 0 additions & 16 deletions client/app/upgrades/v0_10_0/constants.go

This file was deleted.

64 changes: 0 additions & 64 deletions client/app/upgrades/v0_10_0/upgrades.go

This file was deleted.

23 changes: 15 additions & 8 deletions client/x/evmstaking/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ func (k Keeper) InitGenesis(ctx context.Context, gs *types.GenesisState) error {
if err := k.ValidateGenesis(gs); err != nil {
return err
}

if err := k.SetParams(ctx, gs.Params); err != nil {
return err
}

if err := k.SetValidatorSweepIndex(ctx, gs.ValidatorSweepIndex); err != nil {
return err
}

if err := k.WithdrawalQueue.Initialize(ctx); err != nil {
log.Error(ctx, "InitGenesis.evmstaking not initialized", err)
return err
Expand Down Expand Up @@ -71,20 +76,22 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
panic(err)
}

validatorSweepIndex, err := k.GetValidatorSweepIndex(ctx)
if err != nil {
panic(err)
}

return &types.GenesisState{
Params: params,
Params: params,
ValidatorSweepIndex: validatorSweepIndex,
}
}

//nolint:revive // TODO: validate genesis
func (k Keeper) ValidateGenesis(gs *types.GenesisState) error {
if err := types.ValidateMaxWithdrawalPerBlock(gs.Params.MaxWithdrawalPerBlock); err != nil {
return err
}

if err := types.ValidateMaxSweepPerBlock(gs.Params.MaxSweepPerBlock, gs.Params.MaxWithdrawalPerBlock); err != nil {
return err
if err := gs.Params.Validate(); err != nil {
return errors.Wrap(err, "validate genesis state params")
}

return types.ValidateMinPartialWithdrawalAmount(gs.Params.MinPartialWithdrawalAmount)
return nil
}
16 changes: 8 additions & 8 deletions client/x/evmstaking/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,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 uint64, nextValDelIndex uint64) error {
func (k Keeper) SetValidatorSweepIndex(ctx context.Context, validatorSweepIndex types.ValidatorSweepIndex) error {
store := k.storeService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(&types.ValidatorSweepIndex{
NextValIndex: nextValIndex,
NextValDelIndex: nextValDelIndex,
NextValIndex: validatorSweepIndex.NextValIndex,
NextValDelIndex: validatorSweepIndex.NextValDelIndex,
})
if err != nil {
return errors.Wrap(err, "marshal validator sweep index")
Expand All @@ -89,24 +89,24 @@ func (k Keeper) SetValidatorSweepIndex(ctx context.Context, nextValIndex uint64,
return nil
}

func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (nextValIndex uint64, nextValDelIndex uint64, err error) {
func (k Keeper) GetValidatorSweepIndex(ctx context.Context) (types.ValidatorSweepIndex, error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.ValidatorSweepIndexKey)
if err != nil {
return nextValIndex, nextValDelIndex, errors.Wrap(err, "get validator sweep index")
return types.ValidatorSweepIndex{}, errors.Wrap(err, "get validator sweep index")
}

if bz == nil {
return nextValIndex, nextValDelIndex, nil
return types.ValidatorSweepIndex{}, nil
}

var sweepIndex types.ValidatorSweepIndex
err = k.cdc.Unmarshal(bz, &sweepIndex)
if err != nil {
return nextValIndex, nextValDelIndex, errors.Wrap(err, "unmarshal validator sweep index")
return types.ValidatorSweepIndex{}, errors.Wrap(err, "unmarshal validator sweep index")
}

return sweepIndex.NextValIndex, sweepIndex.NextValDelIndex, nil
return sweepIndex, nil
}

func (k Keeper) GetOldValidatorSweepIndex(ctx context.Context) (nextValIndex uint64, err error) {
Expand Down
20 changes: 10 additions & 10 deletions client/x/evmstaking/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,31 @@ func (s *TestSuite) TestMinPartialWithdrawalAmount() {
func (s *TestSuite) TestSetValidatorSweepIndex() {
require := s.Require()
ctx, keeper := s.Ctx, s.EVMStakingKeeper
existingNextValIndex, existingNextValDelIndex, err := keeper.GetValidatorSweepIndex(ctx)
existingSweepIndex, err := keeper.GetValidatorSweepIndex(ctx)
require.NoError(err)

// set new sweep index
newNextValIndex := uint64(10)
newNextValDelIndex := uint64(100)
// make sure new value is different from existing value
require.NotEqual(existingNextValIndex, newNextValIndex)
require.NotEqual(existingNextValDelIndex, newNextValDelIndex)
require.NoError(keeper.SetValidatorSweepIndex(ctx, newNextValIndex, newNextValDelIndex))
require.NotEqual(existingSweepIndex.NextValIndex, newNextValIndex)
require.NotEqual(existingSweepIndex.NextValDelIndex, newNextValDelIndex)
require.NoError(keeper.SetValidatorSweepIndex(ctx, types.NewValidatorSweepIndex(newNextValIndex, newNextValDelIndex)))

// check new sweep index is set correctly
nextValIndex, nextValDelIndex, err := keeper.GetValidatorSweepIndex(ctx)
sweepIndex, err := keeper.GetValidatorSweepIndex(ctx)
require.NoError(err)
require.Equal(newNextValIndex, nextValIndex)
require.Equal(newNextValDelIndex, nextValDelIndex)
require.Equal(newNextValIndex, sweepIndex.NextValIndex)
require.Equal(newNextValDelIndex, sweepIndex.NextValDelIndex)
}

func (s *TestSuite) TestGetValidatorSweepIndex() {
require := s.Require()
ctx, keeper := s.Ctx, s.EVMStakingKeeper
nextValIndex, nextValDelIndex, err := keeper.GetValidatorSweepIndex(ctx)
sweepIndex, err := keeper.GetValidatorSweepIndex(ctx)
require.NoError(err)
require.Equal(uint64(0), nextValIndex)
require.Equal(uint64(0), nextValDelIndex)
require.Equal(uint64(0), sweepIndex.NextValIndex)
require.Equal(uint64(0), sweepIndex.NextValDelIndex)
}

func (s *TestSuite) TestGetOldValidatorSweepIndex() {
Expand Down
10 changes: 4 additions & 6 deletions client/x/evmstaking/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ func (k Keeper) ProcessRewardWithdrawals(ctx context.Context) error {
}

func (k Keeper) ExpectedRewardWithdrawals(ctx context.Context) ([]RewardWithdrawal, error) {
nextValIndex, nextValDelIndex, err := k.GetValidatorSweepIndex(ctx)
validatorSweepIndex, err := k.GetValidatorSweepIndex(ctx)
if err != nil {
return nil, err
}

nextValIndex, nextValDelIndex := validatorSweepIndex.NextValIndex, validatorSweepIndex.NextValDelIndex

// Get all validators first, and then do a circular sweep
validatorSet, err := (k.stakingKeeper.(*skeeper.Keeper)).GetAllValidators(ctx)
if err != nil {
Expand Down Expand Up @@ -231,11 +233,7 @@ func (k Keeper) ExpectedRewardWithdrawals(ctx context.Context) ([]RewardWithdraw
}

// Update the validator sweep index.
if err := k.SetValidatorSweepIndex(
ctx,
nextValIndex,
nextValDelIndex,
); err != nil {
if err := k.SetValidatorSweepIndex(ctx, types.NewValidatorSweepIndex(nextValIndex, nextValDelIndex)); err != nil {
return nil, err
}

Expand Down
23 changes: 17 additions & 6 deletions client/x/evmstaking/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package types
func NewGenesisState(params Params) *GenesisState {
return &GenesisState{
Params: params,
ValidatorSweepIndex: &ValidatorSweepIndex{
ValidatorSweepIndex: ValidatorSweepIndex{
NextValIndex: 0,
NextValDelIndex: 0,
},
Expand All @@ -13,10 +13,21 @@ func NewGenesisState(params Params) *GenesisState {
// DefaultGenesisState returns the default genesis state.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
ValidatorSweepIndex: &ValidatorSweepIndex{
NextValIndex: 0,
NextValDelIndex: 0,
},
Params: DefaultParams(),
ValidatorSweepIndex: DefaultValidatorSweepIndex(),
}
}

func NewValidatorSweepIndex(nextValIndex, nextValDelIndex uint64) ValidatorSweepIndex {
return ValidatorSweepIndex{
NextValIndex: nextValIndex,
NextValDelIndex: nextValDelIndex,
}
}

func DefaultValidatorSweepIndex() ValidatorSweepIndex {
return ValidatorSweepIndex{
NextValIndex: 0,
NextValDelIndex: 0,
}
}
Loading

0 comments on commit f1b194c

Please sign in to comment.