|
|
@@ -21,45 +21,52 @@ import (
|
|
|
"sync"
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
- "github.com/ethereum/go-ethereum/core"
|
|
|
+ "github.com/ethereum/go-ethereum/core/types"
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
)
|
|
|
|
|
|
-// pendingBlock is a small collection of metadata about a locally mined block
|
|
|
-// that is placed into a pending set for canonical chain inclusion tracking.
|
|
|
-type pendingBlock struct {
|
|
|
+// headerRetriever is used by the unconfirmed block set to verify whether a previously
|
|
|
+// mined block is part of the canonical chain or not.
|
|
|
+type headerRetriever interface {
|
|
|
+ // GetHeaderByNumber retrieves the canonical header associated with a block number.
|
|
|
+ GetHeaderByNumber(number uint64) *types.Header
|
|
|
+}
|
|
|
+
|
|
|
+// unconfirmedBlock is a small collection of metadata about a locally mined block
|
|
|
+// that is placed into a unconfirmed set for canonical chain inclusion tracking.
|
|
|
+type unconfirmedBlock struct {
|
|
|
index uint64
|
|
|
hash common.Hash
|
|
|
}
|
|
|
|
|
|
-// pendingBlockSet implements a data structure to maintain locally mined blocks
|
|
|
+// unconfirmedBlocks implements a data structure to maintain locally mined blocks
|
|
|
// have have not yet reached enough maturity to guarantee chain inclusion. It is
|
|
|
// used by the miner to provide logs to the user when a previously mined block
|
|
|
// has a high enough guarantee to not be reorged out of te canonical chain.
|
|
|
-type pendingBlockSet struct {
|
|
|
- chain *core.BlockChain // Blockchain to verify canonical status through
|
|
|
- depth uint // Depth after which to discard previous blocks
|
|
|
- blocks *ring.Ring // Block infos to allow canonical chain cross checks
|
|
|
- lock sync.RWMutex // Protects the fields from concurrent access
|
|
|
+type unconfirmedBlocks struct {
|
|
|
+ chain headerRetriever // Blockchain to verify canonical status through
|
|
|
+ depth uint // Depth after which to discard previous blocks
|
|
|
+ blocks *ring.Ring // Block infos to allow canonical chain cross checks
|
|
|
+ lock sync.RWMutex // Protects the fields from concurrent access
|
|
|
}
|
|
|
|
|
|
-// newPendingBlockSet returns new data structure to track currently pending blocks.
|
|
|
-func newPendingBlockSet(chain *core.BlockChain, depth uint) *pendingBlockSet {
|
|
|
- return &pendingBlockSet{
|
|
|
+// newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
|
|
|
+func newUnconfirmedBlocks(chain headerRetriever, depth uint) *unconfirmedBlocks {
|
|
|
+ return &unconfirmedBlocks{
|
|
|
chain: chain,
|
|
|
depth: depth,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Insert adds a new block to the set of pending ones.
|
|
|
-func (set *pendingBlockSet) Insert(index uint64, hash common.Hash) {
|
|
|
+// Insert adds a new block to the set of unconfirmed ones.
|
|
|
+func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) {
|
|
|
// If a new block was mined locally, shift out any old enough blocks
|
|
|
set.Shift(index)
|
|
|
|
|
|
// Create the new item as its own ring
|
|
|
item := ring.New(1)
|
|
|
- item.Value = &pendingBlock{
|
|
|
+ item.Value = &unconfirmedBlock{
|
|
|
index: index,
|
|
|
hash: hash,
|
|
|
}
|
|
|
@@ -72,25 +79,20 @@ func (set *pendingBlockSet) Insert(index uint64, hash common.Hash) {
|
|
|
} else {
|
|
|
set.blocks.Move(-1).Link(item)
|
|
|
}
|
|
|
- // Display a log for the user to notify of a new mined block pending
|
|
|
+ // Display a log for the user to notify of a new mined block unconfirmed
|
|
|
glog.V(logger.Info).Infof("🔨 mined potential block #%d [%x…], waiting for %d blocks to confirm", index, hash.Bytes()[:4], set.depth)
|
|
|
}
|
|
|
|
|
|
-// Shift drops all pending blocks from the set which exceed the pending sets depth
|
|
|
+// Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth
|
|
|
// allowance, checking them against the canonical chain for inclusion or staleness
|
|
|
// report.
|
|
|
-func (set *pendingBlockSet) Shift(height uint64) {
|
|
|
+func (set *unconfirmedBlocks) Shift(height uint64) {
|
|
|
set.lock.Lock()
|
|
|
defer set.lock.Unlock()
|
|
|
|
|
|
- // Short circuit if there are no pending blocks to shift
|
|
|
- if set.blocks == nil {
|
|
|
- return
|
|
|
- }
|
|
|
- // Otherwise shift all blocks below the depth allowance
|
|
|
for set.blocks != nil {
|
|
|
- // Retrieve the next pending block and abort if too fresh
|
|
|
- next := set.blocks.Value.(*pendingBlock)
|
|
|
+ // Retrieve the next unconfirmed block and abort if too fresh
|
|
|
+ next := set.blocks.Value.(*unconfirmedBlock)
|
|
|
if next.index+uint64(set.depth) > height {
|
|
|
break
|
|
|
}
|