state_processor.go 5.2 KB

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