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)) { _syncManager.AddPeer(handler); _transactionPool.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.UpdateNodes(new[] { 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}"); } } }; }
private async Task OnProtocolInitialized(object sender, ProtocolInitializedEventArgs e) { var session = (IP2PSession)sender; if (_activePeers.TryGetValue(session.RemoteNodeId, out var peer)) { switch (e.ProtocolHandler) { case P2PProtocolHandler _: //TODO test log //_logger.Info($"ETH62TESTCLIENTID: {((P2PProtocolInitializedEventArgs)e).ClientId}"); peer.NodeStats.NodeDetails.ClientId = ((P2PProtocolInitializedEventArgs)e).ClientId; var result = await ValidateProtocol(Protocol.P2P, peer, e); if (!result) { return; } peer.NodeStats.AddNodeStatsEvent(NodeStatsEvent.P2PInitialized); break; case Eth62ProtocolHandler ethProtocolhandler: result = await ValidateProtocol(Protocol.Eth, peer, e); if (!result) { return; } peer.NodeStats.AddNodeStatsEvent(NodeStatsEvent.Eth62Initialized); peer.SynchronizationPeer = ethProtocolhandler; if (_logger.IsInfoEnabled) { _logger.Info($"Eth62 initialized, adding sync peer: {peer.Node.Id.ToString(false)}"); } //Add peer to the storage and to sync manager _peerStorage.UpdatePeers(new [] { peer }); await _synchronizationManager.AddPeer(ethProtocolhandler); break; } if (_logger.IsInfoEnabled) { _logger.Info($"Protocol Initialized: {session.RemoteNodeId.ToString(false)}, {e.ProtocolHandler.GetType().Name}"); } } else { if (_logger.IsErrorEnabled) { _logger.Error($"Protocol initialized for peer not present in active collection, id: {session.RemoteNodeId.ToString(false)}."); } } }
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) { if (_syncPeers.TryAdd(session.SessionId, handler)) { _syncManager.AddPeer(handler); _transactionPool.AddPeer(handler); } else { session.InitiateDisconnect(DisconnectReason.ClientQuitting); } handler.ClientId = session.Node.ClientId; if (_logger.IsTrace) { _logger.Trace($"Eth version {handler.ProtocolVersion} initialized, adding sync peer: {session.Node.Id}"); } //Add/Update peer to the storage and to sync manager _peerStorage.UpdateNodes(new[] { new NetworkNode(session.Node.Id, session.Node.Host, session.Node.Port, _stats.GetOrAdd(session.Node).NewPersistedNodeReputation) }); } if (_logger.IsTrace) { _logger.Trace($"ETH Protocol Initialized: {session.RemoteNodeId}"); } }; }
public async Task Retrieves_missing_blocks_in_batches() { _remoteBlockTree = Build.A.BlockTree(_genesisBlock).OfChainLength(QueueBasedSyncManager.MaxBatchSize * 2).TestObject; ISynchronizationPeer peer = new SynchronizationPeerMock(_remoteBlockTree); ManualResetEvent resetEvent = new ManualResetEvent(false); _manager.SyncEvent += (sender, args) => { if (args.SyncStatus == SyncStatus.Completed || args.SyncStatus == SyncStatus.Failed) { resetEvent.Set(); } }; _manager.Start(); Task addPeerTask = _manager.AddPeer(peer); Task firstToComplete = await Task.WhenAny(addPeerTask, Task.Delay(_standardTimeoutUnit)); Assert.AreSame(addPeerTask, firstToComplete); resetEvent.WaitOne(_standardTimeoutUnit); Assert.AreEqual(QueueBasedSyncManager.MaxBatchSize * 2 - 1, (int)_blockTree.BestSuggested.Number); }
private async Task OnProtocolInitialized(object sender, ProtocolInitializedEventArgs e) { var session = (IP2PSession)sender; if (session.ClientConnectionType == ClientConnectionType.In && e.ProtocolHandler is P2PProtocolHandler handler) { if (!await ProcessIncomingConnection(session, handler)) { return; } } if (!_activePeers.TryGetValue(session.RemoteNodeId, out var peer)) { if (_candidatePeers.TryGetValue(session.RemoteNodeId, out var candidateNode)) { if (_logger.IsWarnEnabled) { var timeFromLastDisconnect = candidateNode.NodeStats.LastDisconnectTime.HasValue ? DateTime.Now.Subtract(candidateNode.NodeStats.LastDisconnectTime.Value).TotalMilliseconds.ToString() : "no disconnect"; _logger.Warn($"Protocol initialized for peer not present in active collection, id: {session.RemoteNodeId}, time from last disconnect: {timeFromLastDisconnect}."); } } else { if (_logger.IsErrorEnabled) { _logger.Error($"Protocol initialized for peer not present in active collection, id: {session.RemoteNodeId}, peer not in candidate collection."); } } //Initializing disconnect if it hasnt been done already - in case of e.g. timeout earier and unexcepted further connection await session.InitiateDisconnectAsync(DisconnectReason.Other); return; } switch (e.ProtocolHandler) { case P2PProtocolHandler p2PProtocolHandler: peer.NodeStats.NodeDetails.ClientId = ((P2PProtocolInitializedEventArgs)e).ClientId; var result = await ValidateProtocol(Protocol.P2P, peer, e); if (!result) { return; } peer.NodeStats.AddNodeStatsEvent(NodeStatsEvent.P2PInitialized); peer.P2PMessageSender = p2PProtocolHandler; break; case Eth62ProtocolHandler ethProtocolhandler: result = await ValidateProtocol(Protocol.Eth, peer, e); if (!result) { return; } peer.NodeStats.AddNodeStatsEvent(NodeStatsEvent.Eth62Initialized); //TODO move this outside, so syncManager have access to NodeStats and NodeDetails ethProtocolhandler.ClientId = peer.NodeStats.NodeDetails.ClientId; peer.SynchronizationPeer = ethProtocolhandler; if (_logger.IsInfoEnabled) { _logger.Info($"Eth62 initialized, adding sync peer: {peer.Node.Id}"); } //Add peer to the storage and to sync manager _peerStorage.UpdatePeers(new [] { peer }); await _synchronizationManager.AddPeer(ethProtocolhandler); break; } if (_logger.IsInfoEnabled) { _logger.Info($"Protocol Initialized: {session.RemoteNodeId}, {e.ProtocolHandler.GetType().Name}"); } }