github.com/m3shine/gochain@v2.2.26+incompatible/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 "context" 21 "time" 22 23 "github.com/gochain-io/gochain/common" 24 "github.com/gochain-io/gochain/common/mclock" 25 "github.com/gochain-io/gochain/core/types" 26 "github.com/gochain-io/gochain/log" 27 ) 28 29 // insertStats tracks and reports on block insertion. 30 type insertStats struct { 31 queued, processed, ignored int 32 usedGas uint64 33 lastIndex int 34 startTime mclock.AbsTime 35 } 36 37 // statsReportLimit is the time limit during import and export after which we 38 // always print out progress. This avoids the user wondering what's going on. 39 const statsReportLimit = 8 * time.Second 40 41 // report prints statistics if some number of blocks have been processed 42 // or more than a few seconds have passed since the last message. 43 func (st *insertStats) report(chain []*types.Block, index int, cache common.StorageSize) { 44 // Fetch the timings for the batch 45 var ( 46 now = mclock.Now() 47 elapsed = time.Duration(now) - time.Duration(st.startTime) 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 "blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000, 61 "elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed), 62 "number", end.Number(), "hash", end.Hash(), 63 } 64 if timestamp := time.Unix(end.Time().Int64(), 0); time.Since(timestamp) > time.Minute { 65 context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...) 66 } 67 context = append(context, []interface{}{"cache", cache}...) 68 69 if st.queued > 0 { 70 context = append(context, []interface{}{"queued", st.queued}...) 71 } 72 if st.ignored > 0 { 73 context = append(context, []interface{}{"ignored", st.ignored}...) 74 } 75 log.Info("Imported new chain segment", context...) 76 77 // Bump the stats reported to the next section 78 *st = insertStats{startTime: now, lastIndex: index + 1} 79 } 80 } 81 82 // insertIterator is a helper to assist during chain import. 83 type insertIterator struct { 84 chain types.Blocks 85 results <-chan error 86 index int 87 validator Validator 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 index: -1, 97 validator: validator, 98 } 99 } 100 101 // next returns the next block in the iterator, along with any potential validation 102 // error for that block. When the end is reached, it will return (nil, nil). 103 func (it *insertIterator) next(ctx context.Context) (*types.Block, error) { 104 if it.index+1 >= len(it.chain) { 105 it.index = len(it.chain) 106 return nil, nil 107 } 108 it.index++ 109 if err := <-it.results; err != nil { 110 return it.chain[it.index], err 111 } 112 return it.chain[it.index], it.validator.ValidateBody(ctx, it.chain[it.index], true) 113 } 114 115 // current returns the current block that's being processed. 116 func (it *insertIterator) current() *types.Block { 117 if it.index < 0 || it.index+1 >= len(it.chain) { 118 return nil 119 } 120 return it.chain[it.index] 121 } 122 123 // previous returns the previous block was being processed, or nil 124 func (it *insertIterator) previous() *types.Block { 125 if it.index < 1 { 126 return nil 127 } 128 return it.chain[it.index-1] 129 } 130 131 // first returns the first block in the it. 132 func (it *insertIterator) first() *types.Block { 133 return it.chain[0] 134 } 135 136 // remaining returns the number of remaining blocks. 137 func (it *insertIterator) remaining() int { 138 return len(it.chain) - it.index 139 } 140 141 // processed returns the number of processed blocks. 142 func (it *insertIterator) processed() int { 143 return it.index + 1 144 }