feat: generate all missing epochs on begin block; only panic on smaller block height

This commit is contained in:
MiniFrenchBread 2024-08-10 10:01:38 +08:00
parent c0e9f28582
commit 645729fa89
3 changed files with 26 additions and 6 deletions

View File

@ -16,7 +16,8 @@ type Ballot struct {
content []byte 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) epochNumber, err := k.GetEpochNumber(ctx)
if err != nil { if err != nil {
k.Logger(ctx).Error("[BeginBlock] cannot get epoch number") 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) params := k.GetParams(ctx)
expectedEpoch := uint64(ctx.BlockHeight()) / params.EpochBlocks expectedEpoch := uint64(ctx.BlockHeight()) / params.EpochBlocks
if expectedEpoch == epochNumber { if expectedEpoch == epochNumber {
return return false
} }
if expectedEpoch > epochNumber+1 || expectedEpoch < epochNumber { if expectedEpoch < epochNumber {
panic("block height is not continuous") panic("block height is not continuous")
} }
expectedEpoch = epochNumber + 1
// new epoch // new epoch
registrations := []Ballot{} registrations := []Ballot{}
k.IterateRegistrations(ctx, expectedEpoch, func(account string, signature []byte) (stop bool) { 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 // save to store
k.SetEpochQuorums(ctx, expectedEpoch, quorums) k.SetEpochQuorums(ctx, expectedEpoch, quorums)
k.SetEpochNumber(ctx, expectedEpoch) k.SetEpochNumber(ctx, expectedEpoch)
return true
}
func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
for k.generateOneEpoch(ctx) {
}
} }

View File

@ -21,8 +21,20 @@ func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
// dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) // dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
params := suite.Keeper.GetParams(suite.Ctx) params := suite.Keeper.GetParams(suite.Ctx)
suite.Assert().EqualValues(params, types.DefaultGenesisState().Params) 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") }, "block height is not continuous")
} }

View File

@ -18,7 +18,7 @@ func DefaultGenesisState() *GenesisState {
TokensPerVote: 10, TokensPerVote: 10,
MaxVotesPerSigner: 1024, MaxVotesPerSigner: 1024,
MaxQuorums: 10, MaxQuorums: 10,
EpochBlocks: 5760, EpochBlocks: 2,
EncodedSlices: 3072, EncodedSlices: 3072,
}, 0, make([]*Signer, 0), []*Quorums{{ }, 0, make([]*Signer, 0), []*Quorums{{
Quorums: make([]*Quorum, 0), Quorums: make([]*Quorum, 0),