Skip to content

Commit

Permalink
avoid nil indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankonly committed Mar 11, 2020
1 parent b81240e commit 6e268e1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
8 changes: 6 additions & 2 deletions blockchain/blockchain_test.go
Expand Up @@ -788,16 +788,18 @@ func TestLoadBlockchainfromDB(t *testing.T) {
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
require.NoError(rp.Register(registry))
var indexer blockindex.Indexer
var indexers []blockdao.BlockIndexer
if _, gateway := cfg.Plugins[config.GatewayPlugin]; gateway && !cfg.Chain.EnableAsyncIndexWrite {
// create indexer
cfg.DB.DbPath = cfg.Chain.IndexDBPath
indexer, err = blockindex.NewIndexer(db.NewBoltDB(cfg.DB), cfg.Genesis.Hash())
require.NoError(err)
indexers = append(indexers, indexer)
}
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String()
// create BlockDAO
cfg.DB.DbPath = cfg.Chain.ChainDBPath
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), []blockdao.BlockIndexer{indexer}, cfg.Chain.CompressBlock, cfg.DB)
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), indexers, cfg.Chain.CompressBlock, cfg.DB)
require.NotNil(dao)
bc := NewBlockchain(
cfg,
Expand Down Expand Up @@ -1490,16 +1492,18 @@ func newChain(t *testing.T, statetx bool) (Blockchain, factory.Factory, db.KVSto
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
require.NoError(rp.Register(registry))
var indexer blockindex.Indexer
var indexers []blockdao.BlockIndexer
if _, gateway := cfg.Plugins[config.GatewayPlugin]; gateway && !cfg.Chain.EnableAsyncIndexWrite {
// create indexer
cfg.DB.DbPath = cfg.Chain.IndexDBPath
indexer, err = blockindex.NewIndexer(db.NewBoltDB(cfg.DB), cfg.Genesis.Hash())
require.NoError(err)
indexers = append(indexers, indexer)
}
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String()
// create BlockDAO
cfg.DB.DbPath = cfg.Chain.ChainDBPath
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), []blockdao.BlockIndexer{indexer}, cfg.Chain.CompressBlock, cfg.DB)
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), indexers, cfg.Chain.CompressBlock, cfg.DB)
require.NotNil(dao)
bc := NewBlockchain(
cfg,
Expand Down
7 changes: 2 additions & 5 deletions blockchain/blockdao/blockdao.go
Expand Up @@ -12,7 +12,6 @@ import (
"io/ioutil"
"os"
"path"
"reflect"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -128,12 +127,10 @@ func NewBlockDAO(kvStore db.KVStore, indexers []BlockIndexer, compressBlock bool
compressBlock: compressBlock,
kvStore: kvStore,
cfg: cfg,
indexers: indexers,
}
for _, indexer := range indexers {
if indexer != nil && !reflect.ValueOf(indexer).IsNil() {
blockDAO.indexers = append(blockDAO.indexers, indexer)
blockDAO.lifecycle.Add(indexer)
}
blockDAO.lifecycle.Add(indexer)
}
if cfg.MaxCacheSize > 0 {
blockDAO.headerCache = cache.NewThreadSafeLruCache(cfg.MaxCacheSize)
Expand Down
9 changes: 1 addition & 8 deletions blockchain/blockdao/blockdao_test.go
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/systemlog"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/testutil"
)
Expand Down Expand Up @@ -290,7 +289,7 @@ func BenchmarkBlockCache(b *testing.B) {

db := config.Default.DB
db.MaxCacheSize = cacheSize
blkDao := NewBlockDAO(store, []BlockIndexer{nil}, false, db)
blkDao := NewBlockDAO(store, []BlockIndexer{}, false, db)
require.NoError(b, blkDao.Start(context.Background()))
defer func() {
require.NoError(b, blkDao.Stop(context.Background()))
Expand Down Expand Up @@ -333,9 +332,3 @@ func BenchmarkBlockCache(b *testing.B) {
test(0, b)
})
}

func TestNilIndexer(t *testing.T) {
var nilPointer *systemlog.Indexer
blkDao := NewBlockDAO(nil, []BlockIndexer{nil, nilPointer}, false, config.DB{})
require.Equal(t, 0, len(blkDao.(*blockDAO).indexers))
}

0 comments on commit 6e268e1

Please sign in to comment.