state_processor.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/consensus"
  20. "github.com/ethereum/go-ethereum/consensus/misc"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/core/vm"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // StateProcessor is a basic Processor, which takes care of transitioning
  28. // state from one point to another.
  29. //
  30. // StateProcessor implements Processor.
  31. type StateProcessor struct {
  32. config *params.ChainConfig // Chain configuration options
  33. bc *BlockChain // Canonical block chain
  34. engine consensus.Engine // Consensus engine used for block rewards
  35. }
  36. // NewStateProcessor initialises a new StateProcessor.
  37. func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor {
  38. return &StateProcessor{
  39. config: config,
  40. bc: bc,
  41. engine: engine,
  42. }
  43. }
  44. // Process processes the state changes according to the Ethereum rules by running
  45. // the transaction messages using the statedb and applying any rewards to both
  46. // the processor (coinbase) and any included uncles.
  47. //
  48. // Process returns the receipts and logs accumulated during the process and
  49. // returns the amount of gas that was used in the process. If any of the
  50. // transactions failed to execute due to insufficient gas it will return an error.
  51. func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
  52. var (
  53. receipts types.Receipts
  54. usedGas = new(uint64)
  55. header = block.Header()
  56. allLogs []*types.Log
  57. gp = new(GasPool).AddGas(block.GasLimit())
  58. )
  59. // Mutate the the block and state according to any hard-fork specs
  60. if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
  61. misc.ApplyDAOHardFork(statedb)
  62. }
  63. // Iterate over and process the individual transactions
  64. for i, tx := range block.Transactions() {
  65. statedb.Prepare(tx.Hash(), block.Hash(), i)
  66. receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg)
  67. if err != nil {
  68. return nil, nil, 0, err
  69. }
  70. receipts = append(receipts, receipt)
  71. allLogs = append(allLogs, receipt.Logs...)
  72. }
  73. // Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
  74. p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts)
  75. return receipts, allLogs, *usedGas, nil
  76. }
  77. // ApplyTransaction attempts to apply a transaction to the given state database
  78. // and uses the input parameters for its environment. It returns the receipt
  79. // for the transaction, gas used and an error if the transaction failed,
  80. // indicating the block was invalid.
  81. func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error) {
  82. msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
  83. if err != nil {
  84. return nil, 0, err
  85. }
  86. // Create a new context to be used in the EVM environment
  87. context := NewEVMContext(msg, header, bc, author)
  88. // Create a new environment which holds all relevant information
  89. // about the transaction and calling mechanisms.
  90. vmenv := vm.NewEVM(context, statedb, config, cfg)
  91. // Apply the transaction to the current state (included in the env)
  92. _, gas, failed, err := ApplyMessage(vmenv, msg, gp)
  93. if err != nil {
  94. return nil, 0, err
  95. }
  96. // Update the state with pending changes
  97. var root []byte
  98. if config.IsByzantium(header.Number) {
  99. statedb.Finalise(true)
  100. } else {
  101. root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
  102. }
  103. *usedGas += gas
  104. // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
  105. // based on the eip phase, we're passing wether the root touch-delete accounts.
  106. receipt := types.NewReceipt(root, failed, *usedGas)
  107. receipt.TxHash = tx.Hash()
  108. receipt.GasUsed = gas
  109. // if the transaction created a contract, store the creation address in the receipt.
  110. if msg.To() == nil {
  111. receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
  112. }
  113. // Set the receipt logs and create a bloom for filtering
  114. receipt.Logs = statedb.GetLogs(tx.Hash())
  115. receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
  116. return receipt, gas, err
  117. }