public void ProcessNeighborsMsg(NeighborsMsg?msg) { if (msg is null) { return; } if (!IsBonded) { return; } if (_isNeighborsExpected) { NodeStats.AddNodeStatsEvent(NodeStatsEventType.DiscoveryNeighboursIn); RefreshNodeContactTime(); foreach (Node node in msg.Nodes) { if (node.Address.Address.ToString().Contains("127.0.0.1")) { if (_logger.IsTrace) { _logger.Trace($"Received localhost as node address from: {msg.FarPublicKey}, node: {node}"); } continue; } //If node is new it will create a new nodeLifecycleManager and will update state to New, which will trigger Ping _discoveryManager.GetNodeLifecycleManager(node); } } _isNeighborsExpected = false; }
private void AddPersistedNodes() { if (!_configurationProvider.IsDiscoveryNodesPersistenceOn) { return; } var nodes = _discoveryStorage.GetPersistedNodes(); foreach (var networkNode in nodes) { var node = _nodeFactory.CreateNode(networkNode.NodeId, networkNode.Host, networkNode.Port); var manager = _discoveryManager.GetNodeLifecycleManager(node, true); if (manager == null) { if (_logger.IsDebug) { _logger.Debug($"Skiping persisted node {networkNode.NodeId}@{networkNode.Host}:{networkNode.Port}, manager couldnt be created"); } continue;; } manager.NodeStats.CurrentPersistedNodeReputation = networkNode.Reputation; if (_logger.IsTrace) { _logger.Trace($"Adding persisted node {networkNode.NodeId}@{networkNode.Host}:{networkNode.Port}"); } } if (_logger.IsInfo) { _logger.Info($"Added persisted discovery nodes: {nodes.Length}"); } }
private void OnRemoteConnectionInitialized(object sender, ConnectionInitializedEventArgs eventArgs) { if (eventArgs.ClientConnectionType == ClientConnectionType.In) { //If connection was initiated by remote peer we allow handshake to take place before potencially disconnecting eventArgs.Session.ProtocolInitialized += async(s, e) => await OnProtocolInitialized(s, e); eventArgs.Session.PeerDisconnected += async(s, e) => await OnPeerDisconnected(s, e); if (_logger.IsInfoEnabled) { _logger.Info($"Initiated IN connection (PeerManager)(handshake completed) for peer: {eventArgs.Session.RemoteNodeId}"); } return; } var id = eventArgs.Session.RemoteNodeId; if (!_activePeers.TryGetValue(id, out Peer peer)) { if (_logger.IsErrorEnabled) { _logger.Error($"Initiated rlpx connection (out) with Peer without adding it to Active collection: {id}"); } return; } peer.ClientConnectionType = eventArgs.ClientConnectionType; peer.Session = eventArgs.Session; peer.Session.PeerDisconnected += async(s, e) => await OnPeerDisconnected(s, e); peer.Session.ProtocolInitialized += async(s, e) => await OnProtocolInitialized(s, e); if (_logger.IsInfoEnabled) { _logger.Info($"Initializing OUT connection (PeerManager) for peer: {eventArgs.Session.RemoteNodeId}"); } if (!_isDiscoveryEnabled || peer.NodeLifecycleManager != null) { return; } //In case peer was initiated outside of discovery and discovery is enabled, we are adding it to discovery for future use (e.g. trusted peer) var manager = _discoveryManager.GetNodeLifecycleManager(peer.Node); peer.NodeLifecycleManager = manager; }
public void ProcessNeighborsMessage(NeighborsMessage discoveryMessage) { if (_isNeighborsExpected) { NodeStats.AddNodeStatsEvent(NodeStatsEvent.DiscoveryNeighboursIn); RefreshNodeContactTime(); foreach (var node in discoveryMessage.Nodes) { //If node is new it will create a new nodeLifecycleManager and will update state to New, which will trigger Ping _discoveryManager.GetNodeLifecycleManager(node); } } _isNeighborsExpected = false; }
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); }
private void OnRemoteConnectionInitialized(object sender, ConnectionInitializedEventArgs eventArgs) { var id = eventArgs.Session.RemoteNodeId; if (!_activePeers.TryGetValue(id, out Peer peer)) { if (_logger.IsErrorEnabled) { _logger.Error($"Initiated rlpx connection with Peer without adding it to Active collection: {id.ToString(false)}"); } return; } peer.Session = eventArgs.Session; peer.Session.PeerDisconnected += OnPeerDisconnected; peer.Session.ProtocolInitialized += async(s, e) => await OnProtocolInitialized(s, e); if (!_isDiscoveryEnabled || peer.NodeLifecycleManager != null) { return; } //In case peer was initiated outside of discovery and discovery is enabled, we are adding it to discovery for future use (e.g. trusted peer) var manager = _discoveryManager.GetNodeLifecycleManager(peer.Node); peer.NodeLifecycleManager = manager; }
public void OnPongMessageTest() { //receiving pong _discoveryManager.OnIncomingMessage(new PongMessage { FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port), FarPublicKey = _publicKey }); //expecting to activate node as valid peer var nodes = _nodeTable.GetClosestNodes(); Assert.AreEqual(1, nodes.Length); var node = nodes.First(); Assert.AreEqual(_host, node.Host); Assert.AreEqual(_port, node.Port); var manager = _discoveryManager.GetNodeLifecycleManager(node); Assert.AreEqual(NodeLifecycleState.Active, manager.State); }
private Result SendFindNodeSync(Node destinationNode, byte[] searchedNodeId) { var nodeManager = _discoveryManager.GetNodeLifecycleManager(destinationNode); nodeManager.SendFindNode(searchedNodeId ?? _masterNode.Id.Bytes); if (_discoveryManager.WasMessageReceived(destinationNode.IdHashText, MessageType.Neighbors, _configurationProvider.SendNodeTimeout)) { return(Result.Success()); } return(Result.Fail($"Did not receive Neighbors reponse in time from: {destinationNode.Host}")); }
private void AddPersistedNodes() { if (!_configurationProvider.IsDiscoveryNodesPersistenceOn) { return; } var nodes = _discoveryStorage.GetPersistedNodes(); foreach (var node in nodes) { var manager = _discoveryManager.GetNodeLifecycleManager(node.Node, true); manager.NodeStats.CurrentPersistedNodeReputation = node.PersistedReputation; if (_logger.IsDebugEnabled) { _logger.Debug($"Adding persisted node {node.Node.Id}@{node.Node.Host}:{node.Node.Port}"); } } if (_logger.IsInfoEnabled) { _logger.Info($"Added persisted discovery nodes: {nodes.Length}"); } }
private async Task <Result> SendFindNode(Node destinationNode, byte[]?searchedNodeId) { try { INodeLifecycleManager?nodeManager = _discoveryManager.GetNodeLifecycleManager(destinationNode); nodeManager?.SendFindNode(searchedNodeId ?? _masterNode !.Id.Bytes); return(await _discoveryManager.WasMessageReceived(destinationNode.IdHash, MsgType.Neighbors, _discoveryConfig.SendNodeTimeout) ? Result.Success : Result.Fail($"Did not receive Neighbors response in time from: {destinationNode.Host}")); } catch (OperationCanceledException) { return(Result.Fail("Cancelled")); } }
public void ActiveStateTest() { var node = new Node(_host, _port); var manager = _discoveryManager.GetNodeLifecycleManager(node); Assert.AreEqual(NodeLifecycleState.New, manager.State); manager.ProcessPongMessage(new PongMessage { FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port), FarPublicKey = _nodeIds[0] }); Assert.AreEqual(NodeLifecycleState.Active, manager.State); }
public void Wrong_pong_will_get_ignored() { var node = new Node(_host, _port); var manager = _discoveryManager.GetNodeLifecycleManager(node); Assert.AreEqual(NodeLifecycleState.New, manager.State); manager.ProcessPongMessage(new PongMessage { FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port), FarPublicKey = _nodeIds[0], PingMdc = new byte[32] }); Assert.AreEqual(NodeLifecycleState.New, manager.State); }
private async Task <Result> SendFindNode(Node destinationNode, byte[] searchedNodeId, CancellationToken cancellationToken) { try { var nodeManager = _discoveryManager.GetNodeLifecycleManager(destinationNode); nodeManager?.SendFindNode(searchedNodeId ?? _masterNode.Id.Bytes); if (await _discoveryManager.WasMessageReceived(destinationNode.IdHash, MessageType.Neighbors, _configurationProvider.SendNodeTimeout)) { return(Result.Success); } return(Result.Fail($"Did not receive Neighbors reponse in time from: {destinationNode.Host}")); } catch (OperationCanceledException) { return(Result.Fail("Cancelled")); } }
public void Wrong_pong_will_get_ignored() { Node node = new(TestItem.PublicKeyB, _host, _port); INodeLifecycleManager?manager = _discoveryManager.GetNodeLifecycleManager(node); Assert.AreEqual(NodeLifecycleState.New, manager?.State); PongMsg msgI = new (_nodeIds[0], GetExpirationTime(), new byte[32]); msgI.FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port); _discoveryManager.OnIncomingMsg(msgI); Assert.AreEqual(NodeLifecycleState.New, manager?.State); }
public void AddNodeToDiscovery(Node node) { _discoveryManager.GetNodeLifecycleManager(node); }