mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 07:57:27 +00:00 
			
		
		
		
	fix: missing designers (#62)
* fix: add dasigners back * test: remove manually initialize genesis * feat: generate all missing epochs on begin block; only panic on smaller block height * chore: add logs, fix EpochBlocks
This commit is contained in:
		
							parent
							
								
									04ce67f6a9
								
							
						
					
					
						commit
						57943ec0e0
					
				@ -687,6 +687,7 @@ func NewApp(
 | 
			
		||||
		mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper, nil, mintSubspace),
 | 
			
		||||
		precisebank.NewAppModule(app.precisebankKeeper, app.bankKeeper, app.accountKeeper),
 | 
			
		||||
		ibcwasm.NewAppModule(app.ibcWasmClientKeeper),
 | 
			
		||||
		dasigners.NewAppModule(app.dasignersKeeper, *app.stakingKeeper),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	// Warning: Some begin blockers must run before others. Ensure the dependencies are understood before modifying this list.
 | 
			
		||||
@ -732,6 +733,7 @@ func NewApp(
 | 
			
		||||
		packetforwardtypes.ModuleName,
 | 
			
		||||
		precisebanktypes.ModuleName,
 | 
			
		||||
		ibcwasmtypes.ModuleName,
 | 
			
		||||
		dasignerstypes.ModuleName,
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	// Warning: Some end blockers must run before others. Ensure the dependencies are understood before modifying this list.
 | 
			
		||||
@ -767,6 +769,7 @@ func NewApp(
 | 
			
		||||
		packetforwardtypes.ModuleName,
 | 
			
		||||
		precisebanktypes.ModuleName,
 | 
			
		||||
		ibcwasmtypes.ModuleName,
 | 
			
		||||
		dasignerstypes.ModuleName,
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	// Warning: Some init genesis methods must run before others. Ensure the dependencies are understood before modifying this list
 | 
			
		||||
@ -801,6 +804,7 @@ func NewApp(
 | 
			
		||||
		precisebanktypes.ModuleName, // Must be run after x/bank to verify reserve balance
 | 
			
		||||
		crisistypes.ModuleName,      // runs the invariants at genesis, should run after other modules
 | 
			
		||||
		ibcwasmtypes.ModuleName,
 | 
			
		||||
		dasignerstypes.ModuleName,
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	app.mm.RegisterInvariants(&app.crisisKeeper)
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ package keeper
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/big"
 | 
			
		||||
	"sort"
 | 
			
		||||
 | 
			
		||||
@ -16,7 +17,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,12 +27,14 @@ 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
 | 
			
		||||
	k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] generating epoch %v", expectedEpoch))
 | 
			
		||||
	registrations := []Ballot{}
 | 
			
		||||
	k.IterateRegistrations(ctx, expectedEpoch, func(account string, signature []byte) (stop bool) {
 | 
			
		||||
		registrations = append(registrations, Ballot{
 | 
			
		||||
@ -106,4 +110,11 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
 | 
			
		||||
	// save to store
 | 
			
		||||
	k.SetEpochQuorums(ctx, expectedEpoch, quorums)
 | 
			
		||||
	k.SetEpochNumber(ctx, expectedEpoch)
 | 
			
		||||
	k.Logger(ctx).Info(fmt.Sprintf("[BeginBlock] epoch %v generated, with %v quorums", expectedEpoch, len(quorums.Quorums)))
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
 | 
			
		||||
	for k.generateOneEpoch(ctx) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,6 @@ package keeper_test
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/types"
 | 
			
		||||
@ -19,16 +18,29 @@ type AbciTestSuite struct {
 | 
			
		||||
 | 
			
		||||
func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
 | 
			
		||||
	// suite.App.InitializeFromGenesisStates()
 | 
			
		||||
	dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	// dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	params := suite.Keeper.GetParams(suite.Ctx)
 | 
			
		||||
	suite.Require().Panics(func() {
 | 
			
		||||
		suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*2)), abci.RequestBeginBlock{})
 | 
			
		||||
	suite.Assert().EqualValues(params, types.DefaultGenesisState().Params)
 | 
			
		||||
 | 
			
		||||
	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")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (suite *AbciTestSuite) TestBeginBlock_Success() {
 | 
			
		||||
	// suite.App.InitializeFromGenesisStates()
 | 
			
		||||
	dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	// dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	suite.Keeper.SetParams(suite.Ctx, types.Params{
 | 
			
		||||
		TokensPerVote:     10,
 | 
			
		||||
		MaxVotesPerSigner: 200,
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,6 @@ import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/0glabs/0g-chain/crypto/bn254util"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
 | 
			
		||||
	"github.com/0glabs/0g-chain/x/dasigners/v1/types"
 | 
			
		||||
@ -312,7 +311,7 @@ func (suite *KeeperTestSuite) queryAggregatePubkeyG1(params types.Params) {
 | 
			
		||||
 | 
			
		||||
func (suite *KeeperTestSuite) Test_Keeper() {
 | 
			
		||||
	// suite.App.InitializeFromGenesisStates()
 | 
			
		||||
	dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	// dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
 | 
			
		||||
	// add delegation
 | 
			
		||||
	params := suite.Keeper.GetParams(suite.Ctx)
 | 
			
		||||
	suite.AddDelegation(signer1, signer1, keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)))
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user