public override Task GetAllCombinedRegistryBlocksPerSync(ByHeightRequest request, IServerStreamWriter <CombinedRegistryBlockInfo> responseStream, ServerCallContext context) { return(Task.Run(() => { IEnumerable <PacketBase> blocks = _syncChainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_RegistryCombinationBlock).Where(b => ((SynchronizationRegistryCombinedBlock)b).SyncBlockHeight == request.Height); foreach (PacketBase blockBase in blocks) { SynchronizationRegistryCombinedBlock registryCombinedBlock = blockBase as SynchronizationRegistryCombinedBlock; CombinedRegistryBlockInfo combinedRegistryBlockInfo = new CombinedRegistryBlockInfo { SyncBlockHeight = registryCombinedBlock.SyncBlockHeight, Height = registryCombinedBlock.BlockHeight, CombinedRegistryBlocksCount = (uint)registryCombinedBlock.BlockHashes.Length, }; foreach (byte[] item in registryCombinedBlock.BlockHashes) { combinedRegistryBlockInfo.BlockDescriptors.Add(new FullBlockDescriptor { BlockHash = ByteString.CopyFrom(item) }); } responseStream.WriteAsync(combinedRegistryBlockInfo); } })); }
public override async Task GetCombinedRegistryBlocksInfoSinceHeight(ByHeightRequest request, IServerStreamWriter <CombinedRegistryBlockInfo> responseStream, ServerCallContext context) { IEnumerable <BlockBase> blocks = _syncChainDataService.GetAll(new BlockTypeLowHeightKey(BlockTypes.Synchronization_RegistryCombinationBlock, request.Height)); foreach (BlockBase blockBase in blocks) { SynchronizationRegistryCombinedBlock registryCombinedBlock = blockBase as SynchronizationRegistryCombinedBlock; CombinedRegistryBlockInfo combinedRegistryBlockInfo = new CombinedRegistryBlockInfo { SyncBlockHeight = registryCombinedBlock.SyncBlockHeight, Height = registryCombinedBlock.BlockHeight, CombinedRegistryBlocksCount = (uint)registryCombinedBlock.BlockHashes.Length, }; foreach (byte[] hash in registryCombinedBlock.BlockHashes) { RegistryFullBlock registryFullBlock = (RegistryFullBlock)_registryChainDataService.Get(new SyncHashKey(registryCombinedBlock.SyncBlockHeight, hash)); if (registryFullBlock != null) { combinedRegistryBlockInfo.BlockDescriptors.Add( new FullBlockDescriptor { SyncBlockHeight = registryCombinedBlock.SyncBlockHeight, Round = registryFullBlock.BlockHeight, TransactionsCount = (uint)registryFullBlock.TransactionHeaders.Count, BlockHash = ByteString.CopyFrom(hash) }); } } await responseStream.WriteAsync(combinedRegistryBlockInfo); } }
public async void Update() { if (_isUpdating) { return; } lock (_sync) { if (_isUpdating) { return; } _isUpdating = true; } BulkUpdate bulkUpdate = new BulkUpdate(); try { ulong lastUpdatedSyncHeight = 0; AsyncServerStreamingCall <SyncBlockDescriptor> serverStreamingCall = _syncManagerClient.GetDeltaSyncBlocks(new ByHeightRequest { Height = _lastUpdatedSyncHeight }); while (await serverStreamingCall.ResponseStream.MoveNext()) { SyncBlockDescriptor syncBlockDescriptor = serverStreamingCall.ResponseStream.Current; if (syncBlockDescriptor.Height > _lastUpdatedSyncHeight) { bulkUpdate.SyncBlockInfos.Add(new Models.SyncBlockInfo { SyncBlockHeight = syncBlockDescriptor.Height }); if (lastUpdatedSyncHeight < syncBlockDescriptor.Height) { lastUpdatedSyncHeight = syncBlockDescriptor.Height; } } } if (_lastUpdatedSyncHeight < lastUpdatedSyncHeight) { _lastUpdatedSyncHeight = lastUpdatedSyncHeight; } ulong lastUpdatedCombinedBlockHeight = 0; AsyncServerStreamingCall <CombinedRegistryBlockInfo> serverStreamingCall2 = _syncManagerClient.GetCombinedRegistryBlocksInfoSinceHeight(new ByHeightRequest { Height = _lastUpdatedCombinedBlockHeight }); while (await serverStreamingCall2.ResponseStream.MoveNext()) { CombinedRegistryBlockInfo combinedRegistryBlockInfo = serverStreamingCall2.ResponseStream.Current; if (combinedRegistryBlockInfo.Height > _lastUpdatedCombinedBlockHeight) { bulkUpdate.CombinedBlockInfos.Add( new CombinedBlockInfo { SyncBlockHeight = combinedRegistryBlockInfo.SyncBlockHeight, BlockHeight = combinedRegistryBlockInfo.Height, CombinedRegistryBlocksCount = combinedRegistryBlockInfo.CombinedRegistryBlocksCount, RegistryFullBlockInfos = combinedRegistryBlockInfo.BlockDescriptors.Select( b => new RegistryFullBlockInfo { SyncBlockHeight = b.SyncBlockHeight, Round = b.Round, TransactionsCount = b.TransactionsCount }).ToList() }); if (lastUpdatedCombinedBlockHeight < combinedRegistryBlockInfo.Height) { lastUpdatedCombinedBlockHeight = combinedRegistryBlockInfo.Height; } } } if (_lastUpdatedCombinedBlockHeight < lastUpdatedCombinedBlockHeight) { _lastUpdatedCombinedBlockHeight = lastUpdatedCombinedBlockHeight; } } finally { _isUpdating = false; } foreach (IObserver <BulkUpdate> observer in _observers) { observer.OnNext(bulkUpdate); } }