github.com/corverroos/quorum@v21.1.0+incompatible/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/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/consensus"
    22  	"github.com/ethereum/go-ethereum/consensus/misc"
    23  	"github.com/ethereum/go-ethereum/core/state"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/ethereum/go-ethereum/params"
    28  	"github.com/ethereum/go-ethereum/permission/core"
    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 Ethereum 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, privateState *state.StateDB, cfg vm.Config) (types.Receipts, types.Receipts, []*types.Log, uint64, error) {
    58  
    59  	var (
    60  		receipts types.Receipts
    61  		usedGas  = new(uint64)
    62  		header   = block.Header()
    63  		allLogs  []*types.Log
    64  		gp       = new(GasPool).AddGas(block.GasLimit())
    65  
    66  		privateReceipts types.Receipts
    67  	)
    68  	// Mutate the block and state according to any hard-fork specs
    69  	if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
    70  		misc.ApplyDAOHardFork(statedb)
    71  	}
    72  	// Iterate over and process the individual transactions
    73  	for i, tx := range block.Transactions() {
    74  		statedb.Prepare(tx.Hash(), block.Hash(), i)
    75  		privateState.Prepare(tx.Hash(), block.Hash(), i)
    76  
    77  		receipt, privateReceipt, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, privateState, header, tx, usedGas, cfg)
    78  		if err != nil {
    79  			return nil, nil, nil, 0, err
    80  		}
    81  		receipts = append(receipts, receipt)
    82  		allLogs = append(allLogs, receipt.Logs...)
    83  
    84  		// if the private receipt is nil this means the tx was public
    85  		// and we do not need to apply the additional logic.
    86  		if privateReceipt != nil {
    87  			privateReceipts = append(privateReceipts, privateReceipt)
    88  			allLogs = append(allLogs, privateReceipt.Logs...)
    89  			p.bc.CheckAndSetPrivateState(privateReceipt.Logs, privateState)
    90  		}
    91  	}
    92  	// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
    93  	p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
    94  
    95  	return receipts, privateReceipts, allLogs, *usedGas, nil
    96  }
    97  
    98  // Quorum
    99  // returns the privateStateDB to be used for a transaction
   100  func PrivateStateDBForTxn(isQuorum, isPrivate bool, stateDb, privateStateDB *state.StateDB) *state.StateDB {
   101  	if !isQuorum || !isPrivate {
   102  		return stateDb
   103  	}
   104  	return privateStateDB
   105  }
   106  
   107  // /Quorum
   108  
   109  // ApplyTransaction attempts to apply a transaction to the given state database
   110  // and uses the input parameters for its environment. It returns the receipt
   111  // for the transaction, gas used and an error if the transaction failed,
   112  // indicating the block was invalid.
   113  func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common.Address, gp *GasPool, statedb, privateState *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.Receipt, error) {
   114  	// Quorum - decide the privateStateDB to use
   115  	privateStateDbToUse := PrivateStateDBForTxn(config.IsQuorum, tx.IsPrivate(), statedb, privateState)
   116  	// /Quorum
   117  
   118  	// Quorum - check for account permissions to execute the transaction
   119  	if core.IsV2Permission() {
   120  		if err := core.CheckAccountPermission(tx.From(), tx.To(), tx.Value(), tx.Data(), tx.Gas(), tx.GasPrice()); err != nil {
   121  			return nil, nil, err
   122  		}
   123  	}
   124  
   125  	if config.IsQuorum && tx.GasPrice() != nil && tx.GasPrice().Cmp(common.Big0) > 0 {
   126  		return nil, nil, ErrInvalidGasPrice
   127  	}
   128  
   129  	msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
   130  	if err != nil {
   131  		return nil, nil, err
   132  	}
   133  	// Create a new context to be used in the EVM environment
   134  	context := NewEVMContext(msg, header, bc, author)
   135  	// Create a new environment which holds all relevant information
   136  	// about the transaction and calling mechanisms.
   137  	vmenv := vm.NewEVM(context, statedb, privateStateDbToUse, config, cfg)
   138  	vmenv.SetCurrentTX(tx)
   139  
   140  	// Apply the transaction to the current state (included in the env)
   141  	_, gas, failed, err := ApplyMessage(vmenv, msg, gp)
   142  	if err != nil {
   143  		return nil, nil, err
   144  	}
   145  	// Update the state with pending changes
   146  	var root []byte
   147  	if config.IsByzantium(header.Number) {
   148  		statedb.Finalise(true)
   149  	} else {
   150  		root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
   151  	}
   152  	*usedGas += gas
   153  
   154  	// If this is a private transaction, the public receipt should always
   155  	// indicate success.
   156  	publicFailed := !(config.IsQuorum && tx.IsPrivate()) && failed
   157  
   158  	// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
   159  	// based on the eip phase, we're passing wether the root touch-delete accounts.
   160  	receipt := types.NewReceipt(root, publicFailed, *usedGas)
   161  	receipt.TxHash = tx.Hash()
   162  	receipt.GasUsed = gas
   163  	// if the transaction created a contract, store the creation address in the receipt.
   164  	if msg.To() == nil {
   165  		receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
   166  	}
   167  	// Set the receipt logs and create a bloom for filtering
   168  	receipt.Logs = statedb.GetLogs(tx.Hash())
   169  	receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
   170  	receipt.BlockHash = statedb.BlockHash()
   171  	receipt.BlockNumber = header.Number
   172  	receipt.TransactionIndex = uint(statedb.TxIndex())
   173  
   174  	var privateReceipt *types.Receipt
   175  	if config.IsQuorum && tx.IsPrivate() {
   176  		var privateRoot []byte
   177  		if config.IsByzantium(header.Number) {
   178  			privateState.Finalise(true)
   179  		} else {
   180  			privateRoot = privateState.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
   181  		}
   182  		privateReceipt = types.NewReceipt(privateRoot, failed, *usedGas)
   183  		privateReceipt.TxHash = tx.Hash()
   184  		privateReceipt.GasUsed = gas
   185  		if msg.To() == nil {
   186  			privateReceipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
   187  		}
   188  
   189  		privateReceipt.Logs = privateState.GetLogs(tx.Hash())
   190  		privateReceipt.Bloom = types.CreateBloom(types.Receipts{privateReceipt})
   191  	}
   192  
   193  	return receipt, privateReceipt, err
   194  }