network: count only handshaked peers as connected

This commit is contained in:
Evgenii Stratonikov 2019-12-02 10:51:45 +03:00
parent 65332f5e7f
commit 7c900edd2d

View file

@ -253,7 +253,7 @@ func (s *Server) tryStartConsensus() {
return return
} }
if s.PeerCount() >= s.MinPeers { if s.HandshakedPeersCount() >= s.MinPeers {
log.Info("minimum amount of peers were connected to") log.Info("minimum amount of peers were connected to")
if s.connected.CAS(false, true) { if s.connected.CAS(false, true) {
s.consensus.Start() s.consensus.Start()
@ -282,6 +282,23 @@ func (s *Server) PeerCount() int {
return len(s.peers) 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 // startProtocol starts a long running background loop that interacts
// every ProtoTickInterval with the peer. // every ProtoTickInterval with the peer.
func (s *Server) startProtocol(p Peer) { func (s *Server) startProtocol(p Peer) {