|
| 1 | +package staking_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "testing" |
| 6 | + |
| 7 | + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" |
| 8 | + cmttime "github.com/cometbft/cometbft/types/time" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/mock/gomock" |
| 11 | + |
| 12 | + sdkmath "cosmossdk.io/math" |
| 13 | + storetypes "cosmossdk.io/store/types" |
| 14 | + |
| 15 | + "github.com/cosmos/cosmos-sdk/baseapp" |
| 16 | + "github.com/cosmos/cosmos-sdk/codec/address" |
| 17 | + "github.com/cosmos/cosmos-sdk/runtime" |
| 18 | + "github.com/cosmos/cosmos-sdk/testutil" |
| 19 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 20 | + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" |
| 21 | + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" |
| 22 | + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 23 | + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" |
| 24 | + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" |
| 25 | + stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil" |
| 26 | + "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 27 | +) |
| 28 | + |
| 29 | +func BenchmarkApplyAndReturnValidatorSetUpdates(b *testing.B) { |
| 30 | + // goal of this benchmark to measure the performance changes in ApplyAndReturnValidatorSetUpdates |
| 31 | + // for dropping the bench32 conversion and different index types. |
| 32 | + // therefore the validator power, max or state is not modified to focus on comparing the valset |
| 33 | + // for an update only. |
| 34 | + const validatorCount = 150 |
| 35 | + testEnv := newTestEnvironment(b) |
| 36 | + keeper, ctx := testEnv.stakingKeeper, testEnv.ctx |
| 37 | + r := rand.New(rand.NewSource(int64(1))) |
| 38 | + vals, valAddrs := setupState(b, r, validatorCount) |
| 39 | + |
| 40 | + params, err := keeper.GetParams(ctx) |
| 41 | + require.NoError(b, err) |
| 42 | + params.MaxValidators = uint32(validatorCount) |
| 43 | + require.NoError(b, keeper.SetParams(ctx, params)) |
| 44 | + |
| 45 | + b.Logf("vals count: %d", validatorCount) |
| 46 | + for i, validator := range vals { |
| 47 | + require.NoError(b, keeper.SetValidator(ctx, validator)) |
| 48 | + require.NoError(b, keeper.SetValidatorByConsAddr(ctx, validator)) |
| 49 | + require.NoError(b, keeper.SetValidatorByPowerIndex(ctx, validator)) |
| 50 | + require.NoError(b, keeper.SetLastValidatorPower(ctx, valAddrs[i], validator.ConsensusPower(sdk.DefaultPowerReduction))) |
| 51 | + } |
| 52 | + ctx, _ = testEnv.ctx.CacheContext() |
| 53 | + b.ResetTimer() |
| 54 | + for i := 0; i < b.N; i++ { |
| 55 | + _, _ = keeper.ApplyAndReturnValidatorSetUpdates(ctx) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +type KeeperTestEnvironment struct { |
| 60 | + ctx sdk.Context |
| 61 | + stakingKeeper *stakingkeeper.Keeper |
| 62 | + bankKeeper *stakingtestutil.MockBankKeeper |
| 63 | + accountKeeper *stakingtestutil.MockAccountKeeper |
| 64 | + queryClient types.QueryClient |
| 65 | + msgServer types.MsgServer |
| 66 | +} |
| 67 | + |
| 68 | +func newTestEnvironment(tb testing.TB) *KeeperTestEnvironment { |
| 69 | + tb.Helper() |
| 70 | + key := storetypes.NewKVStoreKey(types.StoreKey) |
| 71 | + storeService := runtime.NewKVStoreService(key) |
| 72 | + testCtx := testutil.DefaultContextWithDB(tb, key, storetypes.NewTransientStoreKey("transient_test")) |
| 73 | + ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) |
| 74 | + encCfg := moduletestutil.MakeTestEncodingConfig() |
| 75 | + |
| 76 | + ctrl := gomock.NewController(tb) |
| 77 | + accountKeeper := stakingtestutil.NewMockAccountKeeper(ctrl) |
| 78 | + accountKeeper.EXPECT().GetModuleAddress(types.BondedPoolName). |
| 79 | + Return(authtypes.NewEmptyModuleAccount(types.BondedPoolName).GetAddress()) |
| 80 | + accountKeeper.EXPECT().GetModuleAddress(types.NotBondedPoolName). |
| 81 | + Return(authtypes.NewEmptyModuleAccount(types.NotBondedPoolName).GetAddress()) |
| 82 | + accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes() |
| 83 | + |
| 84 | + bankKeeper := stakingtestutil.NewMockBankKeeper(ctrl) |
| 85 | + |
| 86 | + keeper := stakingkeeper.NewKeeper( |
| 87 | + encCfg.Codec, |
| 88 | + storeService, |
| 89 | + accountKeeper, |
| 90 | + bankKeeper, |
| 91 | + authtypes.NewModuleAddress(govtypes.ModuleName).String(), |
| 92 | + address.NewBech32Codec("cosmosvaloper"), |
| 93 | + address.NewBech32Codec("cosmosvalcons"), |
| 94 | + ) |
| 95 | + require.NoError(tb, keeper.SetParams(ctx, types.DefaultParams())) |
| 96 | + |
| 97 | + testEnv := &KeeperTestEnvironment{ |
| 98 | + ctx: ctx, |
| 99 | + stakingKeeper: keeper, |
| 100 | + bankKeeper: bankKeeper, |
| 101 | + accountKeeper: accountKeeper, |
| 102 | + } |
| 103 | + types.RegisterInterfaces(encCfg.InterfaceRegistry) |
| 104 | + queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry) |
| 105 | + types.RegisterQueryServer(queryHelper, stakingkeeper.Querier{Keeper: keeper}) |
| 106 | + testEnv.queryClient = types.NewQueryClient(queryHelper) |
| 107 | + testEnv.msgServer = stakingkeeper.NewMsgServerImpl(keeper) |
| 108 | + return testEnv |
| 109 | +} |
| 110 | + |
| 111 | +func setupState(b *testing.B, r *rand.Rand, numBonded int) ([]types.Validator, []sdk.ValAddress) { |
| 112 | + b.Helper() |
| 113 | + accs := simtypes.RandomAccounts(r, numBonded) |
| 114 | + initialStake := sdkmath.NewInt(r.Int63n(1000) + 10) |
| 115 | + |
| 116 | + validators := make([]types.Validator, numBonded) |
| 117 | + valAddrs := make([]sdk.ValAddress, numBonded) |
| 118 | + |
| 119 | + for i := 0; i < numBonded; i++ { |
| 120 | + valAddr := sdk.ValAddress(accs[i].Address) |
| 121 | + valAddrs[i] = valAddr |
| 122 | + |
| 123 | + maxCommission := sdkmath.LegacyNewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) |
| 124 | + commission := types.NewCommission( |
| 125 | + simtypes.RandomDecAmount(r, maxCommission), |
| 126 | + maxCommission, |
| 127 | + simtypes.RandomDecAmount(r, maxCommission), |
| 128 | + ) |
| 129 | + |
| 130 | + validator, err := types.NewValidator(valAddr.String(), accs[i].ConsKey.PubKey(), types.Description{}) |
| 131 | + require.NoError(b, err) |
| 132 | + startStake := sdkmath.NewInt(r.Int63n(1000) + initialStake.Int64()) |
| 133 | + validator.Tokens = startStake.Mul(sdk.DefaultPowerReduction) |
| 134 | + validator.DelegatorShares = sdkmath.LegacyNewDecFromInt(initialStake) |
| 135 | + validator.Commission = commission |
| 136 | + validator.Status = types.Bonded |
| 137 | + validators[i] = validator |
| 138 | + } |
| 139 | + return validators, valAddrs |
| 140 | +} |
0 commit comments