blockchain_insert.go 5.7 KB

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