github.com/zjj1991/quorum@v0.0.0-20190524123704-ae4b0a1e1a19/core/state_processor.go (about) 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 17 package core 18 19 import ( 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/consensus" 22 "github.com/ethereum/go-ethereum/consensus/misc" 23 "github.com/ethereum/go-ethereum/core/state" 24 "github.com/ethereum/go-ethereum/core/types" 25 "github.com/ethereum/go-ethereum/core/vm" 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/params" 28 ) 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 *params.ChainConfig // Chain configuration options 36 bc *BlockChain // Canonical block chain 37 engine consensus.Engine // Consensus engine used for block rewards 38 } 39 40 // NewStateProcessor initialises a new StateProcessor. 41 func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor { 42 return &StateProcessor{ 43 config: config, 44 bc: bc, 45 engine: engine, 46 } 47 } 48 49 // Process processes the state changes according to the Ethereum rules by running 50 // the transaction messages using the statedb and applying any rewards to both 51 // the processor (coinbase) and any included uncles. 52 // 53 // Process returns the receipts and logs accumulated during the process and 54 // returns the amount of gas that was used in the process. If any of the 55 // transactions failed to execute due to insufficient gas it will return an error. 56 func (p *StateProcessor) Process(block *types.Block, statedb, privateState *state.StateDB, cfg vm.Config) (types.Receipts, types.Receipts, []*types.Log, uint64, error) { 57 58 var ( 59 receipts types.Receipts 60 usedGas = new(uint64) 61 header = block.Header() 62 allLogs []*types.Log 63 gp = new(GasPool).AddGas(block.GasLimit()) 64 65 privateReceipts types.Receipts 66 ) 67 // Mutate the block and state according to any hard-fork specs 68 if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { 69 misc.ApplyDAOHardFork(statedb) 70 } 71 // Iterate over and process the individual transactions 72 for i, tx := range block.Transactions() { 73 statedb.Prepare(tx.Hash(), block.Hash(), i) 74 privateState.Prepare(tx.Hash(), block.Hash(), i) 75 76 receipt, privateReceipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, privateState, header, tx, usedGas, cfg) 77 if err != nil { 78 return nil, nil, nil, 0, err 79 } 80 receipts = append(receipts, receipt) 81 allLogs = append(allLogs, receipt.Logs...) 82 83 // if the private receipt is nil this means the tx was public 84 // and we do not need to apply the additional logic. 85 if privateReceipt != nil { 86 privateReceipts = append(privateReceipts, privateReceipt) 87 allLogs = append(allLogs, privateReceipt.Logs...) 88 } 89 } 90 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) 91 p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts) 92 93 return receipts, privateReceipts, allLogs, *usedGas, nil 94 } 95 96 // ApplyTransaction attempts to apply a transaction to the given state database 97 // and uses the input parameters for its environment. It returns the receipt 98 // for the transaction, gas used and an error if the transaction failed, 99 // indicating the block was invalid. 100 func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common.Address, gp *GasPool, statedb, privateState *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.Receipt, uint64, error) { 101 if !config.IsQuorum || !tx.IsPrivate() { 102 privateState = statedb 103 } 104 105 // if config.IsQuorum && tx.GasPrice() != nil && tx.GasPrice().Cmp(common.Big0) > 0 { 106 // return nil, nil, 0, ErrInvalidGasPrice 107 // } 108 109 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 110 if err != nil { 111 return nil, nil, 0, err 112 } 113 // Create a new context to be used in the EVM environment 114 context := NewEVMContext(msg, header, bc, author) 115 // Create a new environment which holds all relevant information 116 // about the transaction and calling mechanisms. 117 vmenv := vm.NewEVM(context, statedb, privateState, config, cfg) 118 119 // Apply the transaction to the current state (included in the env) 120 _, gas, failed, err := ApplyMessage(vmenv, msg, gp) 121 if err != nil { 122 return nil, nil, 0, err 123 } 124 // Update the state with pending changes 125 var root []byte 126 if config.IsByzantium(header.Number) { 127 statedb.Finalise(true) 128 } else { 129 root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 130 } 131 *usedGas += gas 132 133 // If this is a private transaction, the public receipt should always 134 // indicate success. 135 publicFailed := !(config.IsQuorum && tx.IsPrivate()) && failed 136 137 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 138 // based on the eip phase, we're passing wether the root touch-delete accounts. 139 receipt := types.NewReceipt(root, publicFailed, *usedGas) 140 receipt.TxHash = tx.Hash() 141 receipt.GasUsed = gas 142 // if the transaction created a contract, store the creation address in the receipt. 143 if msg.To() == nil { 144 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 145 } 146 // Set the receipt logs and create a bloom for filtering 147 receipt.Logs = statedb.GetLogs(tx.Hash()) 148 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 149 150 var privateReceipt *types.Receipt 151 if config.IsQuorum && tx.IsPrivate() { 152 var privateRoot []byte 153 if config.IsByzantium(header.Number) { 154 privateState.Finalise(true) 155 } else { 156 privateRoot = privateState.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 157 } 158 privateReceipt = types.NewReceipt(privateRoot, failed, *usedGas) 159 privateReceipt.TxHash = tx.Hash() 160 privateReceipt.GasUsed = gas 161 if msg.To() == nil { 162 privateReceipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 163 } 164 165 privateReceipt.Logs = privateState.GetLogs(tx.Hash()) 166 privateReceipt.Bloom = types.CreateBloom(types.Receipts{privateReceipt}) 167 } 168 169 return receipt, privateReceipt, gas, err 170 }