예제 #1
0
        private void Handle(StatusMessage status)
        {
            Metrics.StatusesReceived++;
            if (_statusReceived)
            {
                throw new SubprotocolException($"{nameof(StatusMessage)} has already been received in the past");
            }

            _statusReceived      = true;
            _remoteHeadBlockHash = status.BestHash;

            ReceivedProtocolInitMsg(status);

            SyncPeerProtocolInitializedEventArgs eventArgs = new SyncPeerProtocolInitializedEventArgs(this)
            {
                ChainId         = (long)status.ChainId,
                BestHash        = status.BestHash,
                GenesisHash     = status.GenesisHash,
                Protocol        = status.Protocol,
                ProtocolVersion = status.ProtocolVersion,
                TotalDifficulty = status.TotalDifficulty
            };

            HeadHash        = status.BestHash;
            TotalDifficulty = status.TotalDifficulty;
            ProtocolInitialized?.Invoke(this, eventArgs);
        }
예제 #2
0
        public void Handle(StatusMessage status)
        {
            Metrics.LesStatusesReceived++;

            // set defaults
            if (!status.AnnounceType.HasValue)
            {
                status.AnnounceType = 1;
            }

            if (_statusReceived)
            {
                throw new SubprotocolException($"{nameof(StatusMessage)} has already been received in the past");
            }

            _statusReceived = true;
            if (Logger.IsTrace)
            {
                Logger.Trace($"LES received status from {Session.Node:c} with" +
                             Environment.NewLine + $" prot version\t{status.ProtocolVersion}" +
                             Environment.NewLine + $" network ID\t{status.ChainId}," +
                             Environment.NewLine + $" genesis hash\t{status.GenesisHash}," +
                             Environment.NewLine + $" best hash\t{status.BestHash}," +
                             Environment.NewLine + $" head blockno\t{status.HeadBlockNo}," +
                             Environment.NewLine + $" difficulty\t{status.TotalDifficulty}" +
                             Environment.NewLine + $" announce type\t{status.AnnounceType}" +
                             Environment.NewLine + $" serve headers\t{status.ServeHeaders}" +
                             Environment.NewLine + $" serve chain since\t{status.ServeChainSince}" +
                             Environment.NewLine + $" serve recent chain\t{status.ServeRecentChain}" +
                             Environment.NewLine + $" serve state since\t{status.ServeStateSince}" +
                             Environment.NewLine + $" serve recent state\t{status.ServeRecentState}" +
                             Environment.NewLine + $" transaction relay\t{status.TxRelay}" +
                             Environment.NewLine + $" buffer limit\t{status.BufferLimit}" +
                             Environment.NewLine + $" max recharge\t{status.MaximumRechargeRate}");
            }
            // todo - log max request costs table

            _remoteHeadBlockHash = status.BestHash;

            ReceivedProtocolInitMsg(status);
            SyncPeerProtocolInitializedEventArgs eventArgs = new SyncPeerProtocolInitializedEventArgs(this)
            {
                ChainId         = (long)status.ChainId,
                BestHash        = status.BestHash,
                GenesisHash     = status.GenesisHash,
                Protocol        = status.Protocol,
                ProtocolVersion = status.ProtocolVersion,
                TotalDifficulty = status.TotalDifficulty
            };

            TotalDifficulty       = status.TotalDifficulty;
            RequestedAnnounceType = (LesAnnounceType)status.AnnounceType.Value;
            if (RequestedAnnounceType == LesAnnounceType.Signed)
            {
                throw new NotImplementedException("Signed announcements are not yet supported.");
            }

            ProtocolInitialized?.Invoke(this, eventArgs);
        }
예제 #3
0
        public bool DisconnectOnInvalid(string protocol, ISession session, ProtocolInitializedEventArgs eventArgs)
        {
            switch (protocol)
            {
            case Protocol.P2P:
                P2PProtocolInitializedEventArgs args = (P2PProtocolInitializedEventArgs)eventArgs;
                if (!ValidateP2PVersion(args.P2PVersion))
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Initiating disconnect with peer: {session.RemoteNodeId}, incorrect P2PVersion: {args.P2PVersion}");
                    }
                    _nodeStatsManager.ReportFailedValidation(session.Node, CompatibilityValidationType.P2PVersion);
                    Disconnect(session, DisconnectReason.IncompatibleP2PVersion, $"p2p.{args.P2PVersion}");
                    if (session.Node.IsStatic && _logger.IsWarn)
                    {
                        _logger.Warn($"Disconnected an invalid static node: {session.Node.Host}:{session.Node.Port}, reason: {DisconnectReason.IncompatibleP2PVersion}");
                    }
                    return(false);
                }

                if (!ValidateCapabilities(args.Capabilities))
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Initiating disconnect with peer: {session.RemoteNodeId}, no Eth62 capability, supported capabilities: [{string.Join(",", args.Capabilities.Select(x => $"{x.ProtocolCode}v{x.Version}"))}]");
                    }
                    _nodeStatsManager.ReportFailedValidation(session.Node, CompatibilityValidationType.Capabilities);
                    Disconnect(session, DisconnectReason.UselessPeer, "capabilities");
                    if (session.Node.IsStatic && _logger.IsWarn)
                    {
                        _logger.Warn($"Disconnected an invalid static node: {session.Node.Host}:{session.Node.Port}, reason: {DisconnectReason.UselessPeer} (capabilities)");
                    }
                    return(false);
                }

                break;

            case Protocol.Eth:
            case Protocol.Les:
                SyncPeerProtocolInitializedEventArgs syncPeerArgs = (SyncPeerProtocolInitializedEventArgs)eventArgs;
                if (!ValidateChainId(syncPeerArgs.ChainId))
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Initiating disconnect with peer: {session.RemoteNodeId}, different chainId: {ChainId.GetChainName((int) syncPeerArgs.ChainId)}, our chainId: {ChainId.GetChainName(_blockTree.ChainId)}");
                    }
                    _nodeStatsManager.ReportFailedValidation(session.Node, CompatibilityValidationType.ChainId);
                    Disconnect(session, DisconnectReason.UselessPeer, $"invalid chain id - {syncPeerArgs.ChainId}");
                    if (session.Node.IsStatic && _logger.IsWarn)
                    {
                        _logger.Warn($"Disconnected an invalid static node: {session.Node.Host}:{session.Node.Port}, reason: {DisconnectReason.UselessPeer} (invalid chain id - {syncPeerArgs.ChainId})");
                    }
                    return(false);
                }

                if (syncPeerArgs.GenesisHash != _blockTree.Genesis.Hash)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Initiating disconnect with peer: {session.RemoteNodeId}, different genesis hash: {syncPeerArgs.GenesisHash}, our: {_blockTree.Genesis.Hash}");
                    }
                    _nodeStatsManager.ReportFailedValidation(session.Node, CompatibilityValidationType.DifferentGenesis);
                    Disconnect(session, DisconnectReason.BreachOfProtocol, "invalid genesis");
                    if (session.Node.IsStatic && _logger.IsWarn)
                    {
                        _logger.Warn($"Disconnected an invalid static node: {session.Node.Host}:{session.Node.Port}, reason: {DisconnectReason.BreachOfProtocol} (invalid genesis)");
                    }
                    return(false);
                }

                break;
            }

            return(true);
        }