Skip to content

Commit 4a97c50

Browse files
committed
Rmove commission check on delegate/redelegate msgs
The commission check on delegate/redelegate messages is not needed since it's expected that there are no validator's with commission-rate below minCommission.
1 parent 2cceec8 commit 4a97c50

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

app/ante/ante.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
3535
ante.NewValidateMemoDecorator(options.AccountKeeper),
3636
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
3737
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
38-
NewValidateMinCommissionDecorator(options.StakingKeeper), // Custom decorator to ensure the minimum commission rate of validators
39-
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
38+
NewValidateMinCommissionDecorator(options.StakingKeeper, options.BankKeeper), // Custom decorator to ensure the minimum commission rate of validators
39+
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
4040
ante.NewValidateSigCountDecorator(options.AccountKeeper),
4141
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
4242
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),

app/ante/commission.go

+47-28
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ var maxVotingPower = sdk.NewDecWithPrec(10, 2) // 10%
1616
// ValidateMinCommissionDecorator validates that the validator commission is always
1717
// greater than or equal to the min commission rate
1818
type ValidateMinCommissionDecorator struct {
19-
sk stakingkeeper.Keeper
19+
sk stakingkeeper.Keeper
20+
bankKeeper stakingtypes.BankKeeper
2021
}
2122

2223
// ValidateMinCommissionDecorator creates a new ValidateMinCommissionDecorator
23-
func NewValidateMinCommissionDecorator(sk stakingkeeper.Keeper) ValidateMinCommissionDecorator {
24+
func NewValidateMinCommissionDecorator(sk stakingkeeper.Keeper, bk stakingtypes.BankKeeper) ValidateMinCommissionDecorator {
2425
return ValidateMinCommissionDecorator{
25-
sk: sk,
26+
sk: sk,
27+
bankKeeper: bk,
2628
}
2729
}
2830

@@ -51,9 +53,6 @@ func (vcd ValidateMinCommissionDecorator) getValidator(ctx sdk.Context, bech32Va
5153
return val, nil
5254
}
5355

54-
// validateMsg checks if the tx contains one of the following msg types & errors if the validator's commission rate is below the min threshold
55-
// For validators: create validator or edit validator
56-
// For delegators: delegate to validator or withdraw delegator rewards
5756
func (vcd ValidateMinCommissionDecorator) validateMsg(ctx sdk.Context, msg sdk.Msg) error {
5857
switch msg := msg.(type) {
5958
case *stakingtypes.MsgCreateValidator:
@@ -73,18 +72,8 @@ func (vcd ValidateMinCommissionDecorator) validateMsg(ctx sdk.Context, msg sdk.M
7372
if err != nil {
7473
return err
7574
}
76-
if val.GetCommission().LT(minCommission) {
77-
return sdkerrors.Wrapf(
78-
sdkerrors.ErrInvalidRequest,
79-
"cannot delegate to validator with commission lower than minimum of %s", minCommission)
80-
}
8175

82-
validatorTokens := sdk.NewDecFromInt(val.GetTokens())
83-
totalBondedTokens := sdk.NewDecFromInt(vcd.sk.TotalBondedTokens(ctx))
84-
votingPower := validatorTokens.Quo(totalBondedTokens)
85-
if err != nil {
86-
return err
87-
}
76+
votingPower := vcd.calculateProvisionalVotingPower(ctx, val, sdk.NewDecFromInt(msg.Amount.Amount))
8877
if votingPower.GTE(maxVotingPower) {
8978
return sdkerrors.Wrapf(
9079
sdkerrors.ErrInvalidRequest,
@@ -95,18 +84,8 @@ func (vcd ValidateMinCommissionDecorator) validateMsg(ctx sdk.Context, msg sdk.M
9584
if err != nil {
9685
return err
9786
}
98-
if val.GetCommission().LT(minCommission) {
99-
return sdkerrors.Wrapf(
100-
sdkerrors.ErrInvalidRequest,
101-
"cannot redelegate to validator with commission lower than minimum of %s", minCommission)
102-
}
10387

104-
validatorTokens := sdk.NewDecFromInt(val.GetTokens())
105-
totalBondedTokens := sdk.NewDecFromInt(vcd.sk.TotalBondedTokens(ctx))
106-
votingPower := validatorTokens.Quo(totalBondedTokens).Mul(sdk.NewDec(100))
107-
if err != nil {
108-
return err
109-
}
88+
votingPower := vcd.calculateProvisionalVotingPower(ctx, val, sdk.NewDecFromInt(msg.Amount.Amount))
11089
if votingPower.GTE(maxVotingPower) {
11190
return sdkerrors.Wrapf(
11291
sdkerrors.ErrInvalidRequest,
@@ -115,3 +94,43 @@ func (vcd ValidateMinCommissionDecorator) validateMsg(ctx sdk.Context, msg sdk.M
11594
}
11695
return nil
11796
}
97+
98+
func (vcd ValidateMinCommissionDecorator) calculateProvisionalVotingPower(ctx sdk.Context, validator stakingtypes.ValidatorI, delegateAmount sdk.Dec) sdk.Dec {
99+
// TODO: watch for divide by zero?
100+
validatorTokens := sdk.NewDecFromInt(validator.GetTokens())
101+
provisionalValidatorTokens := validatorTokens.Add(delegateAmount)
102+
totalBondedTokens := sdk.NewDecFromInt(vcd.sk.TotalBondedTokens(ctx))
103+
104+
//return validatorTokens.Quo(totalBondedTokens)
105+
106+
provisionalTotalBondedTokens := sdk.ZeroDec()
107+
if validator.IsBonded() {
108+
provisionalTotalBondedTokens = totalBondedTokens.Add(delegateAmount)
109+
} else {
110+
bondedValidators := vcd.sk.GetBondedValidatorsByPower(ctx)
111+
weakestValidatorTokens := sdk.Dec(bondedValidators[len(bondedValidators)-1].Tokens)
112+
113+
if weakestValidatorTokens.LT(provisionalValidatorTokens) {
114+
//validator will still not be bonded so will have no voting power
115+
return sdk.ZeroDec()
116+
}
117+
118+
// validator will become bonded
119+
provisionalTotalBondedTokens = totalBondedTokens.Add(delegateAmount).Sub(weakestValidatorTokens)
120+
}
121+
122+
return provisionalValidatorTokens.Quo(provisionalTotalBondedTokens)
123+
}
124+
125+
func (vcd ValidateMinCommissionDecorator) getDelegatedTokens(ctx sdk.Context) sdk.Int {
126+
127+
bondDenom := vcd.sk.BondDenom(ctx)
128+
bondedPool := vcd.sk.GetBondedPool(ctx)
129+
notBondedPool := vcd.sk.GetNotBondedPool(ctx)
130+
131+
bondedTokens := vcd.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
132+
notBondedTokens := vcd.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
133+
134+
return bondedTokens.Add(notBondedTokens)
135+
136+
}

app/ante/handler_options.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"github.com/cosmos/cosmos-sdk/x/auth/ante"
77
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
88
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
9+
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
910
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
1011
)
1112

1213
// HandlerOptions defines the list of module keepers required to run the Sifnode
1314
// AnteHandler decorators.
1415
type HandlerOptions struct {
1516
AccountKeeper ante.AccountKeeper
16-
BankKeeper authtypes.BankKeeper
17+
BankKeeper bankkeeper.Keeper
1718
FeegrantKeeper ante.FeegrantKeeper
1819
StakingKeeper stakingkeeper.Keeper
1920
SignModeHandler authsigning.SignModeHandler

scripts/init.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ echo "race draft rival universe maid cheese steel logic crowd fork comic easy tr
1212
echo "Generating deterministic account - akasha"
1313
echo "hand inmate canvas head lunar naive increase recycle dog ecology inhale december wide bubble hockey dice worth gravity ketchup feed balance parent secret orchard" | sifnoded keys add akasha --recover --keyring-backend=test
1414

15+
echo "Generating deterministic account - alice"
16+
echo "crunch enable gauge equip sadness venture volcano capable boil pole lounge because service level giggle decide south deposit bike antique consider olympic girl butter" | sifnoded keys add alice --recover --keyring-backend=test
1517

1618
sifnoded keys add mkey --multisig sif,akasha --multisig-threshold 2 --keyring-backend=test
1719

1820
sifnoded add-genesis-account $(sifnoded keys show sif -a --keyring-backend=test) 500000000000000000000000000000000rowan,500000000000000000000000catk,500000000000000000000000cbtk,500000000000000000000000000000000ceth,990000000000000000000000000stake,500000000000000000000000cdash,500000000000000000000000clink,5000000000000cusdt,90000000000000000000ibc/96D7172B711F7F925DFC7579C6CCC3C80B762187215ABD082CDE99F81153DC80 --keyring-backend=test
1921
sifnoded add-genesis-account $(sifnoded keys show akasha -a --keyring-backend=test) 500000000000000000000000rowan,500000000000000000000000catk,500000000000000000000000cbtk,500000000000000000000000ceth,990000000000000000000000000stake,500000000000000000000000cdash,500000000000000000000000clink --keyring-backend=test
22+
sifnoded add-genesis-account $(sifnoded keys show alice -a --keyring-backend=test) 500000000000000000000000rowan,500000000000000000000000catk,500000000000000000000000cbtk,500000000000000000000000ceth,990000000000000000000000000stake,500000000000000000000000cdash,500000000000000000000000clink --keyring-backend=test
2023

2124
sifnoded add-genesis-clp-admin $(sifnoded keys show sif -a --keyring-backend=test) --keyring-backend=test
2225
sifnoded add-genesis-clp-admin $(sifnoded keys show akasha -a --keyring-backend=test) --keyring-backend=test
@@ -27,7 +30,7 @@ sifnoded add-genesis-validators $(sifnoded keys show sif -a --bech val --keyring
2730
sifnoded set-genesis-whitelister-admin sif --keyring-backend=test
2831
sifnoded set-gen-denom-whitelist scripts/denoms.json
2932

30-
sifnoded gentx sif 1000000000000000000000000stake --chain-id=localnet --keyring-backend=test
33+
sifnoded gentx sif 1000000000000000000000000stake --moniker sif_val --chain-id=localnet --keyring-backend=test
3134

3235
echo "Collecting genesis txs..."
3336
sifnoded collect-gentxs

0 commit comments

Comments
 (0)