@@ -95,10 +95,11 @@ type BlockChain struct {
9595 currentBlock * types.Block // Current head of the block chain
9696 currentFastBlock * types.Block // Current head of the fast-sync chain (may be above the block chain!)
9797
98- bodyCache * lru.Cache // Cache for the most recent block bodies
99- bodyRLPCache * lru.Cache // Cache for the most recent block bodies in RLP encoded format
100- blockCache * lru.Cache // Cache for the most recent entire blocks
101- futureBlocks * lru.Cache // future blocks are blocks added for later processing
98+ stateCache * state.StateDB // State database to reuse between imports (contains state cache)
99+ bodyCache * lru.Cache // Cache for the most recent block bodies
100+ bodyRLPCache * lru.Cache // Cache for the most recent block bodies in RLP encoded format
101+ blockCache * lru.Cache // Cache for the most recent entire blocks
102+ futureBlocks * lru.Cache // future blocks are blocks added for later processing
102103
103104 quit chan struct {} // blockchain quit channel
104105 running int32 // running must be called atomically
@@ -198,11 +199,18 @@ func (self *BlockChain) loadLastState() error {
198199 self .currentFastBlock = block
199200 }
200201 }
202+ // Initialize a statedb cache to ensure singleton account bloom filter generation
203+ statedb , err := state .New (self .currentBlock .Root (), self .chainDb )
204+ if err != nil {
205+ return err
206+ }
207+ self .stateCache = statedb
208+ self .stateCache .GetAccount (common.Address {})
209+
201210 // Issue a status log and return
202211 headerTd := self .GetTd (self .hc .CurrentHeader ().Hash ())
203212 blockTd := self .GetTd (self .currentBlock .Hash ())
204213 fastTd := self .GetTd (self .currentFastBlock .Hash ())
205-
206214 glog .V (logger .Info ).Infof ("Last header: #%d [%x…] TD=%v" , self .hc .CurrentHeader ().Number , self .hc .CurrentHeader ().Hash ().Bytes ()[:4 ], headerTd )
207215 glog .V (logger .Info ).Infof ("Last block: #%d [%x…] TD=%v" , self .currentBlock .Number (), self .currentBlock .Hash ().Bytes ()[:4 ], blockTd )
208216 glog .V (logger .Info ).Infof ("Fast block: #%d [%x…] TD=%v" , self .currentFastBlock .Number (), self .currentFastBlock .Hash ().Bytes ()[:4 ], fastTd )
@@ -817,7 +825,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
817825 tstart = time .Now ()
818826
819827 nonceChecked = make ([]bool , len (chain ))
820- statedb * state.StateDB
821828 )
822829
823830 // Start the parallel nonce verifier.
@@ -884,29 +891,30 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
884891
885892 // Create a new statedb using the parent block and report an
886893 // error if it fails.
887- if statedb == nil {
888- statedb , err = state .New (self .GetBlock (block .ParentHash ()).Root (), self .chainDb )
889- } else {
890- err = statedb .Reset (chain [i - 1 ].Root ())
894+ switch {
895+ case i == 0 :
896+ err = self .stateCache .Reset (self .GetBlock (block .ParentHash ()).Root ())
897+ default :
898+ err = self .stateCache .Reset (chain [i - 1 ].Root ())
891899 }
892900 if err != nil {
893901 reportBlock (block , err )
894902 return i , err
895903 }
896904 // Process block using the parent state as reference point.
897- receipts , logs , usedGas , err := self .processor .Process (block , statedb , self .config .VmConfig )
905+ receipts , logs , usedGas , err := self .processor .Process (block , self . stateCache , self .config .VmConfig )
898906 if err != nil {
899907 reportBlock (block , err )
900908 return i , err
901909 }
902910 // Validate the state using the default validator
903- err = self .Validator ().ValidateState (block , self .GetBlock (block .ParentHash ()), statedb , receipts , usedGas )
911+ err = self .Validator ().ValidateState (block , self .GetBlock (block .ParentHash ()), self . stateCache , receipts , usedGas )
904912 if err != nil {
905913 reportBlock (block , err )
906914 return i , err
907915 }
908916 // Write state changes to database
909- _ , err = statedb .Commit ()
917+ _ , err = self . stateCache .Commit ()
910918 if err != nil {
911919 return i , err
912920 }
0 commit comments