2019-12-30 17:54:38 -08:00
|
|
|
package zcash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/btcsuite/btcd/peer"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Seeder) onVerAck(p *peer.Peer, msg *wire.MsgVerAck) {
|
2020-05-20 15:08:34 -07:00
|
|
|
pk := peerKeyFromPeer(p)
|
|
|
|
|
2019-12-30 17:54:38 -08:00
|
|
|
// Check if we're expecting to hear from this peer
|
2020-05-20 15:08:34 -07:00
|
|
|
_, ok := s.pendingPeers.Load(pk)
|
2019-12-30 17:54:38 -08:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
s.logger.Printf("Got verack from unexpected peer %s", p.Addr())
|
2020-06-08 13:48:53 -07:00
|
|
|
// Disconnect from the peer sending us out-of-order veracks
|
|
|
|
s.DisconnectPeer(pk)
|
2019-12-30 17:54:38 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to set of live peers
|
2020-05-20 15:08:34 -07:00
|
|
|
s.livePeers.Store(pk, p)
|
2019-12-30 17:54:38 -08:00
|
|
|
|
|
|
|
// Remove from set of pending peers
|
2020-05-20 15:08:34 -07:00
|
|
|
s.pendingPeers.Delete(pk)
|
2019-12-30 17:54:38 -08:00
|
|
|
|
|
|
|
// Signal successful connection
|
2020-05-20 15:08:34 -07:00
|
|
|
if signal, ok := s.handshakeSignals.Load(pk); ok {
|
2019-12-30 17:54:38 -08:00
|
|
|
signal.(chan struct{}) <- struct{}{}
|
|
|
|
} else {
|
|
|
|
s.logger.Printf("Got verack from peer without a callback channel: %s", p.Addr())
|
2020-05-20 15:08:34 -07:00
|
|
|
s.DisconnectPeer(pk)
|
2019-12-30 17:54:38 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:40:43 -07:00
|
|
|
// If we've already connected to this peer, update the last-valid time.
|
2020-05-20 15:08:34 -07:00
|
|
|
if s.addrBook.IsKnown(pk) {
|
|
|
|
s.addrBook.Touch(pk)
|
2019-12-30 17:54:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Seeder) onAddr(p *peer.Peer, msg *wire.MsgAddr) {
|
|
|
|
if len(msg.AddrList) == 0 {
|
|
|
|
s.logger.Printf("Got empty addr message from peer %s. Disconnecting.", p.Addr())
|
|
|
|
s.DisconnectPeer(peerKeyFromPeer(p))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.logger.Printf("Got %d addrs from peer %s", len(msg.AddrList), p.Addr())
|
|
|
|
|
|
|
|
for _, na := range msg.AddrList {
|
2020-05-21 22:02:32 -07:00
|
|
|
// By checking if we know them before adding to the queue, we create
|
|
|
|
// the end condition for the crawler thread: it will time out after
|
|
|
|
// not processing any new addresses.
|
|
|
|
if s.addrBook.IsKnown(peerKeyFromNA(na)) {
|
|
|
|
s.logger.Printf("Already knew about %s:%d", na.IP, na.Port)
|
|
|
|
continue
|
|
|
|
}
|
2020-05-20 16:10:55 -07:00
|
|
|
s.addrQueue <- na
|
2019-12-30 17:54:38 -08:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 05:44:01 -08:00
|
|
|
|
|
|
|
// onAddrV2 handles addrv2 messages.
|
|
|
|
//
|
|
|
|
// If the address is IPv4 or IPv6, it handles it the same way as an addr message.
|
|
|
|
// If other address type is received, the address is ignored.
|
|
|
|
func (s *Seeder) onAddrV2(p *peer.Peer, msg *wire.MsgAddrV2) {
|
|
|
|
if len(msg.AddrList) == 0 {
|
|
|
|
s.logger.Printf("Got empty addrv2 message from peer %s. Disconnecting.", p.Addr())
|
|
|
|
s.DisconnectPeer(peerKeyFromPeer(p))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.logger.Printf("Got %d addrv2s from peer %s", len(msg.AddrList), p.Addr())
|
|
|
|
|
|
|
|
for _, na := range msg.AddrList {
|
|
|
|
if na.NetworkID == wire.NIIPV4 || na.NetworkID == wire.NIIPV6 {
|
|
|
|
// By checking if we know them before adding to the queue, we create
|
|
|
|
// the end condition for the crawler thread: it will time out after
|
|
|
|
// not processing any new addresses.
|
|
|
|
if s.addrBook.IsKnown(peerKeyFromNAV2(na)) {
|
|
|
|
s.logger.Printf("Already knew about %s:%d", na.IP, na.Port)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.addrQueue <- &na.NetAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|