network: move Version sending to the tcp transport

We have a race between reader and writer goroutines for the same connection
that leads to handshake failures when reader is faster to read the incoming
version (and try to reply to it) than writer is to write our own Version:

WARN[0000] peer disconnected                             addr="172.200.0.4:20334" peerCount=5 reason="invalid handshake: tried to send VersionAck, but didn't send Version yet

Fix it by moving Version sending before the reader loop starts.
This commit is contained in:
Roman Khimov 2019-11-29 11:05:42 +03:00
parent 2d41450ac9
commit 734338ad70
2 changed files with 6 additions and 6 deletions

View file

@ -200,12 +200,6 @@ func (s *Server) run() {
} }
return return
case p := <-s.register: case p := <-s.register:
// When a new peer is connected we send out our version immediately.
if err := s.sendVersion(p); err != nil {
log.WithFields(log.Fields{
"addr": p.RemoteAddr(),
}).Error(err)
}
s.lock.Lock() s.lock.Lock()
s.peers[p] = true s.peers[p] = true
s.lock.Unlock() s.lock.Unlock()

View file

@ -78,6 +78,12 @@ func (t *TCPTransport) handleConn(conn net.Conn) {
t.server.register <- p t.server.register <- p
// When a new peer is connected we send out our version immediately.
if err := t.server.sendVersion(p); err != nil {
log.WithFields(log.Fields{
"addr": p.RemoteAddr(),
}).Error(err)
}
r := io.NewBinReaderFromIO(p.conn) r := io.NewBinReaderFromIO(p.conn)
for { for {
msg := &Message{} msg := &Message{}