public async Task Connect_ConfirmFailed_Test() { var mockClient = new Mock <PeerService.PeerServiceClient>(); mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Throws <Exception>(); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.FakeIpEndpoint, NetworkTestConstants.FakePubkey, mockClient.Object); peer.UpdateLastReceivedHandshake(new Handshake { HandshakeData = new HandshakeData { LastIrreversibleBlockHeight = 1, BestChainHash = HashHelper.ComputeFrom("BestChainHash"), BestChainHeight = 10, Time = TimestampHelper.GetUtcNow().AddMinutes(-1) } }); _peerPool.TryAddPeer(peer); AElfPeerEndpointHelper.TryParse(NetworkTestConstants.GoodPeerEndpoint, out var endpoint); _connectionService.ConnectAsync(endpoint).ShouldThrow <Exception>(); var currentPeer = _peerPool.FindPeerByEndpoint(endpoint); currentPeer.ShouldBeNull(); AElfPeerEndpointHelper.TryParse(NetworkTestConstants.FakeIpEndpoint, out endpoint); currentPeer = _peerPool.FindPeerByEndpoint(endpoint); currentPeer.ShouldBeNull(); }
private GrpcPeer CreatePeerAndAddToPeerPool(string ip = NetworkTestConstants.FakeIpEndpoint, string pubkey = NetworkTestConstants.FakePubkey) { var peer = GrpcTestPeerHelper.CreateBasicPeer(ip, pubkey); var added = _peerPool.TryAddPeer(peer); Assert.True(added); return(peer); }
public GrpcPeerTests() { _osTestHelper = GetRequiredService <OSTestHelper>(); _pool = GetRequiredService <IPeerPool>(); _grpcPeer = GrpcTestPeerHelper.CreateNewPeer(); _grpcPeer.IsConnected = true; _nonInterceptedPeer = MockServiceClient("127.0.0.1:2000"); _pool.TryAddPeer(_grpcPeer); }
public async Task TrySchedulePeerReconnection_IsInboundAndInBootNode_Test() { var peer = GrpcTestPeerHelper.CreateBasicPeer("127.0.0.1:2020", NetworkTestConstants.FakePubkey); peer.Info.IsInbound = true; var result = await _connectionService.TrySchedulePeerReconnectionAsync(peer); result.ShouldBeTrue(); peer.IsConnected.ShouldBeFalse(); peer.IsShutdown.ShouldBeTrue(); _reconnectionService.GetReconnectingPeer("127.0.0.1:2020").ShouldNotBeNull(); }
public async Task Connect_InboundPeerIsEarlier_Test() { var mockClient = new Mock <PeerService.PeerServiceClient>(); var confirmHandshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(new VoidReply()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Returns(confirmHandshakeCall); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.FakeIpEndpoint, NetworkTestConstants.FakePubkey, mockClient.Object); peer.UpdateLastReceivedHandshake(new Handshake { HandshakeData = new HandshakeData { LastIrreversibleBlockHeight = 1, BestChainHash = HashHelper.ComputeFrom("BestChainHash"), BestChainHeight = 10, Time = TimestampHelper.GetUtcNow().AddMinutes(-1) } }); _peerPool.TryAddPeer(peer); AElfPeerEndpointHelper.TryParse(NetworkTestConstants.GoodPeerEndpoint, out var endpoint); var added = await _connectionService.ConnectAsync(endpoint); added.ShouldBeTrue(); var currentPeer = _peerPool.FindPeerByEndpoint(endpoint); currentPeer.ShouldBeNull(); AElfPeerEndpointHelper.TryParse(NetworkTestConstants.FakeIpEndpoint, out endpoint); currentPeer = _peerPool.FindPeerByEndpoint(endpoint); currentPeer.ShouldNotBeNull(); }
private GrpcPeer MockServiceClient(string ipAddress) { var mockClient = new Mock <PeerService.PeerServiceClient>(); var testCompletionSource = Task.FromResult(new VoidReply()); // setup mock announcement stream var announcementStreamCall = MockStreamCall <BlockAnnouncement, VoidReply>(testCompletionSource); mockClient.Setup(m => m.AnnouncementBroadcastStream(It.IsAny <Metadata>(), null, CancellationToken.None)) .Returns(announcementStreamCall); // setup mock transaction stream var transactionStreamCall = MockStreamCall <Transaction, VoidReply>(testCompletionSource); mockClient.Setup(m => m.TransactionBroadcastStream(It.IsAny <Metadata>(), null, CancellationToken.None)) .Returns(transactionStreamCall); // setup mock block stream var blockStreamCall = MockStreamCall <BlockWithTransactions, VoidReply>(testCompletionSource); mockClient.Setup(m => m.BlockBroadcastStream(It.IsAny <Metadata>(), null, CancellationToken.None)) .Returns(blockStreamCall); // setup mock lib stream var libAnnouncementStreamCall = MockStreamCall <LibAnnouncement, VoidReply>(testCompletionSource); mockClient.Setup(m => m.LibAnnouncementBroadcastStream(It.IsAny <Metadata>(), null, CancellationToken.None)) .Returns(libAnnouncementStreamCall); // create peer var grpcPeer = GrpcTestPeerHelper.CreatePeerWithClient(ipAddress, NetworkTestConstants.FakePubkey, mockClient.Object); grpcPeer.IsConnected = true; return(grpcPeer); }
public override void ConfigureServices(ServiceConfigurationContext context) { Configure <NetworkOptions>(o => { o.ListeningPort = 2001; o.MaxPeers = 2; }); context.Services.AddTransient(o => { var mockBlockchainService = new Mock <IBlockchainService>(); var keyPair = CryptoHelper.GenerateKeyPair(); mockBlockchainService.Setup(b => b.GetChainAsync()).ReturnsAsync(new Chain { Id = NetworkTestConstants.DefaultChainId }); mockBlockchainService.Setup(b => b.GetChainId()).Returns(NetworkTestConstants.DefaultChainId); mockBlockchainService.Setup(b => b.GetBlockHeaderByHashAsync(It.IsAny <Hash>())).ReturnsAsync( NetworkTestHelper.CreateFakeBlockHeader(NetworkTestConstants.DefaultChainId, 1, keyPair)); return(mockBlockchainService.Object); }); context.Services.AddTransient(sp => { var mockDialer = new Mock <IPeerDialer>(); mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants.FakeIpEndpoint))) .Returns <DnsEndPoint>(s => { var peer = GrpcTestPeerHelper.CreateBasicPeer(NetworkTestConstants.FakeIpEndpoint, NetworkTestConstants.FakePubkey); return(Task.FromResult(peer)); }); mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants.FakeIpEndpoint2))) .Returns <DnsEndPoint>(s => { var peer = GrpcTestPeerHelper.CreateBasicPeer(NetworkTestConstants.FakeIpEndpoint2, NetworkTestConstants.FakePubkey); return(Task.FromResult(peer)); }); mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants .DialExceptionIpEndpoint))) .Returns(Task.FromResult <GrpcPeer>(null)); mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants.HandshakeWithNetExceptionIp))) .Returns <DnsEndPoint>(s => { var mockClient = new Mock <PeerService.PeerServiceClient>(); mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)) .Throws(new AggregateException()); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.FakeIpEndpoint2, NetworkTestConstants.FakePubkey, mockClient.Object); return(Task.FromResult(peer)); }); // Incorrect handshake mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants.BadHandshakeIp))) .Returns <DnsEndPoint>((s) => { var handshakeReply = new HandshakeReply(); var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); var mockClient = new Mock <PeerService.PeerServiceClient>(); mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Returns(handshakeCall); mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Throws(new AggregateException()); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint, NetworkTestConstants.FakePubkey, mockClient.Object); return(Task.FromResult(peer)); }); // Incorrect handshake signature mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants .HandshakeWithDataExceptionIp))) .Returns <string>(async(s) => { var handshakeProvider = context.Services.GetServiceLazy <IHandshakeProvider>().Value; var handshake = await handshakeProvider.GetHandshakeAsync(); handshake.HandshakeData.Time = null; var handshakeReply = new HandshakeReply { Handshake = handshake }; var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); var mockClient = new Mock <PeerService.PeerServiceClient>(); mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Returns(handshakeCall); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint, NetworkTestConstants.FakePubkey, mockClient.Object); return(peer); }); // This peer will pass all checks with success. mockDialer.Setup(d => d.DialPeerAsync(It.Is <DnsEndPoint>(endPoint => endPoint.ToString() == NetworkTestConstants.GoodPeerEndpoint))) .Returns <DnsEndPoint>(s => { var keyPair = CryptoHelper.GenerateKeyPair(); var handshakeReply = new HandshakeReply { Handshake = NetworkTestHelper.CreateValidHandshake(keyPair, 10) }; var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); var confirmHandshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(new VoidReply()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); var mockClient = new Mock <PeerService.PeerServiceClient>(); mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Returns(handshakeCall); mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None)).Returns(confirmHandshakeCall); var peer = GrpcTestPeerHelper.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint, NetworkTestConstants.FakePubkey, mockClient.Object); peer.UpdateLastSentHandshake(handshakeReply.Handshake); return(Task.FromResult(peer)); }); mockDialer.Setup(d => d.DialBackPeerAsync(It.IsAny <DnsEndPoint>(), It.IsAny <Handshake>())) .Returns <DnsEndPoint, Handshake>((endPoint, handshake) => { if (endPoint.ToString() == NetworkTestConstants.GoodPeerEndpoint) { var peer = GrpcTestPeerHelper.CreateBasicPeer(NetworkTestConstants.GoodPeerEndpoint, NetworkTestConstants.FakePubkey); return(Task.FromResult(peer)); } return(Task.FromResult <GrpcPeer>(null)); }); return(mockDialer.Object); }); }
private GrpcPeer CreatePeer(PeerService.PeerServiceClient client) { return(GrpcTestPeerHelper.CreatePeerWithClient("127.0.0.1:2000", NetworkTestConstants.FakePubkey, client)); }