示例#1
0
        public PeerInfo Select(PeerInfo currentPeer, IEnumerable <PeerInfo> peers, INodeStatsManager nodeStatsManager, IBlockTree blockTree)
        {
            IPeerSelectionStrategy strategy = _priority ? BySpeedSelectionStrategy.Fastest : BySpeedSelectionStrategy.Slowest;

            peers = _minNumber == null ? peers : peers.Where(p => p.HeadNumber > _minNumber);
            return(strategy.Select(currentPeer, peers, nodeStatsManager, blockTree));
        }
示例#2
0
        public Task <SyncPeerAllocation> BorrowAsync(IPeerSelectionStrategy peerSelectionStrategy, string description = "", int timeoutMilliseconds = 0)
        {
            SyncPeerAllocation allocation = new SyncPeerAllocation(peerSelectionStrategy);

            allocation.AllocateBestPeer(UsefulPeers.Where(p => !p.IsAllocated), new NodeStatsManager(new StatsConfig(), LimboLogs.Instance), SyncPeerTree, description);
            return(Task.FromResult(allocation));
        }
示例#3
0
        public async Task <SyncPeerAllocation> BorrowAsync(IPeerSelectionStrategy peerSelectionStrategy, string description = "", int timeoutMilliseconds = 0)
        {
            int      tryCount  = 1;
            DateTime startTime = DateTime.UtcNow;

            SyncPeerAllocation allocation = new SyncPeerAllocation(peerSelectionStrategy);

            while (true)
            {
                lock (_isAllocatedChecks)
                {
                    allocation.AllocateBestPeer(UsefulPeers.Where(p => !p.IsAllocated), _stats, _blockTree, "INIT");
                    if (allocation.HasPeer)
                    {
                        if (peerSelectionStrategy.CanBeReplaced)
                        {
                            _replaceableAllocations.TryAdd(allocation, null);
                        }

                        return(allocation);
                    }
                }

                bool timeoutReached = timeoutMilliseconds == 0 ||
                                      (DateTime.UtcNow - startTime).TotalMilliseconds > timeoutMilliseconds;
                if (timeoutReached)
                {
                    return(SyncPeerAllocation.FailedAllocation);
                }

                int waitTime = 10 * tryCount++;

                await _signals.WaitOneAsync(waitTime, CancellationToken.None);

                _signals.Reset(); // without this we have no delay
            }
        }
示例#4
0
 public SyncPeerAllocation(IPeerSelectionStrategy peerSelectionStrategy)
 {
     _peerSelectionStrategy = peerSelectionStrategy;
 }
示例#5
0
 public TotalDiffFilter(IPeerSelectionStrategy strategy, UInt256 requiredDifficulty)
 {
     _strategy           = strategy ?? throw new ArgumentNullException(nameof(strategy));
     _requiredDifficulty = requiredDifficulty;
 }