public void Disconnect_Should_Disconnect_From_Node() { var commandContext = TestCommandHelpers.GenerateCliCommandContext(); var nodeRpcClient = TestCommandHelpers.MockNodeRpcClient(); TestCommandHelpers.MockActiveConnection(commandContext, nodeRpcClient); TestCommandHelpers.MockNodeRpcClientFactory(commandContext, nodeRpcClient); var rpcNodeConfig = TestCommandHelpers.MockRpcNodeConfig(commandContext); var socketClientRegistry = TestCommandHelpers.AddClientSocketRegistry(commandContext, _testScheduler); var clientHashCode = socketClientRegistry.GenerateClientHashCode( EndpointBuilder.BuildNewEndPoint(rpcNodeConfig.HostAddress, rpcNodeConfig.Port)); socketClientRegistry.AddClientToRegistry(clientHashCode, nodeRpcClient); var commands = new List <ICommand> { new DisconnectCommand(commandContext, Substitute.For <ILogger>()) }; var console = new CatalystCli(commandContext.UserOutput, commands); var isCommandParsed = console.ParseCommand("disconnect", "-n", "node1"); isCommandParsed.Should().BeTrue(); socketClientRegistry.Registry.Count.Should().Be(0); }
/// <summary> /// Tests the data/communication through protobuf /// </summary> /// <returns></returns> private GetPeerInfoResponse GetPeerInfoTest(PeerId peerId) { var testScheduler = new TestScheduler(); _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); var senderPeerIdentifier = PeerIdHelper.GetPeerId("sender"); var getPeerInfoRequest = new GetPeerInfoRequest { PublicKey = peerId.PublicKey, Ip = peerId.Ip }; var protocolMessage = getPeerInfoRequest.ToProtocolMessage(senderPeerIdentifier); var messageStream = MessageStreamHelper.CreateStreamWithMessage(_fakeContext, testScheduler, protocolMessage); var peerSettings = senderPeerIdentifier.ToSubstitutedPeerSettings(); var handler = new GetPeerInfoRequestObserver(peerSettings, _logger, _peerRepository); handler.StartObserving(messageStream); testScheduler.Start(); var receivedCalls = _fakeContext.Channel.ReceivedCalls().ToList(); receivedCalls.Count.Should().Be(1); var sentResponseDto = (IMessageDto <ProtocolMessage>)receivedCalls[0].GetArguments().Single(); return(sentResponseDto.Content.FromProtocolMessage <GetPeerInfoResponse>()); }
/// <summary> /// use to parse array of IPEndpoints from config /// </summary> /// <param name="configurationRoot"></param> /// <param name="section"></param> /// <returns></returns> public static IPEndPoint[] GetIpEndpointArrValues(IConfigurationRoot configurationRoot, string section) { return(configurationRoot.GetSection("CatalystNodeConfiguration") .GetSection("Peer") .GetSection(section) .GetChildren() .Select(p => EndpointBuilder.BuildNewEndPoint(p.Value)).ToArray()); }
/// <summary>Executes the test case.</summary> /// <param name="fakePeers">The fake peers.</param> /// <param name="withPublicKey">if set to <c>true</c> [send message to handler with the public key].</param> private void ExecuteTestCase(IReadOnlyCollection <string> fakePeers, bool withPublicKey) { var testScheduler = new TestScheduler(); IPeerRepository peerRepository = Substitute.For <IPeerRepository>(); Peer targetPeerToDelete = null; var fakePeerList = fakePeers.ToList().Select(fakePeer => { var peer = new Peer { Reputation = 0, LastSeen = DateTime.Now.Subtract(TimeSpan.FromSeconds(fakePeers.ToList().IndexOf(fakePeer))), PeerId = PeerIdHelper.GetPeerId(fakePeer) }; if (targetPeerToDelete == null) { targetPeerToDelete = peer; } return(peer); }).ToList(); peerRepository.FindAll(Arg.Any <ISpecification <Peer> >()).Returns(withPublicKey ? new List <Peer> { targetPeerToDelete } : fakePeerList); // Build a fake remote endpoint _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); var peerId = PeerIdHelper.GetPeerId("sender"); var removePeerRequest = new RemovePeerRequest { PeerIp = targetPeerToDelete.PeerId.Ip, PublicKey = withPublicKey ? targetPeerToDelete.PeerId.PublicKey : ByteString.Empty }; var protocolMessage = removePeerRequest.ToProtocolMessage(peerId); var messageStream = MessageStreamHelper.CreateStreamWithMessage(_fakeContext, testScheduler, protocolMessage); var peerSettings = peerId.ToSubstitutedPeerSettings(); var handler = new RemovePeerRequestObserver(peerSettings, peerRepository, _logger); handler.StartObserving(messageStream); testScheduler.Start(); var receivedCalls = _fakeContext.Channel.ReceivedCalls().ToList(); receivedCalls.Count().Should().Be(1); var sentResponseDto = (IMessageDto <ProtocolMessage>)receivedCalls[0].GetArguments().Single(); var signResponseMessage = sentResponseDto.Content.FromProtocolMessage <RemovePeerResponse>(); signResponseMessage.DeletedCount.Should().Be(withPublicKey ? 1 : (uint)fakePeers.Count); }
/// <inheritdoc cref="GetConnectedNode" /> public IRpcClient GetConnectedNode(string nodeId) { Guard.Argument(nodeId, nameof(nodeId)).NotNull().NotEmpty().Compatible <string>(); var nodeConfig = _rpcNodeConfigs.SingleOrDefault(node => node.NodeId.Equals(nodeId)); Guard.Argument(nodeConfig, nameof(nodeConfig)).NotNull(); var registryId = SocketClientRegistry.GenerateClientHashCode( EndpointBuilder.BuildNewEndPoint(nodeConfig?.HostAddress, nodeConfig.Port)); var nodeRpcClient = SocketClientRegistry.GetClientFromRegistry(registryId); Guard.Argument(nodeRpcClient).Require(IsSocketChannelActive(nodeRpcClient)); return(nodeRpcClient); }
protected override bool ExecuteCommand(DisconnectOptions option) { var nodeConfig = CommandContext.GetNodeConfig(option.Node); Guard.Argument(nodeConfig, nameof(nodeConfig)).NotNull(); var registryId = CommandContext.SocketClientRegistry.GenerateClientHashCode( EndpointBuilder.BuildNewEndPoint(nodeConfig.HostAddress, nodeConfig.Port)); var node = CommandContext.SocketClientRegistry.GetClientFromRegistry(registryId); Guard.Argument(node, nameof(node)).Require(CommandContext.IsSocketChannelActive(node)); node.Dispose(); CommandContext.SocketClientRegistry.RemoveClientFromRegistry(registryId); return(true); }
/// <summary> /// Set attributes /// </summary> /// <param name="rootSection"></param> public PeerSettings(IConfigurationRoot rootSection) { Guard.Argument(rootSection, nameof(rootSection)).NotNull(); var section = rootSection.GetSection("CatalystNodeConfiguration").GetSection("Peer"); Enum.TryParse(section.GetSection("Network").Value, out _networkType); PublicKey = section.GetSection("PublicKey").Value; Port = int.Parse(section.GetSection("Port").Value); PayoutAddress = section.GetSection("PayoutAddress").Value; BindAddress = IPAddress.Parse(section.GetSection("BindAddress").Value); PublicIpAddress = IPAddress.Parse(section.GetSection("PublicIpAddress").Value); SeedServers = section.GetSection("SeedServers").GetChildren().Select(p => p.Value).ToList(); DnsServers = section.GetSection("DnsServers") .GetChildren() .Select(p => EndpointBuilder.BuildNewEndPoint(p.Value)).ToArray(); PeerId = PublicKey.BuildPeerIdFromBase32Key(PublicIpAddress, Port); }
public void TestPeerListRequestResponse(int fakePeers) { var peerService = Substitute.For <IPeerRepository>(); var peerList = new List <Peer>(); for (var i = 0; i < fakePeers; i++) { peerList.Add(new Peer { Reputation = 0, LastSeen = DateTime.Now, PeerId = PeerIdHelper.GetPeerId(i.ToString()) }); } // Build a fake remote endpoint _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); peerService.GetAll().Returns(peerList); var sendPeerId = PeerIdHelper.GetPeerId("sender"); var protocolMessage = new GetPeerCountRequest().ToProtocolMessage(PeerIdHelper.GetPeerId("sender")); var messageStream = MessageStreamHelper.CreateStreamWithMessage(_fakeContext, _testScheduler, protocolMessage); var peerSettings = sendPeerId.ToSubstitutedPeerSettings(); var handler = new PeerCountRequestObserver(peerSettings, peerService, _logger); handler.StartObserving(messageStream); _testScheduler.Start(); var receivedCalls = _fakeContext.Channel.ReceivedCalls().ToList(); receivedCalls.Count.Should().Be(1); var sentResponseDto = (IMessageDto <ProtocolMessage>)receivedCalls[0].GetArguments().Single(); var responseContent = sentResponseDto.Content.FromProtocolMessage <GetPeerCountResponse>(); responseContent.PeerCount.Should().Be(fakePeers); }
public void TestPeerListRequestResponse(params string[] fakePeers) { var testScheduler = new TestScheduler(); var peerService = Substitute.For <IPeerRepository>(); var peerList = new List <Peer>(); fakePeers.ToList().ForEach(fakePeer => { peerList.Add(new Peer { Reputation = 0, LastSeen = DateTime.Now, PeerId = PeerIdHelper.GetPeerId(fakePeer) }); }); // Let peerRepository return the fake peer list peerService.GetAll().Returns(peerList.ToArray()); // Build a fake remote endpoint _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); var protocolMessage = new GetPeerListRequest().ToProtocolMessage(PeerIdHelper.GetPeerId("sender")); var messageStream = MessageStreamHelper.CreateStreamWithMessage(_fakeContext, testScheduler, protocolMessage); var peerSettings = PeerIdHelper.GetPeerId("sender").ToSubstitutedPeerSettings(); var handler = new PeerListRequestObserver(peerSettings, _logger, peerService); handler.StartObserving(messageStream); testScheduler.Start(); var receivedCalls = _fakeContext.Channel.ReceivedCalls().ToList(); receivedCalls.Count.Should().Be(1); var sentResponseDto = (IMessageDto <ProtocolMessage>)receivedCalls[0].GetArguments().Single(); var responseContent = sentResponseDto.Content.FromProtocolMessage <GetPeerListResponse>(); responseContent.Peers.Count.Should().Be(fakePeers.Length); }
public PeerBlackListingRequestObserverTests() { _logger = Substitute.For <ILogger>(); _fakeContext = Substitute.For <IChannelHandlerContext>(); var fakeChannel = Substitute.For <IChannel>(); _fakeContext.Channel.Returns(fakeChannel); _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); _testScheduler = new TestScheduler(); _peerRepository = Substitute.For <IPeerRepository>(); var fakePeers = PreparePeerRepositoryContent(); _peerRepository.GetAll().Returns(fakePeers); _senderId = PeerIdHelper.GetPeerId("sender"); }
protected override bool ExecuteCommand(ConnectOptions option) { var rpcNodeConfigs = CommandContext.GetNodeConfig(option.Node); Guard.Argument(rpcNodeConfigs, nameof(rpcNodeConfigs)).NotNull(); //Connect to the node and store it in the socket client registry var nodeRpcClient = CommandContext.RpcClientFactory.GetClient( CommandContext.CertificateStore.ReadOrCreateCertificateFile(rpcNodeConfigs.PfxFileName), rpcNodeConfigs).ConfigureAwait(false).GetAwaiter().GetResult(); if (!CommandContext.IsSocketChannelActive(nodeRpcClient)) { CommandContext.UserOutput.WriteLine(InvalidSocketChannel); return(false); } var clientHashCode = CommandContext.SocketClientRegistry.GenerateClientHashCode( EndpointBuilder.BuildNewEndPoint(rpcNodeConfigs.HostAddress, rpcNodeConfigs.Port)); CommandContext.SocketClientRegistry.AddClientToRegistry(clientHashCode, nodeRpcClient); CommandContext.UserOutput.WriteLine($"Connected to Node {nodeRpcClient.Channel.RemoteAddress}"); return(true); }