public void DisconnectOnTooManyPeersTest() { var node = _nodeFactory.CreateNode("192.1.1.1", 3333); ((NetworkConfig)_configurationProvider.GetConfig <INetworkConfig>()).ActivePeersMaxCount = 0; //trigger connection initialized var p2pSession = new TestP2PSession(); p2pSession.RemoteNodeId = node.Id; p2pSession.ConnectionDirection = ConnectionDirection.In; _localPeer.TriggerSessionCreated(p2pSession); p2pSession.TriggerHandshakeComplete(); //trigger p2p initialization //var p2pProtocol = new P2PProtocolHandler(p2pSession, new MessageSerializationService(), p2pSession.RemoteNodeId, p2pSession.RemotePort ?? 0, _logManager); //var p2pArgs = new P2PProtocolInitializedEventArgs(p2pProtocol) //{ // P2PVersion = 1, // Capabilities = new[] { new Capability(Protocol.Eth, 62) }.ToList() //}; //p2pSession.TriggerProtocolInitialized(p2pArgs); Assert.IsTrue(p2pSession.Disconected); Assert.AreEqual(DisconnectReason.TooManyPeers, p2pSession.DisconnectReason); }
private TestP2PSession InitializeNode(ClientConnectionType clientConnectionType = ClientConnectionType.Out) { var node = _nodeFactory.CreateNode("192.1.1.1", 3333); _discoveryManager.GetNodeLifecycleManager(node); //verify new peer is added Assert.AreEqual(1, _peerManager.CandidatePeers.Count); Assert.AreEqual(node.Id, _peerManager.CandidatePeers.First().Node.Id); //trigger connection start var task = _peerManager.RunPeerUpdate(); task.Wait(); Assert.AreEqual(1, _localPeer.ConnectionAsyncCallsCounter); Assert.AreEqual(1, _peerManager.CandidatePeers.Count); Assert.AreEqual(1, _peerManager.ActivePeers.Count); //trigger connection initialized var p2pSession = new TestP2PSession(); p2pSession.RemoteNodeId = node.Id; p2pSession.ClientConnectionType = clientConnectionType; _localPeer.TriggerConnectionInitialized(p2pSession); var peer = _peerManager.ActivePeers.First(); if (clientConnectionType == ClientConnectionType.Out) { Assert.IsNotNull(peer.Session); } return(p2pSession); }
public void InPeerBecomesActiveAndDisconnectTest() { var node = _nodeFactory.CreateNode("192.1.1.1", 3333); //trigger connection initialized var p2pSession = new TestP2PSession(); p2pSession.RemoteNodeId = node.Id; p2pSession.RemoteHost = node.Host; p2pSession.RemotePort = node.Port; p2pSession.ConnectionDirection = ConnectionDirection.In; _localPeer.TriggerSessionCreated(p2pSession); p2pSession.TriggerHandshakeComplete(); //trigger p2p initialization var p2pProtocol = new P2PProtocolHandler(p2pSession, new MessageSerializationService(), p2pSession.RemoteNodeId, p2pSession.RemotePort ?? 0, _logManager, new PerfService(_logManager)); var p2pArgs = new P2PProtocolInitializedEventArgs(p2pProtocol) { P2PVersion = 4, Capabilities = new[] { new Capability(Protocol.Eth, 62), new Capability(Protocol.Eth, 63) }.ToList() }; p2pSession.TriggerProtocolInitialized(p2pArgs); AssertTrue(() => _peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.P2PInitialized), 5000); //Assert.IsTrue(_peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.P2PInitialized)); //trigger eth62 initialization var eth62 = new Eth62ProtocolHandler(p2pSession, new MessageSerializationService(), _synchronizationManager, _logManager, new PerfService(_logManager), _blockTree, _transactionPool, _timestamp); var args = new EthProtocolInitializedEventArgs(eth62) { ChainId = _synchronizationManager.ChainId }; p2pSession.TriggerProtocolInitialized(args); AssertTrue(() => _peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.Eth62Initialized), 5000); Assert.AreEqual(1, _peerManager.CandidatePeers.Count); Assert.AreEqual(1, _peerManager.ActivePeers.Count); //Assert.IsTrue(_peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.Eth62Initialized)); AssertTrue(() => _peerManager.ActivePeers.First().SynchronizationPeer != null, 5000); //Assert.NotNull(_peerManager.ActivePeers.First().SynchronizationPeer); //verify active peer was added to synch manager _synchronizationManager.Received(1).AddPeer(Arg.Any <ISynchronizationPeer>()); //trigger disconnect p2pSession.TriggerPeerDisconnected(); AssertTrue(() => _peerManager.ActivePeers.Count == 0, 5000); Assert.AreEqual(1, _peerManager.CandidatePeers.Count); //Assert.AreEqual(0, _peerManager.ActivePeers.Count); //verify active peer was removed from synch manager _synchronizationManager.Received(1).RemovePeer(Arg.Any <ISynchronizationPeer>()); }
public void DisconnectOnTooManyPeersTest() { var node = _nodeFactory.CreateNode("192.1.1.1", 3333); ((NetworkConfig)_configurationProvider.GetConfig <INetworkConfig>()).ActivePeersMaxCount = 0; //trigger connection initialized var p2pSession = new TestP2PSession(); p2pSession.RemoteNodeId = node.Id; p2pSession.ConnectionDirection = ConnectionDirection.In; _localPeer.TriggerSessionCreated(p2pSession); p2pSession.TriggerHandshakeComplete(); AssertTrue(() => p2pSession.Disconected, 5000); //Assert.IsTrue(p2pSession.Disconected); Assert.AreEqual(DisconnectReason.TooManyPeers, p2pSession.DisconnectReason); }