Skip to content

Commit 07311f3

Browse files
committed
miner: rename pending to unconformed, add bounds and ops tests
1 parent 17637ed commit 07311f3

File tree

3 files changed

+117
-30
lines changed

3 files changed

+117
-30
lines changed

miner/pending.go renamed to miner/unconfirmed.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

miner/unconfirmed_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
17+
package miner
18+
19+
import (
20+
"testing"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/core/types"
24+
)
25+
26+
// noopHeaderRetriever is an implementation of headerRetriever that always
27+
// returns nil for any requested headers.
28+
type noopHeaderRetriever struct{}
29+
30+
func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header {
31+
return nil
32+
}
33+
34+
// Tests that inserting blocks into the unconfirmed set accumulates them until
35+
// the desired depth is reached, after which they begin to be dropped.
36+
func TestUnconfirmedInsertBounds(t *testing.T) {
37+
limit := uint(10)
38+
39+
pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
40+
for depth := uint64(0); depth < 2*uint64(limit); depth++ {
41+
// Insert multiple blocks for the same level just to stress it
42+
for i := 0; i < int(depth); i++ {
43+
pool.Insert(depth, common.Hash([32]byte{byte(depth), byte(i)}))
44+
}
45+
// Validate that no blocks below the depth allowance are left in
46+
pool.blocks.Do(func(block interface{}) {
47+
if block := block.(*unconfirmedBlock); block.index+uint64(limit) <= depth {
48+
t.Errorf("depth %d: block %x not dropped", depth, block.hash)
49+
}
50+
})
51+
}
52+
}
53+
54+
// Tests that shifting blocks out of the unconfirmed set works both for normal
55+
// cases as well as for corner cases such as empty sets, empty shifts or full
56+
// shifts.
57+
func TestUnconfirmedShifts(t *testing.T) {
58+
// Create a pool with a few blocks on various depths
59+
limit, start := uint(10), uint64(25)
60+
61+
pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
62+
for depth := start; depth < start+uint64(limit); depth++ {
63+
pool.Insert(depth, common.Hash([32]byte{byte(depth)}))
64+
}
65+
// Try to shift below the limit and ensure no blocks are dropped
66+
pool.Shift(start + uint64(limit) - 1)
67+
if n := pool.blocks.Len(); n != int(limit) {
68+
t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit)
69+
}
70+
// Try to shift half the blocks out and verify remainder
71+
pool.Shift(start + uint64(limit) - 1 + uint64(limit/2))
72+
if n := pool.blocks.Len(); n != int(limit)/2 {
73+
t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit/2)
74+
}
75+
// Try to shift all the remaining blocks out and verify emptyness
76+
pool.Shift(start + 2*uint64(limit))
77+
if n := pool.blocks.Len(); n != 0 {
78+
t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
79+
}
80+
// Try to shift out from the empty set and make sure it doesn't break
81+
pool.Shift(start + 3*uint64(limit))
82+
if n := pool.blocks.Len(); n != 0 {
83+
t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
84+
}
85+
}

miner/worker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type worker struct {
117117
txQueueMu sync.Mutex
118118
txQueue map[common.Hash]*types.Transaction
119119

120-
minedBlocks *pendingBlockSet // set of locally mined blocks pending canonicalness confirmations
120+
unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations
121121

122122
// atomic status counters
123123
mining int32
@@ -140,7 +140,7 @@ func newWorker(config *params.ChainConfig, coinbase common.Address, eth Backend,
140140
coinbase: coinbase,
141141
txQueue: make(map[common.Hash]*types.Transaction),
142142
agents: make(map[Agent]struct{}),
143-
minedBlocks: newPendingBlockSet(eth.BlockChain(), 5),
143+
unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), 5),
144144
fullValidation: false,
145145
}
146146
worker.events = worker.mux.Subscribe(core.ChainHeadEvent{}, core.ChainSideEvent{}, core.TxPreEvent{})
@@ -341,7 +341,7 @@ func (self *worker) wait() {
341341
}(block, work.state.Logs(), work.receipts)
342342
}
343343
// Insert the block into the set of pending ones to wait for confirmations
344-
self.minedBlocks.Insert(block.NumberU64(), block.Hash())
344+
self.unconfirmed.Insert(block.NumberU64(), block.Hash())
345345

346346
if mustCommitNewWork {
347347
self.commitNewWork()
@@ -514,7 +514,7 @@ func (self *worker) commitNewWork() {
514514
// We only care about logging if we're actually mining.
515515
if atomic.LoadInt32(&self.mining) == 1 {
516516
glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", work.Block.Number(), work.tcount, len(uncles), time.Since(tstart))
517-
self.minedBlocks.Shift(work.Block.NumberU64() - 1)
517+
self.unconfirmed.Shift(work.Block.NumberU64() - 1)
518518
}
519519
self.push(work)
520520
}

0 commit comments

Comments
 (0)