diff --git a/x/dasigners/v1/keeper/abci.go b/x/dasigners/v1/keeper/abci.go index cb1bde33..a749ad66 100644 --- a/x/dasigners/v1/keeper/abci.go +++ b/x/dasigners/v1/keeper/abci.go @@ -16,7 +16,8 @@ type Ballot struct { content []byte } -func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { +// generateOneEpoch generate one epoch and returns true if there is a new epoch generated +func (k Keeper) generateOneEpoch(ctx sdk.Context) bool { epochNumber, err := k.GetEpochNumber(ctx) if err != nil { k.Logger(ctx).Error("[BeginBlock] cannot get epoch number") @@ -25,11 +26,12 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { params := k.GetParams(ctx) expectedEpoch := uint64(ctx.BlockHeight()) / params.EpochBlocks if expectedEpoch == epochNumber { - return + return false } - if expectedEpoch > epochNumber+1 || expectedEpoch < epochNumber { + if expectedEpoch < epochNumber { panic("block height is not continuous") } + expectedEpoch = epochNumber + 1 // new epoch registrations := []Ballot{} k.IterateRegistrations(ctx, expectedEpoch, func(account string, signature []byte) (stop bool) { @@ -106,4 +108,10 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { // save to store k.SetEpochQuorums(ctx, expectedEpoch, quorums) k.SetEpochNumber(ctx, expectedEpoch) + return true +} + +func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { + for k.generateOneEpoch(ctx) { + } } diff --git a/x/dasigners/v1/keeper/abci_test.go b/x/dasigners/v1/keeper/abci_test.go index 536bdac7..424abe9c 100644 --- a/x/dasigners/v1/keeper/abci_test.go +++ b/x/dasigners/v1/keeper/abci_test.go @@ -21,8 +21,20 @@ func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() { // dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) params := suite.Keeper.GetParams(suite.Ctx) suite.Assert().EqualValues(params, types.DefaultGenesisState().Params) - suite.Require().Panics(func() { - suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*2)), abci.RequestBeginBlock{}) + + epoch, err := suite.Keeper.GetEpochNumber(suite.Ctx) + suite.Require().NoError(err) + suite.Assert().EqualValues(epoch, 0) + + suite.Assert().NotPanics(func() { + suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*10)), abci.RequestBeginBlock{}) + }) + epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx) + suite.Require().NoError(err) + suite.Assert().EqualValues(epoch, 10) + + suite.Assert().Panics(func() { + suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*9)), abci.RequestBeginBlock{}) }, "block height is not continuous") } diff --git a/x/dasigners/v1/types/genesis.go b/x/dasigners/v1/types/genesis.go index 0515758d..789f7042 100644 --- a/x/dasigners/v1/types/genesis.go +++ b/x/dasigners/v1/types/genesis.go @@ -18,7 +18,7 @@ func DefaultGenesisState() *GenesisState { TokensPerVote: 10, MaxVotesPerSigner: 1024, MaxQuorums: 10, - EpochBlocks: 5760, + EpochBlocks: 2, EncodedSlices: 3072, }, 0, make([]*Signer, 0), []*Quorums{{ Quorums: make([]*Quorum, 0),