Esempio n. 1
0
        public virtual void HandleMessage(Packet message)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace($"{Session.RemoteNodeId} {nameof(Eth62ProtocolHandler)} handling a message with code {message.PacketType}.");
            }

            if (message.PacketType != Eth62MessageCode.Status && !_statusReceived)
            {
                throw new SubprotocolException($"{Session.RemoteNodeId} No {nameof(StatusMessage)} received prior to communication.");
            }

            switch (message.PacketType)
            {
            case Eth62MessageCode.Status:
                StatusMessage statusMessage = Deserialize <StatusMessage>(message.Data);
                if (statusMessage.StrangePrefix != null)
                {
//                        Node.ClientId = $"STRANGE_PREFIX({statusMessage.StrangePrefix}) " + Node.ClientId;
                }
                Handle(statusMessage);
                break;

            case Eth62MessageCode.NewBlockHashes:
                Metrics.Eth62NewBlockHashesReceived++;
                Handle(Deserialize <NewBlockHashesMessage>(message.Data));
                break;

            case Eth62MessageCode.Transactions:
                Metrics.Eth62TransactionsReceived++;
                if (!_isDowngradedDueToTxFlooding || 10 > _random.Next(0, 99))     // TODO: disable that when IsMining is set to true
                {
                    Handle(Deserialize <TransactionsMessage>(message.Data));
                }
                break;

            case Eth62MessageCode.GetBlockHeaders:
                Metrics.Eth62GetBlockHeadersReceived++;
                Handle(Deserialize <GetBlockHeadersMessage>(message.Data));
                break;

            case Eth62MessageCode.BlockHeaders:
                Metrics.Eth62BlockHeadersReceived++;
                Handle(Deserialize <BlockHeadersMessage>(message.Data));
                break;

            case Eth62MessageCode.GetBlockBodies:
                Metrics.Eth62GetBlockBodiesReceived++;
                Handle(Deserialize <GetBlockBodiesMessage>(message.Data));
                break;

            case Eth62MessageCode.BlockBodies:
                Metrics.Eth62BlockBodiesReceived++;
                Handle(Deserialize <BlockBodiesMessage>(message.Data));
                break;

            case Eth62MessageCode.NewBlock:
                Metrics.Eth62NewBlockReceived++;
                Handle(Deserialize <NewBlockMessage>(message.Data));
                break;
            }
        }
Esempio n. 2
0
        public virtual void HandleMessage(Packet message)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace($"{Session.RemoteNodeId} {nameof(Eth62ProtocolHandler)} handling a message with code {message.PacketType}.");
            }

            if (message.PacketType != Eth62MessageCode.Status && !_statusReceived)
            {
                throw new SubprotocolException($"{Session.RemoteNodeId} No {nameof(StatusMessage)} received prior to communication.");
            }

            switch (message.PacketType)
            {
            case Eth62MessageCode.Status:
                StatusMessage statusMessage = Deserialize <StatusMessage>(message.Data);
                Handle(statusMessage);
                break;

            case Eth62MessageCode.NewBlockHashes:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} NewBlockHashes from {Node:s}");
                }
                Metrics.Eth62NewBlockHashesReceived++;
                Handle(Deserialize <NewBlockHashesMessage>(message.Data));
                break;

            case Eth62MessageCode.Transactions:
                Interlocked.Increment(ref _counter);
                Metrics.Eth62TransactionsReceived++;
                if (!_isDowngradedDueToTxFlooding || 10 > _random.Next(0, 99))     // TODO: disable that when IsMining is set to true
                {
                    Handle(Deserialize <TransactionsMessage>(message.Data));
                }

                break;

            case Eth62MessageCode.GetBlockHeaders:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} GetBlockHeaders from {Node:s}");
                }
                Metrics.Eth62GetBlockHeadersReceived++;
                Handle(Deserialize <GetBlockHeadersMessage>(message.Data));
                break;

            case Eth62MessageCode.BlockHeaders:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} BlockHeaders from {Node:s}");
                }
                Metrics.Eth62BlockHeadersReceived++;
                Handle(Deserialize <BlockHeadersMessage>(message.Data));
                break;

            case Eth62MessageCode.GetBlockBodies:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} GetBlockBodies from {Node:s}");
                }
                Metrics.Eth62GetBlockBodiesReceived++;
                Handle(Deserialize <GetBlockBodiesMessage>(message.Data));
                break;

            case Eth62MessageCode.BlockBodies:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} BlockBodies from {Node:s}");
                }
                Metrics.Eth62BlockBodiesReceived++;
                Handle(Deserialize <BlockBodiesMessage>(message.Data));
                break;

            case Eth62MessageCode.NewBlock:
                Interlocked.Increment(ref _counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{_counter:D5} NewBlock from {Node:s}");
                }
                Metrics.Eth62NewBlockReceived++;
                Handle(Deserialize <NewBlockMessage>(message.Data));
                break;
            }
        }
        public virtual void HandleMessage(ZeroPacket message)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace($"Handling {message} message from {Session.Node:c}.");
            }

            if (message.PacketType != Eth62MessageCode.Status && !_statusReceived)
            {
                throw new SubprotocolException($"No {nameof(StatusMessage)} received prior to communication with {Session.Node:c}.");
            }

            int size = message.Content.ReadableBytes;

            // Logger.Warn($"Received a message {message.Protocol}.{Enum.GetName(typeof(Eth62MessageCode), message.PacketType)} of size {size/1024}kb");

            switch (message.PacketType)
            {
            case Eth62MessageCode.Status:
                StatusMessage statusMessage = Deserialize <StatusMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, statusMessage.ToString());
                }
                Handle(statusMessage);
                break;

            case Eth62MessageCode.NewBlockHashes:
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, nameof(NewBlockHashesMessage));
                }
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} NewBlockHashes from {Node:c}");
                }
                Metrics.Eth62NewBlockHashesReceived++;
                Handle(Deserialize <NewBlockHashesMessage>(message.Content));
                break;

            case Eth62MessageCode.Transactions:
                Interlocked.Increment(ref Counter);
                Metrics.Eth62TransactionsReceived++;
                TransactionsMessage transactionsMessage = Deserialize <TransactionsMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(TransactionsMessage)}({transactionsMessage.Transactions.Length})");
                }
                if (!_isDowngradedDueToTxFlooding || 10 > _random.Next(0, 99))     // TODO: disable that when IsMining is set to true
                {
                    Handle(transactionsMessage);
                }

                break;

            case Eth62MessageCode.GetBlockHeaders:
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} GetBlockHeaders from {Node:c}");
                }
                Metrics.Eth62GetBlockHeadersReceived++;
                GetBlockHeadersMessage getBlockHeadersMessage = Deserialize <GetBlockHeadersMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(GetBlockHeadersMessage)}({getBlockHeadersMessage.StartingBlockNumber}|{getBlockHeadersMessage.StartingBlockHash}, {getBlockHeadersMessage.MaxHeaders})");
                }
                Handle(getBlockHeadersMessage);
                break;

            case Eth62MessageCode.BlockHeaders:
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} BlockHeaders from {Node:c}");
                }
                Metrics.Eth62BlockHeadersReceived++;
                BlockHeadersMessage blockHeadersMessage = Deserialize <BlockHeadersMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(BlockHeadersMessage)}({blockHeadersMessage.BlockHeaders.Length})");
                }
                Handle(blockHeadersMessage, size);
                break;

            case Eth62MessageCode.GetBlockBodies:
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} GetBlockBodies from {Node:c}");
                }
                Metrics.Eth62GetBlockBodiesReceived++;
                GetBlockBodiesMessage getBlockBodiesMessage = Deserialize <GetBlockBodiesMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(GetBlockBodiesMessage)}({getBlockBodiesMessage.BlockHashes.Count})");
                }
                Handle(getBlockBodiesMessage);
                break;

            case Eth62MessageCode.BlockBodies:
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} BlockBodies from {Node:c}");
                }
                Metrics.Eth62BlockBodiesReceived++;
                BlockBodiesMessage blockBodiesMessage = Deserialize <BlockBodiesMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(BlockBodiesMessage)}({blockBodiesMessage.Bodies.Length})");
                }
                Handle(blockBodiesMessage, size);
                break;

            case Eth62MessageCode.NewBlock:
                Interlocked.Increment(ref Counter);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Counter:D5} NewBlock from {Node:c}");
                }
                Metrics.Eth62NewBlockReceived++;
                NewBlockMessage newBlockMessage = Deserialize <NewBlockMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(NewBlockMessage)}({newBlockMessage.Block.Number})");
                }
                Handle(newBlockMessage);
                break;
            }
        }