mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:29:40 +00:00
WIP: i2p: make a time gap between creating transient sessions and using them #32065
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!32065
Loading…
Reference in a new issue
No description provided.
Delete branch "i2p_early_create_session"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Connecting to an I2P peer consists of creating a session (the
SESSION CREATEcommand) and then connecting to the peer using thatsession (
STREAM CONNECT ID=session_id ...).This change is only relevant for transient sessions because when a
persistent session is used it is created once and used for all
connections.
Before this change Bitcoin Core would create the session and use it in
quick succession. That is, the
SESSION CREATEcommand would beimmediately followed by
STREAM CONNECT. This could ease networkactivity monitoring by an adversary.
To mitigate that, this change creates sessions upfront without an
immediate demand for new sessions and later uses them. This creates a
time gap between
SESSION CREATEandSTREAM CONNECT. Note that thereis always some demand for new I2P connections due to disconnects.
Summary of the changes in the code:
Create the session from the
Sessionconstructor (sendSESSION CREATEto the I2P SAM proxy). This constructor was only called when transient
sessions were needed and was immediately followed by
Connect()whichwould have created the session. So this is a noop change if viewed
in isolation.
Every time we try to connect to any peer (not just I2P) create a new
I2P session, up to a limit (currently 10). This way a bunch of
sessions are created (
SESSION CREATE) at times that are decoupledfrom the time when sessions will be used (
STREAM CONNECT).Tweak the code in
CConnman::ConnectNode()to avoid creating sessionobjects while holding
m_unused_i2p_sessions_mutexbecause nowcreating
i2p::sam::Sessioninvolves network activity and could takea few seconds.
Before (note the timestamps):
2025-03-13T09:23:40Z [opencon] [i2p:info] Transient SAM session cd7ea49a7e created
2025-03-13T09:23:43Z [opencon] [i2p] -> STREAM CONNECT ID=cd7ea49a7e DESTINATION=...
2025-03-13T09:23:51Z [opencon] [i2p:info] Transient SAM session cb0956207e created
2025-03-13T09:23:52Z [opencon] [i2p] -> STREAM CONNECT ID=cb0956207e DESTINATION=...
2025-03-13T09:24:04Z [opencon] [i2p:info] Transient SAM session 35ad67f343 created
2025-03-13T09:24:04Z [opencon] [i2p] -> STREAM CONNECT ID=35ad67f343 DESTINATION=...
After:
2025-03-13T17:08:58Z [opencon] [i2p:info] Transient SAM session 16098cd4ea created
2025-03-13T17:09:34Z [opencon] [i2p] -> STREAM CONNECT ID=16098cd4ea DESTINATION=...
2025-03-13T17:09:38Z [opencon] [i2p:info] Transient SAM session 5d209bf1bc created
2025-03-13T17:09:59Z [opencon] [i2p] -> STREAM CONNECT ID=5d209bf1bc DESTINATION=...
2025-03-13T17:09:50Z [opencon] [i2p:info] Transient SAM session e78aa6d2e7 created
2025-03-13T17:12:09Z [opencon] [i2p] -> STREAM CONNECT ID=e78aa6d2e7 DESTINATION=...
This was suggested by @eyedeekay some time ago (in 2023 actually) and has been lingering in my TODO since then 👀. A further suggestion is to also add a time gap between the activity cease and the closing of the session. For this, when
CNodeis destroyedCNode::m_i2p_sam_sessionwould have to be moved elsewhere and destroyed at a later time. cc @zzzi2pThe following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
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:
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.
Thanks for CC'ing me. I also asked eyedeekay and orignal from i2pd to take a look.
10 standby sessions is a lot, except maybe at startup. If your I2P connections limit is 10, then you're doubling your network resource usage. 2 or 3 is probably enough, depending on how fast your connections churn and how long is long enough for a unused session to be up to give you the anonymity you're looking for.
A couple of alternatives:
or
However, I'm also concerned about starting a whole bunch of sessions at once, especially at startup, and if the user started his router when he started bitcoin. If this change will double or triple or more the session-creation rate at startup, that's probably too fast. The Java router has some tunnel build rate-limiting that may kick in. I won't attempt any pseudocode for it, but some rate limiting in AddI2PSessionsIfNeeded() or some other special case for the first few minutes after startup is probably desirable.
Concept ACK.
Concept ACK
I agree with zzz, the concept ACK is fine but I'm very concerned about having the session bank be 10 sessions long. I think the best method of managing the session bank sort of depends on the rate at which you'll need them.
If you're creating sessions fast enough to need a bank of 10 sessions, then I think at some point you have no choice but to keep the sessions alive longer and re-use them more. On the other hand, if you aren't creating them fast enough to need a bank of 10 sessions, then keeping a bank of 10 of them is pretty wasteful. How you solve it is going to depend on the actual rates you expect people to need sessions. If the need for sessions is too unpredictable, then maybe the bank size limit needs to change size based on need from a very small number to a larger number and back.
I'm going to take a closer look at the code here and try to make some more concrete suggestions.
A few notes:
Bitcoin Core would try to maintain up to 12 outbound connections to peers from all networks that are reachable (IPv4, IPv6, Tor, I2P, CJDNS). From these usually just a few are I2P. So, it is reasonable to assume that from 2,3,4 to 12 I2P connections will be needed. But the case with 12 is when I2P is the only reachable network which is not advisable because of the increased Sybil likelihood.
This PR is not going to create a storm of I2P sessions creation at startup because new session creation is not being done in a tight loop, deliberately. See the timings in the logs below.
The lifetime of an I2P connection varies a lot. I would like to avoid adding assumptions about that. How often I2P connections are needed depends on the lifetime + whether a randomly selected address from the address database for the new connection will be I2P.
Session reuse would degrade privacy because Bitcoin Core would connect to >1 peer from the same I2P source address. This is when transient sessions are used, affected by this PR. Note that there is another less-private mode if Bitcoin Core is listening and accepting new I2P connections. Then a single persistent I2P session is used - it accepts new connections to its permanent address and makes all outgoing connections from that address (= a single I2P session), this PR is not changing that.
The code before this PR would keep a session for later if it cannot use it for connecting to the given I2P peer (
NAMING LOOKUPreturns an error). I chose10forMAX_UNUSED_I2P_SESSIONS_SIZEarbitrarily to avoid a broken runaway code from filling all of the Bitcoin Core's memory with unused sessions. I think in practice the number of stored sessions is always0or1inmaster, before this PR.Below is some data for when sessions are created, used and how long do they live. From startup to about 10 minutes after that, with this PR. It is just an example from my node. I guess timings will vary a lot between runs and in different environments.
t0is the startup time.MAX_UNUSED_I2P_SESSIONS_SIZE=10:MAX_UNUSED_I2P_SESSIONS_SIZE=4:Given the above, I changed
MAX_UNUSED_I2P_SESSIONS_SIZEfrom10to4in42f98c9dfa...a22a3b3c4c.Two thoughts:
As far as I understand it (https://geti2p.net/en/about/performance/future), i2p tunnels last for 10 minutes by default. If we don't use these extra sessions before they expire, their creation would be a waste of ressources. I would image this to be particularly relevant in a scenario where i2p is used together with other networks. When we have no need for new regular outbounds because we are full and none of our peers disconnect, we make feeler connections every 2 minutes and extra block relay conns every 5 minutes - that's like 7 new connections every 10 minutes that would be divided over all reachable networks. Would be interesting to run with i2p and clearnet, and see how many of these tunnels expire before they are used.
When using
-onlynet=i2p, this may not have much of an effect right after startup -m_unused_i2p_sessionswill only grow if a connection attempt fails, for successful connections the newly formed connections will be used right away. Maybe it could make a sense to fill them_unused_i2p_sessionsbuffer with 4 connections right after startup?I wonder what happens after the 10 minutes? For example, if a client does
SESSION CREATEto acquire a session id and then waits for 12 minutes and doesSTREAM CONNECTwith that session id. Is a new tunnel established by the I2P router immediately and transparently to the client at the 10th minute, or when the session is actually used at the 12th minute?Thinking about this more, given that opening of new connections is done serially, one after another (not in parallel), and occasionally (except during startup) maybe it would be best to pre-create just one session?
Tunnels get built when the SAM session is opened, and are automatically rebuilt when they expire, the router does that for you as long as the SAM session stays open. A session is just a pool of inbound and outbound tunnels. I assume that's what you are trying to do here, have the session open and ready before it's needed.
a22a3b3c4c...48a636cdde: rebase due to conflicts and reduce the pre-created sessions to 1, given the above. Some logs from running this:🚧 At least one of the CI tasks failed.
Debug: https://github.com/bitcoin/bitcoin/runs/39105567001
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.
48a636cdde...859c259092: fix CI859c259092...632eb1a3c9: rebase due to conflictsSomehow I hadn't seen this PR yet.
If I understand correctly, this change is only for if a node is configured with
-i2pacceptincoming=0to not accept inbound I2P connections?Yes. That is because if it is accepting incoming connections (has a permanent listening I2P address) then there is just one I2P session which is associated with that address and that session is used for all incoming and outgoing connections.
632eb1a3c9...a42449cb30: rebase due to conflictsa42449cb30...fc1ba52a4d: rebase due to conflictsfc1ba52a4d...539d8b474e: rebase due to conflicts539d8b474edb2232c0a40001acbf3f29966ab430...6da9cf56a90e65491b1c891257383b6d09459068: rebase due to conflicts and put the mechanical rename into its own commit so that it does not clutter the other changes.🚧 At least one of the CI tasks failed.
Task
test max 6 ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/21281979901/job/61253918040LLM reason (✨ experimental): C++ compilation failed in test_util due to missing std::queue include (and related guarded_by attribute warnings), causing the CI to abort.
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.
6da9cf56a90e65491b1c891257383b6d09459068...8df10cfdd30115c18284ac83ddf3b619c84a2981: fix includes intest/util/net.h- it usesstd::queuebut did not include<queue>. It was working before becausenet.hused to include<queue>andtest/util/net.hincludesnet.h. But this PR removed the inclusion of<queue>fromnet.h.8df10cfdd30115c18284ac83ddf3b619c84a2981...1808139f52f1f0578352acce9d4761fd04519612: rebase due to conflicts🐙 This pull request conflicts with the target branch and needs rebase.
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
This has required rebase for many months now. Are you still interested in the change @vasild?
Should this be drafted for now?
1 additional verified finding in the full report.
🔴 Major · Persistent I2P sessions disable I2P private broadcasting
With -privatebroadcast, -i2psam and incoming I2P enabled, every I2P private-broadcast connection fails before contacting SAM, leaving no private-broadcast route if Tor is unavailable.
Location:
src/net.cpp:3415(head).🟠 Minor · The reachability gate blocks manual I2P connections excluded by -onlynet
With an I2P proxy configured and incoming I2P disabled, -onlynet excluding I2P now prevents explicit manual I2P connections, contrary to the documented exception.
Location:
src/net.cpp:3414(head).🟠 Minor · An empty spare slot leaves I2P session creation coupled to its use
Successful serial I2P-only connections leave the spare empty, so the next attempt creates and consumes its own session without the intended independent preparation interval.
Location:
src/net.cpp:442(head).🔵 Suggestion · Speculative SAM preparation blocks unrelated connection attempts
With transient I2P enabled and an empty spare, IPv4, IPv6 and Tor connection attempts now wait for SAM session preparation before starting their own connection.
Location:
src/net.cpp:442(head).🔵 Suggestion · The transient constructor still promises lazy, I/O-free construction
The transient Session constructor now attempts blocking SAM communication, but its declaration still tells callers that construction performs no I/O.
Location:
src/i2p.cpp:135(head).🔵 Suggestion · feature_proxy.py now contacts the SAM endpoint it says it never uses
Speculative preparation makes feature_proxy.py contact localhost:7656 and can create a real transient session if a developer's SAM service is listening there.
Location:
src/net.cpp:442(head).Review coverage was incomplete.
Reviewed:
1808139f52f1· Full review report · Previous reportspermalink
ACK modulo rebase
Concept ACK
Would any of these options be a minor improvement? Feel free to ignore or defer for later.
Pre-create only when I2P is the preferred network:
Skip pre-create when the current attempt is I2P:
same, only slightly more precise when a hostname resolves to multiple addresses:
There are some review comments from ralph here which I believe to be credible and need addressing.
@willcl-ark agree, good finds
What is the status here?
Closing - up for grabs. This would be nice to have but has lacked review interest and I have been slow to keep it up to date.
I might revisit / reopen it later if I find the time, or if somebody else picks it up, poke me for review.