예제 #1
0
        private void AllocationOnReplaced(object sender, AllocationChangeEventArgs e)
        {
            if (e.Previous == null)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Allocating {e.Current} on {_blocksSyncAllocation}.");
                }
            }
            else
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Replacing {e.Previous} with {e.Current} on {_blocksSyncAllocation}.");
                }
            }

            if (e.Previous != null)
            {
                _cancelDueToBetterPeer = true;
                _peerSyncCancellation?.Cancel();
            }

            PeerInfo    newPeer       = e.Current;
            BlockHeader bestSuggested = _blockTree.BestSuggestedHeader;

            if (newPeer.TotalDifficulty > bestSuggested.TotalDifficulty)
            {
                RequestSynchronization(SyncTriggerType.PeerChange);
            }
        }
예제 #2
0
        public void ReplaceCurrent(PeerInfo betterPeer)
        {
            if (betterPeer == null)
            {
                throw new ArgumentNullException(nameof(betterPeer));
            }

            AllocationChangeEventArgs args;

            lock (_allocationLock)
            {
                PeerInfo current = Current;
                if (current != null && !CanBeReplaced)
                {
                    return;
                }

                if (betterPeer.IsAllocated)
                {
                    return;
                }

                betterPeer.IsAllocated = true;
                if (current != null)
                {
                    current.IsAllocated = false;
                }

                args    = new AllocationChangeEventArgs(current, betterPeer);
                Current = betterPeer;
            }

            Replaced?.Invoke(this, args);
        }
예제 #3
0
        public void AllocateBestPeer(IEnumerable <PeerInfo> peers, INodeStatsManager nodeStatsManager, IBlockTree blockTree, string reason)
        {
            PeerInfo current  = Current;
            PeerInfo selected = _peerSelectionStrategy.Select(Current, peers, nodeStatsManager, blockTree);

            if (selected == current)
            {
                return;
            }

            AllocationChangeEventArgs args = null;

            lock (_allocationLock)
            {
                if (selected.IsAllocated)
                {
                    throw new InvalidAsynchronousStateException("Selected an already allocated peer");
                }

                selected.IsAllocated = true;
                Current = selected;
                args    = new AllocationChangeEventArgs(current, selected);
                if (current != null)
                {
                    current.IsAllocated = false;
                }
            }

            Replaced?.Invoke(this, args);
        }
예제 #4
0
 private void AllocationOnCancelled(object sender, AllocationChangeEventArgs e)
 {
     if (_logger.IsDebug)
     {
         _logger.Debug($"Cancelling {e.Previous} on {_blocksSyncAllocation}.");
     }
     _peerSyncCancellation?.Cancel();
 }
예제 #5
0
        public void Cancel()
        {
            PeerInfo current = Current;

            if (current == null)
            {
                return;
            }

            lock (_allocationLock)
            {
                current.IsAllocated = false;
                Current             = null;
            }

            AllocationChangeEventArgs args = new AllocationChangeEventArgs(current, null);

            Cancelled?.Invoke(this, args);
        }