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); }
public void Initialize(CancellationToken cancellationToken) { if (_isInitialized) { return; } lock (_sync) { if (_isInitialized) { return; } _logger.Info("Initialization Started"); try { _communicationService.Init(new Network.Communication.SocketSettings(_communicationConfiguration.MaxConnections, _communicationConfiguration.ReceiveBufferSize, _communicationConfiguration.ListeningPort, System.Net.Sockets.AddressFamily.InterNetwork)); if (_dataAccessService.GetLastSyncBlock(out ulong syncBlockHeight, out byte[] syncBlockHash)) { _lastSyncDescriptor = new Entities.SyncBlockDescriptor(syncBlockHeight, syncBlockHash); } else { _lastSyncDescriptor = new Entities.SyncBlockDescriptor(0, new byte[32]); } if (_dataAccessService.GetLastRegistryCombinedBlock(out ulong combinedBlockHeight, out byte[] combinedBlockContent)) { _lastCombinedBlockDescriptor = new RegistryCombinedBlockDescriptor(combinedBlockHeight, combinedBlockContent, _defaultHashCalculation.CalculateHash(combinedBlockContent)); } else { _lastCombinedBlockDescriptor = new RegistryCombinedBlockDescriptor(0, null, new byte[32]); } foreach (string nodeDescriptor in _synchronizerConfiguration.Nodes) { string[] pair = nodeDescriptor.Split(':'); byte[] keyBytes = pair[0].HexStringToByteArray(); IKey nodeKey = _identityKeyProvider.GetKey(keyBytes); IPAddress ipAddress = IPAddress.Parse(pair[1]); NodeAddress nodeAddress = new NodeAddress(nodeKey, ipAddress); _nodesResolutionService.UpdateSingleNode(nodeAddress); } IKey syncNodeKey = _identityKeyProvider.GetKey(_synchronizerConfiguration.SyncNodeKey.HexStringToByteArray()); _registryNodeKey = _identityKeyProvider.GetKey(_synchronizerConfiguration.RegistryNodeKey.HexStringToByteArray()); _storageNodeKey = _identityKeyProvider.GetKey(_synchronizerConfiguration.StorageNodeKey.HexStringToByteArray()); IPAddress syncNodeAddress = _nodesResolutionService.ResolveNodeAddress(syncNodeKey); IPAddress storageNodeAddress = _nodesResolutionService.ResolveNodeAddress(_storageNodeKey); Channel syncLayerChannel = new Channel(syncNodeAddress.ToString(), 5050, ChannelCredentials.Insecure); Channel storageLayerChannel = new Channel(storageNodeAddress.ToString(), 5050, ChannelCredentials.Insecure); _syncLayerSyncManagerClient = new SyncManager.SyncManagerClient(syncLayerChannel); _storageLayerSyncManagerClient = new TransactionalChainManager.TransactionalChainManagerClient(storageLayerChannel); _cancellationToken = cancellationToken; _isInitialized = true; _logger.Info($"Last Sync Block Height = {_lastSyncDescriptor.Height}; Last Registry Combined Block Height = {_lastCombinedBlockDescriptor.Height}"); } catch (Exception ex) { _logger.Error("Failure during initializtion", ex); } finally { _logger.Info("Initialization completed"); } } }