Exemple #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);
        }
        public override void Init()
        {
            ProtocolInitialized?.Invoke(this, new ProtocolInitializedEventArgs(this));

            _peerManager.AddPeer(this);
            _session.Disconnected += SessionDisconnected;
        }
        public override void Init()
        {
            ProtocolInitialized?.Invoke(this, new ProtocolInitializedEventArgs(this));

            _userOperationPool.AddPeer(this);
            _session.Disconnected += SessionDisconnected;
        }
        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);
        }
Exemple #5
0
        private void Handle(StatusMessage status)
        {
            Metrics.StatusesReceived++;

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

            _statusReceived = true;
            if (Logger.IsTrace)
            {
                Logger.Trace($"{Session.RemoteNodeId} ETH received status 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 + $" difficulty\t{status.TotalDifficulty}");
            }

            _remoteHeadBlockHash = status.BestHash;

            ReceivedProtocolInitMsg(status);

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

            if (status.BestHash == new Keccak("0x828f6e9967f75742364c7ab5efd6e64428e60ad38e218789aaf108fbd0232973"))
            {
                InitiateDisconnect(DisconnectReason.UselessPeer, "One of the Rinkeby nodes stuck at Constantinople transition");
            }

            TotalDifficultyOnSessionStart = status.TotalDifficulty;
            ProtocolInitialized?.Invoke(this, eventArgs);
        }
Exemple #6
0
        private void Handle(StatusMessage status)
        {
            if (_statusReceived)
            {
                throw new SubprotocolException($"{nameof(StatusMessage)} has already been received in the past");
            }

            _statusReceived = true;
            if (Logger.IsTrace)
            {
                Logger.Trace($"{P2PSession.RemoteNodeId} ETH received status 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 + $" difficulty\t{status.TotalDifficulty}");
            }

            _remoteHeadBlockHash       = status.BestHash;
            _remoteHeadBlockDifficulty = status.TotalDifficulty;

            //if (!_statusSent)
            //{
            //    throw new InvalidOperationException($"Received status from {P2PSession.RemoteNodeId} before calling Init");
            //}

            ReceivedProtocolInitMsg(status);

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

            ProtocolInitialized?.Invoke(this, eventArgs);
        }
        private void Handle(StatusMessage status)
        {
            Metrics.StatusesReceived++;

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

            _statusReceived = true;
            if (Logger.IsTrace)
            {
                Logger.Trace($"{Session.RemoteNodeId} ETH received status 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 + $" difficulty\t{status.TotalDifficulty}");
            }

            _remoteHeadBlockHash = status.BestHash;

            ReceivedProtocolInitMsg(status);

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

            TotalDifficultyOnSessionStart = status.TotalDifficulty;
            ProtocolInitialized?.Invoke(this, eventArgs);
        }
        private void HandleHello(HelloMessage hello)
        {
            ReportIn(hello);
            bool isInbound = !_sentHello;

            if (Logger.IsTrace)
            {
                Logger.Trace($"{Session} P2P received hello.");
            }

            if (!hello.NodeId.Equals(Session.RemoteNodeId))
            {
                if (Logger.IsDebug)
                {
                    Logger.Debug($"Inconsistent Node ID details - expected {Session.RemoteNodeId}, " +
                                 $"received hello with {hello.NodeId} " +
                                 $"on {(isInbound ? "IN connection" : "OUT connection")}");
                }
                // it does not really matter if there is mismatch - we do not use it anywhere
//                throw new NodeDetailsMismatchException();
            }

            RemoteClientId        = hello.ClientId;
            Session.Node.ClientId = hello.ClientId;

            if (Logger.IsTrace)
            {
                Logger.Trace(!_sentHello
                ? $"{Session.RemoteNodeId} P2P initiating inbound {hello.Protocol}.{hello.P2PVersion} " +
                             $"on {hello.ListenPort} ({hello.ClientId})"
                : $"{Session.RemoteNodeId} P2P initiating outbound {hello.Protocol}.{hello.P2PVersion} " +
                             $"on {hello.ListenPort} ({hello.ClientId})");
            }

            // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8.md
            // Clients implementing a newer version simply send a packet with higher version and possibly additional list elements.
            // * If such a packet is received by a node with lower version,
            //   it will blindly assume that the remote end is backwards-compatible and respond with the old handshake.
            // * If the packet is received by a node with equal version,
            //   new features of the protocol can be used.
            // * If the packet is received by a node with higher version,
            //   it can enable backwards-compatibility logic or drop the connection.

            ProtocolVersion = hello.P2PVersion;

            List <Capability> capabilities = hello.Capabilities;

            AvailableCapabilities = new List <Capability>(capabilities);
            foreach (Capability theirCapability in capabilities)
            {
                if (SupportedCapabilities.Contains(theirCapability))
                {
                    if (Logger.IsTrace)
                    {
                        Logger.Trace($"{Session.RemoteNodeId} Agreed on {theirCapability.ProtocolCode} v{theirCapability.Version}");
                    }
                    AgreedCapabilities.Add(theirCapability);
                }
                else
                {
                    if (Logger.IsTrace)
                    {
                        Logger.Trace($"{Session.RemoteNodeId} Capability not supported " +
                                     $"{theirCapability.ProtocolCode} v{theirCapability.Version}");
                    }
                }
            }

            if (!capabilities.Any(c => SupportedCapabilities.Contains(c)))
            {
                Session.InitiateDisconnect(
                    DisconnectReason.UselessPeer,
                    $"capabilities: {string.Join(", ", capabilities)}");
            }

            ReceivedProtocolInitMsg(hello);

            P2PProtocolInitializedEventArgs eventArgs = new P2PProtocolInitializedEventArgs(this)
            {
                P2PVersion   = ProtocolVersion,
                ClientId     = RemoteClientId,
                Capabilities = capabilities,
                ListenPort   = hello.ListenPort
            };

            ProtocolInitialized?.Invoke(this, eventArgs);
        }
Exemple #9
0
        public void HandleHello(HelloMessage hello)
        {
            Logger.Info($"{P2PSession.RemoteNodeId} P2P received hello.");
            if (!hello.NodeId.Equals(P2PSession.RemoteNodeId))
            {
                throw new NodeDetailsMismatchException();
            }

            //P2PSession.RemoteNodeId = hello.NodeId;
            //P2PSession.RemotePort = hello.ListenPort;
            RemoteClientId = hello.ClientId;

            Logger.Info(!_sentHello
                ? $"{P2PSession.RemoteNodeId} P2P initiating inbound {hello.Protocol} v{hello.P2PVersion} protocolHandler on {hello.ListenPort} ({hello.ClientId})"
                : $"{P2PSession.RemoteNodeId} P2P initiating outbound {hello.Protocol} v{hello.P2PVersion} protocolHandler on {hello.ListenPort} ({hello.ClientId})");

            // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8.md
            // Clients implementing a newer version simply send a packet with higher version and possibly additional list elements.
            // * If such a packet is received by a node with lower version, it will blindly assume that the remote end is backwards-compatible and respond with the old handshake.
            // * If the packet is received by a node with equal version, new features of the protocol can be used.
            // * If the packet is received by a node with higher version, it can enable backwards-compatibility logic or drop the connection.

            //Moved that validation to PeerManager
            //if (hello.P2PVersion < 4 || hello.P2PVersion > 5)
            //{
            //    //triggers disconnect on the session, which will trigger it on all protocol handlers
            //    P2PSession.InitiateDisconnectAsync(DisconnectReason.IncompatibleP2PVersion);
            //    //Disconnect(DisconnectReason.IncompatibleP2PVersion);
            //    return;
            //}

            ProtocolVersion = hello.P2PVersion;

            //TODO Check required capabilities and disconnect if not supported

            foreach (Capability remotePeerCapability in hello.Capabilities)
            {
                if (SupportedCapabilities.Contains(remotePeerCapability))
                {
                    Logger.Info($"{P2PSession.RemoteNodeId} Agreed on {remotePeerCapability.ProtocolCode} v{remotePeerCapability.Version}");
                    AgreedCapabilities.Add(remotePeerCapability);
                }
                else
                {
                    Logger.Info($"{P2PSession.RemoteNodeId} Capability not supported {remotePeerCapability.ProtocolCode} v{remotePeerCapability.Version}");
                }
            }

            //if (!_sentHello)
            //{
            //    throw new InvalidOperationException($"Handling {nameof(HelloMessage)} from peer before sending our own");
            //}
            _isInitialized = true;
            ReceivedProtocolInitMsg(hello);

            var eventArgs = new P2PProtocolInitializedEventArgs(this)
            {
                P2PVersion   = hello.P2PVersion,
                ClientId     = hello.ClientId,
                Capabilities = hello.Capabilities
            };

            ProtocolInitialized?.Invoke(this, eventArgs);
        }
        public void HandleHello(HelloMessage hello)
        {
            bool isInbound = !_sentHello;

            if (Logger.IsTrace)
            {
                Logger.Trace($"{P2PSession.RemoteNodeId} P2P received hello.");
            }


            if (!hello.NodeId.Equals(P2PSession.RemoteNodeId))
            {
                if (Logger.IsDebug)
                {
                    Logger.Debug($"Inconsistent Node ID details - expected {P2PSession.RemoteNodeId}, received hello with {hello.NodeId} on " + (isInbound ? "IN connection" : "OUT connection"));
                }
                // it does not really matter if there is mismatch - we do not use it anywhere
//                throw new NodeDetailsMismatchException();
            }

            RemoteClientId = hello.ClientId;

            Logger.Trace(!_sentHello
                ? $"{P2PSession.RemoteNodeId} P2P initiating inbound {hello.Protocol} v{hello.P2PVersion} on {hello.ListenPort} ({hello.ClientId})"
                : $"{P2PSession.RemoteNodeId} P2P initiating outbound {hello.Protocol} v{hello.P2PVersion} on {hello.ListenPort} ({hello.ClientId})");

            // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8.md
            // Clients implementing a newer version simply send a packet with higher version and possibly additional list elements.
            // * If such a packet is received by a node with lower version, it will blindly assume that the remote end is backwards-compatible and respond with the old handshake.
            // * If the packet is received by a node with equal version, new features of the protocol can be used.
            // * If the packet is received by a node with higher version, it can enable backwards-compatibility logic or drop the connection.

            ProtocolVersion = hello.P2PVersion;

            var capabilities = hello.Capabilities;

            foreach (Capability remotePeerCapability in capabilities)
            {
                if (SupportedCapabilities.Contains(remotePeerCapability))
                {
                    if (Logger.IsTrace)
                    {
                        Logger.Trace($"{P2PSession.RemoteNodeId} Agreed on {remotePeerCapability.ProtocolCode} v{remotePeerCapability.Version}");
                    }
                    AgreedCapabilities.Add(remotePeerCapability);
                }
                else
                {
                    if (Logger.IsTrace)
                    {
                        Logger.Trace($"{P2PSession.RemoteNodeId} Capability not supported {remotePeerCapability.ProtocolCode} v{remotePeerCapability.Version}");
                    }
                }
            }

            _isInitialized = true;

            if (!capabilities.Any(x => x.ProtocolCode == Protocol.Eth && (x.Version == 62 || x.Version == 63)))
            {
                Disconnect(DisconnectReason.UselessPeer);
            }

            ReceivedProtocolInitMsg(hello);

            var eventArgs = new P2PProtocolInitializedEventArgs(this)
            {
                P2PVersion   = ProtocolVersion,
                ClientId     = RemoteClientId,
                Capabilities = capabilities,
                ListenPort   = hello.ListenPort
            };

            ProtocolInitialized?.Invoke(this, eventArgs);
        }
 public override void Init()
 {
     ProtocolInitialized?.Invoke(this, new ProtocolInitializedEventArgs(this));
 }
 public void TriggerProtocolInitialized(ProtocolInitializedEventArgs eventArgs)
 {
     ProtocolInitialized?.Invoke(this, eventArgs);
     //var task = Task.Delay(1000);
     //task.Wait();
 }
Exemple #13
0
 public void TriggerProtocolInitialized(ProtocolInitializedEventArgs eventArgs)
 {
     ProtocolInitialized?.Invoke(this, eventArgs);
 }
 public override void Init()
 {
     ProtocolInitialized?.Invoke(this, new ProtocolInitializedEventArgs(this));
     // GetBlockWitnessHashes(Keccak.Zero, CancellationToken.None);
 }
        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);
            LesProtocolInitializedEventArgs eventArgs = new LesProtocolInitializedEventArgs(this)
            {
                Protocol            = status.Protocol,
                ProtocolVersion     = status.ProtocolVersion,
                ChainId             = (long)status.ChainId, // todo should these really be different data types?
                TotalDifficulty     = status.TotalDifficulty,
                BestHash            = status.BestHash,
                HeadBlockNo         = status.HeadBlockNo,
                GenesisHash         = status.GenesisHash,
                AnnounceType        = status.AnnounceType.Value,
                ServeHeaders        = status.ServeHeaders,
                ServeChainSince     = status.ServeChainSince,
                ServeRecentChain    = status.ServeRecentChain,
                ServeStateSince     = status.ServeStateSince,
                ServeRecentState    = status.ServeRecentState,
                TxRelay             = status.TxRelay,
                BufferLimit         = status.BufferLimit,
                MaximumRechargeRate = status.MaximumRechargeRate,
                MaximumRequestCosts = status.MaximumRequestCosts
            };

            if (status.BestHash == new Keccak("0x828f6e9967f75742364c7ab5efd6e64428e60ad38e218789aaf108fbd0232973"))
            {
                InitiateDisconnect(DisconnectReason.UselessPeer, "One of the Rinkeby nodes stuck at Constantinople transition");
            }

            TotalDifficulty       = status.TotalDifficulty;
            RequestedAnnounceType = status.AnnounceType.Value;

            ProtocolInitialized?.Invoke(this, eventArgs);
        }