예제 #1
0
        private void UpdateReputationAndMaxPeersCount()
        {
            var storedNodes = _peerStorage.GetPersistedNodes();

            foreach (var node in storedNodes)
            {
                _activePeers.TryGetValue(node.NodeId, out Peer peer);
                if (peer == null)
                {
                    _candidatePeers.TryGetValue(node.NodeId, out peer);
                }

                if (peer == null)
                {
                    continue;
                }

                long newRep = _stats.GetNewPersistedReputation(peer.Node);
                if (newRep != node.Reputation)
                {
                    node.Reputation = newRep;
                    _peerStorage.UpdateNode(node);
                }
            }

            //if we have more persisted nodes then the threshold, we run cleanup process
            if (storedNodes.Length > _networkConfig.PersistedPeerCountCleanupThreshold)
            {
                var activePeers = _activePeers.Values;
                CleanupPersistedPeers(activePeers, storedNodes);
            }
        }
예제 #2
0
        private void UpdateReputationAndMaxPeersCount()
        {
            NetworkNode[] storedNodes = _peerStorage.GetPersistedNodes();
            foreach (NetworkNode node in storedNodes)
            {
                if (node.Port < 0 || node.Port > ushort.MaxValue)
                {
                    continue;
                }

                Peer peer   = _peerPool.GetOrAdd(node, false);
                long newRep = _stats.GetNewPersistedReputation(peer.Node);
                if (newRep != node.Reputation)
                {
                    node.Reputation = newRep;
                    _peerStorage.UpdateNode(node);
                }
            }

            //if we have more persisted nodes then the threshold, we run cleanup process
            if (storedNodes.Length > _networkConfig.PersistedPeerCountCleanupThreshold)
            {
                ICollection <Peer> activePeers = _activePeers.Values;
                CleanupPersistedPeers(activePeers, storedNodes);
            }
        }
예제 #3
0
        private void InitEthProtocol(ISession session, Eth62ProtocolHandler handler)
        {
            handler.ProtocolInitialized += (sender, args) =>
            {
                if (!RunBasicChecks(session, handler.ProtocolCode, handler.ProtocolVersion))
                {
                    return;
                }
                var typedArgs = (EthProtocolInitializedEventArgs)args;
                _stats.ReportEthInitializeEvent(session.Node, new EthNodeDetails
                {
                    ChainId         = typedArgs.ChainId,
                    BestHash        = typedArgs.BestHash,
                    GenesisHash     = typedArgs.GenesisHash,
                    ProtocolVersion = typedArgs.ProtocolVersion,
                    TotalDifficulty = typedArgs.TotalDifficulty
                });

                bool isValid = _protocolValidator.DisconnectOnInvalid(Protocol.Eth, session, args);
                if (isValid)
                {
                    handler.ClientId = session.Node.ClientId;

                    if (_syncPeers.TryAdd(session.SessionId, handler))
                    {
                        _syncPool.AddPeer(handler);
                        _txPool.AddPeer(handler);
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"{handler.ClientId} sync peer {session} created.");
                        }
                    }
                    else
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"Not able to add a sync peer on {session} for {session.Node:s}");
                        }
                        session.InitiateDisconnect(DisconnectReason.AlreadyConnected, "sync peer");
                    }

                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Finalized ETH protocol initialization on {session} - adding sync peer {session.Node:s}");
                    }

                    //Add/Update peer to the storage and to sync manager
                    _peerStorage.UpdateNode(new NetworkNode(session.Node.Id, session.Node.Host, session.Node.Port, _stats.GetOrAdd(session.Node).NewPersistedNodeReputation));
                }
                else
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"|NetworkTrace| {handler.ProtocolCode}{handler.ProtocolVersion} is invalid on {session}");
                    }
                }
            };
        }