github.com/aquanetwork/aquachain@v1.7.8/core/state_processor.go (about) 1 // Copyright 2015 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "gitlab.com/aquachain/aquachain/common" 21 "gitlab.com/aquachain/aquachain/common/log" 22 "gitlab.com/aquachain/aquachain/consensus" 23 "gitlab.com/aquachain/aquachain/consensus/misc" 24 "gitlab.com/aquachain/aquachain/core/state" 25 "gitlab.com/aquachain/aquachain/core/types" 26 "gitlab.com/aquachain/aquachain/core/vm" 27 "gitlab.com/aquachain/aquachain/crypto" 28 "gitlab.com/aquachain/aquachain/params" 29 ) 30 31 // StateProcessor is a basic Processor, which takes care of transitioning 32 // state from one point to another. 33 // 34 // StateProcessor implements Processor. 35 type StateProcessor struct { 36 config *params.ChainConfig // Chain configuration options 37 bc *BlockChain // Canonical block chain 38 engine consensus.Engine // Consensus engine used for block rewards 39 } 40 41 // NewStateProcessor initialises a new StateProcessor. 42 func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor { 43 return &StateProcessor{ 44 config: config, 45 bc: bc, 46 engine: engine, 47 } 48 } 49 50 // Process processes the state changes according to the AquaChain rules by running 51 // the transaction messages using the statedb and applying any rewards to both 52 // the processor (coinbase) and any included uncles. 53 // 54 // Process returns the receipts and logs accumulated during the process and 55 // returns the amount of gas that was used in the process. If any of the 56 // transactions failed to execute due to insufficient gas it will return an error. 57 func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { 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 66 header.Version = p.config.GetBlockVersion(header.Number) 67 68 // Mutate the the block and state according to any hard-fork specs 69 if hf4 := p.config.GetHF(4); hf4 != nil && hf4.Cmp(header.Number) == 0 { 70 log.Info("Activating Hardfork", "HF", 4, "BlockNumber", p.config.GetHF(4)) 71 misc.ApplyHardFork4(statedb) 72 } 73 74 if hf5 := p.config.GetHF(5); hf5 != nil && hf5.Cmp(header.Number) == 0 { 75 misc.ApplyHardFork5(statedb) 76 } 77 // Iterate over and process the individual transactions 78 for i, tx := range block.Transactions() { 79 statedb.Prepare(tx.Hash(), block.Hash(), i) 80 receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg) 81 if err != nil { 82 return nil, nil, 0, 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 p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts) 89 90 return receipts, allLogs, *usedGas, nil 91 } 92 93 // ApplyTransaction attempts to apply a transaction to the given state database 94 // and uses the input parameters for its environment. It returns the receipt 95 // for the transaction, gas used and an error if the transaction failed, 96 // indicating the block was invalid. 97 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) { 98 msg, err := tx.AsMessage(types.MakeSigner(config, header.Number)) 99 if err != nil { 100 return nil, 0, err 101 } 102 // Create a new context to be used in the EVM environment 103 context := NewEVMContext(msg, header, bc, author) 104 // Create a new environment which holds all relevant information 105 // about the transaction and calling mechanisms. 106 vmenv := vm.NewEVM(context, statedb, config, cfg) 107 // Apply the transaction to the current state (included in the env) 108 _, gas, failed, err := ApplyMessage(vmenv, msg, gp) 109 if err != nil { 110 return nil, 0, err 111 } 112 // Update the state with pending changes 113 var root []byte 114 if config.IsByzantium(header.Number) { 115 statedb.Finalise(true) 116 } else { 117 root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() 118 } 119 *usedGas += gas 120 121 // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx 122 // based on the eip phase, we're passing wether the root touch-delete accounts. 123 receipt := types.NewReceipt(root, failed, *usedGas) 124 receipt.TxHash = tx.Hash() 125 receipt.GasUsed = gas 126 // if the transaction created a contract, store the creation address in the receipt. 127 if msg.To() == nil { 128 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce()) 129 } 130 // Set the receipt logs and create a bloom for filtering 131 receipt.Logs = statedb.GetLogs(tx.Hash()) 132 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 133 134 return receipt, gas, err 135 }