예제 #1
0
        public async Task <bool> TestAccess(bool isOnion)
        {
            var data    = GetData();
            var summary = _nbXplorerSummaryProvider.GetSummary(data.CryptoCode);

            if (summary.State != NBXplorerState.Ready)
            {
                return(false);
            }

            try
            {
                using (var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT))
                {
                    var client = ConstructClient(data);
                    LightningNodeInformation info = null;
                    try
                    {
                        info = await client.GetInfo(cts.Token);
                    }
                    catch (OperationCanceledException) when(cts.IsCancellationRequested)
                    {
                        throw new Exception($"The lightning node did not reply in a timely manner");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Error while connecting to the API ({ex.Message})");
                    }

                    var nodeInfo = info.NodeInfoList.FirstOrDefault(i => i.IsTor == isOnion) ??
                                   info.NodeInfoList.FirstOrDefault();
                    if (nodeInfo == null)
                    {
                        throw new Exception($"No lightning node public address has been configured");
                    }

                    var blocksGap = summary.Status.ChainHeight - info.BlockHeight;
                    if (blocksGap > 10)
                    {
                        throw new Exception($"The lightning node is not synched ({blocksGap} blocks left)");
                    }

                    if (!EndPointParser.TryParse(nodeInfo.Host, nodeInfo.Port, out var endpoint))
                    {
                        throw new Exception($"Could not parse the endpoint {nodeInfo.Host}");
                    }

                    using (await _socketFactory.ConnectAsync(endpoint, cts.Token))
                    {
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public async Task TestConnection(NodeInfo nodeInfo, CancellationToken cancellation)
        {
            try
            {
                if (!Utils.TryParseEndpoint(nodeInfo.Host, nodeInfo.Port, out var endpoint))
                {
                    throw new PaymentMethodUnavailableException($"Could not parse the endpoint {nodeInfo.Host}");
                }

                using var tcp = await _socketFactory.ConnectAsync(endpoint, cancellation);
            }
            catch (Exception ex)
            {
                throw new PaymentMethodUnavailableException($"Error while connecting to the lightning node via {nodeInfo.Host}:{nodeInfo.Port} ({ex.Message})");
            }
        }