Ejemplo n.º 1
0
        /// <summary>
        ///     Handle the request for GetPeerInfo
        /// </summary>
        /// <param name="getPeerInfoRequest">The request</param>
        /// <param name="channelHandlerContext">The channel handler context</param>
        /// <param name="senderPeerId">The sender peer identifier</param>
        /// <param name="correlationId">The correlationId</param>
        /// <returns>The GetPeerInfoResponse</returns>
        protected override GetPeerInfoResponse HandleRequest(GetPeerInfoRequest getPeerInfoRequest,
                                                             IChannelHandlerContext channelHandlerContext,
                                                             PeerId senderPeerId,
                                                             ICorrelationId correlationId)
        {
            Guard.Argument(getPeerInfoRequest, nameof(getPeerInfoRequest)).NotNull();
            Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull();
            Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull();
            Logger.Debug("received message of type GetPeerInfoRequest");

            var ip = getPeerInfoRequest.Ip;

            var peerInfo = _peerRepository.GetPeersByIpAndPublicKey(ip, getPeerInfoRequest.PublicKey)
                           .Select(x =>
                                   new PeerInfo
            {
                PeerId        = x.PeerId,
                Reputation    = x.Reputation,
                IsBlacklisted = x.BlackListed,
                IsUnreachable = x.IsAwolPeer,
                InactiveFor   = x.InactiveFor.ToDuration(),
                LastSeen      = x.LastSeen.ToTimestamp(),
                Modified      = x.Modified?.ToTimestamp(),
                Created       = x.Created.ToTimestamp()
            }).ToList();

            var response = new GetPeerInfoResponse();

            response.PeerInfo.AddRange(peerInfo);
            return(response);
        }
Ejemplo n.º 2
0
        public async Task RunAsync(CancellationToken ct)
        {
            _logger.Information("Starting the Catalyst Node");
            _logger.Information($"***** using PublicKey: {_publicKey.Bytes.ToBase32()} *****");

            await StartSocketsAsync().ConfigureAwait(false);

            _dfsService.StartAsync().ConfigureAwait(false);
            _synchronizer.StartAsync().ConfigureAwait(false);

            _synchronizer.SyncCompleted.Subscribe((x) =>
            {
                var currentPoaNode = _peerRepository.GetPeersByIpAndPublicKey(_peerSettings.PeerId.Ip, _peerSettings.PeerId.PublicKey).FirstOrDefault();
                if (currentPoaNode == null)
                {
                    _logger.Information($"Current node with IP address '{_peerSettings.PeerId.Ip}' is not found in poa node list. So this node will not take part in consensus.");
                }
                else
                {
                    Consensus.StartProducing();
                }
            });

            bool exit;

            do
            {
                await Task.Delay(300, ct); //just to get the exit message at the bottom

                _logger.Debug("Type 'exit' to exit, anything else to continue");
                exit = string.Equals(Console.ReadLine(), "exit", StringComparison.OrdinalIgnoreCase);
            } while (!ct.IsCancellationRequested);

            _logger.Debug("Stopping the Catalyst Node");
        }
Ejemplo n.º 3
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            if (!_syncState.IsSynchronized)
            {
                return;
            }

            try
            {
                Logger.Verbose("received {message} from {port}", messageDto.Payload.CorrelationId.ToCorrelationId(),
                               messageDto.Payload.PeerId.Port);

                // @TODO here we use the protobuff message to parse rather than using the CandidateDeltaBroadcastDao
                /////////////////////////////////////////////////////////////////////////////////////////////////
                var deserialized            = messageDto.Payload.FromProtocolMessage <CandidateDeltaBroadcast>();
                var previousDeltaDfsHashCid = deserialized.PreviousDeltaDfsHash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(previousDeltaDfsHashCid.Hash.ToArray()))
                {
                    Logger.Error("PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                /////////////////////////////////////////////////////////////////////////////////////////////////
                var hashCid = deserialized.Hash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(hashCid.Hash.ToArray()))
                {
                    Logger.Error("Hash is not a valid hash");
                    return;
                }


                var messagePoaNode = _peerRepository.GetPeersByIpAndPublicKey(messageDto.Payload.PeerId.Ip, messageDto.Payload.PeerId.PublicKey).FirstOrDefault();
                if (messagePoaNode == null)
                {
                    Logger.Error($"Message from IP address '{messageDto.Payload.PeerId.Ip}' with public key '{messageDto.Payload.PeerId.PublicKey}' is not found in producer node list.");
                    return;
                }

                if (deserialized.IsValid())
                {
                    _deltaVoter.OnNext(deserialized);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception,
                             $"Failed to process candidate delta broadcast {messageDto.Payload.ToJsonString()}.");
            }
        }
Ejemplo n.º 4
0
        public void HandleBroadcast_Should_Cast_Hashes_To_Multihash_And_Try_Update()
        {
            var newHash         = _hashProvider.ComputeUtf8MultiHash("newHash").ToCid();
            var prevHash        = _hashProvider.ComputeUtf8MultiHash("prevHash").ToCid();
            var receivedMessage = PrepareReceivedMessage(newHash.ToArray(), prevHash.ToArray());

            _peerRepository.GetPeersByIpAndPublicKey(receivedMessage.Payload.PeerId.Ip, receivedMessage.Payload.PeerId.PublicKey).Returns(new List <Peer>()
            {
                new Peer()
            });
            var deltaDfsHashObserver = new DeltaDfsHashObserver(_deltaHashProvider, _syncState, _peerRepository, _logger);

            deltaDfsHashObserver.HandleBroadcast(receivedMessage);

            _deltaHashProvider.Received(1).TryUpdateLatestHash(prevHash, newHash);
        }
Ejemplo n.º 5
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            if (!_syncState.IsSynchronized)
            {
                return;
            }

            try
            {
                var deserialized = messageDto.Payload.FromProtocolMessage <FavouriteDeltaBroadcast>();

                var previousDeltaDfsHashCid = deserialized.Candidate.PreviousDeltaDfsHash.ToByteArray().ToCid();
                if (!_hashProvider.IsValidHash(previousDeltaDfsHashCid.Hash.ToArray()))
                {
                    Logger.Error("PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                var hashCid = deserialized.Candidate.Hash.ToByteArray().ToCid();
                if (!_hashProvider.IsValidHash(hashCid.Hash.ToArray()))
                {
                    Logger.Error("Hash is not a valid hash");
                    return;
                }


                var messagePoaNode = _peerRepository.GetPeersByIpAndPublicKey(messageDto.Payload.PeerId.Ip, messageDto.Payload.PeerId.PublicKey).FirstOrDefault();
                if (messagePoaNode == null)
                {
                    Logger.Error($"Message from IP address '{messageDto.Payload.PeerId.Ip}' with public key '{messageDto.Payload.PeerId.PublicKey}' is not found in producer node list.");
                    return;
                }

                deserialized.IsValid();

                _deltaElector.OnNext(deserialized);
            }
            catch (Exception exception)
            {
                Logger.Error(exception,
                             $"Failed to process favourite delta broadcast {messageDto.Payload.ToJsonString()}.");
            }
        }
Ejemplo n.º 6
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            if (!_syncState.IsSynchronized)
            {
                return;
            }

            try
            {
                var deserialised = messageDto.Payload.FromProtocolMessage <DeltaDfsHashBroadcast>();

                var previousHash = deserialised.PreviousDeltaDfsHash.ToByteArray().ToCid();
                if (previousHash == null)
                {
                    Logger.Error("PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                var newHash = deserialised.DeltaDfsHash.ToByteArray().ToCid();
                if (newHash == null)
                {
                    Logger.Error("DeltaDfsHash is not a valid hash");
                    return;
                }

                var messagePoaNode = _peerRepository.GetPeersByIpAndPublicKey(messageDto.Payload.PeerId.Ip, messageDto.Payload.PeerId.PublicKey).FirstOrDefault();
                if (messagePoaNode == null)
                {
                    Logger.Error($"Message from IP address '{messageDto.Payload.PeerId.Ip}' with public key '{messageDto.Payload.PeerId.PublicKey}' is not found in producer node list.");
                    return;
                }

                _deltaHashProvider.TryUpdateLatestHash(previousHash, newHash);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, "Failed to update latest delta hash from incoming broadcast message.");
            }
        }