github.com/Gessiux/neatchain@v1.3.1/chain/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/Gessiux/neatchain/chain/core/types" 23 "github.com/Gessiux/neatchain/chain/log" 24 "github.com/Gessiux/neatchain/utilities/common" 25 "github.com/Gessiux/neatchain/utilities/common/mclock" 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, dirty 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, " New height", end.Number(), 60 } 61 if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute { 62 context = append(context, []interface{}{" You are behind", common.PrettyAge(timestamp)}...) 63 } 64 //context = append(context, []interface{}{"dirty", dirty}...) 65 66 if st.queued > 0 { 67 context = append(context, []interface{}{"queued", st.queued}...) 68 } 69 if st.ignored > 0 { 70 context = append(context, []interface{}{"ignored", st.ignored}...) 71 } 72 log.Info("Imported new blockchain segment:", context...) 73 74 // Bump the stats reported to the next section 75 *st = insertStats{startTime: now, lastIndex: index + 1} 76 } 77 } 78 79 // insertIterator is a helper to assist during chain import. 80 type insertIterator struct { 81 chain types.Blocks // Chain of blocks being iterated over 82 83 results <-chan error // Verification result sink from the consensus engine 84 errors []error // Header verification errors for the blocks 85 86 index int // Current offset of the iterator 87 validator Validator // Validator to run if verification succeeds 88 } 89 90 // newInsertIterator creates a new iterator based on the given blocks, which are 91 // assumed to be a contiguous chain. 92 func newInsertIterator(chain types.Blocks, results <-chan error, validator Validator) *insertIterator { 93 return &insertIterator{ 94 chain: chain, 95 results: results, 96 errors: make([]error, 0, len(chain)), 97 index: -1, 98 validator: validator, 99 } 100 } 101 102 // next returns the next block in the iterator, along with any potential validation 103 // error for that block. When the end is reached, it will return (nil, nil). 104 func (it *insertIterator) next() (*types.Block, error) { 105 // If we reached the end of the chain, abort 106 if it.index+1 >= len(it.chain) { 107 it.index = len(it.chain) 108 return nil, nil 109 } 110 // Advance the iterator and wait for verification result if not yet done 111 it.index++ 112 if len(it.errors) <= it.index { 113 it.errors = append(it.errors, <-it.results) 114 } 115 if it.errors[it.index] != nil { 116 return it.chain[it.index], it.errors[it.index] 117 } 118 // Block header valid, run body validation and return 119 return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index]) 120 } 121 122 // peek returns the next block in the iterator, along with any potential validation 123 // error for that block, but does **not** advance the iterator. 124 // 125 // Both header and body validation errors (nil too) is cached into the iterator 126 // to avoid duplicating work on the following next() call. 127 func (it *insertIterator) peek() (*types.Block, error) { 128 // If we reached the end of the chain, abort 129 if it.index+1 >= len(it.chain) { 130 return nil, nil 131 } 132 // Wait for verification result if not yet done 133 if len(it.errors) <= it.index+1 { 134 it.errors = append(it.errors, <-it.results) 135 } 136 if it.errors[it.index+1] != nil { 137 return it.chain[it.index+1], it.errors[it.index+1] 138 } 139 // Block header valid, ignore body validation since we don't have a parent anyway 140 return it.chain[it.index+1], nil 141 } 142 143 // previous returns the previous header that was being processed, or nil. 144 func (it *insertIterator) previous() *types.Header { 145 if it.index < 1 { 146 return nil 147 } 148 return it.chain[it.index-1].Header() 149 } 150 151 // first returns the first block in the it. 152 func (it *insertIterator) first() *types.Block { 153 return it.chain[0] 154 } 155 156 // remaining returns the number of remaining blocks. 157 func (it *insertIterator) remaining() int { 158 return len(it.chain) - it.index 159 } 160 161 // processed returns the number of processed blocks. 162 func (it *insertIterator) processed() int { 163 return it.index + 1 164 }