-
Notifications
You must be signed in to change notification settings - Fork 122
fix(discovery): replace panics in config validation with typed errors #1529
Description
Summary
Two locations in the discovery crate currently use panic! to signal invalid configuration, which causes the entire process to crash on misconfiguration instead of returning a recoverable error.
Locations
1. code/crates/discovery/src/config.rs:107
DiscoveryConfig::set_peers_bounds panics if num_inbound_peers < num_outbound_peers.
2. code/crates/discovery/src/handlers/selection/selector.rs:28
make_selector panics if Selector::Kademlia is used with a non-Kademlia
bootstrap protocol.
Proposed Fix
- Introduce a
DiscoveryConfigErrorenum (or extend an existing error type)
with variants for each invalid case - Change
set_peers_boundsto returnResult<(), DiscoveryConfigError> - Change
make_selector(or its caller) to returnResultand propagate
the error up to the initialization boundary
This follows the pattern already used elsewhere in the codebase and aligns with the library's goal of being embeddable — callers should be able to
handle configuration errors gracefully without crashing.
Impact
Any application embedding Malachite that passes invalid discovery config will crash unconditionally. Returning a typed error allows callers to
validate config early and handle mistakes gracefully.
Note
I'd be happy to work on this if you're open to a contribution. Please assign this to me if so.