Example #1
0
        public void Start()
        {
            _logger.Info("Started");

            _communicationService.Start();

            PeriodicTaskFactory.Start(async() =>
            {
                if (_isProcessing)
                {
                    return;
                }

                lock (_sync)
                {
                    if (_isProcessing)
                    {
                        return;
                    }

                    _isProcessing = true;
                }

                try
                {
                    Proto.Model.SyncBlockDescriptor syncBlockDescriptor = _syncLayerSyncManagerClient.GetLastSyncBlock(new Empty());
                    if (_lastSyncDescriptor.Height < syncBlockDescriptor.Height)
                    {
                        _lastSyncDescriptor = new Entities.SyncBlockDescriptor(syncBlockDescriptor.Height, syncBlockDescriptor.Hash.ToByteArray());
                        _dataAccessService.UpdateLastSyncBlock(syncBlockDescriptor.Height, syncBlockDescriptor.Hash.ToByteArray());
                    }

                    ulong lastCombinedBlockHeight = _lastCombinedBlockDescriptor.Height;
                    SynchronizationRegistryCombinedBlock lastCombinedBlock = null;
                    AsyncServerStreamingCall <TransactionInfo> asyncCall   = _syncLayerSyncManagerClient.GetCombinedRegistryBlocksContentSinceHeight(new ByHeightRequest {
                        Height = _lastCombinedBlockDescriptor.Height
                    });
                    while (await asyncCall.ResponseStream.MoveNext(_cancellationToken))
                    {
                        SynchronizationRegistryCombinedBlock combinedBlock = GetRegistryCombinedBlock(asyncCall.ResponseStream.Current);

                        if (combinedBlock == null)
                        {
                            continue;
                        }

                        try
                        {
                            _dataAccessService.UpdateLastRegistryCombinedBlock(combinedBlock.BlockHeight, combinedBlock.RawData.ToArray());
                            if (lastCombinedBlockHeight < combinedBlock.BlockHeight)
                            {
                                lastCombinedBlockHeight = combinedBlock.BlockHeight;
                                lastCombinedBlock       = combinedBlock;
                            }

                            foreach (byte[] fullRegistryBlockHash in combinedBlock.BlockHashes)
                            {
                                await UpdateTransactionsByFullRegistryBlock(combinedBlock, fullRegistryBlockHash);
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error($"Failure during obtaining transactions at Registry Combined Block with height {combinedBlock.BlockHeight}", ex);
                        }
                    }

                    if (lastCombinedBlock != null)
                    {
                        _lastCombinedBlockDescriptor = new RegistryCombinedBlockDescriptor(lastCombinedBlock.BlockHeight, lastCombinedBlock.RawData.ToArray(), _defaultHashCalculation.CalculateHash(lastCombinedBlock.RawData));
                        _dataAccessService.UpdateLastRegistryCombinedBlock(_lastCombinedBlockDescriptor.Height, _lastCombinedBlockDescriptor.Hash);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Failure during updating blockchain", ex);
                }
                finally
                {
                    _isProcessing = false;
                }
            }, 5000, cancelToken: _cancellationToken);
        }
Example #2
0
 private void FillSyncData(UtxoConfidentialBase block)
 {
     Proto.Model.SyncBlockDescriptor lastSyncBlock = _networkAdapter.GetLastSyncBlock();
     block.SyncBlockHeight = lastSyncBlock.Height;
     block.PowHash         = GetPowHash(lastSyncBlock.Hash.ToByteArray(), 0);
 }