Exemplo n.º 1
0
        public async Task NodeVersionTwiceShouldUseSameClient()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            // Act
            IBeaconNodeApi beaconNodeProxy = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            string version2 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION");
            version2.ShouldBe("TESTVERSION");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(1);
        }
Exemplo n.º 2
0
        public async Task NodeVersionInitialFailTryAgain()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient1 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient1.VersionAsync(Arg.Any <CancellationToken>()).Throws(new HttpRequestException("TESTEXCEPTION"));
            IBeaconNodeOApiClient beaconNodeOApiClient2 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient2.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient1, beaconNodeOApiClient2);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            // Act
            IBeaconNodeApi beaconNodeProxy = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(2);
        }
Exemplo n.º 3
0
        public async Task NodeVersionInitialFailAfterSuccessTryAgain()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient1 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient1.VersionAsync(Arg.Any <CancellationToken>()).Returns(
                callInfo => "TESTVERSION1",
                callInfo => throw new HttpRequestException("TESTEXCEPTION")
                );
            beaconNodeOApiClient1.BaseUrl.Returns("CLIENT1");
            IBeaconNodeOApiClient beaconNodeOApiClient2 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient2.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION2");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient1, beaconNodeOApiClient2);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            // Act
            IBeaconNodeApi beaconNodeProxy = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            string version2 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION1");
            version2.ShouldBe("TESTVERSION2");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(2);

            List <ICall> client1Received = beaconNodeOApiClient1.ReceivedCalls().ToList();

            client1Received.Count(x => x.GetMethodInfo().Name == nameof(beaconNodeOApiClient1.VersionAsync)).ShouldBe(2);

            List <ICall> client2Received = beaconNodeOApiClient2.ReceivedCalls().ToList();

            client2Received.Count(x => x.GetMethodInfo().Name == nameof(beaconNodeOApiClient1.VersionAsync)).ShouldBe(1);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> GetAsync(CancellationToken cancellationToken)
        {
            if (_logger.IsInfo())
            {
                Log.NodeVersionRequested(_logger, HttpContext.Connection.RemoteIpAddress, null);
            }

            ApiResponse <string> apiResponse =
                await _beaconNode.GetNodeVersionAsync(cancellationToken).ConfigureAwait(false);

            if (apiResponse.StatusCode == Core2.Api.StatusCode.Success)
            {
                return(Ok(apiResponse.Content));
            }

            return(Problem("Beacon node internal error.", statusCode: (int)apiResponse.StatusCode));
        }
Exemplo n.º 5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            if (_logger.IsInfo())
            {
                Log.HonestValidatorWorkerExecuteStarted(_logger, _clientVersion.Description,
                                                        _environment.EnvironmentName, Thread.CurrentThread.ManagedThreadId, null);
            }

            try
            {
                // Config
                // List of nodes
                // Validator private keys (or quickstart)
                // Seconds per slot

                string nodeVersion = string.Empty;
                while (nodeVersion == string.Empty)
                {
                    try
                    {
                        nodeVersion = await _beaconNodeApi.GetNodeVersionAsync(stoppingToken).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Log.WaitingForNodeVersion(_logger, ex);
                        await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken).ConfigureAwait(false);
                    }
                }

                ulong genesisTime = 0;
                while (genesisTime == 0)
                {
                    try
                    {
                        genesisTime = await _beaconNodeApi.GetGenesisTimeAsync(stoppingToken).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Log.WaitingForGenesisTime(_logger, ex);
                        await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken).ConfigureAwait(false);
                    }
                }

                Log.HonestValidatorWorkerConnected(_logger, nodeVersion, genesisTime, null);

                await _beaconChain.SetGenesisTimeAsync(genesisTime).ConfigureAwait(false);

                while (!stoppingToken.IsCancellationRequested && !_stopped)
                {
                    try
                    {
                        DateTimeOffset clockTime = _clock.UtcNow();
                        ulong          time      = (ulong)clockTime.ToUnixTimeSeconds();

                        if (time > genesisTime)
                        {
                            await _validatorClient.OnTickAsync(_beaconChain, time, stoppingToken).ConfigureAwait(false);
                        }

                        // Wait for remaining time, if any
                        // NOTE: To fast forward time during testing, have the second call to test _clock.Now() jump forward to avoid waiting.
                        DateTimeOffset nextClockTime = DateTimeOffset.FromUnixTimeSeconds((long)time + 1);
                        TimeSpan       remaining     = nextClockTime - _clock.UtcNow();
                        if (remaining > TimeSpan.Zero)
                        {
                            await Task.Delay(remaining, stoppingToken);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // This is expected when exiting
                    }
                    catch (Exception ex)
                    {
                        if (_logger.IsError())
                        {
                            Log.HonestValidatorWorkerLoopError(_logger, ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.HonestValidatorWorkerCriticalError(_logger, ex);
                throw;
            }

            if (_logger.IsDebug())
            {
                LogDebug.HonestValidatorWorkerExecuteExiting(_logger, Thread.CurrentThread.ManagedThreadId, null);
            }
        }
Exemplo n.º 6
0
 /// <summary>Get version string of the running beacon node.</summary>
 /// <returns>Request successful</returns>
 public async Task <string> VersionAsync()
 {
     return(await _beaconNode.GetNodeVersionAsync());
 }
Exemplo n.º 7
0
 /// <summary>Get version string of the running beacon node.</summary>
 /// <returns>Request successful</returns>
 public async Task <string> VersionAsync()
 {
     return(await _beaconNode.GetNodeVersionAsync(CancellationToken.None));
 }