|
@ -129,7 +129,7 @@ func (r *PEXReactor) AddPeer(p Peer) { |
|
|
// either via DialPeersAsync or r.Receive.
|
|
|
// either via DialPeersAsync or r.Receive.
|
|
|
// Ask it for more peers if we need.
|
|
|
// Ask it for more peers if we need.
|
|
|
if r.book.NeedMoreAddrs() { |
|
|
if r.book.NeedMoreAddrs() { |
|
|
r.RequestPEX(p) |
|
|
|
|
|
|
|
|
r.RequestAddrs(p) |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// For inbound peers, the peer is its own source,
|
|
|
// For inbound peers, the peer is its own source,
|
|
@ -159,15 +159,24 @@ func (r *PEXReactor) Receive(chID byte, src Peer, msgBytes []byte) { |
|
|
|
|
|
|
|
|
switch msg := msg.(type) { |
|
|
switch msg := msg.(type) { |
|
|
case *pexRequestMessage: |
|
|
case *pexRequestMessage: |
|
|
// We received a request for peers from src.
|
|
|
|
|
|
|
|
|
// Check we're not receiving too many requests
|
|
|
if err := r.receiveRequest(src); err != nil { |
|
|
if err := r.receiveRequest(src); err != nil { |
|
|
r.Switch.StopPeerForError(src, err) |
|
|
r.Switch.StopPeerForError(src, err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
r.SendAddrs(src, r.book.GetSelection()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Seeds disconnect after sending a batch of addrs
|
|
|
|
|
|
if r.config.SeedMode { |
|
|
|
|
|
// TODO: should we be more selective ?
|
|
|
|
|
|
r.SendAddrs(src, r.book.GetSelection()) |
|
|
|
|
|
r.Switch.StopPeerGracefully(src) |
|
|
|
|
|
} else { |
|
|
|
|
|
r.SendAddrs(src, r.book.GetSelection()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
case *pexAddrsMessage: |
|
|
case *pexAddrsMessage: |
|
|
// We received some peer addresses from src.
|
|
|
|
|
|
if err := r.ReceivePEX(msg.Addrs, src); err != nil { |
|
|
|
|
|
|
|
|
// If we asked for addresses, add them to the book
|
|
|
|
|
|
if err := r.ReceiveAddrs(msg.Addrs, src); err != nil { |
|
|
r.Switch.StopPeerForError(src, err) |
|
|
r.Switch.StopPeerForError(src, err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -202,9 +211,9 @@ func (r *PEXReactor) receiveRequest(src Peer) error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// RequestPEX asks peer for more addresses if we do not already
|
|
|
|
|
|
|
|
|
// RequestAddrs asks peer for more addresses if we do not already
|
|
|
// have a request out for this peer.
|
|
|
// have a request out for this peer.
|
|
|
func (r *PEXReactor) RequestPEX(p Peer) { |
|
|
|
|
|
|
|
|
func (r *PEXReactor) RequestAddrs(p Peer) { |
|
|
id := string(p.ID()) |
|
|
id := string(p.ID()) |
|
|
if r.requestsSent.Has(id) { |
|
|
if r.requestsSent.Has(id) { |
|
|
return |
|
|
return |
|
@ -213,10 +222,10 @@ func (r *PEXReactor) RequestPEX(p Peer) { |
|
|
p.Send(PexChannel, struct{ PexMessage }{&pexRequestMessage{}}) |
|
|
p.Send(PexChannel, struct{ PexMessage }{&pexRequestMessage{}}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ReceivePEX adds the given addrs to the addrbook if theres an open
|
|
|
|
|
|
|
|
|
// ReceiveAddrs adds the given addrs to the addrbook if theres an open
|
|
|
// request for this peer and deletes the open request.
|
|
|
// request for this peer and deletes the open request.
|
|
|
// If there's no open request for the src peer, it returns an error.
|
|
|
// If there's no open request for the src peer, it returns an error.
|
|
|
func (r *PEXReactor) ReceivePEX(addrs []*NetAddress, src Peer) error { |
|
|
|
|
|
|
|
|
func (r *PEXReactor) ReceiveAddrs(addrs []*NetAddress, src Peer) error { |
|
|
id := string(src.ID()) |
|
|
id := string(src.ID()) |
|
|
|
|
|
|
|
|
if !r.requestsSent.Has(id) { |
|
|
if !r.requestsSent.Has(id) { |
|
@ -323,7 +332,7 @@ func (r *PEXReactor) ensurePeers() { |
|
|
if peersCount > 0 { |
|
|
if peersCount > 0 { |
|
|
peer := peers[rand.Int()%peersCount] // nolint: gas
|
|
|
peer := peers[rand.Int()%peersCount] // nolint: gas
|
|
|
r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer) |
|
|
r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer) |
|
|
r.RequestPEX(peer) |
|
|
|
|
|
|
|
|
r.RequestAddrs(peer) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -421,7 +430,7 @@ func (of oldestFirst) Less(i, j int) bool { return of[i].LastAttempt.Before(of[j |
|
|
func (r *PEXReactor) getPeersToCrawl() []crawlPeerInfo { |
|
|
func (r *PEXReactor) getPeersToCrawl() []crawlPeerInfo { |
|
|
var of oldestFirst |
|
|
var of oldestFirst |
|
|
|
|
|
|
|
|
// TODO: not this. be more selective
|
|
|
|
|
|
|
|
|
// TODO: be more selective
|
|
|
addrs := r.book.ListOfKnownAddresses() |
|
|
addrs := r.book.ListOfKnownAddresses() |
|
|
for _, addr := range addrs { |
|
|
for _, addr := range addrs { |
|
|
if len(addr.ID()) == 0 { |
|
|
if len(addr.ID()) == 0 { |
|
@ -462,7 +471,7 @@ func (r *PEXReactor) crawlPeers() { |
|
|
if now.Sub(pi.LastAttempt) >= defaultCrawlPeerInterval { |
|
|
if now.Sub(pi.LastAttempt) >= defaultCrawlPeerInterval { |
|
|
peer := r.Switch.Peers().Get(pi.Addr.ID) |
|
|
peer := r.Switch.Peers().Get(pi.Addr.ID) |
|
|
if peer != nil { |
|
|
if peer != nil { |
|
|
r.RequestPEX(peer) |
|
|
|
|
|
|
|
|
r.RequestAddrs(peer) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|