Skip to content
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

refactor: Move validator voting restrictions from Msg handler to Keeper #104

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,29 @@ func (k Keeper) Delegate(
panic(err)
}

currHeight := ctx.BlockHeight()
// If Delegations are allowed again, limit validator power to 20%
if currHeight >= DelegatePowerRevertHeight {
// Get the last Total Power of the validator set
lastPower := k.GetLastTotalPower(ctx)

// Get the power of the current validator power
validatorLastPower := sdk.TokensToConsensusPower(validator.Tokens, k.PowerReduction(ctx))

// Get the new power of the validator if delegated the bond amount
validatorNewPower := int64(validatorLastPower) + sdk.TokensToConsensusPower(bondAmt, k.PowerReduction(ctx))

// Compute what the Total Consensus Power would be if this Delegation goes through
newTotalPower := lastPower.Int64() + sdk.TokensToConsensusPower(bondAmt, k.PowerReduction(ctx))

// Compute what the new Validator voting power would be in relation to the new total power
validatorIncreasedDelegationPercent := float32(validatorNewPower) / float32(newTotalPower)

// If Delegations are allowed, and the Delegation would have increased the Validator to over 20% of the staking power, do not allow the Delegation to proceed
if validatorIncreasedDelegationPercent > 0.2 {
panic("validator power is over the allowed limit")
}
}
// if subtractAccount is true then we are
// performing a delegation and not a redelegation, thus the source tokens are
// all non bonded
Expand Down
23 changes: 0 additions & 23 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,29 +239,6 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
)
}

// If Delegations are allowed again, limit validator power to 20%
if currHeight >= DelegatePowerRevertHeight {
// Get the Total Consensus Power of all Validators
lastPower := k.Keeper.GetLastTotalPower(ctx)

// Get the selected Validator's voting power
validatorLastPower := k.Keeper.GetLastValidatorPower(ctx, valAddr)

// Compute what the Validator's new power would be if this Delegation goes through
validatorNewPower := int64(validatorLastPower) + sdk.TokensToConsensusPower(msg.Amount.Amount, k.Keeper.PowerReduction(ctx))

// Compute what the Total Consensus Power would be if this Delegation goes through
newTotalPower := lastPower.Int64() + sdk.TokensToConsensusPower(msg.Amount.Amount, k.Keeper.PowerReduction(ctx))

// Compute what the new Validator voting power would be in relation to the new total power
validatorIncreasedDelegationPercent := float32(validatorNewPower) / float32(newTotalPower)

// If Delegations are allowed, and the Delegation would have increased the Validator to over 20% of the staking power, do not allow the Delegation to proceed
if validatorIncreasedDelegationPercent > 0.2 {
return nil, sdkerrors.Wrapf(types.ErrMsgNotSupported, "message type %T is over the allowed limit at height %d", msg, currHeight)
}
}

// NOTE: source funds are always unbonded
newShares, err := k.Keeper.Delegate(ctx, delegatorAddress, msg.Amount.Amount, types.Unbonded, validator, true)
if err != nil {
Expand Down