Пример #1
0
        private static IPeer CreatePeer(string ip = NetworkTestConstants.FakeIpEndpoint)
        {
            var peerMock = new Mock <IPeer>();

            var keyPair = CryptoHelper.GenerateKeyPair();
            var pubkey  = keyPair.PublicKey.ToHex();

            var peerInfo = new PeerInfo
            {
                Pubkey          = pubkey,
                ProtocolVersion = KernelConstants.ProtocolVersion,
                ConnectionTime  = TimestampHelper.GetUtcNow().Seconds,
                IsInbound       = true
            };

            if (!IpEndpointHelper.TryParse(ip, out var endpoint))
            {
                throw new Exception($"Endpoint {ip} could not be parsed.");
            }

            peerMock.Setup(p => p.RemoteEndpoint).Returns(endpoint);
            peerMock.Setup(p => p.Info).Returns(peerInfo);

            return(peerMock.Object);
        }
Пример #2
0
        public async Task DialPeerAsync_DialException_ShouldReturnFalse()
        {
            IpEndpointHelper.TryParse(NetworkTestConstants.DialExceptionIpEndpoint, out var endpoint);
            var added = await _networkServer.ConnectAsync(endpoint);

            added.ShouldBeFalse();
            _peerPool.PeerCount.ShouldBe(0);
        }
Пример #3
0
        public async Task DialPeerAsync_HandshakeError_ShouldReturnFalse()
        {
            IpEndpointHelper.TryParse(NetworkTestConstants.BadHandshakeIp, out var endpoint);
            var added = await _networkServer.ConnectAsync(endpoint);

            added.ShouldBeFalse();
            _peerPool.PeerCount.ShouldBe(0);
        }
Пример #4
0
        public async Task <bool> AddPeerAsync(string address)
        {
            if (IpEndpointHelper.TryParse(address, out IPEndPoint endpoint))
            {
                return(await _networkServer.ConnectAsync(endpoint));
            }

            return(false);
        }
Пример #5
0
        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();
        }
Пример #6
0
        public async Task DialPeerAsync_KeyAlreadyInPool_ShouldReturnFalse()
        {
            // two different hosts with the same pubkey.
            AddPeerToPool();

            IpEndpointHelper.TryParse(NetworkTestConstants.FakeIpEndpoint2, out var endpoint);
            var added = await _networkServer.ConnectAsync(endpoint);

            added.ShouldBeFalse();
            _netTestHelpers.AllPeersWhereCleaned().ShouldBeTrue();
        }
Пример #7
0
        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();
        }
Пример #8
0
        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);
        }
Пример #9
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 =>
            {
                if (!IpEndpointHelper.TryParse(node, out IPEndPoint endpoint))
                {
                    return(false);
                }

                return(await ConnectAsync(endpoint));
            }).ToList();

            await Task.WhenAll(taskList.ToArray <Task>());
        }
Пример #10
0
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            base.OnApplicationInitialization(context);

            var pool    = context.ServiceProvider.GetRequiredService <IPeerPool>();
            var channel = new Channel(NetworkTestConstants.FakeIpEndpoint, ChannelCredentials.Insecure);

            var connectionInfo = new PeerInfo
            {
                Pubkey          = NetworkTestConstants.FakePubkey2,
                ProtocolVersion = KernelConstants.ProtocolVersion,
                ConnectionTime  = TimestampHelper.GetUtcNow().Seconds,
                IsInbound       = true
            };

            if (!IpEndpointHelper.TryParse(NetworkTestConstants.FakeIpEndpoint, out var peerEnpdoint))
            {
                throw new Exception($"Ip {NetworkTestConstants.FakeIpEndpoint} is invalid.");
            }

            pool.TryAddPeer(new GrpcPeer(new GrpcClient(channel, new PeerService.PeerServiceClient(channel)), peerEnpdoint, connectionInfo));
        }