// Collect votes from peers, and return true if our sampling algorithm has terminated. Else, return false.
Collect votes from peers, and return true if our sampling algorithm has terminated. Else, return false.
// Collect votes from peers and return true if our sampling algorithm has terminated . Else return false .
func (s *SyncManager) collectVotesFromPeers(sampler *Snowball, votes chan<- Vote, samplerK int) (bool, error) {
	latestHeight := s.blocks.LatestHeight()
	peers, err := SelectPeers(s.client.ClosestPeers(), samplerK)
	if err != nil {
		s.logger.Warn().Msg("It looks like there are no peers for us to sync with. Retrying after 1 second...")
		s.wait(1 * time.Second)
		return false, errors.Wrap(err, "no peers for us to sync with")
	}
	var wg sync.WaitGroup
	wg.Add(len(peers))
	for _, p := range peers {
		go func(peer skademlia.ClosestPeer) {
			defer wg.Done()
			ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
			defer cancel()
			maybe, err := s.askIfWereOutOfSync(ctx, peer, latestHeight)
			if err != nil {
				return
			}
			votes <- &syncVote{voter: peer.ID(), outOfSync: maybe}
		}(p)
	}
	wg.Wait()
	if sampler.Decided() {
		close(votes)
		return true, nil
	}
	return false, nil
}
func ( s * SyncManager ) collectVotesFromPeers ( sampler * Snowball , votes chan <- Vote , samplerK int ) ( bool , error ) { latestHeight := s . blocks . LatestHeight ( ) peers , err := SelectPeers ( s . client . ClosestPeers ( ) , samplerK ) if err != nil { s . logger . Warn ( ) . Msg ( " " ) s . wait ( 1 * time . Second ) return false , errors . Wrap ( err , " " ) } var wg sync . WaitGroup wg . Add ( len ( peers ) ) for _ , p := range peers { go func ( peer skademlia . ClosestPeer ) { defer wg . Done ( ) ctx , cancel := context . WithTimeout ( context . Background ( ) , 1 * time . Second ) defer cancel ( ) maybe , err := s . askIfWereOutOfSync ( ctx , peer , latestHeight ) if err != nil { return } votes <- & syncVote { voter : peer . ID ( ) , outOfSync : maybe } } ( p ) } wg . Wait ( ) if sampler . Decided ( ) { close ( votes ) return true , nil } return false , nil }
Collect votes from peers, and return true if our sampling algorithm has terminated.
Collect votes from peers and return true if our sampling algorithm has terminated .
// Our sampling algorithm has terminated: stop processing votes and see if our sampler // has concluded that the majority of the network has told us to sync to the latest state.