Ejemplo n.º 1
0
        private async Task <bool> UpdateNode(StateOfNeo.Data.Models.Node node, string ip)
        {
            try
            {
                if (node.Latitude == null || node.Longitude == null)
                {
                    var response = await this.CheckIpCall(ip);

                    if (response.IsSuccessStatusCode)
                    {
                        var responseText = await response.Content.ReadAsStringAsync();

                        var responseOject = JsonConvert.DeserializeObject <LocationModel>(responseText);

                        node.FlagUrl   = responseOject.Flag;
                        node.Location  = responseOject.CountryName;
                        node.Latitude  = responseOject.Latitude;
                        node.Longitude = responseOject.Longitude;

                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                return(false);
            }

            return(false);
        }
Ejemplo n.º 2
0
        public async Task <int?> GetNodeHeight(StateOfNeo.Data.Models.Node node)
        {
            int?result     = null;
            var httpResult = await this.MakeRPCCall <RPCResponseBody <int> >(node);

            if (httpResult?.Result > 0)
            {
                result = httpResult.Result;
            }

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <string> GetNodeVersion(StateOfNeo.Data.Models.Node node)
        {
            if (string.IsNullOrEmpty(node.Version))
            {
                var result = await MakeRPCCall <RPCResponseBody <RPCResultGetVersion> >(node, "getversion");

                if (result?.Result != null)
                {
                    return(result.Result.Useragent);
                }
            }

            return(node.Version);
        }
Ejemplo n.º 4
0
        private async Task <T> MakeRPCCall <T>(StateOfNeo.Data.Models.Node node, string method = "getblockcount")
        {
            HttpResponseMessage response = null;
            bool succesfulCall           = false;
            var  successUrl = string.Empty;
            var  url        = string.Empty;

            var rpcRequest = new RPCRequestBody
            {
                Method = method
            };

            if (!string.IsNullOrEmpty(node.SuccessUrl))
            {
                response = await this.SendRPCCall(HttpMethod.Post, $"{node.SuccessUrl}", rpcRequest);

                if (response.IsSuccessStatusCode)
                {
                    successUrl    = node.SuccessUrl;
                    succesfulCall = true;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(successUrl))
                {
                    if (!string.IsNullOrEmpty(node.Protocol) &&
                        !string.IsNullOrEmpty(node.Url))
                    {
                        foreach (var address in node.NodeAddresses)
                        {
                            if (address.Port.HasValue)
                            {
                                url      = $"{node.Protocol}://{node.Url}:{address.Port}";
                                response = await SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                if (response.IsSuccessStatusCode)
                                {
                                    node.Type    = NodeAddressType.RPC;
                                    address.Type = NodeAddressType.RPC;
                                    successUrl   = url;
                                    break;
                                }
                            }
                        }

                        if (string.IsNullOrEmpty(successUrl))
                        {
                            foreach (var port in this.netSettings.GetPorts())
                            {
                                url      = $"{node.Protocol}://{node.Url}:{port}";
                                response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                if (response.IsSuccessStatusCode)
                                {
                                    node.Type  = NodeAddressType.RPC;
                                    successUrl = url;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (string.IsNullOrEmpty(successUrl))
                {
                    if (string.IsNullOrEmpty(node.Protocol) &&
                        !string.IsNullOrEmpty(node.Url))
                    {
                        if (node.Url.Contains(RPCCallConstants.PROTOCOL_TYPES_TESTS[0]) ||
                            node.Url.Contains(RPCCallConstants.PROTOCOL_TYPES_TESTS[1]))
                        {
                            foreach (var address in node.NodeAddresses)
                            {
                                if (address.Port.HasValue)
                                {
                                    url      = $"{node.Url}:{address.Port}";
                                    response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                    if (response.IsSuccessStatusCode)
                                    {
                                        node.Type     = NodeAddressType.RPC;
                                        address.Type  = NodeAddressType.RPC;
                                        successUrl    = url;
                                        succesfulCall = true;
                                        break;
                                    }
                                }
                            }
                            if (string.IsNullOrEmpty(successUrl))
                            {
                                foreach (var port in this.netSettings.GetPorts())
                                {
                                    url      = $"{node.Url}:{port}";
                                    response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                    if (response.IsSuccessStatusCode)
                                    {
                                        node.Type  = NodeAddressType.RPC;
                                        successUrl = url;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (var address in node.NodeAddresses)
                            {
                                if (address.Port.HasValue)
                                {
                                    foreach (var protocol in RPCCallConstants.PROTOCOL_TYPES_TESTS)
                                    {
                                        url      = $"{protocol}://{node.Url}:{address.Port}";
                                        response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                        if (response.IsSuccessStatusCode)
                                        {
                                            node.Type     = NodeAddressType.RPC;
                                            address.Type  = NodeAddressType.RPC;
                                            successUrl    = url;
                                            succesfulCall = true;
                                            break;
                                        }
                                    }
                                }

                                if (succesfulCall)
                                {
                                    break;
                                }
                            }

                            if (string.IsNullOrEmpty(successUrl))
                            {
                                foreach (var port in netSettings.GetPorts())
                                {
                                    foreach (var protocol in RPCCallConstants.PROTOCOL_TYPES_TESTS)
                                    {
                                        url      = $"{protocol}://{node.Url}:{port}";
                                        response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                        if (response.IsSuccessStatusCode)
                                        {
                                            node.Type     = NodeAddressType.RPC;
                                            successUrl    = url;
                                            succesfulCall = true;
                                            break;
                                        }
                                    }

                                    if (succesfulCall)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (string.IsNullOrEmpty(successUrl))
                {
                    foreach (var address in node.NodeAddresses)
                    {
                        if (address.Port.HasValue)
                        {
                            foreach (var protocol in RPCCallConstants.PROTOCOL_TYPES_TESTS)
                            {
                                url      = $"{protocol}://{address.Ip}:{address.Port}";
                                response = await this.SendRPCCall(HttpMethod.Post, $"{protocol}://{address.Ip}:{address.Port}", rpcRequest);

                                if (response.IsSuccessStatusCode)
                                {
                                    node.Type     = NodeAddressType.RPC;
                                    address.Type  = NodeAddressType.RPC;
                                    successUrl    = url;
                                    succesfulCall = true;
                                    break;
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(successUrl))
                        {
                            foreach (var port in this.netSettings.GetPorts())
                            {
                                foreach (var protocol in RPCCallConstants.PROTOCOL_TYPES_TESTS)
                                {
                                    url      = $"{protocol}://{address.Ip}:{port}";
                                    response = await this.SendRPCCall(HttpMethod.Post, url, rpcRequest);

                                    if (response.IsSuccessStatusCode)
                                    {
                                        node.Type     = NodeAddressType.RPC;
                                        address.Type  = NodeAddressType.RPC;
                                        successUrl    = url;
                                        succesfulCall = true;
                                        break;
                                    }
                                }

                                if (succesfulCall)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(successUrl))
            {
                node.SuccessUrl = successUrl;
                if (response != null && response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    var serializedResult = JsonConvert.DeserializeObject <T>(result);
                    return(serializedResult);
                }
            }

            return(default(T));
        }
Ejemplo n.º 5
0
        public async Task <RPCPeersResponse> GetNodePeers(StateOfNeo.Data.Models.Node node)
        {
            var result = await this.MakeRPCCall <RPCResponseBody <RPCPeersResponse> >(node, "getpeers");

            return(result?.Result);
        }