From 524f8199bfaddebc96798827e943667a909a03e7 Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Mon, 23 Mar 2015 12:22:58 +0100
Subject: [PATCH] added some nil checks for cache (testing specific)
---
 core/chain_manager.go | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/core/chain_manager.go b/core/chain_manager.go
index 1bc8edea6..755487900 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -106,12 +106,7 @@ func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *Chai
 	// Take ownership of this particular state
 	bc.txState = state.ManageState(bc.State().Copy())
 
-	// load in last `blockCacheLimit` - 1 blocks. Last block is the current.
-	ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
-	ancestors = append(ancestors, bc.currentBlock)
-	for _, block := range ancestors {
-		bc.cache.Push(block)
-	}
+	bc.makeCache()
 
 	go bc.update()
 
@@ -194,6 +189,18 @@ func (bc *ChainManager) setLastBlock() {
 	chainlogger.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td)
 }
 
+func (bc *ChainManager) makeCache() {
+	if bc.cache == nil {
+		bc.cache = NewBlockCache(blockCacheLimit)
+	}
+	// load in last `blockCacheLimit` - 1 blocks. Last block is the current.
+	ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
+	ancestors = append(ancestors, bc.currentBlock)
+	for _, block := range ancestors {
+		bc.cache.Push(block)
+	}
+}
+
 // Block creation & chain handling
 func (bc *ChainManager) NewBlock(coinbase common.Address) *types.Block {
 	bc.mu.RLock()
@@ -240,10 +247,15 @@ func (bc *ChainManager) Reset() {
 		bc.removeBlock(block)
 	}
 
+	if bc.cache == nil {
+		bc.cache = NewBlockCache(blockCacheLimit)
+	}
+
 	// Prepare the genesis block
 	bc.write(bc.genesisBlock)
 	bc.insert(bc.genesisBlock)
 	bc.currentBlock = bc.genesisBlock
+	bc.makeCache()
 
 	bc.setTotalDifficulty(common.Big("0"))
 }
@@ -265,6 +277,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
 	bc.write(bc.genesisBlock)
 	bc.insert(bc.genesisBlock)
 	bc.currentBlock = bc.genesisBlock
+	bc.makeCache()
 }
 
 // Export writes the active chain to the given writer.
-- 
GitLab