Skip to content

Commit 95038fc

Browse files
committed
Idle too long error incorrectly triggered even when peer sending new blocks
- fix status chain map uses common.Hash as key - add badPeers increment to IncorrectTD errors (forgotten) - signal head info update to head section process even if parent hash is not in pool (inserted), so that idle timer can be set to nil - suicideC -> peer.headInfoTimer - quit -> peer.bestIdleTimer - and they are properly set from within getBlockHashes and handleSection
1 parent 7f85608 commit 95038fc

4 files changed

Lines changed: 31 additions & 28 deletions

File tree

‎blockpool/blockpool.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,9 @@ func (self *BlockPool) checkTD(nodes ...*node) {
757757
plog.DebugDetailf("peer td %v =?= block td %v", n.td, n.block.Td)
758758
if n.td.Cmp(n.block.Td) != 0 {
759759
self.peers.peerError(n.blockBy, ErrIncorrectTD, "on block %x", n.hash)
760+
self.status.lock.Lock()
761+
self.status.badPeers[n.blockBy]++
762+
self.status.lock.Unlock()
760763
}
761764
}
762765
}

‎blockpool/peers.go‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ type peer struct {
4646
// timers for head section process
4747
blockHashesRequestTimer <-chan time.Time
4848
blocksRequestTimer <-chan time.Time
49-
suicideC <-chan time.Time
49+
headInfoTimer <-chan time.Time
50+
bestIdleTimer <-chan time.Time
5051

5152
addToBlacklist func(id string)
5253

@@ -256,8 +257,10 @@ func (self *peers) addPeer(
256257
plog.Debugf("addPeer: <%s> already the best peer. Request new head section info from %s", id, hex(currentBlockHash))
257258

258259
if (previousBlockHash != common.Hash{}) {
260+
plog.DebugDetailf("addPeer: <%s> head changed: %s -> %s ", id, hex(previousBlockHash), hex(currentBlockHash))
261+
p.headSectionC <- nil
259262
if entry := self.bp.get(previousBlockHash); entry != nil {
260-
p.headSectionC <- nil
263+
plog.DebugDetailf("addPeer: <%s> previous head : %v found in pool, activate", id, hex(previousBlockHash))
261264
self.bp.activateChain(entry.section, p, nil)
262265
p.sections = append(p.sections, previousBlockHash)
263266
}
@@ -410,16 +413,19 @@ func (self *peer) handleSection(sec *section) {
410413
self.bp.syncing()
411414
}
412415

413-
self.suicideC = time.After(self.bp.Config.BlockHashesTimeout)
416+
self.headInfoTimer = time.After(self.bp.Config.BlockHashesTimeout)
417+
self.bestIdleTimer = nil
414418

415419
plog.DebugDetailf("HeadSection: <%s> head block hash changed (mined block received). New head %s", self.id, hex(self.currentBlockHash))
416420
} else {
417421
if !self.idle {
418422
self.idle = true
419423
self.bp.wg.Done()
420424
}
421-
plog.DebugDetailf("HeadSection: <%s> (head: %s) head section [%s] created", self.id, hex(self.currentBlockHash), sectionhex(sec))
422-
self.suicideC = time.After(self.bp.Config.IdleBestPeerTimeout)
425+
426+
self.headInfoTimer = nil
427+
self.bestIdleTimer = time.After(self.bp.Config.IdleBestPeerTimeout)
428+
plog.DebugDetailf("HeadSection: <%s> (head: %s) head section [%s] created. Idle...", self.id, hex(self.currentBlockHash), sectionhex(sec))
423429
}
424430
}
425431

@@ -452,14 +458,13 @@ func (self *peer) getCurrentBlock(currentBlock *types.Block) {
452458
self.blocksRequestTimer = nil
453459
}
454460

455-
func (self *peer) getBlockHashes() {
461+
func (self *peer) getBlockHashes() bool {
456462
//if connecting parent is found
457463
if self.bp.hasBlock(self.parentHash) {
458464
plog.DebugDetailf("HeadSection: <%s> parent block %s found in blockchain", self.id, hex(self.parentHash))
459465
err := self.bp.insertChain(types.Blocks([]*types.Block{self.currentBlock}))
460466

461467
self.bp.status.lock.Lock()
462-
self.bp.status.badPeers[self.id]++
463468
self.bp.status.values.BlocksInChain++
464469
self.bp.status.values.BlocksInPool--
465470
if err != nil {
@@ -469,18 +474,18 @@ func (self *peer) getBlockHashes() {
469474
if self.currentBlock.Td != nil {
470475
if self.td.Cmp(self.currentBlock.Td) != 0 {
471476
self.addError(ErrIncorrectTD, "on block %x", self.currentBlockHash)
477+
self.bp.status.badPeers[self.id]++
472478
}
473479
}
474-
headKey := self.parentHash.Str()
480+
headKey := self.parentHash
475481
height := self.bp.status.chain[headKey] + 1
476-
self.bp.status.chain[self.currentBlockHash.Str()] = height
482+
self.bp.status.chain[self.currentBlockHash] = height
477483
if height > self.bp.status.values.LongestChain {
478484
self.bp.status.values.LongestChain = height
479485
}
480486
delete(self.bp.status.chain, headKey)
481487
}
482488
self.bp.status.lock.Unlock()
483-
484489
} else {
485490
if parent := self.bp.get(self.parentHash); parent != nil {
486491
if self.bp.get(self.currentBlockHash) == nil {
@@ -501,15 +506,17 @@ func (self *peer) getBlockHashes() {
501506
plog.DebugDetailf("HeadSection: <%s> section [%s] requestBlockHashes", self.id, sectionhex(self.headSection))
502507
self.requestBlockHashes(self.currentBlockHash)
503508
self.blockHashesRequestTimer = time.After(self.bp.Config.BlockHashesRequestInterval)
504-
return
509+
return false
505510
}
506511
}
507512
self.blockHashesRequestTimer = nil
508513
if !self.idle {
509514
self.idle = true
510-
self.suicideC = nil
515+
self.headInfoTimer = nil
516+
self.bestIdleTimer = time.After(self.bp.Config.IdleBestPeerTimeout)
511517
self.bp.wg.Done()
512518
}
519+
return true
513520
}
514521

515522
// main loop for head section process
@@ -522,9 +529,7 @@ func (self *peer) run() {
522529
self.blockHashesRequestTimer = nil
523530

524531
self.blocksRequestTimer = time.After(0)
525-
self.suicideC = time.After(self.bp.Config.BlockHashesTimeout)
526-
527-
var quit <-chan time.Time
532+
self.headInfoTimer = time.After(self.bp.Config.BlockHashesTimeout)
528533

529534
var ping = time.NewTicker(5 * time.Second)
530535

@@ -539,13 +544,6 @@ LOOP:
539544
// if sec == nil, it signals that chain info has updated (new block message)
540545
case sec := <-self.headSectionC:
541546
self.handleSection(sec)
542-
if sec == nil {
543-
plog.Debugf("HeadSection: <%s> (headsection [%s], received: [%s]) quit channel set to nil, catchup happening", self.id, sectionhex(self.headSection), sectionhex(sec))
544-
quit = nil
545-
} else {
546-
plog.Debugf("HeadSection: <%s> (headsection [%s], received: [%s]) quit channel set to go off in IdleBestPeerTimeout", self.id, sectionhex(self.headSection), sectionhex(sec))
547-
quit = time.After(self.bp.Config.IdleBestPeerTimeout)
548-
}
549547

550548
// periodic check for block hashes or parent block/section
551549
case <-self.blockHashesRequestTimer:
@@ -560,7 +558,7 @@ LOOP:
560558
self.getCurrentBlock(nil)
561559

562560
// quitting on timeout
563-
case <-self.suicideC:
561+
case <-self.headInfoTimer:
564562
self.peerError(self.bp.peers.errors.New(ErrInsufficientChainInfo, "timed out without providing block hashes or head block (td: %v, head: %s)", self.td, hex(self.currentBlockHash)))
565563

566564
self.bp.status.lock.Lock()
@@ -583,7 +581,7 @@ LOOP:
583581
break LOOP
584582

585583
// quit
586-
case <-quit:
584+
case <-self.bestIdleTimer:
587585
self.peerError(self.bp.peers.errors.New(ErrIdleTooLong, "timed out without providing new blocks (td: %v, head: %s)...quitting", self.td, hex(self.currentBlockHash)))
588586

589587
self.bp.status.lock.Lock()

‎blockpool/section.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ func (self *section) addSectionToBlockChain(p *peer) {
176176

177177
self.bp.status.lock.Lock()
178178
if err == nil {
179-
headKey := blocks[0].ParentHash().Str()
179+
headKey := blocks[0].ParentHash()
180180
height := self.bp.status.chain[headKey] + len(blocks)
181-
self.bp.status.chain[blocks[len(blocks)-1].Hash().Str()] = height
181+
self.bp.status.chain[blocks[len(blocks)-1].Hash()] = height
182182
if height > self.bp.status.values.LongestChain {
183183
self.bp.status.values.LongestChain = height
184184
}

‎blockpool/status.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package blockpool
33
import (
44
"fmt"
55
"sync"
6+
7+
"github.com/ethereum/go-ethereum/common"
68
)
79

810
type statusValues struct {
@@ -26,7 +28,7 @@ type statusValues struct {
2628
type status struct {
2729
lock sync.Mutex
2830
values statusValues
29-
chain map[string]int
31+
chain map[common.Hash]int
3032
peers map[string]int
3133
bestPeers map[string]int
3234
badPeers map[string]int
@@ -35,7 +37,7 @@ type status struct {
3537

3638
func newStatus() *status {
3739
return &status{
38-
chain: make(map[string]int),
40+
chain: make(map[common.Hash]int),
3941
peers: make(map[string]int),
4042
bestPeers: make(map[string]int),
4143
badPeers: make(map[string]int),

0 commit comments

Comments
 (0)