public void DialPeerAsync_HandshakeError_ShouldThrowException() { IpEndPointHelper.TryParse(NetworkTestConstants.BadHandshakeIp, out var endpoint); _networkServer.ConnectAsync(endpoint).ShouldThrow <NetworkException>(); _peerPool.PeerCount.ShouldBe(0); }
public async Task DialPeerAsync_ShouldThrowException() { IpEndPointHelper.TryParse(NetworkTestConstants.DialExceptionIpEndpoint, out var endpoint); _networkServer.ConnectAsync(endpoint).ShouldThrow <Exception>(); _peerPool.PeerCount.ShouldBe(0); }
public void DialPeerAsync_HandshakeDataProblem_ShouldThrowException() { IpEndPointHelper.TryParse(NetworkTestConstants.HandshakeWithNetExceptionIp, out var endpoint); _networkServer.ConnectAsync(endpoint).ShouldThrow <Exception>(); _peerPool.PeerCount.ShouldBe(0); }
/// <summary> /// Connects to the boot nodes provided in the network options. /// </summary> private async Task DialBootNodesAsync() { if (NetworkOptions.BootNodes == null || !NetworkOptions.BootNodes.Any()) { Logger.LogWarning("Boot nodes list is empty."); return; } var taskList = NetworkOptions.BootNodes .Select(async node => { bool dialed = false; if (!IpEndPointHelper.TryParse(node, out IPEndPoint endpoint)) { return; } try { dialed = await _connectionService.ConnectAsync(endpoint); } catch (Exception e) { Logger.LogError(e, $"Connect peer failed.{node}"); } if (!dialed) { await _connectionService.SchedulePeerReconnection(endpoint); } }).ToList(); await Task.WhenAll(taskList.ToArray <Task>()); }
private static IPEndPoint ParseEndPoint(string ipEndpoint) { if (!IpEndPointHelper.TryParse(ipEndpoint, out var endpoint)) { throw new Exception($"Endpoint {ipEndpoint} could not be parsed."); } return(endpoint); }
public async Task <bool> AddPeerAsync(string address) { if (IpEndPointHelper.TryParse(address, out IPEndPoint endpoint)) { return(await _networkServer.ConnectAsync(endpoint)); } return(false); }
public async Task DialPeerAsync_GoodPeer_ShouldBeInPool() { IpEndPointHelper.TryParse(NetworkTestConstants.GoodPeerEndpoint, out var endpoint); // two different hosts with the same pubkey. var added = await _networkServer.ConnectAsync(endpoint); added.ShouldBeTrue(); _peerPool.FindPeerByEndpoint(endpoint).ShouldNotBeNull(); }
public async Task DialPeerAsync_GoodPeer_ShouldLaunchConnectionEvent() { PeerConnectedEventData eventData = null; _eventBus.Subscribe <PeerConnectedEventData>(e => { eventData = e; return(Task.CompletedTask); }); // two different hosts with the same pubkey. IpEndPointHelper.TryParse(NetworkTestConstants.GoodPeerEndpoint, out var endpoint); var added = await _networkServer.ConnectAsync(endpoint); added.ShouldBeTrue(); _peerPool.FindPeerByEndpoint(endpoint).ShouldNotBeNull(); eventData.ShouldNotBeNull(); }
public async Task <bool> RemovePeerAsync(string address) { if (!IpEndPointHelper.TryParse(address, out IPEndPoint endpoint)) { return(false); } var peer = _peerPool.FindPeerByEndpoint(endpoint); if (peer == null) { Logger.LogWarning($"Could not find peer at address {address}"); return(false); } await _networkServer.DisconnectAsync(peer); return(true); }
public override void OnApplicationInitialization(ApplicationInitializationContext context) { var pool = context.ServiceProvider.GetRequiredService <IPeerPool>(); var channel = new Channel(NetworkTestConstants.FakeIpEndpoint, ChannelCredentials.Insecure); var connectionInfo = new PeerConnectionInfo { Pubkey = NetworkTestConstants.FakePubkey2, ProtocolVersion = KernelConstants.ProtocolVersion, ConnectionTime = TimestampHelper.GetUtcNow(), IsInbound = true }; if (!IpEndPointHelper.TryParse(NetworkTestConstants.FakeIpEndpoint, out var peerEndpoint)) { throw new Exception($"Ip {NetworkTestConstants.FakeIpEndpoint} is invalid."); } pool.TryAddPeer(new GrpcPeer(new GrpcClient(channel, new PeerService.PeerServiceClient(channel)), peerEndpoint, connectionInfo)); }