state_processor.go 3.7 KB

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