@@ -21,45 +21,52 @@ import (
2121 "sync"
2222
2323 "github.com/ethereum/go-ethereum/common"
24- "github.com/ethereum/go-ethereum/core"
24+ "github.com/ethereum/go-ethereum/core/types "
2525 "github.com/ethereum/go-ethereum/logger"
2626 "github.com/ethereum/go-ethereum/logger/glog"
2727)
2828
29- // pendingBlock is a small collection of metadata about a locally mined block
30- // that is placed into a pending set for canonical chain inclusion tracking.
31- type pendingBlock struct {
29+ // headerRetriever is used by the unconfirmed block set to verify whether a previously
30+ // mined block is part of the canonical chain or not.
31+ type headerRetriever interface {
32+ // GetHeaderByNumber retrieves the canonical header associated with a block number.
33+ GetHeaderByNumber (number uint64 ) * types.Header
34+ }
35+
36+ // unconfirmedBlock is a small collection of metadata about a locally mined block
37+ // that is placed into a unconfirmed set for canonical chain inclusion tracking.
38+ type unconfirmedBlock struct {
3239 index uint64
3340 hash common.Hash
3441}
3542
36- // pendingBlockSet implements a data structure to maintain locally mined blocks
43+ // unconfirmedBlocks implements a data structure to maintain locally mined blocks
3744// have have not yet reached enough maturity to guarantee chain inclusion. It is
3845// used by the miner to provide logs to the user when a previously mined block
3946// has a high enough guarantee to not be reorged out of te canonical chain.
40- type pendingBlockSet struct {
41- chain * core. BlockChain // 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
47+ type unconfirmedBlocks struct {
48+ chain headerRetriever // Blockchain to verify canonical status through
49+ depth uint // Depth after which to discard previous blocks
50+ blocks * ring.Ring // Block infos to allow canonical chain cross checks
51+ lock sync.RWMutex // Protects the fields from concurrent access
4552}
4653
47- // newPendingBlockSet returns new data structure to track currently pending blocks.
48- func newPendingBlockSet (chain * core. BlockChain , depth uint ) * pendingBlockSet {
49- return & pendingBlockSet {
54+ // newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
55+ func newUnconfirmedBlocks (chain headerRetriever , depth uint ) * unconfirmedBlocks {
56+ return & unconfirmedBlocks {
5057 chain : chain ,
5158 depth : depth ,
5259 }
5360}
5461
55- // Insert adds a new block to the set of pending ones.
56- func (set * pendingBlockSet ) Insert (index uint64 , hash common.Hash ) {
62+ // Insert adds a new block to the set of unconfirmed ones.
63+ func (set * unconfirmedBlocks ) Insert (index uint64 , hash common.Hash ) {
5764 // If a new block was mined locally, shift out any old enough blocks
5865 set .Shift (index )
5966
6067 // Create the new item as its own ring
6168 item := ring .New (1 )
62- item .Value = & pendingBlock {
69+ item .Value = & unconfirmedBlock {
6370 index : index ,
6471 hash : hash ,
6572 }
@@ -72,25 +79,20 @@ func (set *pendingBlockSet) Insert(index uint64, hash common.Hash) {
7279 } else {
7380 set .blocks .Move (- 1 ).Link (item )
7481 }
75- // Display a log for the user to notify of a new mined block pending
82+ // Display a log for the user to notify of a new mined block unconfirmed
7683 glog .V (logger .Info ).Infof ("🔨 mined potential block #%d [%x…], waiting for %d blocks to confirm" , index , hash .Bytes ()[:4 ], set .depth )
7784}
7885
79- // Shift drops all pending blocks from the set which exceed the pending sets depth
86+ // Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth
8087// allowance, checking them against the canonical chain for inclusion or staleness
8188// report.
82- func (set * pendingBlockSet ) Shift (height uint64 ) {
89+ func (set * unconfirmedBlocks ) Shift (height uint64 ) {
8390 set .lock .Lock ()
8491 defer set .lock .Unlock ()
8592
86- // Short circuit if there are no pending blocks to shift
87- if set .blocks == nil {
88- return
89- }
90- // Otherwise shift all blocks below the depth allowance
9193 for set .blocks != nil {
92- // Retrieve the next pending block and abort if too fresh
93- next := set .blocks .Value .(* pendingBlock )
94+ // Retrieve the next unconfirmed block and abort if too fresh
95+ next := set .blocks .Value .(* unconfirmedBlock )
9496 if next .index + uint64 (set .depth ) > height {
9597 break
9698 }
0 commit comments