github.com/amazechain/amc@v0.1.3/internal/blockchain_insert.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package internal 18 19 import ( 20 block2 "github.com/amazechain/amc/common/block" 21 "time" 22 ) 23 24 // statsReportLimit is the time limit during import and export after which we 25 // always print out progress. This avoids the user wondering what's going on. 26 const statsReportLimit = 8 * time.Second 27 28 // report prints statistics if some number of blocks have been processed 29 // or more than a few seconds have passed since the last message. 30 //func (st *insertStats) report(chain []*types.Block, index int, dirty common.StorageSize, setHead bool) { 31 // // Fetch the timings for the batch 32 // var ( 33 // now = mclock.Now() 34 // elapsed = now.Sub(st.startTime) 35 // ) 36 // // If we're at the last block of the batch or report period reached, log 37 // if index == len(chain)-1 || elapsed >= statsReportLimit { 38 // // Count the number of transactions in this segment 39 // var txs int 40 // for _, block := range chain[st.lastIndex : index+1] { 41 // txs += len(block.Transactions()) 42 // } 43 // end := chain[index] 44 // 45 // // Assemble the log context and send it to the logger 46 // context := []interface{}{ 47 // "number", end.Number(), "hash", end.Hash(), 48 // "blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000, 49 // "elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed), 50 // } 51 // if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute { 52 // context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...) 53 // } 54 // context = append(context, []interface{}{"dirty", dirty}...) 55 // 56 // if st.queued > 0 { 57 // context = append(context, []interface{}{"queued", st.queued}...) 58 // } 59 // if st.ignored > 0 { 60 // context = append(context, []interface{}{"ignored", st.ignored}...) 61 // } 62 // if setHead { 63 // log.Info("Imported new chain segment", context...) 64 // } else { 65 // log.Info("Imported new potential chain segment", context...) 66 // } 67 // // Bump the stats reported to the next section 68 // *st = insertStats{startTime: now, lastIndex: index + 1} 69 // } 70 //} 71 72 // insertIterator is a helper to assist during chain import. 73 type insertIterator struct { 74 chain []block2.IBlock // Chain of blocks being iterated over 75 76 results <-chan error // Verification result sink from the consensus engine 77 errors []error // Header verification errors for the blocks 78 79 index int // Current offset of the iterator 80 validator Validator // Validator to run if verification succeeds 81 } 82 83 // newInsertIterator creates a new iterator based on the given blocks, which are 84 // assumed to be a contiguous chain. 85 func newInsertIterator(chain []block2.IBlock, results <-chan error, validator Validator) *insertIterator { 86 return &insertIterator{ 87 chain: chain, 88 results: results, 89 errors: make([]error, 0, len(chain)), 90 index: -1, 91 validator: validator, 92 } 93 } 94 95 // next returns the next block in the iterator, along with any potential validation 96 // error for that block. When the end is reached, it will return (nil, nil). 97 func (it *insertIterator) next() (block2.IBlock, error) { 98 // If we reached the end of the chain, abort 99 if it.index+1 >= len(it.chain) { 100 it.index = len(it.chain) 101 return nil, nil 102 } 103 // Advance the iterator and wait for verification result if not yet done 104 it.index++ 105 if len(it.errors) <= it.index { 106 it.errors = append(it.errors, <-it.results) 107 } 108 if it.errors[it.index] != nil { 109 return it.chain[it.index], it.errors[it.index] 110 } 111 // Block header valid, run body validation and return 112 return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index]) 113 } 114 115 // peek returns the next block in the iterator, along with any potential validation 116 // error for that block, but does **not** advance the iterator. 117 // 118 // Both header and body validation errors (nil too) is cached into the iterator 119 // to avoid duplicating work on the following next() call. 120 func (it *insertIterator) peek() (block2.IBlock, error) { 121 // If we reached the end of the chain, abort 122 if it.index+1 >= len(it.chain) { 123 return nil, nil 124 } 125 // Wait for verification result if not yet done 126 if len(it.errors) <= it.index+1 { 127 it.errors = append(it.errors, <-it.results) 128 } 129 if it.errors[it.index+1] != nil { 130 return it.chain[it.index+1], it.errors[it.index+1] 131 } 132 // Block header valid, ignore body validation since we don't have a parent anyway 133 return it.chain[it.index+1], nil 134 } 135 136 // previous returns the previous header that was being processed, or nil. 137 func (it *insertIterator) previous() block2.IHeader { 138 if it.index < 1 { 139 return nil 140 } 141 return it.chain[it.index-1].Header() 142 } 143 144 // current returns the current header that is being processed, or nil. 145 func (it *insertIterator) current() block2.IHeader { 146 if it.index == -1 || it.index >= len(it.chain) { 147 return nil 148 } 149 return it.chain[it.index].Header() 150 } 151 152 // first returns the first block in the it. 153 func (it *insertIterator) first() block2.IBlock { 154 return it.chain[0] 155 } 156 157 // remaining returns the number of remaining blocks. 158 func (it *insertIterator) remaining() int { 159 return len(it.chain) - it.index 160 } 161 162 // processed returns the number of processed blocks. 163 func (it *insertIterator) processed() int { 164 return it.index + 1 165 }