state_processor.go 5.4 KB

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