public async Task <ApiResponse <Syncing> > GetSyncingAsync(CancellationToken cancellationToken) { try { Slot currentSlot = Slot.Zero; if (_store.IsInitialized) { Root head = await _forkChoice.GetHeadAsync(_store).ConfigureAwait(false); BeaconBlock block = (await _store.GetSignedBlockAsync(head).ConfigureAwait(false)).Message; currentSlot = block.Slot; } Slot highestSlot = Slot.Max(currentSlot, _networkPeering.HighestPeerSlot); Slot startingSlot = _networkPeering.SyncStartingSlot; bool isSyncing = highestSlot > currentSlot; Syncing syncing = new Syncing(isSyncing, new SyncingStatus(startingSlot, currentSlot, highestSlot)); return(ApiResponse.Create(StatusCode.Success, syncing)); } catch (Exception ex) { if (_logger.IsWarn()) { Log.ApiErrorGetSyncing(_logger, ex); } throw; } }
public Task SetSyncStatus(Syncing syncing) { NodeIsSyncing = syncing.IsSyncing; if (syncing.SyncStatus != null) { SyncStatus = syncing.SyncStatus; } return(Task.CompletedTask); }
public async Task Syncing_DeserializeWithStatus() { // Arrange JsonSerializerOptions options = new JsonSerializerOptions(); options.ConfigureNethermindCore2(); string jsonString = "{\"is_syncing\":true,\"sync_status\":{\"current_slot\":2,\"highest_slot\":3,\"starting_slot\":1}}"; // Act - deserialize from string await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); Syncing syncing = await JsonSerializer.DeserializeAsync <Syncing>(memoryStream, options); syncing.IsSyncing.ShouldBeTrue(); syncing.SyncStatus !.CurrentSlot.ShouldBe(new Slot(2)); }
public async Task Syncing_DeserializeAlternativeOrderNullStatus() { // Arrange JsonSerializerOptions options = new JsonSerializerOptions(); options.ConfigureNethermindCore2(); string jsonString = "{\"sync_status\":null,\"is_syncing\":true}"; // Act - deserialize from string await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); Syncing syncing = await JsonSerializer.DeserializeAsync <Syncing>(memoryStream, options); syncing.IsSyncing.ShouldBeTrue(); syncing.SyncStatus.ShouldBeNull(); }
public async Task ShouldGetStatusWhenSyncComplete() { // Arrange Slot starting = Slot.One; Slot current = new Slot(11); Slot highest = new Slot(11); INetworkPeering mockNetworkPeering = Substitute.For <INetworkPeering>(); mockNetworkPeering.HighestPeerSlot.Returns(highest); mockNetworkPeering.SyncStartingSlot.Returns(starting); IStore mockStore = Substitute.For <IStore>(); Root root = new Root(Enumerable.Repeat((byte)0x12, 32).ToArray()); Checkpoint checkpoint = new Checkpoint(Epoch.Zero, root); SignedBeaconBlock signedBlock = new SignedBeaconBlock(new BeaconBlock(current, Root.Zero, Root.Zero, BeaconBlockBody.Zero), BlsSignature.Zero); BeaconState state = TestState.Create(slot: current, finalizedCheckpoint: checkpoint); mockStore.GetHeadAsync().Returns(root); mockStore.GetSignedBlockAsync(root).Returns(signedBlock); mockStore.GetBlockStateAsync(root).Returns(state); mockStore.IsInitialized.Returns(true); mockStore.JustifiedCheckpoint.Returns(checkpoint); IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true); testServiceCollection.AddSingleton(mockNetworkPeering); testServiceCollection.AddSingleton(mockStore); testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>()); testServiceCollection.AddSingleton <IEth1DataProvider>(Substitute.For <IEth1DataProvider>()); testServiceCollection.AddSingleton <IOperationPool>(Substitute.For <IOperationPool>()); ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider(); // Act IBeaconNodeApi beaconNode = testServiceProvider.GetService <IBeaconNodeApi>(); beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade)); ApiResponse <Syncing> syncingResponse = await beaconNode.GetSyncingAsync(CancellationToken.None); Syncing syncing = syncingResponse.Content; // Assert syncing.IsSyncing.ShouldBeFalse(); syncing.SyncStatus !.StartingSlot.ShouldBe(Slot.One); syncing.SyncStatus.CurrentSlot.ShouldBe(new Slot(11)); syncing.SyncStatus.HighestSlot.ShouldBe(new Slot(11)); }
public async Task <ApiResponse <Syncing> > GetSyncingAsync(CancellationToken cancellationToken) { string uri = "node/syncing"; using HttpResponseMessage httpResponse = await _httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken); httpResponse.EnsureSuccessStatusCode(); // throws if not 200-299 await using Stream contentStream = await httpResponse.Content.ReadAsStreamAsync(); Syncing content = await JsonSerializer.DeserializeAsync <Syncing>(contentStream, _jsonSerializerOptions, cancellationToken); return(ApiResponse.Create((StatusCode)(int)httpResponse.StatusCode, content)); }
public async Task Syncing_SerializeWithStatus() { // Arrange JsonSerializerOptions options = new JsonSerializerOptions(); options.ConfigureNethermindCore2(); Syncing syncing = new Syncing(true, new SyncingStatus(Slot.One, new Slot(2), new Slot(3))); // Act - serialize to string await using MemoryStream memoryStream = new MemoryStream(); await JsonSerializer.SerializeAsync(memoryStream, syncing, options); string jsonString = Encoding.UTF8.GetString(memoryStream.ToArray()); // Assert jsonString.ShouldBe("{\"is_syncing\":true,\"sync_status\":{\"current_slot\":2,\"highest_slot\":3,\"starting_slot\":1}}"); }
public async Task Syncing_SerializeNullStatus() { // Arrange JsonSerializerOptions options = new JsonSerializerOptions(); options.ConfigureNethermindCore2(); Syncing syncing = new Syncing(true, null); // Act - serialize to string await using MemoryStream memoryStream = new MemoryStream(); await JsonSerializer.SerializeAsync(memoryStream, syncing, options); string jsonString = Encoding.UTF8.GetString(memoryStream.ToArray()); // Assert jsonString.ShouldBe("{\"is_syncing\":true,\"sync_status\":null}"); }
internal virtual void OnSyncing() => Syncing?.Invoke(this, EventArgs.Empty);