state_processor.go 3.4 KB

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