github.com/aswedchain/aswed@v1.0.1/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/aswedchain/aswed/common" 21 "github.com/aswedchain/aswed/consensus" 22 "github.com/aswedchain/aswed/core/state" 23 "github.com/aswedchain/aswed/core/types" 24 "github.com/aswedchain/aswed/core/vm" 25 "github.com/aswedchain/aswed/crypto" 26 "github.com/aswedchain/aswed/log" 27 "github.com/aswedchain/aswed/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 *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { 57 var ( 58 receipts = make([]*types.Receipt, 0) 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 posa, isPoSA := p.engine.(consensus.PoSA) 67 if isPoSA { 68 if err := posa.PreHandle(p.bc, header, statedb); err != nil { 69 return nil, nil, 0, err 70 } 71 } 72 commonTxs := make([]*types.Transaction, 0, len(block.Transactions())) 73 systemTxs := make([]*types.Transaction, 0) 74 for i, tx := range block.Transactions() { 75 if isPoSA { 76 ok, err := posa.IsSysTransaction(tx, header) 77 if err != nil { 78 return nil, nil, 0, err 79 } 80 if ok { 81 systemTxs = append(systemTxs, tx) 82 continue 83 } 84 err = posa.ValidateTx(tx, header, statedb) 85 if err != nil { 86 return nil, nil, 0, err 87 } 88 } 89 statedb.Prepare(tx.Hash(), block.Hash(), i) 90 receipt, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg) 91 if err != nil { 92 return nil, nil, 0, err 93 } 94 receipts = append(receipts, receipt) 95 allLogs = append(allLogs, receipt.Logs...) 96 commonTxs = append(commonTxs, tx) 97 } 98 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) 99 if err := p.engine.Finalize(p.bc, header, statedb, &commonTxs, block.Uncles(), &receipts, systemTxs); err != nil { 100 return nil, nil, 0, err 101 } 102 103 return receipts, allLogs, *usedGas, nil 104 } 105 106 // ApplyTransaction attempts to apply a transaction to the given state database 107 // and uses the input parameters for its environment. It returns the receipt 108 // for the transaction, gas used and an error if the transaction failed, 109 // indicating the block was invalid. 110 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, error) { 111 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 112 if err != nil { 113 return nil, err 114 } 115 // Create a new context to be used in the EVM environment 116 context := NewEVMContext(msg, header, bc, author) 117 // Create a new environment which holds all relevant information 118 // about the transaction and calling mechanisms. 119 vmenv := vm.NewEVM(context, statedb, config, cfg) 120 // Apply the transaction to the current state (included in the env) 121 result, err := ApplyMessage(vmenv, msg, gp) 122 if err != nil { 123 return nil, err 124 } 125 // Update the state with pending changes 126 var root []byte 127 if config.IsByzantium(header.Number) { 128 statedb.Finalise(true) 129 } else { 130 root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 131 } 132 *usedGas += result.UsedGas 133 134 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 135 // based on the eip phase, we're passing whether the root touch-delete accounts. 136 receipt := types.NewReceipt(root, result.Failed(), *usedGas) 137 receipt.TxHash = tx.Hash() 138 receipt.GasUsed = result.UsedGas 139 // if the transaction created a contract, store the creation address in the receipt. 140 if msg.To() == nil { 141 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 142 } 143 // Set the receipt logs and create a bloom for filtering 144 receipt.Logs = statedb.GetLogs(tx.Hash()) 145 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 146 receipt.BlockHash = statedb.BlockHash() 147 receipt.BlockNumber = header.Number 148 receipt.TransactionIndex = uint(statedb.TxIndex()) 149 150 if result.Failed() { 151 log.Debug("apply transaction with evm error", "txHash", tx.Hash().String(), "vmErr", result.Err) 152 } 153 154 return receipt, err 155 }