| 798 | }; |
| 799 | |
| 800 | static std::optional<UniValue> CallIPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const std::string& endpoint, const std::string& username) |
| 801 | { |
| 802 | auto ipcconnect{gArgs.GetArg("-ipcconnect", "auto")}; |
| 803 | if (ipcconnect == "0") return {}; // Do not attempt IPC if -ipcconnect is disabled. |
| 804 | if (gArgs.IsArgSet("-rpcconnect") && !gArgs.IsArgNegated("-rpcconnect")) { |
| 805 | if (ipcconnect == "auto") return {}; // Use HTTP if -ipcconnect=auto is set and -rpcconnect is enabled. |
| 806 | throw std::runtime_error("-rpcconnect and -ipcconnect options cannot both be enabled"); |
| 807 | } |
| 808 | |
| 809 | std::unique_ptr<interfaces::Init> local_init{interfaces::MakeBasicInit("bitcoin-cli")}; |
| 810 | if (!local_init || !local_init->ipc()) { |
| 811 | if (ipcconnect == "auto") return {}; // Use HTTP if -ipcconnect=auto is set and there is no IPC support. |
| 812 | throw std::runtime_error("bitcoin-cli was not built with IPC support"); |
| 813 | } |
| 814 | |
| 815 | std::unique_ptr<interfaces::Init> node_init; |
| 816 | try { |
| 817 | node_init = local_init->ipc()->connectAddress(ipcconnect); |
| 818 | if (!node_init) return {}; // Fall back to HTTP if -ipcconnect=auto connect failed. |
| 819 | } catch (const std::exception& e) { |
| 820 | // Catch connect error if -ipcconnect=unix was specified |
| 821 | throw CConnectionFailed{strprintf("%s\n\n" |
| 822 | "Probably bitcoin-node is not running or not listening on a unix socket. Can be started with:\n\n" |
| 823 | " bitcoin-node -chain=%s -ipcbind=unix", e.what(), gArgs.GetChainTypeString())}; |
| 824 | } |
| 825 | |
| 826 | std::unique_ptr<interfaces::Rpc> rpc{node_init->makeRpc()}; |
| 827 | assert(rpc); |
| 828 | UniValue request{rh->PrepareRequest(strMethod, args)}; |
| 829 | UniValue reply{rpc->executeRpc(std::move(request), endpoint, username)}; |
| 830 | return rh->ProcessReply(reply); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Simple synchronous HTTP client using Sock class. |
no test coverage detected