orbitorg / core

GO implementation of the Terra Classic Protocol
Other
0 stars 0 forks source link

[cosmos-sdk](x/staking/delegation) limit voting power logic #19

Open kien6034 opened 3 weeks ago

kien6034 commented 3 weeks ago

x/staking/keeper/delegation.go/

    if ctx.ChainID() == ColumbusChainID {
        // 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 := 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)
        validatorIncreasedDelegationPercent := sdk.NewDec(validatorNewPower).QuoInt64(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.GT(sdk.NewDecWithPrec(20, 2)) {
            panic("validator power is over the allowed limit")
        }
    }

Solution

We can introduce the AfterDelegationModifed hooks, which check the validator power < 20%

https://github.com/classic-terra/cosmos-sdk/blob/v0.47.10-terra.1/x/staking/keeper/delegation.go#L731-L733

Commit: https://github.com/orbitorg/core/commit/372f6f808b344bc2b3ddac96b3edfaae7f672472