github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/blockchain_insert.go (about) 1 package core 2 3 import ( 4 "time" 5 6 "github.com/neatlab/neatio/chain/core/types" 7 "github.com/neatlab/neatio/chain/log" 8 "github.com/neatlab/neatio/utilities/common" 9 "github.com/neatlab/neatio/utilities/common/mclock" 10 ) 11 12 type insertStats struct { 13 queued, processed, ignored int 14 usedGas uint64 15 lastIndex int 16 startTime mclock.AbsTime 17 } 18 19 const statsReportLimit = 8 * time.Second 20 21 func (st *insertStats) report(chain []*types.Block, index int, dirty common.StorageSize) { 22 23 var ( 24 now = mclock.Now() 25 elapsed = time.Duration(now) - time.Duration(st.startTime) 26 ) 27 28 if index == len(chain)-1 || elapsed >= statsReportLimit { 29 30 var txs int 31 for _, block := range chain[st.lastIndex : index+1] { 32 txs += len(block.Transactions()) 33 } 34 end := chain[index] 35 36 context := []interface{}{ 37 "Blocks", st.processed, " New height", end.Number(), 38 } 39 if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute { 40 context = append(context, []interface{}{" You are behind", common.PrettyAge(timestamp)}...) 41 } 42 43 if st.queued > 0 { 44 context = append(context, []interface{}{"queued", st.queued}...) 45 } 46 if st.ignored > 0 { 47 context = append(context, []interface{}{"ignored", st.ignored}...) 48 } 49 log.Info("Imported new blockchain segment:", context...) 50 51 *st = insertStats{startTime: now, lastIndex: index + 1} 52 } 53 } 54 55 type insertIterator struct { 56 chain types.Blocks 57 58 results <-chan error 59 errors []error 60 61 index int 62 validator Validator 63 } 64 65 func newInsertIterator(chain types.Blocks, results <-chan error, validator Validator) *insertIterator { 66 return &insertIterator{ 67 chain: chain, 68 results: results, 69 errors: make([]error, 0, len(chain)), 70 index: -1, 71 validator: validator, 72 } 73 } 74 75 func (it *insertIterator) next() (*types.Block, error) { 76 77 if it.index+1 >= len(it.chain) { 78 it.index = len(it.chain) 79 return nil, nil 80 } 81 82 it.index++ 83 if len(it.errors) <= it.index { 84 it.errors = append(it.errors, <-it.results) 85 } 86 if it.errors[it.index] != nil { 87 return it.chain[it.index], it.errors[it.index] 88 } 89 90 return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index]) 91 } 92 93 func (it *insertIterator) peek() (*types.Block, error) { 94 95 if it.index+1 >= len(it.chain) { 96 return nil, nil 97 } 98 99 if len(it.errors) <= it.index+1 { 100 it.errors = append(it.errors, <-it.results) 101 } 102 if it.errors[it.index+1] != nil { 103 return it.chain[it.index+1], it.errors[it.index+1] 104 } 105 106 return it.chain[it.index+1], nil 107 } 108 109 func (it *insertIterator) previous() *types.Header { 110 if it.index < 1 { 111 return nil 112 } 113 return it.chain[it.index-1].Header() 114 } 115 116 func (it *insertIterator) first() *types.Block { 117 return it.chain[0] 118 } 119 120 func (it *insertIterator) remaining() int { 121 return len(it.chain) - it.index 122 } 123 124 func (it *insertIterator) processed() int { 125 return it.index + 1 126 }