github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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/pkg/errors" 21 "github.com/vntchain/go-vnt/common" 22 "github.com/vntchain/go-vnt/consensus" 23 "github.com/vntchain/go-vnt/core/state" 24 "github.com/vntchain/go-vnt/core/types" 25 "github.com/vntchain/go-vnt/core/vm" 26 "github.com/vntchain/go-vnt/crypto" 27 "github.com/vntchain/go-vnt/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 VNT rules by running 50 // the transaction messages using the statedb and applying any rewards to both 51 // the processor (coinbase). 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 *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { 57 var ( 58 receipts types.Receipts 59 usedGas = new(uint64) 60 header = block.Header() 61 allLogs []*types.Log 62 gp = new(GasPool).AddGas(block.GasLimit()) 63 ) 64 65 // Iterate over and process the individual transactions 66 for i, tx := range block.Transactions() { 67 statedb.Prepare(tx.Hash(), block.Hash(), i) 68 receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg) 69 if err != nil { 70 return nil, nil, 0, err 71 } 72 receipts = append(receipts, receipt) 73 allLogs = append(allLogs, receipt.Logs...) 74 } 75 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) 76 p.engine.Finalize(p.bc, header, statedb, block.Transactions(), receipts) 77 78 return receipts, allLogs, *usedGas, nil 79 } 80 81 // ApplyTransaction attempts to apply a transaction to the given state database 82 // and uses the input parameters for its environment. It returns the receipt 83 // for the transaction, gas used and an error if the transaction failed, 84 // indicating the block was invalid. 85 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.Receipt, uint64, error) { 86 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 87 if err != nil { 88 return nil, 0, err 89 } 90 // Create a new context to be used in the VM environment 91 context := NewVMContext(msg, header, bc, author) 92 // Create a new environment which holds all relevant information 93 // about the transaction and calling mechanisms. 94 var ( 95 gas uint64 96 failed bool 97 origin common.Address 98 ) 99 100 vmenv := GetVM(msg, context, statedb, config, cfg) 101 102 if vmenv == nil { 103 return nil, 0, errors.New("failed to call contract!") 104 } 105 106 _, gas, failed, err = ApplyMessage(vmenv, msg, gp) 107 if err != nil { 108 return nil, 0, err 109 } 110 origin = vmenv.GetContext().Origin 111 // Update the state with pending changes 112 var root []byte 113 statedb.Finalise(true) 114 *usedGas += gas 115 116 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 117 // based on the eip phase, we're passing wether the root touch-delete accounts. 118 receipt := types.NewReceipt(root, failed, *usedGas) 119 receipt.TxHash = tx.Hash() 120 receipt.GasUsed = gas 121 // if the transaction created a contract, store the creation address in the receipt. 122 if msg.To() == nil { 123 receipt.ContractAddress = crypto.CreateAddress(origin, tx.Nonce()) 124 } 125 // Set the receipt logs and create a bloom for filtering 126 receipt.Logs = statedb.GetLogs(tx.Hash()) 127 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 128 129 return receipt, gas, err 130 }