github.com/klaytn/klaytn@v1.12.1/blockchain/state_processor.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/state_processor.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package blockchain
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/klaytn/klaytn/blockchain/state"
    27  	"github.com/klaytn/klaytn/blockchain/types"
    28  	"github.com/klaytn/klaytn/blockchain/vm"
    29  	"github.com/klaytn/klaytn/consensus"
    30  	"github.com/klaytn/klaytn/params"
    31  )
    32  
    33  // StateProcessor is a basic Processor, which takes care of transitioning
    34  // state from one point to another.
    35  //
    36  // StateProcessor implements Processor.
    37  type StateProcessor struct {
    38  	config *params.ChainConfig // Chain configuration options
    39  	bc     *BlockChain         // Canonical block chain
    40  	engine consensus.Engine    // Consensus engine used for block rewards
    41  }
    42  
    43  // ProcessStats includes the time statistics regarding StateProcessor.Process.
    44  type ProcessStats struct {
    45  	BeforeApplyTxs time.Time
    46  	AfterApplyTxs  time.Time
    47  	AfterFinalize  time.Time
    48  }
    49  
    50  // NewStateProcessor initialises a new StateProcessor.
    51  func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor {
    52  	return &StateProcessor{
    53  		config: config,
    54  		bc:     bc,
    55  		engine: engine,
    56  	}
    57  }
    58  
    59  // Process processes the state changes according to the Klaytn rules by running
    60  // the transaction messages using the statedb and applying any rewards to the processor.
    61  //
    62  // Process returns the receipts and logs accumulated during the process and
    63  // returns the amount of gas that was used in the process. If any of the
    64  // transactions failed to execute due to insufficient gas it will return an error.
    65  func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, []*vm.InternalTxTrace, ProcessStats, error) {
    66  	var (
    67  		receipts         types.Receipts
    68  		usedGas          = new(uint64)
    69  		header           = block.Header()
    70  		allLogs          []*types.Log
    71  		internalTxTraces []*vm.InternalTxTrace
    72  		processStats     ProcessStats
    73  	)
    74  
    75  	// Extract author from the header
    76  	author, _ := p.bc.Engine().Author(header) // Ignore error, we're past header validation
    77  
    78  	processStats.BeforeApplyTxs = time.Now()
    79  	// Iterate over and process the individual transactions
    80  	for i, tx := range block.Transactions() {
    81  		statedb.SetTxContext(tx.Hash(), block.Hash(), i)
    82  		receipt, internalTxTrace, err := p.bc.ApplyTransaction(p.config, &author, statedb, header, tx, usedGas, &cfg)
    83  		if err != nil {
    84  			return nil, nil, 0, nil, processStats, err
    85  		}
    86  		receipts = append(receipts, receipt)
    87  		allLogs = append(allLogs, receipt.Logs...)
    88  		internalTxTraces = append(internalTxTraces, internalTxTrace)
    89  	}
    90  	processStats.AfterApplyTxs = time.Now()
    91  
    92  	// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
    93  	if _, err := p.engine.Finalize(p.bc, header, statedb, block.Transactions(), receipts); err != nil {
    94  		return nil, nil, 0, nil, processStats, err
    95  	}
    96  	processStats.AfterFinalize = time.Now()
    97  
    98  	return receipts, allLogs, *usedGas, internalTxTraces, processStats, nil
    99  }