mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-20 15:05:21 +00:00
feat: generate all missing epochs on begin block; only panic on smaller block height
This commit is contained in:
parent
c0e9f28582
commit
645729fa89
@ -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) {
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user