Repository navigation
Conversation
0c2dfd5 to
c644942
Compare
SWvheerden
left a comment
There was a problem hiding this comment.
Thanks for this. The code is tidy, merges cleanly onto development, and fmt, clippy (-p tari_comms_dht), the 296 dht lib tests and the discover integration tests all pass. The problem is that most of the change guards against conditions that cannot happen on development, and two parts change behaviour for the worse. Please cut it down to the part that is a real improvement.
1. The "spoofing" premise cannot be reached; keep the reorder only as defence-in-depth (service.rs:255-310)
A response with the right nonce and the wrong key cannot come from a third party:
send_discoversends theDiscoveryMessagewithOutboundEncryption::EncryptFor(dest_public_key), so only the target can read the nonce.- The reply goes back with
direct_public_keyand is never forwarded, so relays never see it either. handle_discover_response(inbound/dht_handler/task.rs:480-540) only passes on a response whose signer, source and claimed key are the same.
So the only peer that can produce a mismatched response is one the target gave its own nonce to, and the target could just as well not reply. Peeking before removing is harmless and cheap, so I'm fine keeping it with its test. Please reword the PR description and commit message so they don't claim a spoofing fix.
2. Remove the expiry, cap/eviction, unique-nonce and cleanup-tick machinery (service.rs:57-59, 76-78, 123-147, 174-233, 404-424)
- Expiry is redundant. Every caller goes through
DhtDiscoveryRequester::discover_peer, which times out after the samediscovery_request_timeout(dht.rs:264). Its timer starts before the request is even queued, whilestart_tsis set only aftersend_discoverresolves. Soreply_txis always closed beforeis_expiredcan be true, andis_closed()already covers it. - The table is not unbounded on development. The only insert is in
initiate_peer_discovery, which drops every closed entry first. The table holds the live callers plus whatever was cancelled since the last insert. - The 1024 cap adds a new failure mode: when it is hit, the oldest entry is evicted even if its caller is still waiting, and that caller gets
ReplyCanceledearly instead of its answer. - A collision among at most ~1024 random u64 nonces is about 2^-54, so
next_unique_noncenever loops. - The periodic tick only frees a handful of cancelled entries between two discoveries. That doesn't justify a new timer in the service loop.
Please drop is_expired, MAX_INFLIGHT_DISCOVERIES/evict_oldest_inflight_discovery, next_unique_nonce, the cleanup interval, has_live_request_for, the live_peers sibling-pruning logic and their tests. Keep the original retain(!is_closed) in initiate_peer_discovery.
3. Don't drop valid late responses (service.rs:259, 400-420)
On development, a validly signed response that arrives after its caller gave up is still validated and written to the peer DB, provided no new discovery has pruned the entry yet. The next send to that peer then succeeds without another network-wide discovery.
The PR prunes before the lookup, so these responses are now thrown away, and the tests expired_discovery_response_is_ignored_without_updating_peer_manager and cancelled_sole_discovery_is_pruned_and_late_response_does_not_add_peer lock that in. One concrete case: tari_pulse_service (base_layer/core/src/base_node/tari_pulse_service/mod.rs:467) gives up after 90 s, while the DHT timeout is 120 s, so a target that answers between 90 s and 120 s is never recorded.
The response is signed by the peer and goes through PeerValidator, so there is no security reason to reject it. Please keep the development behaviour.
Non-blocking
- The "skip registration when reply_tx is already closed" check (service.rs:412) runs after the Discovery has already been broadcast, so it saves no network work. If you want an early return, check
reply_tx.is_closed()beforesend_discover. - The commit body is just an empty
Closes #. idle_run_loop_prunes_expired_inflight_discoveriesandresolving_one_peer_preserves_other_peer_cancelled_siblingwould hang onreply_rx.awaitrather than fail if they regressed. Wrap the awaits in atimeoutif any of these tests survive.
No other open PR touches discovery/service.rs. #8368 (per-origin Discovery cooldown) changes only the inbound dht_handler and merges cleanly alongside this.
1526110 to
ae2296b
Compare
|
Thanks for the clear review @SWvheerden! I've narrowed the PR on top of
|
3e841b6 to
d48066d
Compare
eb6d35f to
d48066d
Compare
3152096 to
d48066d
Compare
SWvheerden
left a comment
There was a problem hiding this comment.
Thanks for cutting this down. All three items from the last round are addressed. The key check now runs before the entry is removed, the expiry/cap/cleanup machinery is gone, and late valid responses still reach the peer DB as on development. On this head, nightly fmt, cargo lints clippy -p tari_comms_dht (no new warnings), the 290 dht lib tests and the discover integration tests all pass, and it merges cleanly onto current development.
Before approving we need every new expression to be minimal. Three small changes:
-
comms/dht/src/discovery/service.rs:177-181: don't clone the key on every response..map(|request| (*request.public_key).clone())copies the expected key for every response whose nonce matches, including the normal, matching case. The copy only exists so the warning can print the key on a mismatch. Compare inside the borrow instead.self.inflight_discoveriesandself.dhtare separate fields, so the borrow can live across theban_peerawait. This compiles and passes the tests:if let Some(request) = self.inflight_discoveries.get(&discovery_msg.nonce) && discovery_msg.public_key.as_bytes() != request.public_key.as_bytes() { let expected_public_key = &request.public_key; // ... warn!/ban_peer as now
While you're there, move the comment above the
if letso it describes the check rather than sitting inside the branch. -
service.rs:306-309: generate the nonce after the early returns.rand::rng().next_u64()now runs before thereply_tx.is_closed()and self-discovery returns, which never use it. Movelet nonce = ...down to just beforesend_discover. -
Add a test for the new closed-
reply_txskip. It is new behaviour and nothing covers it. Something like: drop the receiver, callinitiate_peer_discoverydirectly, then assert the outbound mock recorded no call andinflight_discoveriesis empty.
Non-blocking:
- The existing
send_discoverytest (1 ms requester timeout) now depends on the service task being polled before the test task wakes from its timeout. That holds on the single-thread#[tokio::test]runtime, but a short comment there would stop someone switching it tomulti_threadand making it flaky. - The PR description could mention that a requester whose request gets a mismatched response now waits for its own timeout (or the real peer's reply) instead of getting
ReplyCanceledstraight away. That is intended.
…t closed reply_tx
|
Thanks @SWvheerden! Pushed GPG-signed commit
|
SWvheerden
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. All three items from the last round are in: the key is compared by borrow (no clone), the nonce is generated after the early returns, and there is a new test for the closed reply_tx skip. fmt (nightly-2026-06-01) is clean, clippy adds no new warnings, and the tari_comms_dht lib tests (302) pass on a local merge with development.
One thing left, in the new test.
initiate_peer_discovery_skips_closed_reply_channel cannot fail. It hangs, and its call_count assertion always passes (service.rs, new test)
The test never starts the outbound mock. call_count() only goes up inside OutboundServiceMock::run, so assert_eq!(oms_mock_state.call_count().await, 0) holds whatever the code does. If the reply_tx.is_closed() guard is removed, send_discover queues the request on the mock's channel and then waits forever for a reply that only the running mock would send. I checked this locally: with the guard removed, the test hangs past 60 s and never reaches its assertions. In CI that only shows up as a nextest timeout after 6 minutes.
Fix (one line, same as send_discovery does):
let (outbound_requester, outbound_mock) = create_outbound_service_mock();
let oms_mock_state = outbound_mock.get_state();
task::spawn(outbound_mock.run());I checked this locally too. With the line added, the test passes with the guard in place. With the guard removed it fails at once with left: 1, right: 0, and the inflight_discoveries.is_empty() check would fail as well.
Non-blocking: the third bullet in the PR body ("Resolve all matching non-closed in-flight requests ... even if the primary request's receiver was dropped") describes behaviour that is already on development. This PR does not change it, so please drop that bullet.
With the mock started, this is ready to approve from my side.
|
Good catch @SWvheerden, thank you! Pushed GPG-signed commit |
Summary
DiscoveryResponseMessage's public key matches the expected public key of the pendinginflight_discoveriesentry before removing the entry, banning the mismatched responder while keeping the pending request intact so the requester waits for the real peer's reply (or its own timeout) rather than receiving an immediateReplyCanceled.reply_tx.is_closed()wheninitiate_peer_discoveryruns.