github.com/Gessiux/neatchain@v1.3.1/chain/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 "math/big" 21 22 "github.com/Gessiux/neatchain/chain/consensus" 23 "github.com/Gessiux/neatchain/chain/core/state" 24 "github.com/Gessiux/neatchain/chain/core/types" 25 "github.com/Gessiux/neatchain/chain/core/vm" 26 "github.com/Gessiux/neatchain/chain/log" 27 "github.com/Gessiux/neatchain/params" 28 "github.com/Gessiux/neatchain/utilities/common" 29 "github.com/Gessiux/neatchain/utilities/crypto" 30 ) 31 32 // StateProcessor is a basic Processor, which takes care of transitioning 33 // state from one point to another. 34 // 35 // StateProcessor implements Processor. 36 type StateProcessor struct { 37 config *params.ChainConfig // Chain configuration options 38 bc *BlockChain // Canonical block chain 39 engine consensus.Engine // Consensus engine used for block rewards 40 cch CrossChainHelper 41 } 42 43 // NewStateProcessor initialises a new StateProcessor. 44 func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine, cch CrossChainHelper) *StateProcessor { 45 return &StateProcessor{ 46 config: config, 47 bc: bc, 48 engine: engine, 49 cch: cch, 50 } 51 } 52 53 // Process processes the state changes according to the Ethereum rules by running 54 // the transaction messages using the statedb and applying any rewards to both 55 // the processor (coinbase) and any included uncles. 56 // 57 // Process returns the receipts and logs accumulated during the process and 58 // returns the amount of gas that was used in the process. If any of the 59 // transactions failed to execute due to insufficient gas it will return an error. 60 func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, *types.PendingOps, error) { 61 var ( 62 receipts types.Receipts 63 usedGas = new(uint64) 64 header = block.Header() 65 allLogs []*types.Log 66 gp = new(GasPool).AddGas(block.GasLimit()) 67 ops = new(types.PendingOps) 68 ) 69 // Mutate the the block and state according to any hard-fork specs 70 //if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { 71 // misc.ApplyDAOHardFork(statedb) 72 //} 73 totalUsedMoney := big.NewInt(0) 74 // Iterate over and process the individual transactions 75 for i, tx := range block.Transactions() { 76 statedb.Prepare(tx.Hash(), block.Hash(), i) 77 //receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg) 78 receipt, _, err := ApplyTransactionEx(p.config, p.bc, nil, gp, statedb, ops, header, tx, 79 usedGas, totalUsedMoney, cfg, p.cch, false) 80 log.Debugf("(p *StateProcessor) Process(),after ApplyTransactionEx, receipt is %v\n", receipt) 81 if err != nil { 82 return nil, nil, 0, nil, err 83 } 84 receipts = append(receipts, receipt) 85 allLogs = append(allLogs, receipt.Logs...) 86 } 87 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) 88 _, err := p.engine.Finalize(p.bc, header, statedb, block.Transactions(), totalUsedMoney, block.Uncles(), receipts, ops) 89 if err != nil { 90 return nil, nil, 0, nil, err 91 } 92 93 return receipts, allLogs, *usedGas, ops, 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 *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error) { 101 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 102 if err != nil { 103 return nil, 0, err 104 } 105 // Create a new context to be used in the EVM environment 106 context := NewEVMContext(msg, header, bc, author) 107 // Create a new environment which holds all relevant information 108 // about the transaction and calling mechanisms. 109 vmenv := vm.NewEVM(context, statedb, config, cfg) 110 // Apply the transaction to the current state (included in the env) 111 _, gas, failed, err := ApplyMessage(vmenv, msg, gp) 112 if err != nil { 113 return nil, 0, err 114 } 115 // Update the state with pending changes 116 var root []byte 117 if config.IsByzantium(header.Number) { 118 statedb.Finalise(true) 119 } else { 120 root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 121 } 122 *usedGas += gas 123 124 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 125 // based on the eip phase, we're passing wether the root touch-delete accounts. 126 receipt := types.NewReceipt(root, failed, *usedGas) 127 receipt.TxHash = tx.Hash() 128 receipt.GasUsed = gas 129 // if the transaction created a contract, store the creation address in the receipt. 130 if msg.To() == nil { 131 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 132 } 133 // Set the receipt logs and create a bloom for filtering 134 receipt.Logs = statedb.GetLogs(tx.Hash()) 135 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 136 137 return receipt, gas, err 138 }