private void HandleNewAddress(RPCPeer address, StateOfNeoContext db)
        {
            var    newNode    = default(Node);
            string successUrl = null;
            var    ports      = this.netSettings.GetPorts();

            foreach (var portWithType in ports)
            {
                var url = portWithType.GetFullUrl(address.Address.ToMatchedIp());

                try
                {
                    var rpcResult = RpcCaller.MakeRPCCall <RPCResponseBody <int> >(url, "getblockcount")
                                    .GetAwaiter()
                                    .GetResult();


                    if (rpcResult?.Result > 0)
                    {
                        successUrl = url;
                        newNode    = this.CreateNodeOfAddress(address.Address,
                                                              portWithType.Type,
                                                              successUrl,
                                                              NodeAddressType.RPC);
                        break;
                    }
                }
                catch (Exception e)
                {
                    Log.Error($"Get blockcount parse error {e.Message}", e);
                    break;
                }
            }

            if (newNode == default(Node))
            {
                var httpTypes = new string[] { "https", "http" };
                foreach (var httpType in httpTypes)
                {
                    var url            = $"{httpType}://{address.Address.ToMatchedIp()}";
                    var heightResponse = HttpRequester.MakeRestCall <HeightResponseObject>($@"{url}/api/main_net/v1/get_height", HttpMethod.Get)
                                         .GetAwaiter()
                                         .GetResult();

                    if (heightResponse != null)
                    {
                        successUrl = url;
                        newNode    = this.CreateNodeOfAddress(address.Address,
                                                              httpType,
                                                              successUrl,
                                                              NodeAddressType.REST,
                                                              NodeCallsConstants.NeoScan);
                        break;
                    }

                    var versionResponse = HttpRequester.MakeRestCall <NeoNotificationVersionResponse>($@"{url}/v1/version", HttpMethod.Get)
                                          .GetAwaiter()
                                          .GetResult();

                    if (versionResponse != null)
                    {
                        successUrl = url;
                        newNode    = this.CreateNodeOfAddress(address.Address,
                                                              httpType,
                                                              successUrl,
                                                              NodeAddressType.REST,
                                                              NodeCallsConstants.NeoNotification);
                        break;
                    }
                }
            }

            if (newNode != null)
            {
                var newNodeAddress = new NodeAddress
                {
                    Ip   = address.Address.ToMatchedIp(),
                    Node = newNode
                };

                var peer = db.Peers.FirstOrDefault(x => x.Ip == address.Address.ToMatchedIp());

                var result = LocationCaller.UpdateNode(newNode, newNodeAddress.Ip).GetAwaiter().GetResult();

                newNode.NodeAddresses.Add(newNodeAddress);

                db.NodeAddresses.Add(newNodeAddress);
                db.Nodes.Add(newNode);
                peer.Node = newNode;
                db.SaveChanges();
            }
        }
Beispiel #2
0
        private NodeAudit NodeAudit(Node node, Neo.Network.P2P.Payloads.Block block)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int?height = null;

            if (node.Type == StateOfNeo.Common.NodeAddressType.REST)
            {
                if (node.Service == NodeCallsConstants.NeoScan)
                {
                    var heightResponse = HttpRequester.MakeRestCall <HeightResponseObject>($@"{node.Url}get_height", HttpMethod.Get)
                                         .GetAwaiter()
                                         .GetResult();

                    if (heightResponse != null)
                    {
                        height = heightResponse.Height;
                    }
                }
                else if (node.Service == NodeCallsConstants.NeoNotification)
                {
                    var versionResponse = HttpRequester.MakeRestCall <NeoNotificationVersionResponse>($@"{node.Url}version", HttpMethod.Get)
                                          .GetAwaiter()
                                          .GetResult();

                    if (versionResponse != null)
                    {
                        height = versionResponse.Height;
                    }
                }
            }
            else
            {
                height = this.nodeCaller.GetNodeHeight(node).GetAwaiter().GetResult();
            }

            if (height.HasValue)
            {
                node.Height = height.Value;
                this.UpdateNodeTimes(node, block);
                this.UpdateCache(LatencyCacheType, node.Id, stopwatch.ElapsedMilliseconds);

                if (node.Type == StateOfNeo.Common.NodeAddressType.RPC)
                {
                    var peers = this.nodeCaller.GetNodePeers(node).GetAwaiter().GetResult();
                    if (peers != null)
                    {
                        this.UpdateCache(PeersCacheType, node.Id, peers.Connected.Count());
                    }
                }

                if (node.LastAudit == null || node.LastAudit.Value.ToUnixDate().AddHours(1) < block.Timestamp.ToUnixDate())
                {
                    decimal?peers      = null;
                    var     peersValue = this.GetAuditValue(PeersCacheType, node.Id);
                    if (peersValue != null)
                    {
                        peers = (decimal)peersValue.Value;
                    }

                    var audit = new NodeAudit
                    {
                        NodeId    = node.Id,
                        Peers     = peers,
                        Latency   = (int)this.GetAuditValue(LatencyCacheType, node.Id),
                        CreatedOn = DateTime.UtcNow,
                        Timestamp = block.Timestamp
                    };

                    node.LastAudit = block.Timestamp;
                    return(audit);
                }
            }

            return(null);
        }