From 7c900edd2db53fc45c2eca9511dc2951e6e611d6 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 2 Dec 2019 10:51:45 +0300 Subject: [PATCH] network: count only handshaked peers as connected --- pkg/network/server.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index 28e1c71a9..2b5ec376f 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -253,7 +253,7 @@ func (s *Server) tryStartConsensus() { return } - if s.PeerCount() >= s.MinPeers { + if s.HandshakedPeersCount() >= s.MinPeers { log.Info("minimum amount of peers were connected to") if s.connected.CAS(false, true) { s.consensus.Start() @@ -282,6 +282,23 @@ func (s *Server) PeerCount() int { return len(s.peers) } +// HandshakedPeersCount returns the number of connected peers +// which have already performed handshake. +func (s *Server) HandshakedPeersCount() int { + s.lock.RLock() + defer s.lock.RUnlock() + + var count int + + for p := range s.peers { + if p.Handshaked() { + count++ + } + } + + return count +} + // startProtocol starts a long running background loop that interacts // every ProtoTickInterval with the peer. func (s *Server) startProtocol(p Peer) {