state_processor.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "math/big"
  19. "github.com/ethereum/go-ethereum/core/state"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/logger"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. )
  26. var (
  27. big8 = big.NewInt(8)
  28. big32 = big.NewInt(32)
  29. )
  30. // StateProcessor is a basic Processor, which takes care of transitioning
  31. // state from one point to another.
  32. //
  33. // StateProcessor implements Processor.
  34. type StateProcessor struct {
  35. config *ChainConfig
  36. bc *BlockChain
  37. }
  38. // NewStateProcessor initialises a new StateProcessor.
  39. func NewStateProcessor(config *ChainConfig, bc *BlockChain) *StateProcessor {
  40. return &StateProcessor{
  41. config: config,
  42. bc: bc,
  43. }
  44. }
  45. // Process processes the state changes according to the Ethereum rules by running
  46. // the transaction messages using the statedb and applying any rewards to both
  47. // the processor (coinbase) and any included uncles.
  48. //
  49. // Process returns the receipts and logs accumulated during the process and
  50. // returns the amount of gas that was used in the process. If any of the
  51. // transactions failed to execute due to insufficient gas it will return an error.
  52. func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, vm.Logs, *big.Int, error) {
  53. var (
  54. receipts types.Receipts
  55. totalUsedGas = big.NewInt(0)
  56. err error
  57. header = block.Header()
  58. allLogs vm.Logs
  59. gp = new(GasPool).AddGas(block.GasLimit())
  60. )
  61. // Mutate the the block and state according to any hard-fork specs
  62. if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
  63. ApplyDAOHardFork(statedb)
  64. }
  65. // Iterate over and process the individual transactions
  66. for i, tx := range block.Transactions() {
  67. statedb.StartRecord(tx.Hash(), block.Hash(), i)
  68. receipt, logs, _, err := ApplyTransaction(p.config, p.bc, gp, statedb, header, tx, totalUsedGas, cfg)
  69. if err != nil {
  70. return nil, nil, totalUsedGas, err
  71. }
  72. receipts = append(receipts, receipt)
  73. allLogs = append(allLogs, logs...)
  74. }
  75. AccumulateRewards(statedb, header, block.Uncles())
  76. return receipts, allLogs, totalUsedGas, err
  77. }
  78. // ApplyTransaction attempts to apply a transaction to the given state database
  79. // and uses the input parameters for its environment.
  80. //
  81. // ApplyTransactions returns the generated receipts and vm logs during the
  82. // execution of the state transition phase.
  83. func ApplyTransaction(config *ChainConfig, bc *BlockChain, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, cfg vm.Config) (*types.Receipt, vm.Logs, *big.Int, error) {
  84. _, gas, err := ApplyMessage(NewEnv(statedb, config, bc, tx, header, cfg), tx, gp)
  85. if err != nil {
  86. return nil, nil, nil, err
  87. }
  88. // Update the state with pending changes
  89. usedGas.Add(usedGas, gas)
  90. receipt := types.NewReceipt(statedb.IntermediateRoot().Bytes(), usedGas)
  91. receipt.TxHash = tx.Hash()
  92. receipt.GasUsed = new(big.Int).Set(gas)
  93. if MessageCreatesContract(tx) {
  94. from, _ := tx.From()
  95. receipt.ContractAddress = crypto.CreateAddress(from, tx.Nonce())
  96. }
  97. logs := statedb.GetLogs(tx.Hash())
  98. receipt.Logs = logs
  99. receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
  100. glog.V(logger.Debug).Infoln(receipt)
  101. return receipt, logs, gas, err
  102. }
  103. // AccumulateRewards credits the coinbase of the given block with the
  104. // mining reward. The total reward consists of the static block reward
  105. // and rewards for included uncles. The coinbase of each uncle block is
  106. // also rewarded.
  107. func AccumulateRewards(statedb *state.StateDB, header *types.Header, uncles []*types.Header) {
  108. reward := new(big.Int).Set(BlockReward)
  109. r := new(big.Int)
  110. for _, uncle := range uncles {
  111. r.Add(uncle.Number, big8)
  112. r.Sub(r, header.Number)
  113. r.Mul(r, BlockReward)
  114. r.Div(r, big8)
  115. statedb.AddBalance(uncle.Coinbase, r)
  116. r.Div(BlockReward, big32)
  117. reward.Add(reward, r)
  118. }
  119. statedb.AddBalance(header.Coinbase, reward)
  120. }