Skip to content

Commit

Permalink
feat(x/staking): reduce validator number (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Nov 3, 2023
1 parent 39d1497 commit f0dc51f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions x/staking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ import (
)

func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
if ctx.BlockHeight()%2000 == 0 && len(ctx.VoteInfos()) > 5 {
params := k.GetParams(ctx)
params.MaxValidators = params.MaxValidators - 1
if params.MaxValidators >= 5 {
k.SetParams(ctx, params)
}
}

// staking EndBlocker
valUpdates := staking.EndBlocker(ctx, k.Keeper)

Expand Down
41 changes: 41 additions & 0 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/functionx/fx-core/v6/x/staking/types"
Expand Down Expand Up @@ -210,3 +211,43 @@ func (k Keeper) iteratorEditValidator(ctx sdk.Context, validator stakingtypes.Va
}
return nil
}

type msgServer struct {
stakingtypes.MsgServer
}

func NewMsgServerImpl(keeper Keeper) stakingtypes.MsgServer {
return &msgServer{MsgServer: stakingkeeper.NewMsgServerImpl(keeper.Keeper)}
}

func (k msgServer) CreateValidator(goCtx context.Context, msg *stakingtypes.MsgCreateValidator) (*stakingtypes.MsgCreateValidatorResponse, error) {
if sdk.UnwrapSDKContext(goCtx).BlockHeight() > 1e7 {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "unsupported method")
}
return k.MsgServer.CreateValidator(goCtx, msg)
}

func (k msgServer) Delegate(goCtx context.Context, msg *stakingtypes.MsgDelegate) (*stakingtypes.MsgDelegateResponse, error) {
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}

valAddress, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}

if !delegatorAddress.Equals(valAddress) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "only support self delegation")
}

return k.MsgServer.Delegate(goCtx, msg)
}

func (k msgServer) BeginRedelegate(goCtx context.Context, msg *stakingtypes.MsgBeginRedelegate) (*stakingtypes.MsgBeginRedelegateResponse, error) {
if sdk.UnwrapSDKContext(goCtx).BlockHeight() > 1e7 {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "unsupported method")
}
return k.MsgServer.BeginRedelegate(goCtx, msg)
}
4 changes: 3 additions & 1 deletion x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -77,7 +78,8 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak stakingtypes.Account

func (am AppModule) RegisterServices(cfg module.Configurator) {
fxstakingtypes.RegisterMsgServer(cfg.MsgServer(), am.Keeper)
am.AppModule.RegisterServices(cfg)
stakingtypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.Keeper))
stakingtypes.RegisterQueryServer(cfg.QueryServer(), stakingkeeper.Querier{Keeper: am.Keeper.Keeper})
}

// EndBlock returns the end blocker for the staking module. It returns no validator
Expand Down

0 comments on commit f0dc51f

Please sign in to comment.