public NBXplorerClientProvider(IOptions <Config> config, IOptionsMonitor <ChainConfiguration> chainConfig, NRustLightningNetworkProvider networkProvider, IHttpClientFactory httpClientFactory, ILogger <NBXplorerClientProvider> logger) { _logger = logger; foreach (var n in networkProvider.GetAll()) { var chainConf = chainConfig.Get(n.CryptoCode); if (!(chainConf is null)) { var c = new ExplorerClient(n.NbXplorerNetwork, config.Value.NBXplorerUri); if (!string.IsNullOrEmpty(config.Value.NBXCookieFile)) { c.SetCookieAuth(config.Value.NBXCookieFile); } c.SetClient(httpClientFactory.CreateClient(nameof(NBXplorerClientProvider))); // check the connection by getting status. // TODO: Prepare HostedService for waiting NBXplorer and bitcoind gets ready? int sleepMs = 50; Exception e = null; int maxRetry = 6; for (int count = 0; count <= maxRetry; count++) { try { var _ = c.GetStatus(); e = null; } catch (Exception ex) { _logger.LogWarning($"Failed to connect to nbxplorer. retrying in {sleepMs} milliseconds..."); e = ex; Thread.Sleep(sleepMs); sleepMs *= 2; } } if (e != null) { _logger.LogCritical($"Failed to connect nbxplorer. check your settings."); throw e; } explorerClients.Add(n.CryptoCode, c); } } if (explorerClients.Count == 0) { throw new NRustLightningException("Found zero valid nbxplorer instance to connect"); } }
public NBXplorerClientProvider(IOptions <Config> config, IOptionsMonitor <ChainConfiguration> chainConfig, NRustLightningNetworkProvider networkProvider, IHttpClientFactory httpClientFactory, ILogger <NBXplorerClientProvider> logger) { _logger = logger; foreach (var n in networkProvider.GetAll()) { var chainConf = chainConfig.Get(n.CryptoCode); if (!(chainConf is null)) { var c = new ExplorerClient(n.NbXplorerNetwork, config.Value.NBXplorerUri); if (!string.IsNullOrEmpty(config.Value.NBXCookieFile)) { c.SetCookieAuth(config.Value.NBXCookieFile); } c.SetClient(httpClientFactory.CreateClient(nameof(NBXplorerClientProvider))); var timeout = new CancellationTokenSource(4000); try { c.WaitServerStarted(timeout.Token); c.GetStatus(); } catch (Exception) { _logger.LogError($"Failed to connect to nbxplorer! cryptoCode: {n.CryptoCode} url: {config.Value.NBXplorerUri}. cookiefile: {config.Value.NBXCookieFile}"); throw; } explorerClients.Add(n.CryptoCode, c); } } if (explorerClients.Count == 0) { throw new NRustLightningException("Found zero valid nbxplorer instance to connect"); } }