Ejemplo n.º 1
0
        public override void HandleMessage(ZeroPacket message)
        {
            int packetType = message.PacketType;

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

            int size = message.Content.ReadableBytes;

            if (Logger.IsTrace)
            {
                Logger.Trace(
                    $"{Counter:D5} {Eth62MessageCode.GetDescription(packetType)} from {Node:c}");
            }

            switch (packetType)
            {
            case Eth62MessageCode.Status:
                StatusMessage statusMsg = Deserialize <StatusMessage>(message.Content);
                ReportIn(statusMsg);
                Handle(statusMsg);
                break;

            case Eth62MessageCode.NewBlockHashes:
                NewBlockHashesMessage newBlockHashesMessage = Deserialize <NewBlockHashesMessage>(message.Content);
                ReportIn(newBlockHashesMessage);
                Handle(newBlockHashesMessage);
                break;

            case Eth62MessageCode.Transactions:
                Metrics.Eth62TransactionsReceived++;
                if (_floodController.IsAllowed())
                {
                    TransactionsMessage txMsg = Deserialize <TransactionsMessage>(message.Content);
                    ReportIn(txMsg);
                    Handle(txMsg);
                }
                break;

            case Eth62MessageCode.GetBlockHeaders:
                GetBlockHeadersMessage getBlockHeadersMessage
                    = Deserialize <GetBlockHeadersMessage>(message.Content);
                ReportIn(getBlockHeadersMessage);
                Handle(getBlockHeadersMessage);
                break;

            case Eth62MessageCode.BlockHeaders:
                BlockHeadersMessage headersMsg = Deserialize <BlockHeadersMessage>(message.Content);
                ReportIn(headersMsg);
                Handle(headersMsg, size);
                break;

            case Eth62MessageCode.GetBlockBodies:
                GetBlockBodiesMessage getBodiesMsg = Deserialize <GetBlockBodiesMessage>(message.Content);
                ReportIn(getBodiesMsg);
                Handle(getBodiesMsg);
                break;

            case Eth62MessageCode.BlockBodies:
                HandleBodies(message.Content, size);
                break;

            case Eth62MessageCode.NewBlock:
                NewBlockMessage newBlockMsg = Deserialize <NewBlockMessage>(message.Content);
                ReportIn(newBlockMsg);
                Handle(newBlockMsg);
                break;
            }
        }
Ejemplo n.º 2
0
        public override void HandleMessage(ZeroPacket message)
        {
            int packetType = message.PacketType;

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

            int size = message.Content.ReadableBytes;

            if (Logger.IsTrace)
            {
                Logger.Trace($"{Counter:D5} {Eth62MessageCode.GetDescription(packetType)} from {Node:c}");
            }

            switch (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));
                }
                Handle(Deserialize <NewBlockHashesMessage>(message.Content));
                break;

            case Eth62MessageCode.Transactions:
                TransactionsMessage transactionsMessage = Deserialize <TransactionsMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(TransactionsMessage)}({transactionsMessage.Transactions.Count})");
                }
                Handle(transactionsMessage);
                break;

            case Eth62MessageCode.GetBlockHeaders:
                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:
                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:
                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:
                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:
                NewBlockMessage newBlockMessage = Deserialize <NewBlockMessage>(message.Content);
                if (NetworkDiagTracer.IsEnabled)
                {
                    NetworkDiagTracer.ReportIncomingMessage(Session.Node.Host, Name, $"{nameof(NewBlockMessage)}({newBlockMessage.Block.Number})");
                }
                Handle(newBlockMessage);
                break;
            }
        }