github.com/edxfund/validator@v1.8.16-0.20181020093046-c1def72855da/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/EDXFund/Validator/common" 21 "github.com/EDXFund/Validator/consensus" 22 "github.com/EDXFund/Validator/core/state" 23 "github.com/EDXFund/Validator/core/types" 24 "github.com/EDXFund/Validator/core/vm" 25 // "github.com/EDXFund/Validator/crypto" 26 "github.com/EDXFund/Validator/params" 27 ) 28 29 // StateProcessor is a basic Processor, which takes care of transitioning 30 // state from one point to another. 31 // 32 // StateProcessor implements Processor. 33 type StateProcessor struct { 34 config *params.ChainConfig // Chain configuration options 35 bc *BlockChain // Canonical block chain 36 engine consensus.Engine // Consensus engine used for block rewards 37 } 38 39 // NewStateProcessor initialises a new StateProcessor. 40 func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor { 41 return &StateProcessor{ 42 config: config, 43 bc: bc, 44 engine: engine, 45 } 46 } 47 48 // Process processes the state changes according to the Ethereum rules by running 49 // the transaction messages using the statedb and applying any rewards to both 50 // the processor (coinbase) and any included uncles. 51 // 52 // Process returns the receipts and logs accumulated during the process and 53 // returns the amount of gas that was used in the process. If any of the 54 // transactions failed to execute due to insufficient gas it will return an error. 55 func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.ContractResults, uint64, error) { 56 var ( 57 receipts types.ContractResults 58 usedGas = new(uint64) 59 header = block.Header() 60 //allLogs []*types.Log 61 //gp = new(GasPool).AddGas(block.GasLimit()) 62 ) 63 64 65 /* MUST TODO, add contract and token id managements routine 66 for i, tx := range block.Transactions() { 67 68 statedb.Prepare(tx.Hash(), block.Hash(), i) 69 receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg) 70 if err != nil { 71 //return nil, nil, 0, err 72 rejections = append(rejections, &types.RejectInfo{TxHash: tx.Hash(), Reason: 1}) 73 } 74 receipts = append(receipts, receipt) 75 allLogs = append(allLogs, receipt.Logs...) 76 } 77 78 //if master chain process blcokinfos and rejections 79 80 ///// MUST TODO blockInfos has already been included in block 81 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) 82 83 */ 84 p.engine.Finalize(p.bc, header, statedb, block.Transactions(),receipts) 85 86 87 return receipts, *usedGas, nil 88 } 89 90 91 func verifySigner() { 92 //this is unnessary, because tx with invalid signature would not be placed into pool 93 } 94 95 func callSC(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.ContractResult, uint64, error) { 96 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 97 if err != nil { 98 return nil, 0, err 99 } 100 // Create a new context to be used in the EVM environment 101 context := NewEVMContext(msg, header, bc, author) 102 // Create a new environment which holds all relevant information 103 // about the transaction and calling mechanisms. 104 vmenv := vm.NewEVM(context, statedb, config, cfg) 105 // Apply the transaction to the current state (included in the env) 106 _, gas, failed, err := ApplyMessage(vmenv, msg, gp) 107 if err != nil { 108 return nil, 0, err 109 } 110 // Update the state with pending changes 111 var root []byte 112 if config.IsByzantium(header.Number) { 113 statedb.Finalise(true) 114 } else { 115 root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 116 } 117 *usedGas += gas 118 119 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 120 // based on the eip phase, we're passing whether the root touch-delete accounts. 121 receipt := types.NewContractResult(root, failed, *usedGas) 122 receipt.TxHash = tx.Hash() 123 receipt.GasUsed = gas 124 // if the transaction created a contract, store the creation address in the receipt. 125 //if msg.To() == nil { 126 // receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 127 //} 128 // Set the receipt logs and create a bloom for filtering 129 //receipt.Logs = statedb.GetLogs(tx.Hash()) 130 //receipt.Bloom = types.CreateBloom(types.ContractResults{receipt}) 131 132 return receipt, gas, err 133 } 134 //if txType is 'T' : ApplyTransaction validate tx's signature, 135 //if TxType is 'C' : Synchronize data from master node and process data, then create contractResult 136 //if txType is "D" : it should be a token management tx 137 func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.ContractResult, uint64, error) { 138 139 switch tx.Type() { 140 141 case types.TT_TRANSACTION: 142 return nil,0,nil 143 case types.TT_CONTRACT: 144 return callSC(config , bc , author , gp , statedb , header , tx , usedGas, cfg ) 145 case types.TT_TOKEN: 146 return nil,0,nil 147 default: 148 return nil,0,ErrNoGenesis 149 } 150 151 }