Skip to content

Commit 524f819

Browse files
committed
added some nil checks for cache (testing specific)
1 parent 6657d54 commit 524f819

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎core/chain_manager.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,7 @@ func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *Chai
106106
// Take ownership of this particular state
107107
bc.txState = state.ManageState(bc.State().Copy())
108108

109-
// load in last `blockCacheLimit` - 1 blocks. Last block is the current.
110-
ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
111-
ancestors = append(ancestors, bc.currentBlock)
112-
for _, block := range ancestors {
113-
bc.cache.Push(block)
114-
}
109+
bc.makeCache()
115110

116111
go bc.update()
117112

@@ -194,6 +189,18 @@ func (bc *ChainManager) setLastBlock() {
194189
chainlogger.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td)
195190
}
196191

192+
func (bc *ChainManager) makeCache() {
193+
if bc.cache == nil {
194+
bc.cache = NewBlockCache(blockCacheLimit)
195+
}
196+
// load in last `blockCacheLimit` - 1 blocks. Last block is the current.
197+
ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
198+
ancestors = append(ancestors, bc.currentBlock)
199+
for _, block := range ancestors {
200+
bc.cache.Push(block)
201+
}
202+
}
203+
197204
// Block creation & chain handling
198205
func (bc *ChainManager) NewBlock(coinbase common.Address) *types.Block {
199206
bc.mu.RLock()
@@ -240,10 +247,15 @@ func (bc *ChainManager) Reset() {
240247
bc.removeBlock(block)
241248
}
242249

250+
if bc.cache == nil {
251+
bc.cache = NewBlockCache(blockCacheLimit)
252+
}
253+
243254
// Prepare the genesis block
244255
bc.write(bc.genesisBlock)
245256
bc.insert(bc.genesisBlock)
246257
bc.currentBlock = bc.genesisBlock
258+
bc.makeCache()
247259

248260
bc.setTotalDifficulty(common.Big("0"))
249261
}
@@ -265,6 +277,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
265277
bc.write(bc.genesisBlock)
266278
bc.insert(bc.genesisBlock)
267279
bc.currentBlock = bc.genesisBlock
280+
bc.makeCache()
268281
}
269282

270283
// Export writes the active chain to the given writer.

0 commit comments

Comments
 (0)