Skip to content

Commit 3ee0e7f

Browse files
authored
Fix handshake and dial timeout (#247)
* Fix handshake and dial timeout * Add some logs * Fix bpr requester different error * fix locking
1 parent e5eddb0 commit 3ee0e7f

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

internal/blocksync/pool.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,15 @@ func (pool *BlockPool) AddBlock(peerID types.NodeID, block *types.Block, extComm
320320
return fmt.Errorf("peer sent us a block we didn't expect (peer: %s, current height: %d, block height: %d)", peerID, pool.height, block.Height)
321321
}
322322

323-
if requester.setBlock(block, extCommit, peerID) {
323+
setBlockResult := requester.setBlock(block, extCommit, peerID)
324+
if setBlockResult == 0 {
324325
atomic.AddInt32(&pool.numPending, -1)
325326
peer := pool.peers[peerID]
326327
if peer != nil {
327328
peer.decrPending(blockSize)
328329
}
329-
} else {
330-
err := errors.New("requester is different or block already exists")
330+
} else if setBlockResult < 0 {
331+
err := errors.New("bpr requester peer is different from original peer")
331332
pool.sendError(err, peerID)
332333
return fmt.Errorf("%w (peer: %s, requester: %s, block height: %d)", err, peerID, requester.getPeerID(), block.Height)
333334
}
@@ -671,24 +672,26 @@ func (bpr *bpRequester) OnStart(ctx context.Context) error {
671672

672673
func (*bpRequester) OnStop() {}
673674

674-
// Returns true if the peer matches and block doesn't already exist.
675-
func (bpr *bpRequester) setBlock(block *types.Block, extCommit *types.ExtendedCommit, peerID types.NodeID) bool {
675+
// Returns 0 if block doesn't already exist.
676+
// Returns -1 if block exist but peers doesn't match.
677+
// Return 1 if block exist and peer matches.
678+
func (bpr *bpRequester) setBlock(block *types.Block, extCommit *types.ExtendedCommit, peerID types.NodeID) int {
676679
bpr.mtx.Lock()
677-
if bpr.block != nil || bpr.peerID != peerID {
678-
bpr.mtx.Unlock()
679-
return false
680-
}
681-
bpr.block = block
682-
if extCommit != nil {
683-
bpr.extCommit = extCommit
684-
}
685-
bpr.mtx.Unlock()
686-
687-
select {
688-
case bpr.gotBlockCh <- struct{}{}:
689-
default:
680+
defer bpr.mtx.Unlock()
681+
if bpr.block == nil {
682+
bpr.block = block
683+
if extCommit != nil {
684+
bpr.extCommit = extCommit
685+
}
686+
select {
687+
case bpr.gotBlockCh <- struct{}{}:
688+
default:
689+
}
690+
return 0
691+
} else if bpr.peerID == peerID {
692+
return 1
690693
}
691-
return true
694+
return -1
692695
}
693696

694697
func (bpr *bpRequester) getBlock() *types.Block {

internal/p2p/peermanager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ func (m *PeerManager) TryDialNext() (NodeAddress, error) {
553553

554554
// We now have an eligible address to dial. If we're full but have
555555
// upgrade capacity (as checked above), we find a lower-scored peer
556-
// we can replace and mark it as upgrading so noone else claims it.
556+
// we can replace and mark it as upgrading so no one else claims it.
557557
//
558558
// If we don't find one, there is no point in trying additional
559559
// peers, since they will all have the same or lower score than this
@@ -567,6 +567,7 @@ func (m *PeerManager) TryDialNext() (NodeAddress, error) {
567567
}
568568

569569
m.dialing[peer.ID] = true
570+
m.logger.Info(fmt.Sprintf("Going to dial peer %s with address %s", peer.ID, addressInfo.Address))
570571
return addressInfo.Address, nil
571572
}
572573
}

node/node.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,10 @@ func loadStateFromDBOrGenesisDocProvider(stateStore sm.Store, genDoc *types.Gene
808808

809809
func getRouterConfig(conf *config.Config, appClient abciclient.Client) p2p.RouterOptions {
810810
opts := p2p.RouterOptions{
811-
QueueType: conf.P2P.QueueType,
811+
QueueType: conf.P2P.QueueType,
812+
DialTimeout: conf.P2P.DialTimeout,
813+
HandshakeTimeout: conf.P2P.HandshakeTimeout,
814+
ResolveTimeout: conf.P2P.HandshakeTimeout,
812815
}
813816

814817
if conf.FilterPeers && appClient != nil {

0 commit comments

Comments
 (0)