From 6729379eb342a3cb2733fe163f91af1ec6f4e2f3 Mon Sep 17 00:00:00 2001 From: xujiacheng Date: Fri, 24 Aug 2018 11:26:58 +0800 Subject: [PATCH 1/3] fix slash amount issue --- x/stake/keeper/slash.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x/stake/keeper/slash.go b/x/stake/keeper/slash.go index fb9297e9cae5..af3184b6a341 100644 --- a/x/stake/keeper/slash.go +++ b/x/stake/keeper/slash.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/tendermint/tendermint/crypto" + "math" ) // Slash a validator for an infraction committed at a known height @@ -33,6 +34,16 @@ func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight in // ref https://github.com/cosmos/cosmos-sdk/issues/1471 validator, found := k.GetValidatorByPubKey(ctx, pubkey) + + //////////////////// iris/cosmos-sdk begin /////////////////////////// + precisionNumber := math.Pow10(int(validator.TokenPrecision)) + if precisionNumber > math.MaxInt64 { + panic(fmt.Errorf("precision is too high, int64 is overflow")) + } + tokenPrecision := sdk.NewInt(int64(precisionNumber)) + slashAmount = slashAmount.Mul(sdk.NewRatFromInt(tokenPrecision)) + //////////////////// iris/cosmos-sdk end /////////////////////////// + if !found { // If not found, the validator must have been overslashed and removed - so we don't need to do anything // NOTE: Correctness dependent on invariant that unbonding delegations / redelegations must also have been completely From 7b7c6bf397e37a94beca2e56c92fb697812d0193 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Wed, 29 Aug 2018 13:31:54 +0800 Subject: [PATCH 2/3] IRISHUB-227: add affected voting power checking --- x/stake/keeper/delegation.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 0afdad199abd..1da034a48fd7 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/stake/types" + "math" ) // load a delegation @@ -203,7 +204,16 @@ func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { // Perform a delegation, set/update everything necessary within the store. func (k Keeper) Delegate(ctx sdk.Context, delegatorAddr sdk.AccAddress, bondAmt sdk.Coin, validator types.Validator, subtractAccount bool) (newShares sdk.Rat, err sdk.Error) { - + precisionNumber := math.Pow10(int(validator.TokenPrecision)) + if precisionNumber > math.MaxInt64 { + panic("precision is too high, int64 is overflow") + } + tokenPrecision := int64(precisionNumber) + equivalentPower := bondAmt.Amount.Div(sdk.NewInt(tokenPrecision)) + if equivalentPower.Equal(sdk.NewInt(0)) { + err = sdk.ErrInsufficientCoins("affected voting power is zero, this delegation doesn't make sense") + return + } // Get or create the delegator delegation delegation, found := k.GetDelegation(ctx, delegatorAddr, validator.Owner) if !found { From 7c7bb96592dbbd5c056880ea02d6a4db9afb45a5 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Wed, 29 Aug 2018 14:55:13 +0800 Subject: [PATCH 3/3] IRISHUB-227: move voting power check to validator creation --- x/stake/handler.go | 11 +++++++++++ x/stake/keeper/delegation.go | 12 +----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/x/stake/handler.go b/x/stake/handler.go index a4284b1e3ed8..ee7be4911337 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake/tags" "github.com/cosmos/cosmos-sdk/x/stake/types" abci "github.com/tendermint/tendermint/abci/types" + "math" ) func NewHandler(k keeper.Keeper) sdk.Handler { @@ -76,6 +77,16 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k return ErrBadDenom(k.Codespace()).Result() } + precisionNumber := math.Pow10(int(k.GetParams(ctx).DenomPrecision)) + if precisionNumber > math.MaxInt64 { + panic("precision is too high, int64 is overflow") + } + tokenPrecision := int64(precisionNumber) + equivalentPower := msg.Delegation.Amount.Div(sdk.NewInt(tokenPrecision)) + if equivalentPower.Equal(sdk.NewInt(0)) { + return sdk.ErrInsufficientCoins("delegate token amount for new created validator is too small, voting power is zero, this validator creation doesn't make sense").Result() + } + validator := NewValidator(msg.ValidatorAddr, msg.PubKey, msg.Description) validator.TokenPrecision = k.GetParams(ctx).DenomPrecision k.SetValidator(ctx, validator) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 1da034a48fd7..0afdad199abd 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -5,7 +5,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/stake/types" - "math" ) // load a delegation @@ -204,16 +203,7 @@ func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { // Perform a delegation, set/update everything necessary within the store. func (k Keeper) Delegate(ctx sdk.Context, delegatorAddr sdk.AccAddress, bondAmt sdk.Coin, validator types.Validator, subtractAccount bool) (newShares sdk.Rat, err sdk.Error) { - precisionNumber := math.Pow10(int(validator.TokenPrecision)) - if precisionNumber > math.MaxInt64 { - panic("precision is too high, int64 is overflow") - } - tokenPrecision := int64(precisionNumber) - equivalentPower := bondAmt.Amount.Div(sdk.NewInt(tokenPrecision)) - if equivalentPower.Equal(sdk.NewInt(0)) { - err = sdk.ErrInsufficientCoins("affected voting power is zero, this delegation doesn't make sense") - return - } + // Get or create the delegator delegation delegation, found := k.GetDelegation(ctx, delegatorAddr, validator.Owner) if !found {