Beispiel #1
0
        private void CleanupCandidatePeers()
        {
            if (_peerPool.CandidatePeerCount <= _networkConfig.CandidatePeerCountCleanupThreshold)
            {
                return;
            }

            // may further optimize allocations here
            List <Peer> candidates    = _peerPool.NonStaticCandidatePeers;
            int         countToRemove = candidates.Count - _networkConfig.MaxCandidatePeerCount;

            Peer[] failedValidationCandidates = candidates.Where(x => _stats.HasFailedValidation(x.Node))
                                                .OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            Peer[] otherCandidates = candidates.Except(failedValidationCandidates).Except(_activePeers.Values).OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            Peer[] nodesToRemove   = failedValidationCandidates.Length <= countToRemove
                ? failedValidationCandidates
                : failedValidationCandidates.Take(countToRemove).ToArray();
            int failedValidationRemovedCount = nodesToRemove.Length;
            int remainingCount = countToRemove - failedValidationRemovedCount;

            if (remainingCount > 0)
            {
                Peer[] otherToRemove = otherCandidates.Take(remainingCount).ToArray();
                nodesToRemove = nodesToRemove.Length == 0
                    ? otherToRemove :
                                nodesToRemove.Concat(otherToRemove).ToArray();
            }

            if (nodesToRemove.Length > 0)
            {
                _logger.Info($"Removing {nodesToRemove.Length} out of {candidates.Count} peer candidates (candidates cleanup).");
                foreach (Peer peer in nodesToRemove)
                {
                    _peerPool.TryRemove(peer.Node.Id, out _);
                }

                if (_logger.IsDebug)
                {
                    _logger.Debug($"Removing candidate peers: {nodesToRemove.Length}, failedValidationRemovedCount: {failedValidationRemovedCount}, otherRemovedCount: {remainingCount}, prevCount: {candidates.Count}, newCount: {_peerPool.CandidatePeerCount}, CandidatePeerCountCleanupThreshold: {_networkConfig.CandidatePeerCountCleanupThreshold}, MaxCandidatePeerCount: {_networkConfig.MaxCandidatePeerCount}");
                }
            }
        }
Beispiel #2
0
        private void CleanupCandidatePeers()
        {
            if (_candidatePeers.Count <= _networkConfig.CandidatePeerCountCleanupThreshold)
            {
                return;
            }

            // may further optimize allocations here
            var candidates    = _candidatePeers.Values.Where(p => !p.Node.IsStatic).ToArray();
            var countToRemove = candidates.Length - _networkConfig.MaxCandidatePeerCount;
            var failedValidationCandidates = candidates.Where(x => _stats.HasFailedValidation(x.Node))
                                             .OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            var otherCandidates = candidates.Except(failedValidationCandidates).OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            var nodesToRemove   = failedValidationCandidates.Take(countToRemove).ToArray();
            var failedValidationRemovedCount = nodesToRemove.Length;
            var remainingCount = countToRemove - failedValidationRemovedCount;

            if (remainingCount > 0)
            {
                nodesToRemove = nodesToRemove.Concat(otherCandidates.Take(remainingCount).ToArray()).ToArray();
            }

            if (nodesToRemove.Length > 0)
            {
                foreach (var peer in nodesToRemove)
                {
                    _candidatePeers.TryRemove(peer.Node.Id, out _);
                }

                if (_logger.IsDebug)
                {
                    _logger.Debug($"Removing candidate peers: {nodesToRemove.Length}, failedValidationRemovedCount: {failedValidationRemovedCount}, otherRemovedCount: {remainingCount}, prevCount: {candidates.Length}, newCount: {_candidatePeers.Count}, CandidatePeerCountCleanupThreshold: {_networkConfig.CandidatePeerCountCleanupThreshold}, MaxCandidatePeerCount: {_networkConfig.MaxCandidatePeerCount}");
                }
            }

            //_logger.Info($"candidates: \n{string.Join("\n", candidates.Select(x => $"{x.Node.Id}: {x.NodeStats.CurrentNodeReputation}"))}");
            //_logger.Info($"nodesToRemove: \n{string.Join("\n", nodesToRemove.Select(x => $"{x.Node.Id}: {x.NodeStats.CurrentNodeReputation}"))}");
        }
Beispiel #3
0
        private void CleanupCandidatePeers()
        {
            if (_candidatePeers.Count <= _networkConfig.CandidatePeerCountCleanupThreshold)
            {
                return;
            }

            // may further optimize allocations here
            Peer[] candidates    = _candidatePeers.Values.Where(p => !p.Node.IsStatic).ToArray();
            int    countToRemove = candidates.Length - _networkConfig.MaxCandidatePeerCount;

            Peer[] failedValidationCandidates = candidates.Where(x => _stats.HasFailedValidation(x.Node))
                                                .OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            Peer[] otherCandidates = candidates.Except(failedValidationCandidates).OrderBy(x => _stats.GetCurrentReputation(x.Node)).ToArray();
            Peer[] nodesToRemove   = failedValidationCandidates.Take(countToRemove).ToArray();
            int    failedValidationRemovedCount = nodesToRemove.Length;
            int    remainingCount = countToRemove - failedValidationRemovedCount;

            if (remainingCount > 0)
            {
                nodesToRemove = nodesToRemove.Concat(otherCandidates.Take(remainingCount).ToArray()).ToArray();
            }

            if (nodesToRemove.Length > 0)
            {
                foreach (Peer peer in nodesToRemove)
                {
                    _candidatePeers.TryRemove(peer.Node.Id, out _);
                }

                if (_logger.IsDebug)
                {
                    _logger.Debug($"Removing candidate peers: {nodesToRemove.Length}, failedValidationRemovedCount: {failedValidationRemovedCount}, otherRemovedCount: {remainingCount}, prevCount: {candidates.Length}, newCount: {_candidatePeers.Count}, CandidatePeerCountCleanupThreshold: {_networkConfig.CandidatePeerCountCleanupThreshold}, MaxCandidatePeerCount: {_networkConfig.MaxCandidatePeerCount}");
                }
            }
        }