Browse Source

fix nil pointer panic by checking if peer is nil

Fixes #1830

remember that PeerSet#Get can return nil
pull/1836/head
Anton Kaliaev 7 years ago
parent
commit
9752e059e1
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 11 additions and 8 deletions
  1. +6
    -5
      p2p/peer_set.go
  2. +5
    -3
      p2p/pex/pex_reactor.go

+ 6
- 5
p2p/peer_set.go View File

@ -55,8 +55,8 @@ func (ps *PeerSet) Add(peer Peer) error {
return nil
}
// Has returns true iff the PeerSet contains
// the peer referred to by this peerKey.
// Has returns true if the set contains the peer referred to by this
// peerKey, otherwise false.
func (ps *PeerSet) Has(peerKey ID) bool {
ps.mtx.Lock()
_, ok := ps.lookup[peerKey]
@ -64,8 +64,8 @@ func (ps *PeerSet) Has(peerKey ID) bool {
return ok
}
// HasIP returns true if the PeerSet contains the peer referred to by this IP
// address.
// HasIP returns true if the set contains the peer referred to by this IP
// address, otherwise false.
func (ps *PeerSet) HasIP(peerIP net.IP) bool {
ps.mtx.Lock()
defer ps.mtx.Unlock()
@ -85,7 +85,8 @@ func (ps *PeerSet) hasIP(peerIP net.IP) bool {
return false
}
// Get looks up a peer by the provided peerKey.
// Get looks up a peer by the provided peerKey. Returns nil if peer is not
// found.
func (ps *PeerSet) Get(peerKey ID) Peer {
ps.mtx.Lock()
defer ps.mtx.Unlock()


+ 5
- 3
p2p/pex/pex_reactor.go View File

@ -77,10 +77,10 @@ type PEXReactor struct {
attemptsToDial sync.Map // address (string) -> {number of attempts (int), last time dialed (time.Time)}
}
func (pexR *PEXReactor) minReceiveRequestInterval() time.Duration {
func (r *PEXReactor) minReceiveRequestInterval() time.Duration {
// NOTE: must be less than ensurePeersPeriod, otherwise we'll request
// peers too quickly from others and they'll think we're bad!
return pexR.ensurePeersPeriod / 3
return r.ensurePeersPeriod / 3
}
// PEXReactorConfig holds reactor specific configuration data.
@ -628,7 +628,9 @@ func (r *PEXReactor) crawlPeers() {
}
// Ask for more addresses
peer := r.Switch.Peers().Get(pi.Addr.ID)
r.RequestAddrs(peer)
if peer != nil {
r.RequestAddrs(peer)
}
}
}


Loading…
Cancel
Save