From bd984b937a6de2e95e4a1bc5172c52ee0b9fe628 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Fri, 19 Jul 2024 14:26:24 +0800 Subject: [PATCH 1/3] test --- x/epoching/keeper/epochs_test.go | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/x/epoching/keeper/epochs_test.go b/x/epoching/keeper/epochs_test.go index a4a3dff99..5ac62bd5b 100644 --- a/x/epoching/keeper/epochs_test.go +++ b/x/epoching/keeper/epochs_test.go @@ -8,6 +8,7 @@ import ( "github.com/babylonchain/babylon/testutil/datagen" testhelper "github.com/babylonchain/babylon/testutil/helper" + "github.com/babylonchain/babylon/x/epoching/types" ) func FuzzEpochs(f *testing.F) { @@ -23,11 +24,10 @@ func FuzzEpochs(f *testing.F) { require.Equal(t, epoch.EpochNumber, uint64(1)) require.Equal(t, epoch.FirstBlockHeight, uint64(1)) - // set a random epoch interval epochInterval := keeper.GetParams(ctx).EpochInterval // increment a random number of new blocks - numIncBlocks := r.Uint64()%1000 + 1 + numIncBlocks := r.Uint64()%100 + 1 var err error for i := uint64(0); i < numIncBlocks-1; i++ { // TODO: Figure out why when ctx height is 1, ApplyEmptyBlockWithVoteExtension @@ -47,3 +47,55 @@ func FuzzEpochs(f *testing.F) { require.Equal(t, (expectedEpochNumber-1)*epochInterval+1, actualNewEpoch.FirstBlockHeight) }) } + +func FuzzEpochs_UpdateEpochInterval(f *testing.F) { + datagen.AddRandomSeedsToFuzzer(f, 10) + + f.Fuzz(func(t *testing.T, seed int64) { + r := rand.New(rand.NewSource(seed)) + + h := testhelper.NewHelper(t) + keeper := h.App.EpochingKeeper + + // increment a random number of new blocks + numIncBlocks := r.Uint64()%100 + 1 + var err error + for i := uint64(0); i < numIncBlocks-1; i++ { + // When ctx height is 1, ApplyEmptyBlockWithVoteExtension + // will still give ctx height 1 once, then start to increment + _, err = h.ApplyEmptyBlockWithVoteExtension(r) + require.NoError(t, err) + } + // get current epoch metadata + epoch := keeper.GetEpoch(h.Ctx) + + // update the epoch interval in params + newEpochInterval := datagen.RandomInt(r, 20) + 2 + newParams := types.Params{EpochInterval: newEpochInterval} + err = keeper.SetParams(h.Ctx, newParams) + require.NoError(t, err) + + // ensure the current epoch metadata is not affected + epoch2 := keeper.GetEpoch(h.Ctx) + require.Equal(t, epoch, epoch2) + + // enter the last block of the current epoch + lastHeightOfEpoch := epoch.GetLastBlockHeight() + for uint64(h.Ctx.HeaderInfo().Height) < lastHeightOfEpoch { + _, err = h.ApplyEmptyBlockWithVoteExtension(r) + require.NoError(t, err) + } + // enter the next block and thus 1st block of the next epoch + _, err = h.ApplyEmptyBlockWithVoteExtension(r) + require.NoError(t, err) + // ensure + // - the epoch has incremented + // - epoch interval is updated + // - first/last height of the epoch is correct + newEpoch := keeper.GetEpoch(h.Ctx) + require.Equal(t, epoch.EpochNumber+1, newEpoch.EpochNumber) + require.Equal(t, newEpochInterval, newEpoch.CurrentEpochInterval) + require.Equal(t, uint64(h.Ctx.HeaderInfo().Height), newEpoch.FirstBlockHeight) + require.Equal(t, uint64(h.Ctx.HeaderInfo().Height)+newEpochInterval-1, newEpoch.GetLastBlockHeight()) + }) +} From fe8d47df8721adaf4c26e57b4050a96ad72af6f5 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Fri, 19 Jul 2024 14:46:08 +0800 Subject: [PATCH 2/3] use gov prop --- x/epoching/keeper/epochs_test.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/x/epoching/keeper/epochs_test.go b/x/epoching/keeper/epochs_test.go index 5ac62bd5b..a1021e1c0 100644 --- a/x/epoching/keeper/epochs_test.go +++ b/x/epoching/keeper/epochs_test.go @@ -4,11 +4,12 @@ import ( "math/rand" "testing" - "github.com/stretchr/testify/require" - "github.com/babylonchain/babylon/testutil/datagen" testhelper "github.com/babylonchain/babylon/testutil/helper" "github.com/babylonchain/babylon/x/epoching/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/require" ) func FuzzEpochs(f *testing.F) { @@ -69,10 +70,13 @@ func FuzzEpochs_UpdateEpochInterval(f *testing.F) { // get current epoch metadata epoch := keeper.GetEpoch(h.Ctx) - // update the epoch interval in params + // update the epoch interval in params via gov prop account newEpochInterval := datagen.RandomInt(r, 20) + 2 newParams := types.Params{EpochInterval: newEpochInterval} - err = keeper.SetParams(h.Ctx, newParams) + _, err = h.MsgSrvr.UpdateParams(h.Ctx, &types.MsgUpdateParams{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Params: newParams, + }) require.NoError(t, err) // ensure the current epoch metadata is not affected @@ -82,12 +86,12 @@ func FuzzEpochs_UpdateEpochInterval(f *testing.F) { // enter the last block of the current epoch lastHeightOfEpoch := epoch.GetLastBlockHeight() for uint64(h.Ctx.HeaderInfo().Height) < lastHeightOfEpoch { - _, err = h.ApplyEmptyBlockWithVoteExtension(r) + h.Ctx, err = h.ApplyEmptyBlockWithVoteExtension(r) require.NoError(t, err) } - // enter the next block and thus 1st block of the next epoch - _, err = h.ApplyEmptyBlockWithVoteExtension(r) + h.Ctx, err = h.ApplyEmptyBlockWithVoteExtension(r) require.NoError(t, err) + // ensure // - the epoch has incremented // - epoch interval is updated From 63c2ad044f6202adf43749f9212e872aba890fbc Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Fri, 19 Jul 2024 15:06:41 +0800 Subject: [PATCH 3/3] test --- x/epoching/keeper/epochs.go | 15 +++++++++------ x/epoching/keeper/epochs_test.go | 7 +++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/x/epoching/keeper/epochs.go b/x/epoching/keeper/epochs.go index 8ee863952..478ed4ff5 100644 --- a/x/epoching/keeper/epochs.go +++ b/x/epoching/keeper/epochs.go @@ -120,13 +120,16 @@ func (k Keeper) RecordSealerBlockHashForEpoch(ctx context.Context) *types.Epoch // IncEpoch adds epoch number by 1 // CONTRACT: can only be invoked at the first block of an epoch func (k Keeper) IncEpoch(ctx context.Context) types.Epoch { - sdkCtx := sdk.UnwrapSDKContext(ctx) - epochNumber := k.GetEpoch(ctx).EpochNumber - incrementedEpochNumber := epochNumber + 1 - epochInterval := k.GetParams(ctx).EpochInterval - newEpoch := types.NewEpoch(incrementedEpochNumber, epochInterval, uint64(sdkCtx.HeaderInfo().Height), nil) - k.setEpochInfo(ctx, incrementedEpochNumber, &newEpoch) + params := k.GetParams(ctx) + epoch := k.GetEpoch(ctx) + + newEpochNumber := epoch.EpochNumber + 1 + epochInterval := params.EpochInterval + firstHeight := epoch.GetLastBlockHeight() + 1 + + newEpoch := types.NewEpoch(newEpochNumber, epochInterval, firstHeight, nil) + k.setEpochInfo(ctx, newEpochNumber, &newEpoch) return newEpoch } diff --git a/x/epoching/keeper/epochs_test.go b/x/epoching/keeper/epochs_test.go index a1021e1c0..54268c2f8 100644 --- a/x/epoching/keeper/epochs_test.go +++ b/x/epoching/keeper/epochs_test.go @@ -89,8 +89,7 @@ func FuzzEpochs_UpdateEpochInterval(f *testing.F) { h.Ctx, err = h.ApplyEmptyBlockWithVoteExtension(r) require.NoError(t, err) } - h.Ctx, err = h.ApplyEmptyBlockWithVoteExtension(r) - require.NoError(t, err) + keeper.IncEpoch(h.Ctx) // ensure // - the epoch has incremented @@ -99,7 +98,7 @@ func FuzzEpochs_UpdateEpochInterval(f *testing.F) { newEpoch := keeper.GetEpoch(h.Ctx) require.Equal(t, epoch.EpochNumber+1, newEpoch.EpochNumber) require.Equal(t, newEpochInterval, newEpoch.CurrentEpochInterval) - require.Equal(t, uint64(h.Ctx.HeaderInfo().Height), newEpoch.FirstBlockHeight) - require.Equal(t, uint64(h.Ctx.HeaderInfo().Height)+newEpochInterval-1, newEpoch.GetLastBlockHeight()) + require.Equal(t, epoch.GetLastBlockHeight()+1, newEpoch.FirstBlockHeight) + require.Equal(t, epoch.GetLastBlockHeight()+newEpochInterval, newEpoch.GetLastBlockHeight()) }) }