unconfirmed.go 4.8 KB

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