blockchain_insert.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, cache common.StorageSize) {
  37. // Fetch the timings for the batch
  38. var (
  39. now = mclock.Now()
  40. elapsed = time.Duration(now) - time.Duration(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(end.Time().Int64(), 0); time.Since(timestamp) > time.Minute {
  57. context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...)
  58. }
  59. context = append(context, []interface{}{"cache", cache}...)
  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
  74. results <-chan error
  75. index int
  76. validator Validator
  77. }
  78. // newInsertIterator creates a new iterator based on the given blocks, which are
  79. // assumed to be a contiguous chain.
  80. func newInsertIterator(chain types.Blocks, results <-chan error, validator Validator) *insertIterator {
  81. return &insertIterator{
  82. chain: chain,
  83. results: results,
  84. index: -1,
  85. validator: validator,
  86. }
  87. }
  88. // next returns the next block in the iterator, along with any potential validation
  89. // error for that block. When the end is reached, it will return (nil, nil).
  90. func (it *insertIterator) next() (*types.Block, error) {
  91. if it.index+1 >= len(it.chain) {
  92. it.index = len(it.chain)
  93. return nil, nil
  94. }
  95. it.index++
  96. if err := <-it.results; err != nil {
  97. return it.chain[it.index], err
  98. }
  99. return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index])
  100. }
  101. // previous returns the previous block was being processed, or nil
  102. func (it *insertIterator) previous() *types.Block {
  103. if it.index < 1 {
  104. return nil
  105. }
  106. return it.chain[it.index-1]
  107. }
  108. // first returns the first block in the it.
  109. func (it *insertIterator) first() *types.Block {
  110. return it.chain[0]
  111. }
  112. // remaining returns the number of remaining blocks.
  113. func (it *insertIterator) remaining() int {
  114. return len(it.chain) - it.index
  115. }
  116. // processed returns the number of processed blocks.
  117. func (it *insertIterator) processed() int {
  118. return it.index + 1
  119. }