Ejemplo n.º 1
0
        private void ProcessMessagesQueue(CancellationToken token)
        {
            foreach (var messageByKey in _messagesQueue.GetConsumingEnumerable(token))
            {
                IPAddress address = _nodesResolutionService.ResolveNodeAddress(messageByKey.Key);

                ICommunicationChannel communicationChannel = GetChannel(address);

                if (communicationChannel != null)
                {
                    communicationChannel.PostMessage(messageByKey.Value);
                }
            }
        }
Ejemplo n.º 2
0
        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");
                }
            }
        }