blockchain_insert.go 6.1 KB

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