feat: remove epoch and block height hard check

This commit is contained in:
MiniFrenchBread 2024-08-13 15:42:27 +08:00
parent 3017ed9919
commit fd1f2133b8
4 changed files with 13 additions and 23 deletions

View File

@ -17,22 +17,17 @@ type Ballot struct {
content []byte content []byte
} }
// generateOneEpoch generate one epoch and returns true if there is a new epoch generated func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
func (k Keeper) generateOneEpoch(ctx sdk.Context) bool { params := k.GetParams(ctx)
if uint64(ctx.BlockHeight())%params.EpochBlocks != 0 {
return
}
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")
panic(err) panic(err)
} }
params := k.GetParams(ctx) expectedEpoch := epochNumber + 1
expectedEpoch := uint64(ctx.BlockHeight()) / params.EpochBlocks
if expectedEpoch == epochNumber {
return false
}
if expectedEpoch < epochNumber {
panic("block height is not continuous")
}
expectedEpoch = epochNumber + 1
// new epoch // new epoch
k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] generating epoch %v", expectedEpoch)) k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] generating epoch %v", expectedEpoch))
registrations := []Ballot{} registrations := []Ballot{}
@ -110,11 +105,6 @@ func (k Keeper) generateOneEpoch(ctx sdk.Context) bool {
// save to store // save to store
k.SetEpochQuorums(ctx, expectedEpoch, quorums) k.SetEpochQuorums(ctx, expectedEpoch, quorums)
k.SetEpochNumber(ctx, expectedEpoch) k.SetEpochNumber(ctx, expectedEpoch)
k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] epoch %v generated, with %v quorums", expectedEpoch, len(quorums.Quorums))) k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] epoch %v generated at block height %v, with %v quorums", expectedEpoch, ctx.BlockHeight(), len(quorums.Quorums)))
return true return
}
func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
for k.generateOneEpoch(ctx) {
}
} }

View File

@ -31,11 +31,7 @@ func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
}) })
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx) epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
suite.Require().NoError(err) suite.Require().NoError(err)
suite.Assert().EqualValues(epoch, 10) suite.Assert().EqualValues(epoch, 1)
suite.Assert().Panics(func() {
suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*9)), abci.RequestBeginBlock{})
}, "block height is not continuous")
} }
func (suite *AbciTestSuite) TestBeginBlock_Success() { func (suite *AbciTestSuite) TestBeginBlock_Success() {

View File

@ -12,4 +12,5 @@ var (
ErrQuorumBitmapLengthMismatch = errorsmod.Register(ModuleName, 7, "quorum bitmap length mismatch") ErrQuorumBitmapLengthMismatch = errorsmod.Register(ModuleName, 7, "quorum bitmap length mismatch")
ErrInsufficientBonded = errorsmod.Register(ModuleName, 8, "insufficient bonded amount") ErrInsufficientBonded = errorsmod.Register(ModuleName, 8, "insufficient bonded amount")
ErrRowIndexOutOfBound = errorsmod.Register(ModuleName, 9, "row index out of bound") ErrRowIndexOutOfBound = errorsmod.Register(ModuleName, 9, "row index out of bound")
ErrInvalidEpochBlocks = errorsmod.Register(ModuleName, 10, "invalid epoch blocks")
) )

View File

@ -1,5 +1,8 @@
package types package types
func (p *Params) Validate() error { func (p *Params) Validate() error {
if p.EpochBlocks == 0 {
return ErrInvalidEpochBlocks
}
return nil return nil
} }