@@ -92,7 +92,7 @@ type BlockChain struct {
9292 currentBlock * types.Block // Current head of the block chain
9393 currentFastBlock * types.Block // Current head of the fast-sync chain (may be above the block chain!)
9494
95- stateCache * state.StateDB // State database to reuse between imports (contains state cache)
95+ stateCache state.Database // State database to reuse between imports (contains state cache)
9696 bodyCache * lru.Cache // Cache for the most recent block bodies
9797 bodyRLPCache * lru.Cache // Cache for the most recent block bodies in RLP encoded format
9898 blockCache * lru.Cache // Cache for the most recent entire blocks
@@ -125,6 +125,7 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, engine co
125125 bc := & BlockChain {
126126 config : config ,
127127 chainDb : chainDb ,
128+ stateCache : state .NewDatabase (chainDb ),
128129 eventMux : mux ,
129130 quit : make (chan struct {}),
130131 bodyCache : bodyCache ,
@@ -190,7 +191,7 @@ func (bc *BlockChain) loadLastState() error {
190191 return bc .Reset ()
191192 }
192193 // Make sure the state associated with the block is available
193- if _ , err := state .New (currentBlock .Root (), bc .chainDb ); err != nil {
194+ if _ , err := state .New (currentBlock .Root (), bc .stateCache ); err != nil {
194195 // Dangling block without a state associated, init from scratch
195196 log .Warn ("Head state missing, resetting chain" , "number" , currentBlock .Number (), "hash" , currentBlock .Hash ())
196197 return bc .Reset ()
@@ -214,12 +215,6 @@ func (bc *BlockChain) loadLastState() error {
214215 bc .currentFastBlock = block
215216 }
216217 }
217- // Initialize a statedb cache to ensure singleton account bloom filter generation
218- statedb , err := state .New (bc .currentBlock .Root (), bc .chainDb )
219- if err != nil {
220- return err
221- }
222- bc .stateCache = statedb
223218
224219 // Issue a status log for the user
225220 headerTd := bc .GetTd (currentHeader .Hash (), currentHeader .Number .Uint64 ())
@@ -261,7 +256,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
261256 bc .currentBlock = bc .GetBlock (currentHeader .Hash (), currentHeader .Number .Uint64 ())
262257 }
263258 if bc .currentBlock != nil {
264- if _ , err := state .New (bc .currentBlock .Root (), bc .chainDb ); err != nil {
259+ if _ , err := state .New (bc .currentBlock .Root (), bc .stateCache ); err != nil {
265260 // Rewound state missing, rolled back to before pivot, reset to genesis
266261 bc .currentBlock = nil
267262 }
@@ -384,7 +379,7 @@ func (bc *BlockChain) State() (*state.StateDB, error) {
384379
385380// StateAt returns a new mutable state based on a particular point in time.
386381func (bc * BlockChain ) StateAt (root common.Hash ) (* state.StateDB , error ) {
387- return bc . stateCache . New (root )
382+ return state . New (root , bc . stateCache )
388383}
389384
390385// Reset purges the entire blockchain, restoring it to its genesis state.
@@ -531,7 +526,7 @@ func (bc *BlockChain) HasBlockAndState(hash common.Hash) bool {
531526 return false
532527 }
533528 // Ensure the associated state is also present
534- _ , err := state . New (block .Root (), bc . chainDb )
529+ _ , err := bc . stateCache . OpenTrie (block .Root ())
535530 return err == nil
536531}
537532
@@ -959,31 +954,30 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
959954 }
960955 // Create a new statedb using the parent block and report an
961956 // error if it fails.
962- switch {
963- case i == 0 :
964- err = bc .stateCache . Reset ( bc . GetBlock (block .ParentHash (), block .NumberU64 ()- 1 ). Root () )
965- default :
966- err = bc . stateCache . Reset ( chain [i - 1 ]. Root ())
957+ var parent * types. Block
958+ if i == 0 {
959+ parent = bc .GetBlock (block .ParentHash (), block .NumberU64 ()- 1 )
960+ } else {
961+ parent = chain [i - 1 ]
967962 }
963+ state , err := state .New (parent .Root (), bc .stateCache )
968964 if err != nil {
969- bc .reportBlock (block , nil , err )
970965 return i , err
971966 }
972967 // Process block using the parent state as reference point.
973- receipts , logs , usedGas , err := bc .processor .Process (block , bc . stateCache , bc .vmConfig )
968+ receipts , logs , usedGas , err := bc .processor .Process (block , state , bc .vmConfig )
974969 if err != nil {
975970 bc .reportBlock (block , receipts , err )
976971 return i , err
977972 }
978973 // Validate the state using the default validator
979- err = bc .Validator ().ValidateState (block , bc . GetBlock ( block . ParentHash (), block . NumberU64 () - 1 ), bc . stateCache , receipts , usedGas )
974+ err = bc .Validator ().ValidateState (block , parent , state , receipts , usedGas )
980975 if err != nil {
981976 bc .reportBlock (block , receipts , err )
982977 return i , err
983978 }
984979 // Write state changes to database
985- _ , err = bc .stateCache .Commit (bc .config .IsEIP158 (block .Number ()))
986- if err != nil {
980+ if _ , err = state .CommitTo (bc .chainDb , bc .config .IsEIP158 (block .Number ())); err != nil {
987981 return i , err
988982 }
989983
@@ -1021,7 +1015,7 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
10211015 return i , err
10221016 }
10231017 // Write hash preimages
1024- if err := WritePreimages (bc .chainDb , block .NumberU64 (), bc . stateCache .Preimages ()); err != nil {
1018+ if err := WritePreimages (bc .chainDb , block .NumberU64 (), state .Preimages ()); err != nil {
10251019 return i , err
10261020 }
10271021 case SideStatTy :
0 commit comments