core, miner: Add separate SetBlockContext function so mining rewards are cached

This commit is contained in:
Nick Johnson 2016-10-26 11:07:35 +01:00
parent cb3e606012
commit 6d4b9987d3
5 changed files with 14 additions and 11 deletions

View file

@ -922,6 +922,7 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
reportBlock(block, err)
return i, err
}
self.stateCache.SetBlockContext(block.Hash(), block.NumberU64(), self)
// Process block using the parent state as reference point.
receipts, logs, usedGas, err := self.processor.Process(block, self.stateCache, self.config.VmConfig)
if err != nil {

View file

@ -105,7 +105,7 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
if b.gasPool == nil {
b.SetCoinbase(common.Address{})
}
b.statedb.SetTxContext(common.Hash{}, b.header.Number.Uint64(), tx.Hash(), len(b.txs), nil)
b.statedb.SetTxContext(tx.Hash(), len(b.txs))
receipt, _, _, err := ApplyTransaction(MakeChainConfig(), nil, b.gasPool, b.statedb, b.header, tx, b.header.GasUsed, vm.Config{})
if err != nil {
panic(err)

View file

@ -107,7 +107,7 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
refund: new(big.Int),
logs: make(map[common.Hash]vm.Logs),
}
ret.SetTxContext(common.Hash{}, 0, common.Hash{}, 0, nil)
ret.SetBlockContext(common.Hash{}, 0, nil)
return ret, nil
}
@ -130,7 +130,7 @@ func (self *StateDB) New(root common.Hash) (*StateDB, error) {
refund: new(big.Int),
logs: make(map[common.Hash]vm.Logs),
}
ret.SetTxContext(common.Hash{}, 0, common.Hash{}, 0, nil)
ret.SetBlockContext(common.Hash{}, 0, nil)
return ret, nil
}
@ -153,8 +153,8 @@ func (self *StateDB) Reset(root common.Hash) error {
self.logs = make(map[common.Hash]vm.Logs)
self.logSize = 0
self.clearJournalAndRefund()
self.SetTxContext(common.Hash{}, 0, common.Hash{}, 0, nil)
self.SetBlockContext(common.Hash{}, 0, nil)
self.SetTxContext(common.Hash{}, 0)
return nil
}
@ -183,11 +183,8 @@ func (self *StateDB) pushTrie(t *trie.Trie) {
}
}
func (self *StateDB) SetTxContext(blockHash common.Hash, blockNum uint64, txHash common.Hash, txIndex int, validator trie.CacheValidator) {
func (self *StateDB) SetBlockContext(blockHash common.Hash, blockNum uint64, validator trie.CacheValidator) {
self.bhash = blockHash
self.thash = txHash
self.txIndex = txIndex
if validator == nil {
validator = &trie.NullCacheValidator{}
}
@ -195,6 +192,11 @@ func (self *StateDB) SetTxContext(blockHash common.Hash, blockNum uint64, txHash
self.storage = trie.NewSecure(storage, self.db)
}
func (self *StateDB) SetTxContext(txHash common.Hash, txIndex int) {
self.thash = txHash
self.txIndex = txIndex
}
func (self *StateDB) AddLog(log *vm.Log) {
self.journal = append(self.journal, addLogChange{txhash: self.thash})

View file

@ -71,7 +71,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
}
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
statedb.SetTxContext(block.Hash(), block.NumberU64(), tx.Hash(), i, p.bc)
statedb.SetTxContext(tx.Hash(), i)
receipt, logs, _, err := ApplyTransaction(p.config, p.bc, gp, statedb, header, tx, totalUsedGas, cfg)
if err != nil {
return nil, nil, totalUsedGas, err

View file

@ -583,7 +583,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB
continue
}
// Start executing the transaction
env.state.SetTxContext(common.Hash{}, env.header.Number.Uint64(), tx.Hash(), env.tcount, bc)
env.state.SetTxContext(tx.Hash(), env.tcount)
err, logs := env.commitTransaction(tx, bc, gp)
switch {