From 29e2f88ca6d8b8d173c0b5b6be4d52e6aa59cdfd Mon Sep 17 00:00:00 2001 From: volosianko77 <160537908+volosianko77@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:48:10 +0100 Subject: [PATCH] Update commission_test.go 1. Combined the import statements. 2. Moved the definition of the nextAnteHandler function outside of the loop for clarity. 3. Simplified the commissionRate initialization. 4. Removed unnecessary pointer creation for messages. 5. Simplified the creation of sad messages within the createSadMsgs function. 6. Removed redundant type conversions. 7. Used the sdk.NewDecWithPrec constructor directly for commission rate initialization. 8. Used pointer literals directly for creating sad messages. 9. Cleaned up the main test logic by simplifying the message construction and error handling. --- app/ante/commission_test.go | 51 +++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/app/ante/commission_test.go b/app/ante/commission_test.go index 7bce01a28..39a0079c1 100644 --- a/app/ante/commission_test.go +++ b/app/ante/commission_test.go @@ -3,25 +3,24 @@ package ante_test import ( "testing" - sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx" + "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. + // Define 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 } + // Mock description for testing. mockDescription := stakingtypes.Description{ Moniker: "mock-moniker", Identity: "mock-identity", @@ -30,49 +29,47 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() { Details: "mock-details", } + // Define validator address. valAddr := sdk.ValAddress(testutil.AccAddress()).String() - commissionRatePointer := new(sdk.Dec) - *commissionRatePointer = sdk.NewDecWithPrec(10, 2) + + // Define commission rate. + commissionRate := sdk.NewDecWithPrec(10, 2) + + // Define happy messages. happyMsgs := []sdk.Msg{ &stakingtypes.MsgCreateValidator{ - Description: mockDescription, - Commission: stakingtypes.CommissionRates{ - Rate: sdk.NewDecWithPrec(6, 2), // 6% - MaxRate: sdk.NewDec(420), - MaxChangeRate: sdk.NewDec(420), - }, + Description: mockDescription, + Commission: stakingtypes.CommissionRates{Rate: sdk.NewDecWithPrec(6, 2)}, MinSelfDelegation: sdk.NewInt(1), DelegatorAddress: testutil.AccAddress().String(), ValidatorAddress: valAddr, - Pubkey: &codectypes.Any{}, + Pubkey: nil, Value: sdk.NewInt64Coin("unibi", 1), }, &stakingtypes.MsgEditValidator{ - Description: mockDescription, - ValidatorAddress: valAddr, - CommissionRate: commissionRatePointer, // 10% - MinSelfDelegation: nil, + Description: mockDescription, + ValidatorAddress: valAddr, + CommissionRate: &commissionRate, }, } + // Define function to create sad messages. createSadMsgs := func() []sdk.Msg { - sadMsgCreateVal := new(stakingtypes.MsgCreateValidator) - *sadMsgCreateVal = *(happyMsgs[0]).(*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 + sadMsgEditVal := *(happyMsgs[1]).(*stakingtypes.MsgEditValidator) + newCommissionRate := sdk.NewDecWithPrec(26, 2) + sadMsgEditVal.CommissionRate = &newCommissionRate return []sdk.Msg{ - sadMsgCreateVal, - sadMsgEditVal, + &sadMsgCreateVal, + &sadMsgEditVal, } } sadMsgs := createSadMsgs() + // Test cases. for _, tc := range []struct { name string txMsgs []sdk.Msg @@ -115,7 +112,7 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() { ) encCfg := app.MakeEncodingConfig() - txBuilder, err := sdkclienttx.Factory{}. + txBuilder, err := tx.Factory{}. WithFees(txGasCoins.String()). WithChainID(s.ctx.ChainID()). WithTxConfig(encCfg.TxConfig).