@@ -21,45 +21,52 @@ import (
21
21
"sync"
22
22
23
23
"github.com/ethereum/go-ethereum/common"
24
- "github.com/ethereum/go-ethereum/core"
24
+ "github.com/ethereum/go-ethereum/core/types "
25
25
"github.com/ethereum/go-ethereum/logger"
26
26
"github.com/ethereum/go-ethereum/logger/glog"
27
27
)
28
28
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 {
32
39
index uint64
33
40
hash common.Hash
34
41
}
35
42
36
- // pendingBlockSet implements a data structure to maintain locally mined blocks
43
+ // unconfirmedBlocks implements a data structure to maintain locally mined blocks
37
44
// have have not yet reached enough maturity to guarantee chain inclusion. It is
38
45
// used by the miner to provide logs to the user when a previously mined block
39
46
// 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
45
52
}
46
53
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 {
50
57
chain : chain ,
51
58
depth : depth ,
52
59
}
53
60
}
54
61
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 ) {
57
64
// If a new block was mined locally, shift out any old enough blocks
58
65
set .Shift (index )
59
66
60
67
// Create the new item as its own ring
61
68
item := ring .New (1 )
62
- item .Value = & pendingBlock {
69
+ item .Value = & unconfirmedBlock {
63
70
index : index ,
64
71
hash : hash ,
65
72
}
@@ -72,25 +79,20 @@ func (set *pendingBlockSet) Insert(index uint64, hash common.Hash) {
72
79
} else {
73
80
set .blocks .Move (- 1 ).Link (item )
74
81
}
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
76
83
glog .V (logger .Info ).Infof ("🔨 mined potential block #%d [%x…], waiting for %d blocks to confirm" , index , hash .Bytes ()[:4 ], set .depth )
77
84
}
78
85
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
80
87
// allowance, checking them against the canonical chain for inclusion or staleness
81
88
// report.
82
- func (set * pendingBlockSet ) Shift (height uint64 ) {
89
+ func (set * unconfirmedBlocks ) Shift (height uint64 ) {
83
90
set .lock .Lock ()
84
91
defer set .lock .Unlock ()
85
92
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
91
93
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 )
94
96
if next .index + uint64 (set .depth ) > height {
95
97
break
96
98
}
0 commit comments