public override Task GetAllCombinedRegistryBlocksSinceSync(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); } })); }
protected override void InitializeInner() { _logger.Info("Starting Synchronization Initializer"); try { SynchronizationConfirmedBlock synchronizationConfirmedBlock = (SynchronizationConfirmedBlock)_chainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_ConfirmedBlock).Single(); if (synchronizationConfirmedBlock != null) { _synchronizationContext.UpdateLastSyncBlockDescriptor(new SynchronizationDescriptor(synchronizationConfirmedBlock.BlockHeight, _hashCalculation.CalculateHash(synchronizationConfirmedBlock.RawData), synchronizationConfirmedBlock.ReportedTime, DateTime.Now, synchronizationConfirmedBlock.Round)); } SynchronizationRegistryCombinedBlock combinedBlock = (SynchronizationRegistryCombinedBlock)_chainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_RegistryCombinationBlock).Single(); if (combinedBlock != null) { _synchronizationContext.LastRegistrationCombinedBlockHeight = combinedBlock.BlockHeight; } } finally { _logger.Info("Synchronization Initializer completed"); } }
private void CreateAndDistributeCombinedBlock(RegistryFullBlock transactionsFullBlockMostConfident) { lock (_synchronizationContext) { SynchronizationRegistryCombinedBlock lastCombinedBlock = (SynchronizationRegistryCombinedBlock)_synchronizationChainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_RegistryCombinationBlock).Single(); byte[] prevHash = lastCombinedBlock != null?_defaultTransactionHashCalculation.CalculateHash(lastCombinedBlock.RawData) : new byte[Globals.DEFAULT_HASH_SIZE]; byte[] fullBlockHash = _defaultTransactionHashCalculation.CalculateHash(transactionsFullBlockMostConfident?.RawData ?? new byte[Globals.DEFAULT_HASH_SIZE]); //TODO: For initial POC there will be only one participant at Synchronization Layer, thus combination of FullBlocks won't be implemented fully SynchronizationRegistryCombinedBlock synchronizationRegistryCombinedBlock = new SynchronizationRegistryCombinedBlock { SyncBlockHeight = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0, PowHash = _powCalculation.CalculateHash(_synchronizationContext.LastBlockDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE]), BlockHeight = ++_synchronizationContext.LastRegistrationCombinedBlockHeight, HashPrev = prevHash, ReportedTime = DateTime.Now, BlockHashes = new byte[][] { fullBlockHash } }; ISerializer combinedBlockSerializer = _signatureSupportSerializersFactory.Create(synchronizationRegistryCombinedBlock); combinedBlockSerializer.FillBodyAndRowBytes(); IEnumerable <IKey> storageLayerKeys = _nodesResolutionService.GetStorageNodeKeys(combinedBlockSerializer); _communicationService.PostMessage(storageLayerKeys, combinedBlockSerializer); _synchronizationChainDataService.Add(synchronizationRegistryCombinedBlock); } }