diff --git a/blockchain/blockdao/blockindexer.go b/blockchain/blockdao/blockindexer.go index 526c092c6b..a478a45ec8 100644 --- a/blockchain/blockdao/blockindexer.go +++ b/blockchain/blockdao/blockindexer.go @@ -28,13 +28,6 @@ type ( DeleteTipBlock(context.Context, *block.Block) error } - // BlockIndexerWithStart defines an interface to accept block to build index from a start height - BlockIndexerWithStart interface { - BlockIndexer - // StartHeight returns the start height of the indexer - StartHeight() uint64 - } - // BlockIndexerChecker defines a checker of block indexer BlockIndexerChecker struct { dao BlockDAO @@ -74,14 +67,7 @@ func (bic *BlockIndexerChecker) CheckIndexer(ctx context.Context, indexer BlockI if targetHeight == 0 || targetHeight > daoTip { targetHeight = daoTip } - startHeight := tipHeight + 1 - if indexerWS, ok := indexer.(BlockIndexerWithStart); ok { - indexStartHeight := indexerWS.StartHeight() - if indexStartHeight > startHeight { - startHeight = indexStartHeight - } - } - for i := startHeight; i <= targetHeight; i++ { + for i := tipHeight + 1; i <= targetHeight; i++ { // ternimate if context is done if err := ctx.Err(); err != nil { return errors.Wrap(err, "terminate the indexer checking") diff --git a/blockindex/contractstaking/indexer.go b/blockindex/contractstaking/indexer.go index caa9df0932..25322cd04a 100644 --- a/blockindex/contractstaking/indexer.go +++ b/blockindex/contractstaking/indexer.go @@ -84,12 +84,11 @@ func (s *Indexer) Stop(ctx context.Context) error { // Height returns the tip block height func (s *Indexer) Height() (uint64, error) { - return s.cache.Height(), nil -} - -// StartHeight returns the start height of the indexer -func (s *Indexer) StartHeight() uint64 { - return s.config.ContractDeployHeight + h := s.cache.Height() + if h < s.config.ContractDeployHeight { + h = s.config.ContractDeployHeight + } + return h, nil } // ContractAddress returns the contract address diff --git a/blockindex/contractstaking/indexer_test.go b/blockindex/contractstaking/indexer_test.go index 4f4fd04930..3cd1c9614c 100644 --- a/blockindex/contractstaking/indexer_test.go +++ b/blockindex/contractstaking/indexer_test.go @@ -137,7 +137,6 @@ func TestContractStakingIndexerLoadCache(t *testing.T) { newHeight, err := newIndexer.Height() r.NoError(err) r.Equal(height, newHeight) - r.Equal(startHeight, newIndexer.StartHeight()) tbc, err = newIndexer.TotalBucketCount(height) r.EqualValues(1, tbc) r.NoError(err) diff --git a/blockindex/sync_indexers.go b/blockindex/sync_indexers.go index 9006c73258..2185875214 100644 --- a/blockindex/sync_indexers.go +++ b/blockindex/sync_indexers.go @@ -15,9 +15,7 @@ import ( // SyncIndexers is a special index that includes multiple indexes, // which stay in sync when blocks are added. type SyncIndexers struct { - indexers []blockdao.BlockIndexer - startHeights []uint64 // start height of each indexer, which will be determined when the indexer is started - minStartHeight uint64 // minimum start height of all indexers + indexers []blockdao.BlockIndexer } // NewSyncIndexers creates a new SyncIndexers @@ -33,7 +31,7 @@ func (ig *SyncIndexers) Start(ctx context.Context) error { return err } } - return ig.initStartHeight() + return nil } // Stop stops the indexer group @@ -48,11 +46,7 @@ func (ig *SyncIndexers) Stop(ctx context.Context) error { // PutBlock puts a block into the indexers in the group func (ig *SyncIndexers) PutBlock(ctx context.Context, blk *block.Block) error { - for i, indexer := range ig.indexers { - // check if the block is higher than the indexer's start height - if blk.Height() < ig.startHeights[i] { - continue - } + for _, indexer := range ig.indexers { // check if the block is higher than the indexer's height height, err := indexer.Height() if err != nil { @@ -79,11 +73,6 @@ func (ig *SyncIndexers) DeleteTipBlock(ctx context.Context, blk *block.Block) er return nil } -// StartHeight returns the minimum start height of the indexers in the group -func (ig *SyncIndexers) StartHeight() uint64 { - return ig.minStartHeight -} - // Height returns the minimum height of the indexers in the group func (ig *SyncIndexers) Height() (uint64, error) { var height uint64 @@ -98,28 +87,3 @@ func (ig *SyncIndexers) Height() (uint64, error) { } return height, nil } - -// initStartHeight initializes the start height of the indexers in the group -// for every indexer, the start height is the maximum of tipheight+1 and startheight -func (ig *SyncIndexers) initStartHeight() error { - ig.minStartHeight = 0 - ig.startHeights = make([]uint64, len(ig.indexers)) - for i, indexer := range ig.indexers { - tipHeight, err := indexer.Height() - if err != nil { - return err - } - indexStartHeight := tipHeight + 1 - if indexerWithStart, ok := indexer.(blockdao.BlockIndexerWithStart); ok { - startHeight := indexerWithStart.StartHeight() - if startHeight > indexStartHeight { - indexStartHeight = startHeight - } - } - ig.startHeights[i] = indexStartHeight - if i == 0 || indexStartHeight < ig.minStartHeight { - ig.minStartHeight = indexStartHeight - } - } - return nil -} diff --git a/blockindex/sync_indexers_test.go b/blockindex/sync_indexers_test.go index 7c2368ec66..c091cc048a 100644 --- a/blockindex/sync_indexers_test.go +++ b/blockindex/sync_indexers_test.go @@ -67,8 +67,6 @@ func TestSyncIndexers_StartHeight(t *testing.T) { ig := NewSyncIndexers(indexers...) err := ig.Start(context.Background()) require.NoError(err) - height := ig.StartHeight() - require.Equal(c.expect, height) }) } diff --git a/systemcontractindex/common.go b/systemcontractindex/common.go index bfb9f82c0a..118faebe30 100644 --- a/systemcontractindex/common.go +++ b/systemcontractindex/common.go @@ -61,6 +61,9 @@ func (s *IndexerCommon) ContractAddress() string { return s.contractAddress } // Height returns the tip block height func (s *IndexerCommon) Height() uint64 { + if s.height < s.startHeight { + return s.startHeight + } return s.height } diff --git a/systemcontractindex/stakingindex/index.go b/systemcontractindex/stakingindex/index.go index b5e2b13c03..ea1eba95f6 100644 --- a/systemcontractindex/stakingindex/index.go +++ b/systemcontractindex/stakingindex/index.go @@ -32,7 +32,6 @@ type ( StakingIndexer interface { lifecycle.StartStopper Height() (uint64, error) - StartHeight() uint64 ContractAddress() string Buckets(height uint64) ([]*VoteBucket, error) Bucket(id uint64, height uint64) (*VoteBucket, bool, error) @@ -90,13 +89,6 @@ func (s *Indexer) Height() (uint64, error) { return s.common.Height(), nil } -// StartHeight returns the start height of the indexer -func (s *Indexer) StartHeight() uint64 { - s.mutex.RLock() - defer s.mutex.RUnlock() - return s.common.StartHeight() -} - // ContractAddress returns the contract address func (s *Indexer) ContractAddress() string { s.mutex.RLock()