diff --git a/syncing/syncing.go b/syncing/syncing.go index afd1cb892..19bdc561f 100644 --- a/syncing/syncing.go +++ b/syncing/syncing.go @@ -171,14 +171,6 @@ func (syncConfig *SyncConfig) GetHowManyMaxConsensus() (int, int) { return maxFirstID, maxCount } -// InitForTesting used for testing. -func (syncConfig *SyncConfig) InitForTesting(client *downloader.Client, blockHashes [][]byte) { - for i := range syncConfig.peers { - syncConfig.peers[i].blockHashes = blockHashes - syncConfig.peers[i].client = client - } -} - // CleanUpPeers cleans up all peers whose blockHashes are not equal to consensus block hashes. func (syncConfig *SyncConfig) CleanUpPeers(maxFirstID int) { fixedPeer := syncConfig.peers[maxFirstID] @@ -195,6 +187,29 @@ func (syncConfig *SyncConfig) CleanUpPeers(maxFirstID int) { } } +// getBlockHashesConsensusAndCleanUp chesk if all consensus hashes are equal. +func (ss *StateSync) getBlockHashesConsensusAndCleanUp() bool { + // Sort all peers by the blockHashes. + sort.Slice(ss.syncConfig.peers, func(i, j int) bool { + return CompareSyncPeerConfigByblockHashes(ss.syncConfig.peers[i], ss.syncConfig.peers[j]) == -1 + }) + maxFirstID, maxCount := ss.syncConfig.GetHowManyMaxConsensus() + if float64(maxCount) >= ConsensusRatio*float64(ss.activePeerNumber) { + ss.syncConfig.CleanUpPeers(maxFirstID) + ss.CleanUpNilPeers() + return true + } + return false +} + +// InitForTesting used for testing. +func (syncConfig *SyncConfig) InitForTesting(client *downloader.Client, blockHashes [][]byte) { + for i := range syncConfig.peers { + syncConfig.peers[i].blockHashes = blockHashes + syncConfig.peers[i].client = client + } +} + // GetBlockHashesConsensusAndCleanUp chesk if all consensus hashes are equal. func (ss *StateSync) GetBlockHashesConsensusAndCleanUp() bool { // Sort all peers by the blockHashes.