Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epoching: test updating epoch interval #723

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions x/epoching/keeper/epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
63 changes: 59 additions & 4 deletions x/epoching/keeper/epochs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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) {
Expand All @@ -23,11 +25,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
Expand All @@ -47,3 +48,57 @@ 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 via gov prop account
newEpochInterval := datagen.RandomInt(r, 20) + 2
newParams := types.Params{EpochInterval: newEpochInterval}
_, 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
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 {
h.Ctx, err = h.ApplyEmptyBlockWithVoteExtension(r)
require.NoError(t, err)
}
keeper.IncEpoch(h.Ctx)

// 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, epoch.GetLastBlockHeight()+1, newEpoch.FirstBlockHeight)
require.Equal(t, epoch.GetLastBlockHeight()+newEpochInterval, newEpoch.GetLastBlockHeight())
})
}