Skip to content

cli: Replace libevent usage with simple http client#34342

Draft
fjahr wants to merge 3 commits intobitcoin:masterfrom
fjahr:2026-01-cli-noev
Draft

cli: Replace libevent usage with simple http client#34342
fjahr wants to merge 3 commits intobitcoin:masterfrom
fjahr:2026-01-cli-noev

Conversation

@fjahr
Copy link
Contributor

@fjahr fjahr commented Jan 19, 2026

Part of the effort to remove the libevent dependency altogether, see #31194

This takes the HTTPHeaders class from #32061 and puts it into common/http.h|cpp. While it would not be strictly required for that PR to go in first, it might be better to review it first or simultaneously.

Otherwise the change itself replaces the libevent-based HTTP client with a simple synchronous implementation which uses the Sock class directly.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 19, 2026

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34342.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK pinheadmz, bradleystachurski, w0xlt

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32297 (bitcoin-cli: Add -ipcconnect option by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/21139253857/job/60789761512
LLM reason (✨ experimental): Clang-Tidy failure: modernize-use-starts-ends-with flags substr usage in bitcoin-cli.cpp, treated as error and causing CI to fail.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

This will be shared between the http server and the bitcoin-cli http client code.
@fjahr fjahr marked this pull request as ready for review January 23, 2026 23:37
@pinheadmz
Copy link
Member

concept ACK
Nice moves, yo! 🔥

Copy link

@bradleystachurski bradleystachurski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK (and supportive of the broader libevent removal effort)

Reviewed efd74a8

Found a minor behavioral regression: connection failures (e.g. unresolvable host, non-listening port) lose the help text shown on master.

Master:

error: timeout on transient error: Could not connect to the server 127.0.0.1:19999
Make sure the bitcoind server is running and that you are connecting to the correct RPC port.
Use "bitcoin-cli -help" for more info.

Branch:

error: timeout on transient error: Could not connect to the server 127.0.0.1:19999

Catching instead of re-throwing preserves the help text:

diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index 8414ad0317..3cd6e4b29c 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -1258,7 +1258,7 @@ static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, co
     try {
         response = client.Post(endpoint, headers, strRequest);
     } catch (const CConnectionFailed&) {
-        throw;
+        response.status = 0;
     }

     if (response.status == 0) {

Verified this preserves the help text and passes interface_bitcoin_cli.py.

Replace libevent-based HTTP client with a simple synchronous implementation using the Sock class directly.
@fjahr
Copy link
Contributor Author

fjahr commented Feb 8, 2026

@bradleystachurski

Found a minor behavioral regression: connection failures (e.g. unresolvable host, non-listening port) lose the help text shown on master.

You were right, good catch! Your suggested fix looks good to me as well, I have just applied that change.

std::string Stringify() const;

private:
std::unordered_map<std::string, std::string, util::AsciiCaseInsensitiveHash, util::AsciiCaseInsensitiveKeyEqual> m_map;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is making use of code being removed in #32061 (c24f5d3).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a few things we learned in #34158 as well that can be applied. This PR (bitcoin-cli) can maybe be draft until torcontol is ironed out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already draft but thanks for the hints, I will address these once we have made some more progress on torcontol and the server.

@fanquake fanquake marked this pull request as draft February 25, 2026 15:26
@w0xlt
Copy link
Contributor

w0xlt commented Mar 10, 2026

Concept ACK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants