github.com/ethereum/go-ethereum@v1.16.1/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 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, snapDiffItems, snapBufItems, trieDiffNodes, triebufNodes common.StorageSize, setHead bool) { 43 // Fetch the timings for the batch 44 var ( 45 now = mclock.Now() 46 elapsed = now.Sub(st.startTime) + 1 // prevent zero division 47 mgasps = float64(st.usedGas) * 1000 / float64(elapsed) 48 ) 49 // If we're at the last block of the batch or report period reached, log 50 if index == len(chain)-1 || elapsed >= statsReportLimit { 51 // Count the number of transactions in this segment 52 var txs int 53 for _, block := range chain[st.lastIndex : index+1] { 54 txs += len(block.Transactions()) 55 } 56 end := chain[index] 57 58 // Assemble the log context and send it to the logger 59 context := []interface{}{ 60 "number", end.Number(), "hash", end.Hash(), 61 "blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000, 62 "elapsed", common.PrettyDuration(elapsed), "mgasps", mgasps, 63 } 64 if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute { 65 context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...) 66 } 67 if snapDiffItems != 0 || snapBufItems != 0 { // snapshots enabled 68 context = append(context, []interface{}{"snapdiffs", snapDiffItems}...) 69 if snapBufItems != 0 { // future snapshot refactor 70 context = append(context, []interface{}{"snapdirty", snapBufItems}...) 71 } 72 } 73 if trieDiffNodes != 0 { // pathdb 74 context = append(context, []interface{}{"triediffs", trieDiffNodes}...) 75 } 76 context = append(context, []interface{}{"triedirty", triebufNodes}...) 77 78 if st.ignored > 0 { 79 context = append(context, []interface{}{"ignored", st.ignored}...) 80 } 81 if setHead { 82 log.Info("Imported new chain segment", context...) 83 } else { 84 log.Info("Imported new potential chain segment", context...) 85 } 86 // Bump the stats reported to the next section 87 *st = insertStats{startTime: now, lastIndex: index + 1} 88 } 89 } 90 91 // insertIterator is a helper to assist during chain import. 92 type insertIterator struct { 93 chain types.Blocks // Chain of blocks being iterated over 94 95 results <-chan error // Verification result sink from the consensus engine 96 errors []error // Header verification errors for the blocks 97 98 index int // Current offset of the iterator 99 validator Validator // Validator to run if verification succeeds 100 } 101 102 // newInsertIterator creates a new iterator based on the given blocks, which are 103 // assumed to be a contiguous chain. 104 func newInsertIterator(chain types.Blocks, results <-chan error, validator Validator) *insertIterator { 105 return &insertIterator{ 106 chain: chain, 107 results: results, 108 errors: make([]error, 0, len(chain)), 109 index: -1, 110 validator: validator, 111 } 112 } 113 114 // next returns the next block in the iterator, along with any potential validation 115 // error for that block. When the end is reached, it will return (nil, nil). 116 func (it *insertIterator) next() (*types.Block, error) { 117 // If we reached the end of the chain, abort 118 if it.index+1 >= len(it.chain) { 119 it.index = len(it.chain) 120 return nil, nil 121 } 122 // Advance the iterator and wait for verification result if not yet done 123 it.index++ 124 if len(it.errors) <= it.index { 125 it.errors = append(it.errors, <-it.results) 126 } 127 if it.errors[it.index] != nil { 128 return it.chain[it.index], it.errors[it.index] 129 } 130 // Block header valid, run body validation and return 131 return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index]) 132 } 133 134 // peek returns the next block in the iterator, along with any potential validation 135 // error for that block, but does **not** advance the iterator. 136 // 137 // Both header and body validation errors (nil too) is cached into the iterator 138 // to avoid duplicating work on the following next() call. 139 // nolint:unused 140 func (it *insertIterator) peek() (*types.Block, error) { 141 // If we reached the end of the chain, abort 142 if it.index+1 >= len(it.chain) { 143 return nil, nil 144 } 145 // Wait for verification result if not yet done 146 if len(it.errors) <= it.index+1 { 147 it.errors = append(it.errors, <-it.results) 148 } 149 if it.errors[it.index+1] != nil { 150 return it.chain[it.index+1], it.errors[it.index+1] 151 } 152 // Block header valid, ignore body validation since we don't have a parent anyway 153 return it.chain[it.index+1], nil 154 } 155 156 // previous returns the previous header that was being processed, or nil. 157 func (it *insertIterator) previous() *types.Header { 158 if it.index < 1 { 159 return nil 160 } 161 return it.chain[it.index-1].Header() 162 } 163 164 // current returns the current header that is being processed, or nil. 165 func (it *insertIterator) current() *types.Header { 166 if it.index == -1 || it.index >= len(it.chain) { 167 return nil 168 } 169 return it.chain[it.index].Header() 170 } 171 172 // remaining returns the number of remaining blocks. 173 func (it *insertIterator) remaining() int { 174 return len(it.chain) - it.index 175 }