unconfirmed.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2016 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 miner
  17. import (
  18. "container/ring"
  19. "sync"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/log"
  23. )
  24. // headerRetriever is used by the unconfirmed block set to verify whether a previously
  25. // mined block is part of the canonical chain or not.
  26. type headerRetriever interface {
  27. // GetHeaderByNumber retrieves the canonical header associated with a block number.
  28. GetHeaderByNumber(number uint64) *types.Header
  29. }
  30. // unconfirmedBlock is a small collection of metadata about a locally mined block
  31. // that is placed into a unconfirmed set for canonical chain inclusion tracking.
  32. type unconfirmedBlock struct {
  33. index uint64
  34. hash common.Hash
  35. }
  36. // unconfirmedBlocks implements a data structure to maintain locally mined blocks
  37. // have have not yet reached enough maturity to guarantee chain inclusion. It is
  38. // used by the miner to provide logs to the user when a previously mined block
  39. // has a high enough guarantee to not be reorged out of the canonical chain.
  40. type unconfirmedBlocks struct {
  41. chain headerRetriever // Blockchain to verify canonical status through
  42. depth uint // Depth after which to discard previous blocks
  43. blocks *ring.Ring // Block infos to allow canonical chain cross checks
  44. lock sync.RWMutex // Protects the fields from concurrent access
  45. }
  46. // newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
  47. func newUnconfirmedBlocks(chain headerRetriever, depth uint) *unconfirmedBlocks {
  48. return &unconfirmedBlocks{
  49. chain: chain,
  50. depth: depth,
  51. }
  52. }
  53. // Insert adds a new block to the set of unconfirmed ones.
  54. func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) {
  55. // If a new block was mined locally, shift out any old enough blocks
  56. set.Shift(index)
  57. // Create the new item as its own ring
  58. item := ring.New(1)
  59. item.Value = &unconfirmedBlock{
  60. index: index,
  61. hash: hash,
  62. }
  63. // Set as the initial ring or append to the end
  64. set.lock.Lock()
  65. defer set.lock.Unlock()
  66. if set.blocks == nil {
  67. set.blocks = item
  68. } else {
  69. set.blocks.Move(-1).Link(item)
  70. }
  71. // Display a log for the user to notify of a new mined block unconfirmed
  72. log.Info("🔨 mined potential block", "number", index, "hash", hash)
  73. }
  74. // Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth
  75. // allowance, checking them against the canonical chain for inclusion or staleness
  76. // report.
  77. func (set *unconfirmedBlocks) Shift(height uint64) {
  78. set.lock.Lock()
  79. defer set.lock.Unlock()
  80. for set.blocks != nil {
  81. // Retrieve the next unconfirmed block and abort if too fresh
  82. next := set.blocks.Value.(*unconfirmedBlock)
  83. if next.index+uint64(set.depth) > height {
  84. break
  85. }
  86. // Block seems to exceed depth allowance, check for canonical status
  87. header := set.chain.GetHeaderByNumber(next.index)
  88. switch {
  89. case header == nil:
  90. log.Warn("Failed to retrieve header of mined block", "number", next.index, "hash", next.hash)
  91. case header.Hash() == next.hash:
  92. log.Info("🔗 block reached canonical chain", "number", next.index, "hash", next.hash)
  93. default:
  94. log.Info("⑂ block became a side fork", "number", next.index, "hash", next.hash)
  95. }
  96. // Drop the block out of the ring
  97. if set.blocks.Value == set.blocks.Next().Value {
  98. set.blocks = nil
  99. } else {
  100. set.blocks = set.blocks.Move(-1)
  101. set.blocks.Unlink(1)
  102. set.blocks = set.blocks.Move(1)
  103. }
  104. }
  105. }