github.com/axiomzen/go-ethereum@v1.8.27/core/blockchain_insert.go (about)

     1  // Copyright 2018 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  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/common/mclock"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/log"
    26  )
    27  
    28  // insertStats tracks and reports on block insertion.
    29  type insertStats struct {
    30  	queued, processed, ignored int
    31  	usedGas                    uint64
    32  	lastIndex                  int
    33  	startTime                  mclock.AbsTime
    34  }
    35  
    36  // statsReportLimit is the time limit during import and export after which we
    37  // always print out progress. This avoids the user wondering what's going on.
    38  const statsReportLimit = 8 * time.Second
    39  
    40  // report prints statistics if some number of blocks have been processed
    41  // or more than a few seconds have passed since the last message.
    42  func (st *insertStats) report(chain []*types.Block, index int, cache common.StorageSize) {
    43  	// Fetch the timings for the batch
    44  	var (
    45  		now     = mclock.Now()
    46  		elapsed = time.Duration(now) - time.Duration(st.startTime)
    47  	)
    48  	// If we're at the last block of the batch or report period reached, log
    49  	if index == len(chain)-1 || elapsed >= statsReportLimit {
    50  		// Count the number of transactions in this segment
    51  		var txs int
    52  		for _, block := range chain[st.lastIndex : index+1] {
    53  			txs += len(block.Transactions())
    54  		}
    55  		end := chain[index]
    56  
    57  		// Assemble the log context and send it to the logger
    58  		context := []interface{}{
    59  			"blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000,
    60  			"elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
    61  			"number", end.Number(), "hash", end.Hash(),
    62  		}
    63  		if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute {
    64  			context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...)
    65  		}
    66  		context = append(context, []interface{}{"cache", cache}...)
    67  
    68  		if st.queued > 0 {
    69  			context = append(context, []interface{}{"queued", st.queued}...)
    70  		}
    71  		if st.ignored > 0 {
    72  			context = append(context, []interface{}{"ignored", st.ignored}...)
    73  		}
    74  		log.Info("Imported new chain segment", context...)
    75  
    76  		// Bump the stats reported to the next section
    77  		*st = insertStats{startTime: now, lastIndex: index + 1}
    78  	}
    79  }
    80  
    81  // insertIterator is a helper to assist during chain import.
    82  type insertIterator struct {
    83  	chain     types.Blocks
    84  	results   <-chan error
    85  	index     int
    86  	validator Validator
    87  }
    88  
    89  // newInsertIterator creates a new iterator based on the given blocks, which are
    90  // assumed to be a contiguous chain.
    91  func newInsertIterator(chain types.Blocks, results <-chan error, validator Validator) *insertIterator {
    92  	return &insertIterator{
    93  		chain:     chain,
    94  		results:   results,
    95  		index:     -1,
    96  		validator: validator,
    97  	}
    98  }
    99  
   100  // next returns the next block in the iterator, along with any potential validation
   101  // error for that block. When the end is reached, it will return (nil, nil).
   102  func (it *insertIterator) next() (*types.Block, error) {
   103  	if it.index+1 >= len(it.chain) {
   104  		it.index = len(it.chain)
   105  		return nil, nil
   106  	}
   107  	it.index++
   108  	if err := <-it.results; err != nil {
   109  		return it.chain[it.index], err
   110  	}
   111  	return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index])
   112  }
   113  
   114  // previous returns the previous block was being processed, or nil
   115  func (it *insertIterator) previous() *types.Block {
   116  	if it.index < 1 {
   117  		return nil
   118  	}
   119  	return it.chain[it.index-1]
   120  }
   121  
   122  // first returns the first block in the it.
   123  func (it *insertIterator) first() *types.Block {
   124  	return it.chain[0]
   125  }
   126  
   127  // remaining returns the number of remaining blocks.
   128  func (it *insertIterator) remaining() int {
   129  	return len(it.chain) - it.index
   130  }
   131  
   132  // processed returns the number of processed blocks.
   133  func (it *insertIterator) processed() int {
   134  	return it.index + 1
   135  }