public static async Task <RPCCapabilities> TestRPCAsync(NBXplorerNetwork networkInfo, RPCClient rpcClient, CancellationToken cancellation) { var network = networkInfo.NBitcoinNetwork; Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Testing RPC connection to " + rpcClient.Address.AbsoluteUri); RPCResponse blockchainInfo = null; try { int time = 0; retry: try { blockchainInfo = await rpcClient.SendCommandAsync("getblockchaininfo"); blockchainInfo.ThrowIfError(); } catch (RPCException ex) when(IsTransient(ex)) { Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Transient error '{ex.Message}', retrying soon..."); time++; await Task.Delay(Math.Min(1000 * time, 10000), cancellation); goto retry; } } catch (ConfigException) { throw; } catch (RPCException ex) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: Invalid response from RPC server " + ex.Message); throw new ConfigException(); } catch (Exception ex) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: Error connecting to RPC server " + ex.Message); throw new ConfigException(); } Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: RPC connection successful"); var capabilities = await rpcClient.ScanRPCCapabilitiesAsync(); if (capabilities.Version < networkInfo.MinRPCVersion) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: The minimum node version required is {networkInfo.MinRPCVersion} (detected: {capabilities.Version})"); throw new ConfigException(); } Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Full node version detected: {capabilities.Version}"); return(capabilities); }
public void TestRpcGetTransactionIsSuccessful() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateStratisPowNode(new BitcoinRegTest()).WithWallet().Start(); RPCClient rpc = node.CreateRPCClient(); uint256 blockHash = rpc.Generate(1)[0]; Block block = rpc.GetBlock(blockHash); RPCResponse walletTx = rpc.SendCommand(RPCOperations.gettransaction, block.Transactions[0].GetHash().ToString()); walletTx.ThrowIfError(); } }
public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true) { RPCResponse response = null; HttpWebRequest webRequest = response == null?CreateWebRequest() : null; if (response == null) { var writer = new StringWriter(); request.WriteJSON(writer); writer.Flush(); var json = writer.ToString(); var bytes = Encoding.UTF8.GetBytes(json); #if !(PORTABLE || NETCORE) webRequest.ContentLength = bytes.Length; #endif var dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false); await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); await dataStream.FlushAsync().ConfigureAwait(false); dataStream.Dispose(); } WebResponse webResponse = null; WebResponse errorResponse = null; try { webResponse = response == null ? await webRequest.GetResponseAsync().ConfigureAwait(false) : null; response = response ?? RPCResponse.Load(await ToMemoryStreamAsync(webResponse.GetResponseStream()).ConfigureAwait(false)); if (throwIfRPCError) { response.ThrowIfError(); } } catch (WebException ex) { if (ex.Response == null || ex.Response.ContentLength == 0 || !ex.Response.ContentType.Equals("application/json", StringComparison.Ordinal)) { throw; } errorResponse = ex.Response; response = RPCResponse.Load(await ToMemoryStreamAsync(errorResponse.GetResponseStream()).ConfigureAwait(false)); if (throwIfRPCError) { response.ThrowIfError(); } } finally { if (errorResponse != null) { errorResponse.Dispose(); errorResponse = null; } if (webResponse != null) { webResponse.Dispose(); webResponse = null; } } return(response); }
public static async Task <RPCCapabilities> TestRPCAsync(NBXplorerNetwork networkInfo, RPCClient rpcClient, CancellationToken cancellation) { var network = networkInfo.NBitcoinNetwork; Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Testing RPC connection to " + rpcClient.Address.AbsoluteUri); RPCResponse blockchainInfo = null; try { int time = 0; retry: try { blockchainInfo = await rpcClient.SendCommandAsync("getblockchaininfo"); blockchainInfo.ThrowIfError(); } catch (RPCException ex) when(IsTransient(ex)) { Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Transient error '{ex.Message}', retrying soon..."); time++; await Task.Delay(Math.Min(1000 * time, 10000), cancellation); goto retry; } } catch (ConfigException) { throw; } catch (RPCException ex) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: Invalid response from RPC server " + ex.Message); throw new ConfigException(); } catch (Exception ex) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: Error connecting to RPC server " + ex.Message); throw new ConfigException(); } Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: RPC connection successful"); if (blockchainInfo?.Result["chain"]?.Value <string>() is string rpcChain) { List <string> expectedNames = new List <string>(); expectedNames.Add(network.ChainName.ToString()); expectedNames.Add(network.ChainName.ToString().Replace("net", string.Empty, StringComparison.OrdinalIgnoreCase)); if (!expectedNames.Any(e => rpcChain.Equals(e, StringComparison.OrdinalIgnoreCase))) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: NBXplorer expected chain is '{network.ChainName}', but the RPC node is running '{rpcChain}'"); throw new ConfigException(); } } var capabilities = await rpcClient.ScanRPCCapabilitiesAsync(); if (capabilities.Version < networkInfo.MinRPCVersion) { Logs.Configuration.LogError($"{networkInfo.CryptoCode}: The minimum node version required is {networkInfo.MinRPCVersion} (detected: {capabilities.Version})"); throw new ConfigException(); } Logs.Configuration.LogInformation($"{networkInfo.CryptoCode}: Full node version detected: {capabilities.Version}"); return(capabilities); }