@@ -95,10 +95,11 @@ type BlockChain struct {
95
95
currentBlock * types.Block // Current head of the block chain
96
96
currentFastBlock * types.Block // Current head of the fast-sync chain (may be above the block chain!)
97
97
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
102
103
103
104
quit chan struct {} // blockchain quit channel
104
105
running int32 // running must be called atomically
@@ -198,11 +199,18 @@ func (self *BlockChain) loadLastState() error {
198
199
self .currentFastBlock = block
199
200
}
200
201
}
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
+
201
210
// Issue a status log and return
202
211
headerTd := self .GetTd (self .hc .CurrentHeader ().Hash ())
203
212
blockTd := self .GetTd (self .currentBlock .Hash ())
204
213
fastTd := self .GetTd (self .currentFastBlock .Hash ())
205
-
206
214
glog .V (logger .Info ).Infof ("Last header: #%d [%x…] TD=%v" , self .hc .CurrentHeader ().Number , self .hc .CurrentHeader ().Hash ().Bytes ()[:4 ], headerTd )
207
215
glog .V (logger .Info ).Infof ("Last block: #%d [%x…] TD=%v" , self .currentBlock .Number (), self .currentBlock .Hash ().Bytes ()[:4 ], blockTd )
208
216
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) {
817
825
tstart = time .Now ()
818
826
819
827
nonceChecked = make ([]bool , len (chain ))
820
- statedb * state.StateDB
821
828
)
822
829
823
830
// Start the parallel nonce verifier.
@@ -884,29 +891,30 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
884
891
885
892
// Create a new statedb using the parent block and report an
886
893
// 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 ())
891
899
}
892
900
if err != nil {
893
901
reportBlock (block , err )
894
902
return i , err
895
903
}
896
904
// 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 )
898
906
if err != nil {
899
907
reportBlock (block , err )
900
908
return i , err
901
909
}
902
910
// 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 )
904
912
if err != nil {
905
913
reportBlock (block , err )
906
914
return i , err
907
915
}
908
916
// Write state changes to database
909
- _ , err = statedb .Commit ()
917
+ _ , err = self . stateCache .Commit ()
910
918
if err != nil {
911
919
return i , err
912
920
}
0 commit comments