mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
core, miner: reuse journalled trie in miner too
This commit is contained in:
parent
15335b1660
commit
5cbe71a9c4
3 changed files with 28 additions and 3 deletions
|
|
@ -357,7 +357,12 @@ func (self *BlockChain) AuxValidator() pow.PoW { return self.pow }
|
||||||
|
|
||||||
// State returns a new mutable state based on the current HEAD block.
|
// State returns a new mutable state based on the current HEAD block.
|
||||||
func (self *BlockChain) State() (*state.StateDB, error) {
|
func (self *BlockChain) State() (*state.StateDB, error) {
|
||||||
return state.New(self.CurrentBlock().Root(), self.chainDb)
|
return self.StateAt(self.CurrentBlock().Root())
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateAt returns a new mutable state based on a particular point in time.
|
||||||
|
func (self *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
|
||||||
|
return self.stateCache.New(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset purges the entire blockchain, restoring it to its genesis state.
|
// Reset purges the entire blockchain, restoring it to its genesis state.
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,25 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new statedb by reusing any journalled tries to avoid costly
|
||||||
|
// disk io.
|
||||||
|
func (self *StateDB) New(root common.Hash) (*StateDB, error) {
|
||||||
|
tr, err := self.openTrie(root)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
csc, _ := lru.New(codeSizeCacheSize)
|
||||||
|
return &StateDB{
|
||||||
|
db: self.db,
|
||||||
|
trie: tr,
|
||||||
|
codeSizeCache: csc,
|
||||||
|
stateObjects: make(map[common.Address]*StateObject),
|
||||||
|
stateObjectsDirty: make(map[common.Address]struct{}),
|
||||||
|
refund: new(big.Int),
|
||||||
|
logs: make(map[common.Hash]vm.Logs),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Reset clears out all emphemeral state objects from the state db, but keeps
|
// Reset clears out all emphemeral state objects from the state db, but keeps
|
||||||
// the underlying state trie to avoid reloading data for the next operations.
|
// the underlying state trie to avoid reloading data for the next operations.
|
||||||
func (self *StateDB) Reset(root common.Hash) error {
|
func (self *StateDB) Reset(root common.Hash) error {
|
||||||
|
|
@ -110,7 +129,8 @@ func (self *StateDB) Reset(root common.Hash) error {
|
||||||
// from the journal if available.
|
// from the journal if available.
|
||||||
func (self *StateDB) openTrie(root common.Hash) (*trie.SecureTrie, error) {
|
func (self *StateDB) openTrie(root common.Hash) (*trie.SecureTrie, error) {
|
||||||
if self.trie != nil && self.trie.Hash() == root {
|
if self.trie != nil && self.trie.Hash() == root {
|
||||||
return self.trie, nil
|
cpy := *self.trie
|
||||||
|
return &cpy, nil
|
||||||
}
|
}
|
||||||
for i := len(self.pastTries) - 1; i >= 0; i-- {
|
for i := len(self.pastTries) - 1; i >= 0; i-- {
|
||||||
if self.pastTries[i].Hash() == root {
|
if self.pastTries[i].Hash() == root {
|
||||||
|
|
|
||||||
|
|
@ -361,7 +361,7 @@ func (self *worker) push(work *Work) {
|
||||||
|
|
||||||
// makeCurrent creates a new environment for the current cycle.
|
// makeCurrent creates a new environment for the current cycle.
|
||||||
func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error {
|
func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error {
|
||||||
state, err := state.New(parent.Root(), self.eth.ChainDb())
|
state, err := self.chain.StateAt(parent.Root())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue