network: check height before requesting headers

Only request headers from the other peer if his height is bigger than
ours. Otherwise we routinely ask 0-height newcomers for some random headers
that they know nothing about.
This commit is contained in:
Roman Khimov 2019-11-29 11:46:01 +03:00
parent a730529b0c
commit 4d286dcfeb

View file

@ -284,6 +284,8 @@ func (s *Server) PeerCount() int {
// startProtocol starts a long running background loop that interacts
// every ProtoTickInterval with the peer.
func (s *Server) startProtocol(p Peer) {
var err error
log.WithFields(log.Fields{
"addr": p.RemoteAddr(),
"userAgent": string(p.Version().UserAgent),
@ -292,11 +294,13 @@ func (s *Server) startProtocol(p Peer) {
}).Info("started protocol")
s.discovery.RegisterGoodAddr(p.PeerAddr().String())
err := s.requestHeaders(p)
if s.chain.HeaderHeight() < p.Version().StartHeight {
err = s.requestHeaders(p)
if err != nil {
p.Disconnect(err)
return
}
}
timer := time.NewTimer(s.ProtoTickInterval)
for {