pending.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. )
  25. // pendingBlock is a small collection of metadata about a locally mined block
  26. // that is placed into a pending set for canonical chain inclusion tracking.
  27. type pendingBlock struct {
  28. index uint64
  29. hash common.Hash
  30. }
  31. // pendingBlockSet implements a data structure to maintain locally mined blocks
  32. // have have not yet reached enough maturity to guarantee chain inclusion. It is
  33. // used by the miner to provide logs to the user when a previously mined block
  34. // has a high enough guarantee to not be reorged out of te canonical chain.
  35. type pendingBlockSet struct {
  36. chain *core.BlockChain // Blockchain to verify canonical status through
  37. depth uint // Depth after which to discard previous blocks
  38. blocks *ring.Ring // Block infos to allow canonical chain cross checks
  39. lock sync.RWMutex // Protects the fields from concurrent access
  40. }
  41. // newPendingBlockSet returns new data structure to track currently pending blocks.
  42. func newPendingBlockSet(chain *core.BlockChain, depth uint) *pendingBlockSet {
  43. return &pendingBlockSet{
  44. chain: chain,
  45. depth: depth,
  46. }
  47. }
  48. // Insert adds a new block to the set of pending ones.
  49. func (set *pendingBlockSet) Insert(index uint64, hash common.Hash) {
  50. // If a new block was mined locally, shift out any old enough blocks
  51. set.Shift(index)
  52. // Create the new item as its own ring
  53. item := ring.New(1)
  54. item.Value = &pendingBlock{
  55. index: index,
  56. hash: hash,
  57. }
  58. // Set as the initial ring or append to the end
  59. set.lock.Lock()
  60. defer set.lock.Unlock()
  61. if set.blocks == nil {
  62. set.blocks = item
  63. } else {
  64. set.blocks.Move(-1).Link(item)
  65. }
  66. // Display a log for the user to notify of a new mined block pending
  67. glog.V(logger.Info).Infof("🔨 mined potential block #%d [%x…], waiting for %d blocks to confirm", index, hash.Bytes()[:4], set.depth)
  68. }
  69. // Shift drops all pending blocks from the set which exceed the pending sets depth
  70. // allowance, checking them against the canonical chain for inclusion or staleness
  71. // report.
  72. func (set *pendingBlockSet) Shift(height uint64) {
  73. set.lock.Lock()
  74. defer set.lock.Unlock()
  75. // Short circuit if there are no pending blocks to shift
  76. if set.blocks == nil {
  77. return
  78. }
  79. // Otherwise shift all blocks below the depth allowance
  80. for set.blocks != nil {
  81. // Retrieve the next pending block and abort if too fresh
  82. next := set.blocks.Value.(*pendingBlock)
  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. glog.V(logger.Warn).Infof("failed to retrieve header of mined block #%d [%x…]", next.index, next.hash.Bytes()[:4])
  91. case header.Hash() == next.hash:
  92. glog.V(logger.Info).Infof("🔗 mined block #%d [%x…] reached canonical chain", next.index, next.hash.Bytes()[:4])
  93. default:
  94. glog.V(logger.Info).Infof("⑂ mined block #%d [%x…] became a side fork", next.index, next.hash.Bytes()[:4])
  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. }