Skip to content

Commit 1136269

Browse files
committed
miner: differentiate between uncle and lost block
1 parent 67d6d0b commit 1136269

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

‎miner/unconfirmed.go‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ import (
2525
"github.com/ethereum/go-ethereum/log"
2626
)
2727

28-
// headerRetriever is used by the unconfirmed block set to verify whether a previously
28+
// chainRetriever is used by the unconfirmed block set to verify whether a previously
2929
// mined block is part of the canonical chain or not.
30-
type headerRetriever interface {
30+
type chainRetriever interface {
3131
// GetHeaderByNumber retrieves the canonical header associated with a block number.
3232
GetHeaderByNumber(number uint64) *types.Header
33+
34+
// GetBlockByNumber retrieves the canonical block associated with a block number.
35+
GetBlockByNumber(number uint64) *types.Block
3336
}
3437

3538
// unconfirmedBlock is a small collection of metadata about a locally mined block
@@ -44,14 +47,14 @@ type unconfirmedBlock struct {
4447
// used by the miner to provide logs to the user when a previously mined block
4548
// has a high enough guarantee to not be reorged out of the canonical chain.
4649
type unconfirmedBlocks struct {
47-
chain headerRetriever // Blockchain to verify canonical status through
48-
depth uint // Depth after which to discard previous blocks
49-
blocks *ring.Ring // Block infos to allow canonical chain cross checks
50-
lock sync.RWMutex // Protects the fields from concurrent access
50+
chain chainRetriever // Blockchain to verify canonical status through
51+
depth uint // Depth after which to discard previous blocks
52+
blocks *ring.Ring // Block infos to allow canonical chain cross checks
53+
lock sync.RWMutex // Protects the fields from concurrent access
5154
}
5255

5356
// newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
54-
func newUnconfirmedBlocks(chain headerRetriever, depth uint) *unconfirmedBlocks {
57+
func newUnconfirmedBlocks(chain chainRetriever, depth uint) *unconfirmedBlocks {
5558
return &unconfirmedBlocks{
5659
chain: chain,
5760
depth: depth,
@@ -103,7 +106,23 @@ func (set *unconfirmedBlocks) Shift(height uint64) {
103106
case header.Hash() == next.hash:
104107
log.Info("🔗 block reached canonical chain", "number", next.index, "hash", next.hash)
105108
default:
106-
log.Info("⑂ block became a side fork", "number", next.index, "hash", next.hash)
109+
// Block is not canonical, check whether we have an uncle or a lost block
110+
included := false
111+
for number := next.index; !included && number < next.index+uint64(set.depth) && number <= height; number++ {
112+
if block := set.chain.GetBlockByNumber(number); block != nil {
113+
for _, uncle := range block.Uncles() {
114+
if uncle.Hash() == next.hash {
115+
included = true
116+
break
117+
}
118+
}
119+
}
120+
}
121+
if included {
122+
log.Info("⑂ block became an uncle", "number", next.index, "hash", next.hash)
123+
} else {
124+
log.Info("😱 block lost", "number", next.index, "hash", next.hash)
125+
}
107126
}
108127
// Drop the block out of the ring
109128
if set.blocks.Value == set.blocks.Next().Value {

‎miner/unconfirmed_test.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import (
2323
"github.com/ethereum/go-ethereum/core/types"
2424
)
2525

26-
// noopHeaderRetriever is an implementation of headerRetriever that always
26+
// noopChainRetriever is an implementation of headerRetriever that always
2727
// returns nil for any requested headers.
28-
type noopHeaderRetriever struct{}
28+
type noopChainRetriever struct{}
2929

30-
func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header {
30+
func (r *noopChainRetriever) GetHeaderByNumber(number uint64) *types.Header {
31+
return nil
32+
}
33+
func (r *noopChainRetriever) GetBlockByNumber(number uint64) *types.Block {
3134
return nil
3235
}
3336

@@ -36,7 +39,7 @@ func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header {
3639
func TestUnconfirmedInsertBounds(t *testing.T) {
3740
limit := uint(10)
3841

39-
pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
42+
pool := newUnconfirmedBlocks(new(noopChainRetriever), limit)
4043
for depth := uint64(0); depth < 2*uint64(limit); depth++ {
4144
// Insert multiple blocks for the same level just to stress it
4245
for i := 0; i < int(depth); i++ {
@@ -58,7 +61,7 @@ func TestUnconfirmedShifts(t *testing.T) {
5861
// Create a pool with a few blocks on various depths
5962
limit, start := uint(10), uint64(25)
6063

61-
pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
64+
pool := newUnconfirmedBlocks(new(noopChainRetriever), limit)
6265
for depth := start; depth < start+uint64(limit); depth++ {
6366
pool.Insert(depth, common.Hash([32]byte{byte(depth)}))
6467
}

‎miner/worker.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const (
5555
resubmitAdjustChanSize = 10
5656

5757
// miningLogAtDepth is the number of confirmations before logging successful mining.
58-
miningLogAtDepth = 5
58+
miningLogAtDepth = 7
5959

6060
// minRecommitInterval is the minimal time interval to recreate the mining block with
6161
// any newly arrived transactions.

0 commit comments

Comments
 (0)