github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state_processor.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package core
    13  
    14  import (
    15  	"github.com/Sberex/go-sberex/common"
    16  	"github.com/Sberex/go-sberex/consensus"
    17  	"github.com/Sberex/go-sberex/consensus/misc"
    18  	"github.com/Sberex/go-sberex/core/state"
    19  	"github.com/Sberex/go-sberex/core/types"
    20  	"github.com/Sberex/go-sberex/core/vm"
    21  	"github.com/Sberex/go-sberex/crypto"
    22  	"github.com/Sberex/go-sberex/params"
    23  )
    24  
    25  // StateProcessor is a basic Processor, which takes care of transitioning
    26  // state from one point to another.
    27  //
    28  // StateProcessor implements Processor.
    29  type StateProcessor struct {
    30  	config *params.ChainConfig // Chain configuration options
    31  	bc     *BlockChain         // Canonical block chain
    32  	engine consensus.Engine    // Consensus engine used for block rewards
    33  }
    34  
    35  // NewStateProcessor initialises a new StateProcessor.
    36  func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor {
    37  	return &StateProcessor{
    38  		config: config,
    39  		bc:     bc,
    40  		engine: engine,
    41  	}
    42  }
    43  
    44  // Process processes the state changes according to the Sberex rules by running
    45  // the transaction messages using the statedb and applying any rewards to both
    46  // the processor (coinbase) and any included uncles.
    47  //
    48  // Process returns the receipts and logs accumulated during the process and
    49  // returns the amount of gas that was used in the process. If any of the
    50  // transactions failed to execute due to insufficient gas it will return an error.
    51  func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
    52  	var (
    53  		receipts types.Receipts
    54  		usedGas  = new(uint64)
    55  		header   = block.Header()
    56  		allLogs  []*types.Log
    57  		gp       = new(GasPool).AddGas(block.GasLimit())
    58  	)
    59  	// Mutate the the block and state according to any hard-fork specs
    60  	if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
    61  		misc.ApplyDAOHardFork(statedb)
    62  	}
    63  	// Iterate over and process the individual transactions
    64  	for i, tx := range block.Transactions() {
    65  		statedb.Prepare(tx.Hash(), block.Hash(), i)
    66  		receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg)
    67  		if err != nil {
    68  			return nil, nil, 0, err
    69  		}
    70  		receipts = append(receipts, receipt)
    71  		allLogs = append(allLogs, receipt.Logs...)
    72  	}
    73  	// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
    74  	p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts)
    75  
    76  	return receipts, allLogs, *usedGas, nil
    77  }
    78  
    79  // ApplyTransaction attempts to apply a transaction to the given state database
    80  // and uses the input parameters for its environment. It returns the receipt
    81  // for the transaction, gas used and an error if the transaction failed,
    82  // indicating the block was invalid.
    83  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) {
    84  	msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
    85  	if err != nil {
    86  		return nil, 0, err
    87  	}
    88  	// Create a new context to be used in the EVM environment
    89  	context := NewEVMContext(msg, header, bc, author)
    90  	// Create a new environment which holds all relevant information
    91  	// about the transaction and calling mechanisms.
    92  	vmenv := vm.NewEVM(context, statedb, config, cfg)
    93  	// Apply the transaction to the current state (included in the env)
    94  	_, gas, failed, err := ApplyMessage(vmenv, msg, gp)
    95  	if err != nil {
    96  		return nil, 0, err
    97  	}
    98  	// Update the state with pending changes
    99  	var root []byte
   100  	if config.IsByzantium(header.Number) {
   101  		statedb.Finalise(true)
   102  	} else {
   103  		root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
   104  	}
   105  	*usedGas += gas
   106  
   107  	// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
   108  	// based on the eip phase, we're passing wether the root touch-delete accounts.
   109  	receipt := types.NewReceipt(root, failed, *usedGas)
   110  	receipt.TxHash = tx.Hash()
   111  	receipt.GasUsed = gas
   112  	// if the transaction created a contract, store the creation address in the receipt.
   113  	if msg.To() == nil {
   114  		receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
   115  	}
   116  	// Set the receipt logs and create a bloom for filtering
   117  	receipt.Logs = statedb.GetLogs(tx.Hash())
   118  	receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
   119  
   120  	return receipt, gas, err
   121  }