From 83666b3dc3c2226c8d647dad96042ad4f6690450 Mon Sep 17 00:00:00 2001 From: skosito Date: Thu, 28 Mar 2024 00:08:22 +0100 Subject: [PATCH] Add tests for get bond factor --- testutil/keeper/emissions.go | 6 ++ .../keeper/block_rewards_components_test.go | 60 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/testutil/keeper/emissions.go b/testutil/keeper/emissions.go index e61f6cd76e..57c5b64527 100644 --- a/testutil/keeper/emissions.go +++ b/testutil/keeper/emissions.go @@ -128,3 +128,9 @@ func GetEmissionsParamStoreMock(t testing.TB, keeper *keeper.Keeper) *emissionsm require.True(t, ok) return m } + +func GetEmissionsStakingMock(t testing.TB, keeper *keeper.Keeper) *emissionsmocks.EmissionStakingKeeper { + cbk, ok := keeper.GetStakingKeeper().(*emissionsmocks.EmissionStakingKeeper) + require.True(t, ok) + return cbk +} diff --git a/x/emissions/keeper/block_rewards_components_test.go b/x/emissions/keeper/block_rewards_components_test.go index 45c42d2cfd..eb43408d2c 100644 --- a/x/emissions/keeper/block_rewards_components_test.go +++ b/x/emissions/keeper/block_rewards_components_test.go @@ -108,3 +108,63 @@ func TestKeeper_GetBlockRewardComponent(t *testing.T) { require.NotEqual(t, sdk.ZeroDec(), durationFactor) }) } + +func TestKeeper_GetBondFactor(t *testing.T) { + t.Run("should return 0 if current bond ratio is 0", func(t *testing.T) { + k, ctx, _, _ := keepertest.EmissionsKeeper(t) + + bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper()) + require.Equal(t, sdk.ZeroDec(), bondFactor) + }) + + t.Run("should return max bond factor if bond factor exceeds max bond factor", func(t *testing.T) { + k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{ + UseStakingMock: true, + }) + + params := emissionstypes.DefaultParams() + params.TargetBondRatio = "0.5" + params.MaxBondFactor = "1.1" + params.MinBondFactor = "0.9" + k.SetParams(ctx, params) + + stakingMock := keepertest.GetEmissionsStakingMock(t, k) + stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.25")) + bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper()) + require.Equal(t, sdk.MustNewDecFromStr(params.MaxBondFactor), bondFactor) + }) + + t.Run("should return min bond factor if bond factor below min bond factor", func(t *testing.T) { + k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{ + UseStakingMock: true, + }) + + params := emissionstypes.DefaultParams() + params.TargetBondRatio = "0.5" + params.MaxBondFactor = "1.1" + params.MinBondFactor = "0.9" + k.SetParams(ctx, params) + + stakingMock := keepertest.GetEmissionsStakingMock(t, k) + stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.75")) + bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper()) + require.Equal(t, sdk.MustNewDecFromStr(params.MinBondFactor), bondFactor) + }) + + t.Run("should return calculated bond factor if bond factor in range", func(t *testing.T) { + k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{ + UseStakingMock: true, + }) + + params := emissionstypes.DefaultParams() + params.TargetBondRatio = "0.5" + params.MaxBondFactor = "1.1" + params.MinBondFactor = "0.9" + k.SetParams(ctx, params) + + stakingMock := keepertest.GetEmissionsStakingMock(t, k) + stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.5")) + bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper()) + require.Equal(t, sdk.OneDec(), bondFactor) + }) +}