Skip to content

bitreq: TLS modules refactor#569

Open
ton-anywhere wants to merge 4 commits intorust-bitcoin:masterfrom
ton-anywhere:tls-feature-gates
Open

bitreq: TLS modules refactor#569
ton-anywhere wants to merge 4 commits intorust-bitcoin:masterfrom
ton-anywhere:tls-feature-gates

Conversation

@ton-anywhere
Copy link
Copy Markdown
Contributor

@ton-anywhere ton-anywhere commented Apr 23, 2026

Similar to this pull request, I encountered an error using the std and rustls features together.

This PR makes TLS backend selection explicit by using cfg gates with cargo features instead of relying on implicit dependency features.

While working on mixed TLS feature combinations, I ran into cases where backend selection became unclear so I had to decouple sync and async modes.

This PR:

  • splits TLS handling into rustls_stream and native_tls_stream
  • selects sync and async backends independently
  • gives rustls precedence over native-tls when both backends are enabled for the same sync or async mode

Error Example

image

@ton-anywhere
Copy link
Copy Markdown
Contributor Author

CI failing because it's treating _https-rustls as public standalone feature. If this PR is Ok, I can adjust it on the rust-bitcoin-maintainer-tools project.

Copy link
Copy Markdown
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Hmm, rather than an indirect feature can we gate on the actual features we need?

Comment thread bitreq/src/connection/rustls_stream.rs Outdated
use std::sync::OnceLock;

#[cfg(all(feature = "native-tls", not(feature = "rustls")))]
#[cfg(all(feature = "https-native-tls", not(feature = "_https-rustls")))]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we gate this whole module on rustls so this isn't so much of a mess, or at least move to mod rustls_stuff { ... }; pub use rustls_stuff::*?

@ton-anywhere
Copy link
Copy Markdown
Contributor Author

ton-anywhere commented Apr 24, 2026

@TheBlueMatt you mean using any(feature = "https-rustls", feature = "https-rustls-probe" instead of using _https-rustls, and than refactor it into separate modules for rustls and native-tls so we don't have as many cfg gates? I think it would work really well! If yes, I can work on this. 👍

@TheBlueMatt
Copy link
Copy Markdown
Member

Yea, that is what I was thinking.

@ton-anywhere ton-anywhere changed the title bitreq: TLS feature gates bitreq: TLS modules refactor Apr 25, 2026
@ton-anywhere
Copy link
Copy Markdown
Contributor Author

@TheBlueMatt updated

Comment thread bitreq/src/connection.rs
all(
feature = "https-native-tls",
not(any(feature = "https-rustls", feature = "https-rustls-probe"))
),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we intend to allow selection of a separate sync-tls-provider and async one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is your suggestion?
The alternatives I came up were either convoluted or do not pass in tests like this one:

cargo --locked build --no-default-features "--features=async-https-native-tls https async-https-rustls native-tls rustls rustls-webpki std tokio webpki-roots"

I was using this and it failed:

    #[cfg[all(
        any(feature = "async-https-native-tls", feature = "https-native-tls"),
        not(any(feature = "https-rustls", feature = "https-rustls-probe"))
    )]

Because it was opening all async gates(async backend present) but rustls priority was taking over.

The simplest solution I can think of without a large rework on all async gates(many) would be just ignore sync on priorities when an async feature is on. But that would change in behavior we intended(rustls priority over native-tls). Otherwise we could simply not compile, but that's again a change in behavior.

But, I bet most of the users won't mix with both configs at the same time. So if they set a async feature it will be set for both sync/async. The gate was necessary to work on edge cases like the one above..

Comment thread bitreq/src/connection.rs
feature = "https-native-tls",
not(any(feature = "https-rustls", feature = "https-rustls-probe"))
))]
use self::native_tls_stream as sync_tls_stream;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Its probably simpler to just have a sub-module in rustls_stream (maybe renaming it) and re-export. That way there's a single API to connection.rs and we arent conditional-compiling which module to use here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

really? using a single file to store code for both backends is a missing opportunity IMO. You mean like the enabled/disabled stuff I've initially tough for the tls configs on client.rs? That's doable, but I don't see what's are we winning here.

Ok(HttpStream::Secured(Box::new(tls), None))
}

// === ASYNC native-tls ===
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please don't commit claude's useless code section definition comments for a tiny file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@TheBlueMatt come on, do you even read what you write? I'm trying to help here but it's been really hard to work with you.

You commited the same type o comment in the master. this one is no different:
https://github.com/rust-bitcoin/corepc/blame/46c7bf4284748ee3da549d4463841e67db3bd00e/bitreq/src/connection/rustls_stream.rs#L69
image

I don't even use the fking Claude. I'm spending hours on all those pull requests. Come on. Let's be a bit more professionals here.

I agree 100% about removing all those useless comments, but this is the useless pattern you've already used there..

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants