Skip to content

Commit a578db5

Browse files
committed
improve documentation and move one test
1 parent 8767179 commit a578db5

9 files changed

Lines changed: 202 additions & 156 deletions

File tree

‎blockpool/blockpool.go‎

Lines changed: 98 additions & 78 deletions
Large diffs are not rendered by default.

‎blockpool/blockpool_test.go‎

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/ethereum/go-ethereum/core/types"
1010
)
1111

12+
// using the mock framework in blockpool_util_test
13+
// we test various scenarios here
14+
1215
func TestPeerWithKnownBlock(t *testing.T) {
1316
test.LogInit()
1417
_, blockPool, blockPoolTester := newTestBlockPool(t)
@@ -44,50 +47,6 @@ func TestPeerWithKnownParentBlock(t *testing.T) {
4447
blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
4548
}
4649

47-
func TestPeerPromotionByOptionalTdOnBlock(t *testing.T) {
48-
test.LogInit()
49-
_, blockPool, blockPoolTester := newTestBlockPool(t)
50-
blockPoolTester.blockChain[0] = nil
51-
blockPoolTester.initRefBlockChain(4)
52-
peer0 := blockPoolTester.newPeer("peer0", 2, 2)
53-
peer1 := blockPoolTester.newPeer("peer1", 1, 1)
54-
peer2 := blockPoolTester.newPeer("peer2", 4, 4)
55-
56-
blockPool.Start()
57-
blockPoolTester.tds = make(map[int]int)
58-
blockPoolTester.tds[3] = 3
59-
60-
// pool
61-
peer0.AddPeer()
62-
peer0.serveBlocks(1, 2)
63-
best := peer1.AddPeer()
64-
// this tests that peer1 is not promoted over peer0 yet
65-
if best {
66-
t.Errorf("peer1 (TD=1) should not be set as best")
67-
}
68-
best = peer2.AddPeer()
69-
peer2.serveBlocks(3, 4)
70-
peer2.serveBlockHashes(4, 3, 2, 1)
71-
hashes := blockPoolTester.hashPool.IndexesToHashes([]int{2, 3})
72-
peer1.waitBlocksRequests(3)
73-
blockPool.AddBlock(&types.Block{
74-
HeaderHash: common.Hash(hashes[1]),
75-
ParentHeaderHash: common.Hash(hashes[0]),
76-
Td: common.Big3,
77-
}, "peer1")
78-
79-
blockPool.RemovePeer("peer2")
80-
if blockPool.peers.best.id != "peer1" {
81-
t.Errorf("peer1 (TD=3) should be set as best")
82-
}
83-
peer1.serveBlocks(0, 1, 2)
84-
85-
blockPool.Wait(waitTimeout)
86-
blockPool.Stop()
87-
blockPoolTester.refBlockChain[4] = []int{}
88-
blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
89-
}
90-
9150
func TestSimpleChain(t *testing.T) {
9251
test.LogInit()
9352
_, blockPool, blockPoolTester := newTestBlockPool(t)
@@ -166,6 +125,7 @@ func TestNewBlocksOnPartialChain(t *testing.T) {
166125
go peer1.serveBlocks(4, 5) // partially complete section
167126
go peer1.serveBlockHashes(5, 4, 3)
168127
peer1.serveBlocks(3, 4) // partially complete section
128+
169129
// peer1 found new blocks
170130
peer1.td = 7
171131
peer1.currentBlock = 7
@@ -176,7 +136,6 @@ func TestNewBlocksOnPartialChain(t *testing.T) {
176136
go peer1.serveBlocks(5, 6)
177137
go peer1.serveBlockHashes(3, 2, 1) // tests that hash request from known chain root is remembered
178138
peer1.serveBlocks(0, 1, 2)
179-
// blockPool.RemovePeer("peer1")
180139

181140
blockPool.Wait(waitTimeout)
182141
blockPool.Stop()
@@ -468,17 +427,16 @@ func TestForkCompleteSectionSwitchBackByPeerSwitchBack(t *testing.T) {
468427
peer1.AddPeer()
469428
go peer1.serveBlocks(8, 9)
470429
go peer1.serveBlockHashes(9, 8, 7)
471-
peer1.serveBlocks(3, 7, 8) // make sure this section is complete
472-
time.Sleep(1 * time.Second)
430+
peer1.serveBlocks(3, 7, 8) // make sure this section is complete
431+
time.Sleep(1 * time.Second) //
473432
go peer1.serveBlockHashes(7, 3, 2) // block 3/7 is section boundary
474433
peer1.serveBlocks(2, 3) // partially complete sections block 2 missing
475434
peer2.AddPeer() //
476435
go peer2.serveBlocks(5, 6) //
477436
go peer2.serveBlockHashes(6, 5, 4, 3, 2) // peer2 forks on block 3
478437
peer2.serveBlocks(2, 3, 4, 5) // block 2 still missing.
479438
blockPool.RemovePeer("peer2") // peer2 disconnects, peer1 is promoted again as best peer
480-
// peer1.serveBlockHashes(7, 3) // tests that hash request from fork root is remembered even though section process completed
481-
go peer1.serveBlockHashes(2, 1, 0) //
439+
go peer1.serveBlockHashes(2, 1, 0) //
482440
peer1.serveBlocks(0, 1, 2)
483441

484442
blockPool.Wait(waitTimeout)

‎blockpool/blockpool_util_test.go‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func (self *blockPoolTester) Errorf(format string, params ...interface{}) {
6666
}
6767

6868
// blockPoolTester implements the 3 callbacks needed by the blockPool:
69-
// hasBlock, insetChain, verifyPoW
69+
// hasBlock, insetChain, verifyPoW as well as provides the eventer
70+
// to subscribe to head insertions
7071
func (self *blockPoolTester) hasBlock(block common.Hash) (ok bool) {
7172
self.lock.RLock()
7273
defer self.lock.RUnlock()
@@ -77,6 +78,7 @@ func (self *blockPoolTester) hasBlock(block common.Hash) (ok bool) {
7778
return
7879
}
7980

81+
// mock insertChain relies on refBlockChain to determine block validity
8082
func (self *blockPoolTester) insertChain(blocks types.Blocks) error {
8183
self.lock.Lock()
8284
defer self.lock.Unlock()
@@ -127,6 +129,7 @@ func (self *blockPoolTester) insertChain(blocks types.Blocks) error {
127129
return nil
128130
}
129131

132+
// mock soft block validation always succeeds
130133
func (self *blockPoolTester) verifyPoW(pblock pow.Block) bool {
131134
return true
132135
}
@@ -152,24 +155,24 @@ func (self *blockPoolTester) checkBlockChain(blockChain map[int][]int) {
152155
}
153156
}
154157

155-
//
156-
157158
// peerTester provides the peer callbacks for the blockPool
158159
// it registers actual callbacks so that the result can be compared to desired behaviour
159160
// provides helper functions to mock the protocol calls to the blockPool
160161
type peerTester struct {
162+
// containers to record request and error callbacks
161163
blockHashesRequests []int
162164
blocksRequests [][]int
163165
blocksRequestsMap map[int]bool
164166
peerErrors []int
165-
blockPool *BlockPool
166-
hashPool *test.TestHashPool
167-
lock sync.RWMutex
168-
bt *blockPoolTester
169-
id string
170-
td int
171-
currentBlock int
172-
t *testing.T
167+
168+
blockPool *BlockPool
169+
hashPool *test.TestHashPool
170+
lock sync.RWMutex
171+
bt *blockPoolTester
172+
id string
173+
td int
174+
currentBlock int
175+
t *testing.T
173176
}
174177

175178
// peerTester constructor takes hashPool and blockPool from the blockPoolTester
@@ -222,6 +225,7 @@ func (self *peerTester) checkBlockHashesRequests(blocksHashesRequests ...int) {
222225

223226
// waiter function used by peer.serveBlocks
224227
// blocking until requests appear
228+
// this mocks proper wire protocol behaviour
225229
// since block requests are sent to any random peers
226230
// block request map is shared between peers
227231
// times out after waitTimeout
@@ -254,6 +258,7 @@ func (self *peerTester) waitBlocksRequests(blocksRequest ...int) {
254258

255259
// waiter function used by peer.serveBlockHashes
256260
// blocking until requests appear
261+
// this mocks proper wire protocol behaviour
257262
// times out after a period
258263
func (self *peerTester) waitBlockHashesRequests(blocksHashesRequest int) {
259264
timeout := time.After(waitTimeout)
@@ -299,6 +304,7 @@ func (self *peerTester) serveBlockHashes(indexes ...int) {
299304
self.sendBlockHashes(indexes...)
300305
}
301306

307+
// peer sends blockhashes not waiting for request
302308
func (self *peerTester) sendBlockHashes(indexes ...int) {
303309
// fmt.Printf("adding block hashes %v\n", indexes)
304310
hashes := self.hashPool.IndexesToHashes(indexes)
@@ -315,13 +321,14 @@ func (self *peerTester) sendBlockHashes(indexes ...int) {
315321
}
316322

317323
// peer sends blocks if and when there is a request
318-
// (in the shared request store, not necessarily to a person)
324+
// (in the shared request store, not necessarily to a specific peer)
319325
func (self *peerTester) serveBlocks(indexes ...int) {
320326
// fmt.Printf("ready to serve blocks %v\n", indexes[1:])
321327
self.waitBlocksRequests(indexes[1:]...)
322328
self.sendBlocks(indexes...)
323329
}
324330

331+
// peer sends blocks not waiting for request
325332
func (self *peerTester) sendBlocks(indexes ...int) {
326333
// fmt.Printf("adding blocks %v \n", indexes)
327334
hashes := self.hashPool.IndexesToHashes(indexes)
@@ -331,9 +338,10 @@ func (self *peerTester) sendBlocks(indexes ...int) {
331338
}
332339
}
333340

334-
// peer callbacks
335-
// -1 is special: not found (a hash never seen)
341+
// the 3 mock peer callbacks
342+
336343
// records block hashes requests by the blockPool
344+
// -1 is special: not found (a hash never seen)
337345
func (self *peerTester) requestBlockHashes(hash common.Hash) error {
338346
indexes := self.hashPool.HashesToIndexes([]common.Hash{hash})
339347
// fmt.Printf("[%s] block hash request %v %x\n", self.id, indexes[0], hash[:4])

‎blockpool/config_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ func TestBlockPoolConfig(t *testing.T) {
2323
test.CheckDuration("BlocksTimeout", c.BlocksTimeout, blocksTimeout, t)
2424
test.CheckDuration("IdleBestPeerTimeout", c.IdleBestPeerTimeout, idleBestPeerTimeout, t)
2525
test.CheckDuration("PeerSuspensionInterval", c.PeerSuspensionInterval, peerSuspensionInterval, t)
26+
test.CheckDuration("StatusUpdateInterval", c.StatusUpdateInterval, statusUpdateInterval, t)
2627
}
2728

2829
func TestBlockPoolOverrideConfig(t *testing.T) {
2930
test.LogInit()
3031
blockPool := &BlockPool{Config: &Config{}, chainEvents: &event.TypeMux{}}
31-
c := &Config{128, 32, 1, 0, 300 * time.Millisecond, 100 * time.Millisecond, 90 * time.Second, 0, 30 * time.Second, 30 * time.Second}
32+
c := &Config{128, 32, 1, 0, 300 * time.Millisecond, 100 * time.Millisecond, 90 * time.Second, 0, 30 * time.Second, 30 * time.Second, 4 * time.Second}
3233

3334
blockPool.Config = c
3435
blockPool.Start()
@@ -42,4 +43,5 @@ func TestBlockPoolOverrideConfig(t *testing.T) {
4243
test.CheckDuration("BlocksTimeout", c.BlocksTimeout, blocksTimeout, t)
4344
test.CheckDuration("IdleBestPeerTimeout", c.IdleBestPeerTimeout, 30*time.Second, t)
4445
test.CheckDuration("PeerSuspensionInterval", c.PeerSuspensionInterval, 30*time.Second, t)
46+
test.CheckDuration("StatusUpdateInterval", c.StatusUpdateInterval, 4*time.Second, t)
4547
}

‎blockpool/peers.go‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ethereum/go-ethereum/errs"
1212
)
1313

14+
// the blockpool's model of a peer
1415
type peer struct {
1516
lock sync.RWMutex
1617

@@ -104,12 +105,14 @@ func (self *peers) peerError(id string, code int, format string, params ...inter
104105
self.addToBlacklist(id)
105106
}
106107

108+
// record time of offence in blacklist to implement suspension for PeerSuspensionInterval
107109
func (self *peers) addToBlacklist(id string) {
108110
self.lock.Lock()
109111
defer self.lock.Unlock()
110112
self.blacklist[id] = time.Now()
111113
}
112114

115+
// suspended checks if peer is still suspended
113116
func (self *peers) suspended(id string) (s bool) {
114117
self.lock.Lock()
115118
defer self.lock.Unlock()
@@ -160,8 +163,8 @@ func (self *peer) setChainInfoFromBlock(block *types.Block) {
160163
}()
161164
}
162165

166+
// distribute block request among known peers
163167
func (self *peers) requestBlocks(attempts int, hashes []common.Hash) {
164-
// distribute block request among known peers
165168
self.lock.RLock()
166169
defer self.lock.RUnlock()
167170
peerCount := len(self.peers)
@@ -196,7 +199,9 @@ func (self *peers) requestBlocks(attempts int, hashes []common.Hash) {
196199
}
197200

198201
// addPeer implements the logic for blockpool.AddPeer
199-
// returns true iff peer is promoted as best peer in the pool
202+
// returns 2 bool values
203+
// 1. true iff peer is promoted as best peer in the pool
204+
// 2. true iff peer is still suspended
200205
func (self *peers) addPeer(
201206
td *big.Int,
202207
currentBlockHash common.Hash,
@@ -214,10 +219,13 @@ func (self *peers) addPeer(
214219
self.lock.Lock()
215220
p, found := self.peers[id]
216221
if found {
222+
// when called on an already connected peer, it means a newBlockMsg is received
223+
// peer head info is updated
217224
if p.currentBlockHash != currentBlockHash {
218225
previousBlockHash = p.currentBlockHash
219226
plog.Debugf("addPeer: Update peer <%s> with td %v and current block %s (was %v)", id, td, hex(currentBlockHash), hex(previousBlockHash))
220227
p.setChainInfo(td, currentBlockHash)
228+
221229
self.status.lock.Lock()
222230
self.status.values.NewBlocks++
223231
self.status.lock.Unlock()
@@ -235,7 +243,7 @@ func (self *peers) addPeer(
235243
}
236244
self.lock.Unlock()
237245

238-
// check peer current head
246+
// check if peer's current head block is known
239247
if self.bp.hasBlock(currentBlockHash) {
240248
// peer not ahead
241249
plog.Debugf("addPeer: peer <%v> with td %v and current block %s is behind", id, td, hex(currentBlockHash))
@@ -255,6 +263,7 @@ func (self *peers) addPeer(
255263
}
256264
best = true
257265
} else {
266+
// baseline is our own TD
258267
currentTD := self.bp.getTD()
259268
if self.best != nil {
260269
currentTD = self.best.td
@@ -314,6 +323,7 @@ func (self *peers) removePeer(id string) {
314323
func (self *BlockPool) switchPeer(oldp, newp *peer) {
315324

316325
// first quit AddBlockHashes, requestHeadSection and activateChain
326+
// by closing the old peer's switchC channel
317327
if oldp != nil {
318328
plog.DebugDetailf("<%s> quit peer processes", oldp.id)
319329
close(oldp.switchC)
@@ -366,11 +376,12 @@ func (self *BlockPool) switchPeer(oldp, newp *peer) {
366376
// newp activating section process changes the quit channel for this reason
367377
if oldp != nil {
368378
plog.DebugDetailf("<%s> quit section processes", oldp.id)
369-
//
370379
close(oldp.idleC)
371380
}
372381
}
373382

383+
// getPeer looks up peer by id, returns peer and a bool value
384+
// that is true iff peer is current best peer
374385
func (self *peers) getPeer(id string) (p *peer, best bool) {
375386
self.lock.RLock()
376387
defer self.lock.RUnlock()
@@ -381,6 +392,8 @@ func (self *peers) getPeer(id string) (p *peer, best bool) {
381392
return
382393
}
383394

395+
// head section process
396+
384397
func (self *peer) handleSection(sec *section) {
385398
self.lock.Lock()
386399
defer self.lock.Unlock()
@@ -516,7 +529,7 @@ func (self *peer) run() {
516529
LOOP:
517530
for {
518531
select {
519-
// to minitor section process behaviou
532+
// to minitor section process behaviour
520533
case <-ping.C:
521534
plog.Debugf("HeadSection: <%s> section with head %s, idle: %v", self.id, hex(self.currentBlockHash), self.idle)
522535

‎blockpool/peers_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,47 @@ func TestAddPeer(t *testing.T) {
142142

143143
blockPool.Stop()
144144
}
145+
146+
func TestPeerPromotionByOptionalTdOnBlock(t *testing.T) {
147+
test.LogInit()
148+
_, blockPool, blockPoolTester := newTestBlockPool(t)
149+
blockPoolTester.blockChain[0] = nil
150+
blockPoolTester.initRefBlockChain(4)
151+
peer0 := blockPoolTester.newPeer("peer0", 2, 2)
152+
peer1 := blockPoolTester.newPeer("peer1", 1, 1)
153+
peer2 := blockPoolTester.newPeer("peer2", 4, 4)
154+
155+
blockPool.Start()
156+
blockPoolTester.tds = make(map[int]int)
157+
blockPoolTester.tds[3] = 3
158+
159+
// pool
160+
peer0.AddPeer()
161+
peer0.serveBlocks(1, 2)
162+
best := peer1.AddPeer()
163+
// this tests that peer1 is not promoted over peer0 yet
164+
if best {
165+
t.Errorf("peer1 (TD=1) should not be set as best")
166+
}
167+
best = peer2.AddPeer()
168+
peer2.serveBlocks(3, 4)
169+
peer2.serveBlockHashes(4, 3, 2, 1)
170+
hashes := blockPoolTester.hashPool.IndexesToHashes([]int{2, 3})
171+
peer1.waitBlocksRequests(3)
172+
blockPool.AddBlock(&types.Block{
173+
HeaderHash: common.Bytes(hashes[1]),
174+
ParentHeaderHash: common.Bytes(hashes[0]),
175+
Td: common.Big3,
176+
}, "peer1")
177+
178+
blockPool.RemovePeer("peer2")
179+
if blockPool.peers.best.id != "peer1" {
180+
t.Errorf("peer1 (TD=3) should be set as best")
181+
}
182+
peer1.serveBlocks(0, 1, 2)
183+
184+
blockPool.Wait(waitTimeout)
185+
blockPool.Stop()
186+
blockPoolTester.refBlockChain[4] = []int{}
187+
blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
188+
}

0 commit comments

Comments
 (0)