Skip to content

Commit 3e90a65

Browse files
committed
[blockindex] simplify indexer height checking
1 parent b0ac60b commit 3e90a65

File tree

7 files changed

+13
-71
lines changed

7 files changed

+13
-71
lines changed

blockchain/blockdao/blockindexer.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ type (
2828
DeleteTipBlock(context.Context, *block.Block) error
2929
}
3030

31-
// BlockIndexerWithStart defines an interface to accept block to build index from a start height
32-
BlockIndexerWithStart interface {
33-
BlockIndexer
34-
// StartHeight returns the start height of the indexer
35-
StartHeight() uint64
36-
}
37-
3831
// BlockIndexerChecker defines a checker of block indexer
3932
BlockIndexerChecker struct {
4033
dao BlockDAO
@@ -60,6 +53,7 @@ func (bic *BlockIndexerChecker) CheckIndexer(ctx context.Context, indexer BlockI
6053
if err != nil {
6154
return err
6255
}
56+
println("tipHeight =", tipHeight)
6357
daoTip, err := bic.dao.Height()
6458
if err != nil {
6559
return err
@@ -74,14 +68,7 @@ func (bic *BlockIndexerChecker) CheckIndexer(ctx context.Context, indexer BlockI
7468
if targetHeight == 0 || targetHeight > daoTip {
7569
targetHeight = daoTip
7670
}
77-
startHeight := tipHeight + 1
78-
if indexerWS, ok := indexer.(BlockIndexerWithStart); ok {
79-
indexStartHeight := indexerWS.StartHeight()
80-
if indexStartHeight > startHeight {
81-
startHeight = indexStartHeight
82-
}
83-
}
84-
for i := startHeight; i <= targetHeight; i++ {
71+
for i := tipHeight + 1; i <= targetHeight; i++ {
8572
// ternimate if context is done
8673
if err := ctx.Err(); err != nil {
8774
return errors.Wrap(err, "terminate the indexer checking")

blockindex/contractstaking/indexer.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,11 @@ func (s *Indexer) Stop(ctx context.Context) error {
8484

8585
// Height returns the tip block height
8686
func (s *Indexer) Height() (uint64, error) {
87-
return s.cache.Height(), nil
88-
}
89-
90-
// StartHeight returns the start height of the indexer
91-
func (s *Indexer) StartHeight() uint64 {
92-
return s.config.ContractDeployHeight
87+
h := s.cache.Height()
88+
if h < s.config.ContractDeployHeight {
89+
h = s.config.ContractDeployHeight
90+
}
91+
return h, nil
9392
}
9493

9594
// ContractAddress returns the contract address

blockindex/contractstaking/indexer_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func TestContractStakingIndexerLoadCache(t *testing.T) {
137137
newHeight, err := newIndexer.Height()
138138
r.NoError(err)
139139
r.Equal(height, newHeight)
140-
r.Equal(startHeight, newIndexer.StartHeight())
141140
tbc, err = newIndexer.TotalBucketCount(height)
142141
r.EqualValues(1, tbc)
143142
r.NoError(err)

blockindex/sync_indexers.go

+3-39
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ import (
1515
// SyncIndexers is a special index that includes multiple indexes,
1616
// which stay in sync when blocks are added.
1717
type SyncIndexers struct {
18-
indexers []blockdao.BlockIndexer
19-
startHeights []uint64 // start height of each indexer, which will be determined when the indexer is started
20-
minStartHeight uint64 // minimum start height of all indexers
18+
indexers []blockdao.BlockIndexer
2119
}
2220

2321
// NewSyncIndexers creates a new SyncIndexers
@@ -33,7 +31,7 @@ func (ig *SyncIndexers) Start(ctx context.Context) error {
3331
return err
3432
}
3533
}
36-
return ig.initStartHeight()
34+
return nil
3735
}
3836

3937
// Stop stops the indexer group
@@ -48,11 +46,7 @@ func (ig *SyncIndexers) Stop(ctx context.Context) error {
4846

4947
// PutBlock puts a block into the indexers in the group
5048
func (ig *SyncIndexers) PutBlock(ctx context.Context, blk *block.Block) error {
51-
for i, indexer := range ig.indexers {
52-
// check if the block is higher than the indexer's start height
53-
if blk.Height() < ig.startHeights[i] {
54-
continue
55-
}
49+
for _, indexer := range ig.indexers {
5650
// check if the block is higher than the indexer's height
5751
height, err := indexer.Height()
5852
if err != nil {
@@ -79,11 +73,6 @@ func (ig *SyncIndexers) DeleteTipBlock(ctx context.Context, blk *block.Block) er
7973
return nil
8074
}
8175

82-
// StartHeight returns the minimum start height of the indexers in the group
83-
func (ig *SyncIndexers) StartHeight() uint64 {
84-
return ig.minStartHeight
85-
}
86-
8776
// Height returns the minimum height of the indexers in the group
8877
func (ig *SyncIndexers) Height() (uint64, error) {
8978
var height uint64
@@ -98,28 +87,3 @@ func (ig *SyncIndexers) Height() (uint64, error) {
9887
}
9988
return height, nil
10089
}
101-
102-
// initStartHeight initializes the start height of the indexers in the group
103-
// for every indexer, the start height is the maximum of tipheight+1 and startheight
104-
func (ig *SyncIndexers) initStartHeight() error {
105-
ig.minStartHeight = 0
106-
ig.startHeights = make([]uint64, len(ig.indexers))
107-
for i, indexer := range ig.indexers {
108-
tipHeight, err := indexer.Height()
109-
if err != nil {
110-
return err
111-
}
112-
indexStartHeight := tipHeight + 1
113-
if indexerWithStart, ok := indexer.(blockdao.BlockIndexerWithStart); ok {
114-
startHeight := indexerWithStart.StartHeight()
115-
if startHeight > indexStartHeight {
116-
indexStartHeight = startHeight
117-
}
118-
}
119-
ig.startHeights[i] = indexStartHeight
120-
if i == 0 || indexStartHeight < ig.minStartHeight {
121-
ig.minStartHeight = indexStartHeight
122-
}
123-
}
124-
return nil
125-
}

blockindex/sync_indexers_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ func TestSyncIndexers_StartHeight(t *testing.T) {
6767
ig := NewSyncIndexers(indexers...)
6868
err := ig.Start(context.Background())
6969
require.NoError(err)
70-
height := ig.StartHeight()
71-
require.Equal(c.expect, height)
7270
})
7371
}
7472

systemcontractindex/common.go

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func (s *IndexerCommon) ContractAddress() string { return s.contractAddress }
6161

6262
// Height returns the tip block height
6363
func (s *IndexerCommon) Height() uint64 {
64+
if s.height < s.startHeight {
65+
return s.startHeight
66+
}
6467
return s.height
6568
}
6669

systemcontractindex/stakingindex/index.go

-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type (
3232
StakingIndexer interface {
3333
lifecycle.StartStopper
3434
Height() (uint64, error)
35-
StartHeight() uint64
3635
ContractAddress() string
3736
Buckets(height uint64) ([]*VoteBucket, error)
3837
Bucket(id uint64, height uint64) (*VoteBucket, bool, error)
@@ -90,13 +89,6 @@ func (s *Indexer) Height() (uint64, error) {
9089
return s.common.Height(), nil
9190
}
9291

93-
// StartHeight returns the start height of the indexer
94-
func (s *Indexer) StartHeight() uint64 {
95-
s.mutex.RLock()
96-
defer s.mutex.RUnlock()
97-
return s.common.StartHeight()
98-
}
99-
10092
// ContractAddress returns the contract address
10193
func (s *Indexer) ContractAddress() string {
10294
s.mutex.RLock()

0 commit comments

Comments
 (0)