-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ante)!: Ante handler to add a maximum commission rate of 25% for…
… validators. (#1615) * feat(ante)!: Ante handler to add a maximum commission rate of 25% for validators. * test: fix broken tests --------- Co-authored-by: Matthias <[email protected]>
- Loading branch information
1 parent
c3de785
commit 6c24125
Showing
11 changed files
with
226 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ante | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func MAX_COMMISSION() sdk.Dec { return sdk.MustNewDecFromStr("0.25") } | ||
|
||
var _ sdk.AnteDecorator = (*AnteDecoratorStakingCommission)(nil) | ||
|
||
// AnteDecoratorStakingCommission: Implements sdk.AnteDecorator, enforcing the | ||
// maximum staking commission for validators on the network. | ||
type AnteDecoratorStakingCommission struct{} | ||
|
||
func (a AnteDecoratorStakingCommission) AnteHandle( | ||
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler, | ||
) (newCtx sdk.Context, err error) { | ||
for _, msg := range tx.GetMsgs() { | ||
switch msg := msg.(type) { | ||
case *stakingtypes.MsgCreateValidator: | ||
rate := msg.Commission.Rate | ||
if rate.GT(MAX_COMMISSION()) { | ||
return ctx, NewErrMaxValidatorCommission(rate) | ||
} | ||
case *stakingtypes.MsgEditValidator: | ||
rate := msg.CommissionRate | ||
if rate != nil && msg.CommissionRate.GT(MAX_COMMISSION()) { | ||
return ctx, NewErrMaxValidatorCommission(*rate) | ||
} | ||
default: | ||
continue | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
|
||
"github.com/NibiruChain/nibiru/app" | ||
"github.com/NibiruChain/nibiru/app/ante" | ||
"github.com/NibiruChain/nibiru/x/common/testutil" | ||
) | ||
|
||
func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() { | ||
// nextAnteHandler: A no-op next handler to make this a unit test. | ||
var nextAnteHandler sdk.AnteHandler = func( | ||
ctx sdk.Context, tx sdk.Tx, simulate bool, | ||
) (newCtx sdk.Context, err error) { | ||
return ctx, nil | ||
} | ||
|
||
mockDescription := stakingtypes.Description{ | ||
Moniker: "mock-moniker", | ||
Identity: "mock-identity", | ||
Website: "mock-website", | ||
SecurityContact: "mock-security-contact", | ||
Details: "mock-details", | ||
} | ||
|
||
valAddr := sdk.ValAddress(testutil.AccAddress()).String() | ||
commissionRatePointer := new(sdk.Dec) | ||
*commissionRatePointer = sdk.NewDecWithPrec(10, 2) | ||
happyMsgs := []sdk.Msg{ | ||
&stakingtypes.MsgCreateValidator{ | ||
Description: mockDescription, | ||
Commission: stakingtypes.CommissionRates{ | ||
Rate: sdk.NewDecWithPrec(6, 2), // 6% | ||
MaxRate: sdk.NewDec(420), | ||
MaxChangeRate: sdk.NewDec(420), | ||
}, | ||
MinSelfDelegation: sdk.NewInt(1), | ||
DelegatorAddress: testutil.AccAddress().String(), | ||
ValidatorAddress: valAddr, | ||
Pubkey: &codectypes.Any{}, | ||
Value: sdk.NewInt64Coin("unibi", 1), | ||
}, | ||
&stakingtypes.MsgEditValidator{ | ||
Description: mockDescription, | ||
ValidatorAddress: valAddr, | ||
CommissionRate: commissionRatePointer, // 10% | ||
MinSelfDelegation: nil, | ||
}, | ||
} | ||
|
||
createSadMsgs := func() []sdk.Msg { | ||
sadMsgCreateVal := new(stakingtypes.MsgCreateValidator) | ||
*sadMsgCreateVal = *(happyMsgs[0]).(*stakingtypes.MsgCreateValidator) | ||
sadMsgCreateVal.Commission.Rate = sdk.NewDecWithPrec(26, 2) | ||
|
||
sadMsgEditVal := new(stakingtypes.MsgEditValidator) | ||
*sadMsgEditVal = *(happyMsgs[1]).(*stakingtypes.MsgEditValidator) | ||
newCommissionRate := new(sdk.Dec) | ||
*newCommissionRate = sdk.NewDecWithPrec(26, 2) | ||
sadMsgEditVal.CommissionRate = newCommissionRate | ||
|
||
return []sdk.Msg{ | ||
sadMsgCreateVal, | ||
sadMsgEditVal, | ||
} | ||
} | ||
sadMsgs := createSadMsgs() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
txMsgs []sdk.Msg | ||
wantErr string | ||
}{ | ||
{ | ||
name: "happy blank", | ||
txMsgs: []sdk.Msg{}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy msgs", | ||
txMsgs: []sdk.Msg{ | ||
happyMsgs[0], | ||
happyMsgs[1], | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "sad: max commission on create validator", | ||
txMsgs: []sdk.Msg{ | ||
sadMsgs[0], | ||
happyMsgs[1], | ||
}, | ||
wantErr: ante.ErrMaxValidatorCommission.Error(), | ||
}, | ||
{ | ||
name: "sad: max commission on edit validator", | ||
txMsgs: []sdk.Msg{ | ||
happyMsgs[0], | ||
sadMsgs[1], | ||
}, | ||
wantErr: ante.ErrMaxValidatorCommission.Error(), | ||
}, | ||
} { | ||
s.T().Run(tc.name, func(t *testing.T) { | ||
txGasCoins := sdk.NewCoins( | ||
sdk.NewCoin("unibi", sdk.NewInt(1_000)), | ||
sdk.NewCoin("utoken", sdk.NewInt(500)), | ||
) | ||
|
||
encCfg := app.MakeEncodingConfigAndRegister() | ||
txBuilder, err := sdkclienttx.Factory{}. | ||
WithFees(txGasCoins.String()). | ||
WithChainID(s.ctx.ChainID()). | ||
WithTxConfig(encCfg.TxConfig). | ||
BuildUnsignedTx(tc.txMsgs...) | ||
s.NoError(err) | ||
|
||
anteDecorator := ante.AnteDecoratorStakingCommission{} | ||
simulate := true | ||
s.ctx, err = anteDecorator.AnteHandle( | ||
s.ctx, txBuilder.GetTx(), simulate, nextAnteHandler, | ||
) | ||
|
||
if tc.wantErr != "" { | ||
s.ErrorContains(err, tc.wantErr) | ||
return | ||
} | ||
s.NoError(err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ante | ||
|
||
import ( | ||
sdkerrors "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var errorCodeIdx uint32 = 1 | ||
|
||
func registerError(errMsg string) *sdkerrors.Error { | ||
errorCodeIdx += 1 | ||
return sdkerrors.Register("ante-nibiru", errorCodeIdx, errMsg) | ||
} | ||
|
||
// app/ante "sentinel" errors | ||
var ( | ||
ErrOracleAnte = registerError("oracle ante error") | ||
ErrMaxValidatorCommission = registerError("validator commission rate is above max") | ||
) | ||
|
||
func NewErrMaxValidatorCommission(gotCommission sdk.Dec) error { | ||
return ErrMaxValidatorCommission.Wrapf( | ||
"got (%s), max rate is (%s)", gotCommission, MAX_COMMISSION()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.